gnome-lirc-properties r75 - in trunk: . gnome_lirc_properties



Author: hadess
Date: Mon Oct 27 17:09:40 2008
New Revision: 75
URL: http://svn.gnome.org/viewvc/gnome-lirc-properties?rev=75&view=rev

Log:
2008-10-27  Bastien Nocera  <hadess hadess net>

	* gnome_lirc_properties/Makefile.am:
	* gnome_lirc_properties/backend.py:
	* gnome_lirc_properties/lirc.py:
	* gnome_lirc_properties/shellquote.py: Add shellquote helper
	from http://liw.iki.fi/liw/python-licosmamo/, quote and unquote
	configuration options from the hardware configuration file, so
	that 1) input devices with brackets in their names work, 2)
	arbitrary commands can't be added to the default lirc configuration
	file (Closes: #558127)



Added:
   trunk/gnome_lirc_properties/shellquote.py
Modified:
   trunk/ChangeLog
   trunk/gnome_lirc_properties/Makefile.am
   trunk/gnome_lirc_properties/backend.py
   trunk/gnome_lirc_properties/lirc.py

Modified: trunk/gnome_lirc_properties/Makefile.am
==============================================================================
--- trunk/gnome_lirc_properties/Makefile.am	(original)
+++ trunk/gnome_lirc_properties/Makefile.am	Mon Oct 27 17:09:40 2008
@@ -9,5 +9,6 @@
 	hardware.py \
 	lirc.py \
 	model.py \
-	policykit.py
+	policykit.py \
+	shellquote.py
 

Modified: trunk/gnome_lirc_properties/backend.py
==============================================================================
--- trunk/gnome_lirc_properties/backend.py	(original)
+++ trunk/gnome_lirc_properties/backend.py	Mon Oct 27 17:09:40 2008
@@ -22,6 +22,7 @@
 
 from gettext               import gettext as _
 from gnome_lirc_properties import config, lirc
+from gnome_lirc_properties import shellquote as ShellQuote
 from StringIO              import StringIO
 
 # Modern flavors of dbus bindings have that symbol in dbus.lowlevel,
@@ -264,7 +265,7 @@
         self._filename = filename
 
         if device:
-            self._cmdargs.append('--device=%s' % device)
+            self._cmdargs.append('--device=%s' % ShellQuote.shellquote(device))
         if filename:
             self._cmdargs.append(filename)
 
@@ -579,7 +580,7 @@
 
             if value is not None:
                 logging.info('- writing %s"%s"', match.group(0), value)
-                print >> output, ('%s"%s"' % (match.group(0), value))
+                print >> output, ('%s"%s"' % (match.group(0), ShellQuote.shellquote(value)))
                 continue
 
             # Identify directives starting with RECEIVER_ and replacing their values with ours.
@@ -589,7 +590,7 @@
 
             if value is not None:
                 logging.info('- writing %s"%s"', match.group(0), value)
-                print >> output, ('%s"%s"' % (match.group(0), value))
+                print >> output, ('%s"%s"' % (match.group(0), ShellQuote.shellquote(value)))
                 continue
 
             # Deal with the START_LIRCD line:
@@ -604,7 +605,7 @@
                 value = (start_lircd is None) and 'true' or start_lircd
                 start_lircd = None
 
-                print >> output, (match.group(0) + value)
+                print >> output, (match.group(0) + ShellQuote.shellquote(value))
                 continue
 
             output.write(line)
@@ -615,12 +616,12 @@
         if remote_values:
             print >> output, '\n# Remote settings required by gnome-lirc-properties'
         for key, value in remote_values.items():
-            print >> output, ('REMOTE_%s="%s"' % (key, value))
+            print >> output, ('REMOTE_%s="%s"' % (key, ShellQuote.shellquote(value)))
 
         if receiver_values:
             print >> output, '\n# Receiver settings required by gnome-lirc-properties'
         for key, value in receiver_values.items():
-            print >> output, ('RECEIVER_%s="%s"' % (key, value))
+            print >> output, ('RECEIVER_%s="%s"' % (key, ShellQuote.shellquote(value)))
 
         if start_lircd is not None:
             print >> output, '\n# Daemon settings required by gnome-lirc-properties'

Modified: trunk/gnome_lirc_properties/lirc.py
==============================================================================
--- trunk/gnome_lirc_properties/lirc.py	(original)
+++ trunk/gnome_lirc_properties/lirc.py	Mon Oct 27 17:09:40 2008
@@ -1011,6 +1011,7 @@
             key, value = tokens
 
             value = value.strip('"') # Remove quotes
+            value = ''.join(value.split('\\')) # Remove escaping
             self.__values[key] = value
 
     def __getitem__(self, key):

Added: trunk/gnome_lirc_properties/shellquote.py
==============================================================================
--- (empty file)
+++ trunk/gnome_lirc_properties/shellquote.py	Mon Oct 27 17:09:40 2008
@@ -0,0 +1,69 @@
+# Copyright 2005 Lars Wirzenius (liw iki fi)
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation; either version 2 of the License, or (at your
+# option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+# Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+
+"""Quote a word for the Unix sh shell
+
+This module contains the function shellquote, which quotes a string
+so that it can be safely passed to the shell as an argument, e.g.,
+via os.system or os.popen.
+
+Lars Wirzenius <liw iki fi>
+"""
+
+
+import unittest
+
+safechars = ("abcdefghijklmnopqrstuvwxyz" +
+             "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
+             "0123456789" +
+             ",.-_!%/=+:")
+
+def shellquote(str):
+    if str == "":
+        return "''"
+
+    result = []
+    for c in str:
+        if c == "'":
+            result.append("\"'\"")
+        elif c in safechars:
+            result.append(c)
+        else:
+            result.append("\\" + c)
+    return "".join(result)
+
+
+class ShellQuoteTests(unittest.TestCase):
+
+    def testEmpty(self):
+        self.failUnlessEqual(shellquote(""), "''")
+
+    def testSimple(self):
+        self.failUnlessEqual(shellquote("abc"), "abc")
+
+    def testSpace(self):
+        self.failUnlessEqual(shellquote("abc def"), "abc\\ def")
+
+    def testApostrophe(self):
+        self.failUnlessEqual(shellquote("abc'def"), "abc\"'\"def")
+
+    def testQuotes(self):
+        self.failUnlessEqual(shellquote("abc\"def"), "abc\\\"def")
+
+
+if __name__ == "__main__":
+    unittest.main()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]