Source

playerdo / playerdo / install.py

Diff from to

File playerdo/install.py

 import sys
 
 
-KEYBINDINGS_PREFIX = "/desktop/gnome/keybindings"
-
-
 def force_unicode(s):
     if type(s) is not unicode:
         return s.decode('UTF-8')
         return s
 
 
-def install_gnome_shortcuts():
-    already_installed = set()
-    max_count = 0
-    for d in get_gnome_keybindings():
-        if d.startswith('custom'):
-            action = get_gconf_val(KEYBINDINGS_PREFIX + "/" + d + "/action")
-            if action.startswith("player_do "):
-                already_installed.add(action.strip())
-            n = int(d[len('custom'):])
-            if n > max_count:
-                max_count = n
+class GnomeSettingsInstallerBase(object):
 
-    commands = ['play', 'pause', 'playpause', 'stop', 'next', 'prev']
-    n = max_count + 1
-    for cmd in commands:
-        action = "player_do " + cmd
-        name = "player_do - " + cmd
-        if action not in already_installed:
-            install_action("custom%d" % n, action, name)
-            n += 1
-    sys.stdout.write("Launching keybinding editor...\nEdit 'player_do' keybindings in 'Custom shortcuts' section, and close when done.\n")
+    def install_shortcuts(self):
+        already_installed = set()
+        max_count = 0
+        for d in self.get_keybindings():
+            if d.startswith('custom'):
+                action = self.get_gconf_val(self.KEYBINDINGS_CONF_PREFIX + "/" + d + "/action")
+                if action.startswith("player_do "):
+                    already_installed.add(action.strip())
+                n = int(d[len('custom'):])
+                if n > max_count:
+                    max_count = n
 
+        commands = ['play', 'pause', 'playpause', 'stop', 'next', 'prev']
+        n = max_count + 1
+        for cmd in commands:
+            action = "player_do " + cmd
+            name = "player_do - " + cmd
+            if action not in already_installed:
+                self.install_action("custom%d" % n, action, name)
+                n += 1
 
-def install_gnome():
-    """
-    Creates stub keybindings for player_do commands in GNOME, and launches a GUI
-    editor for the user to set keys.
-    """
-    install_gnome_shortcuts()
-    launch_keybinding_editor("gnome-keybinding-properties")
+    def get_keybindings(self):
+        try:
+            p = Popen([self.CONF_TOOL, "--all-dirs", self.KEYBINDINGS_CONF_PREFIX], stdout=PIPE)
+        except OSError:
+            raise Exception("Could not use %s to manipulate settings" % self.CONF_TOOL)
+        stdout, stderr = p.communicate(None)
+        if p.returncode != 0:
+            raise Exception("Could not use %s to manipulate settings" % self.CONF_TOOL)
+        retval = []
+        stdout = force_unicode(stdout)
+        for x in stdout.split("\n"):
+            x = x.strip()
+            if x.startswith(self.KEYBINDINGS_CONF_PREFIX):
+                retval.append(x[len(self.KEYBINDINGS_CONF_PREFIX) + 1:])
+        return retval
 
+    def install_action(self, keybinding_name, action, name):
+        self.set_gconf_val(self.KEYBINDINGS_CONF_PREFIX + "/" + keybinding_name + "/" + "action", action)
+        self.set_gconf_val(self.KEYBINDINGS_CONF_PREFIX + "/" + keybinding_name + "/" + "binding", "")
+        self.set_gconf_val(self.KEYBINDINGS_CONF_PREFIX + "/" + keybinding_name + "/" + "name", name)
+        sys.stdout.write("Keybinding slot for action '%s' created\n" % action)
 
-def install_mate():
-    """
-    Creates stub keybindings for player_do commands in Mate, and launches a GUI
-    editor for the user to set keys.
-    """
-    install_gnome_shortcuts()
-    launch_keybinding_editor("mate-keybinding-properties")
+    def get_gconf_val(self,  key):
+        p = Popen([self.CONF_TOOL, "--get", key], stdout=PIPE)
+        stdout, stderr = p.communicate(None)
+        if p.returncode != 0:
+            raise Exception("Could not use %s to manipulate settings" % self.CONF_TOOL)
+        return force_unicode(stdout)
 
 
-def get_gnome_keybindings():
-    p = Popen(["gconftool-2", "--all-dirs", KEYBINDINGS_PREFIX], stdout=PIPE)
-    stdout, stderr = p.communicate(None)
-    if p.returncode != 0:
-        raise Exception("Could not use gconftool to manipulate settings")
-    retval = []
-    stdout = force_unicode(stdout)
-    for x in stdout.split("\n"):
-        x = x.strip()
-        if x.startswith(KEYBINDINGS_PREFIX):
-            retval.append(x[len(KEYBINDINGS_PREFIX) + 1:])
-    return retval
+    def set_gconf_val(self, key, val):
+        p = Popen([self.CONF_TOOL, "--type", "string", "--set", key, val], stdout=PIPE)
+        stdout, stderr = p.communicate(None)
+        if p.returncode != 0:
+            raise Exception("Could not use %s to manipulate settings" % self.CONF_TOOL)
 
+    def launch_keybinding_editor(self):
+        sys.stdout.write("Launching keybinding editor...\nEdit 'player_do' keybindings in 'Custom shortcuts' section, and close when done.\n")
+        errors = []
+        success = False
+        p = Popen(["which", self.KEYBINDINGS_GUI_EDITOR], stdout=PIPE)
+        stdout, stderr = p.communicate(None)
+        val = force_unicode(stdout).strip()
+        if val != "":
+            call(["nohup %s &" % val], shell=True, stdout=open("/dev/null"), stderr=open("/dev/null"))
+            success = True
+        if not success:
+            sys.stdout.write("Error: Couldn't find program %s for editing keybindings.\n" % self.KEYBINDINGS_GUI_EDITOR)
+            raise SystemExit()
 
-def get_gconf_val(key):
-    p = Popen(["gconftool-2", "--get", key], stdout=PIPE)
-    stdout, stderr = p.communicate(None)
-    if p.returncode != 0:
-        raise Exception("Could not use gconftool to manipulate settings")
-    return force_unicode(stdout)
 
+class GnomeSettingsInstaller(GnomeSettingsInstallerBase):
+    CONF_TOOL = "gconftool-2"
+    KEYBINDINGS_CONF_PREFIX = "/desktop/gnome/keybindings"
+    KEYBINDINGS_GUI_EDITOR = "gnome-keybinding-properties"
 
-def set_gconf_val(key, val):
-    p = Popen(["gconftool-2", "--type", "string", "--set", key, val], stdout=PIPE)
-    stdout, stderr = p.communicate(None)
-    if p.returncode != 0:
-        raise Exception("Could not use gconftool to manipulate settings")
 
+class MateSettingsInstaller(GnomeSettingsInstallerBase):
+    CONF_TOOL = "mateconftool-2"
+    KEYBINDINGS_CONF_PREFIX = "/desktop/mate/keybindings"
+    KEYBINDINGS_GUI_EDITOR = "mate-keybinding-properties"
 
-def install_action(keybinding_name, action, name):
-    set_gconf_val(KEYBINDINGS_PREFIX + "/" + keybinding_name + "/" + "action", action)
-    set_gconf_val(KEYBINDINGS_PREFIX + "/" + keybinding_name + "/" + "binding", "")
-    set_gconf_val(KEYBINDINGS_PREFIX + "/" + keybinding_name + "/" + "name", name)
-    sys.stdout.write("Keybinding slot for action '%s' created\n" % action)
 
+def mk_installer(cls):
+    def installer():
+        i = cls()
+        i.install_shortcuts()
+        i.launch_keybinding_editor()
+    return installer
 
-def launch_keybinding_editor(prog):
-    errors = []
-    success = False
-    p = Popen(["which", prog], stdout=PIPE)
-    stdout, stderr = p.communicate(None)
-    val = force_unicode(stdout).strip()
-    if val != "":
-        call(["nohup %s &" % val], shell=True, stdout=open("/dev/null"), stderr=open("/dev/null"))
-        success = True
-    if not success:
-        sys.stdout.write("Error: Couldn't find program %s for editing keybindings.\n" % prog)
-        raise SystemExit()
+install_gnome = mk_installer(GnomeSettingsInstaller)
+install_mate = mk_installer(MateSettingsInstaller)