From 1a2cb9a6c874f52296b1a55e26686789272c634a Mon Sep 17 00:00:00 2001 From: Mathieu Comandon Date: Tue, 3 Aug 2010 04:09:19 +0200 Subject: [PATCH] gconfwrapper --- .bzrignore | 3 ++ lutris/configure.py | 33 +++++++++++++ lutris/desktop_control.py | 53 +++++++++++---------- lutris/exceptions.py | 26 ++++++++++ lutris/gconfwrapper.py | 99 +++++++++++++++++++++++++++++++++++++++ tests/gconftest.py | 39 +++++++++++++++ 6 files changed, 228 insertions(+), 25 deletions(-) create mode 100644 lutris/configure.py create mode 100644 lutris/exceptions.py create mode 100644 lutris/gconfwrapper.py create mode 100644 tests/gconftest.py diff --git a/.bzrignore b/.bzrignore index e9c0fcefc..059f2073d 100644 --- a/.bzrignore +++ b/.bzrignore @@ -1,2 +1,5 @@ nbproject build +.project +.pydevproject +.settings diff --git a/lutris/configure.py b/lutris/configure.py new file mode 100644 index 000000000..6a29b90a1 --- /dev/null +++ b/lutris/configure.py @@ -0,0 +1,33 @@ +# -*- coding:Utf-8 -*- +############################################################################### +## Lutris +## +## Copyright (C) 2009 Mathieu Comandon strycore@gmail.com +## +## 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 3 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 +############################################################################### + +try: + import gconf + gconf_capable = True +except ImportError: + gconf_capable = False + +def register_lutris_handler(): + if not gconf_capable: + return False + + + diff --git a/lutris/desktop_control.py b/lutris/desktop_control.py index 4f278953e..0ab4fc3b5 100644 --- a/lutris/desktop_control.py +++ b/lutris/desktop_control.py @@ -1,4 +1,3 @@ -import os.path # -*- coding:Utf-8 -*- ############################################################################### ## Lutris @@ -21,8 +20,11 @@ import os.path ############################################################################### import os +import os.path import subprocess import logging +import lutris.gconfwrapper + #Dumb Debian Lenny,they don't even have python-gconf ! try: import gconf @@ -36,18 +38,19 @@ class LutrisDesktopControl(): """ def __init__(self): self.default_resolution = None + self.gconf = GconfWrapper() if gconf_capable: - self.gconf_path = os.path.join(os.path.expanduser("~"),".gconf") + self.gconf_path = os.path.join(os.path.expanduser("~"), ".gconf") self.client = gconf.client_get_default () - + def set_keyboard_repeat(self, gconf_value = False): """ Desactivate key repeats, this is needed in Wolfenstein (2009) for example """ gconf_key = "/desktop/gnome/peripherals/keyboard/repeat" - type= "Boolean" - self.change_gconf_key(gconf_key,type,gconf_value) - + type = "Boolean" + self.change_gconf_key(gconf_key, type, gconf_value) + def hide_panels(self, hide = True): """ Hide any panel that exists on the Gnome desktop @@ -61,44 +64,44 @@ class LutrisDesktopControl(): print "Hiding %s" % panel else: print "Showing %s" % panel - gconf_key = base_dir+panel+"/auto_hide" - self.change_gconf_key(gconf_key,"boolean",hide) + gconf_key = base_dir + panel + "/auto_hide" + self.change_gconf_key(gconf_key, "boolean", hide) - def change_gconf_key(self,gconf_key,gconf_type,gconf_value): - if not hasattr(self,"client"): + def change_gconf_key(self, gconf_key, gconf_type, gconf_value): + if not hasattr(self, "client"): return if gconf_type.lower() == "string": - self.client.set_string(gconf_key,gconf_value) + self.client.set_string(gconf_key, gconf_value) if gconf_type.lower() == "boolean" or gconf_type.lower() == "bool": - self.client.set_bool(gconf_key,gconf_value) + self.client.set_bool(gconf_key, gconf_value) - def get_gconf_key(self,type,gconf_key): - if not hasattr(self,"client"): + def get_gconf_key(self, type, gconf_key): + if not hasattr(self, "client"): return if type == "boolean": return self.client.get_bool(gconf_key) if type == "string": return self.client.get_string(gconf_key) - def all_dirs(self,base_dir): + def all_dirs(self, base_dir): """The same thing as gconftool --all-dirs """ - - if base_dir[0] =="/": + + if base_dir[0] == "/": base_dir = base_dir[1:] path = os.path.join(self.gconf_path, base_dir) dirs = os.listdir(path) dirs.remove("%gconf.xml") return dirs - - def change_resolution(self,resolution): + + def change_resolution(self, resolution): """change desktop resolution""" if resolution not in self.get_resolutions(): return False - subprocess.Popen("xrandr -s %s" % resolution,shell = True).communicate()[0] + subprocess.Popen("xrandr -s %s" % resolution, shell = True).communicate()[0] return True def get_resolutions(self): - xrandr_output = subprocess.Popen("xrandr",stdout=subprocess.PIPE).communicate()[0] + xrandr_output = subprocess.Popen("xrandr", stdout = subprocess.PIPE).communicate()[0] resolution_list = [] for line in xrandr_output.split("\n"): if line.startswith(" "): @@ -106,7 +109,7 @@ class LutrisDesktopControl(): return resolution_list def get_current_resolution(self): - xrandr_output = subprocess.Popen("xrandr",stdout=subprocess.PIPE).communicate()[0] + xrandr_output = subprocess.Popen("xrandr", stdout = subprocess.PIPE).communicate()[0] for line in xrandr_output.split("\n"): if line.startswith(" ") and "*" in line: return line.split()[0] @@ -127,11 +130,11 @@ class LutrisDesktopControl(): #Restore gamma os.popen("xgamma -gamma 1.0") - + def check_joysticks(self): number_joysticks = 0 joysticks = [] - for device_number in range(0,8): + for device_number in range(0, 8): device_name = "/dev/input/js%d" % device_number if os.path.exists(device_name): number_joysticks = number_joysticks + 1 @@ -141,4 +144,4 @@ class LutrisDesktopControl(): if __name__ == "__main__": ldc = LutrisDesktopControl() print ldc.check_joysticks() - #print ldc.get_resolutions() \ No newline at end of file + #print ldc.get_resolutions() diff --git a/lutris/exceptions.py b/lutris/exceptions.py new file mode 100644 index 000000000..12f8de1fc --- /dev/null +++ b/lutris/exceptions.py @@ -0,0 +1,26 @@ +# -*- coding:Utf-8 -*- +############################################################################### +## Lutris +## +## Copyright (C) 2010 Mathieu Comandon strycore@gmail.com +## +## 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 3 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 +############################################################################### + +class GConfBindingsUnavailable(Exception): + def __init__(self, value): + self.value = value + def __str__(self): + return repr(self.value) diff --git a/lutris/gconfwrapper.py b/lutris/gconfwrapper.py new file mode 100644 index 000000000..f3e9ad562 --- /dev/null +++ b/lutris/gconfwrapper.py @@ -0,0 +1,99 @@ +# -*- coding:Utf-8 -*- + +############################################################################### +## GConfWrapper.py +## +## Copyright (c) 2010 Mathieu Comandon +## +## Author: Mathieu Comandon +## +## 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 3 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 +############################################################################### + + +import os +import glib +from lutris.exceptions import GConfBindingsUnavailable + +try: + import gconf +except ImportError: + raise GConfBindingsUnavailable('Install python-gconf') + +class GconfWrapper(): + def __init__(self): + self.gconf_path = os.path.join(os.path.expanduser("~"), ".gconf") + self.client = gconf.client_get_default () + + def has_key(self, key): + key = self.client.get_string(key) + if key: + return True + else: + return False + + def get_key(self, key): + try: + key = self.client.get_string(key) + except glib.GError, err: + if "Type mismatch" in err[0]: + if "got `bool' for key" in err[0]: + key = self.client.get_bool(key) + elif "got `int' for key" in err[0]: + key = self.client.get_int(key) + else: + print err + raise TypeError, "Wrong type" + return key + + def get_key_type(self, key): + value = self.get_key(key) + return type(value) + + def all_dirs(self, base_dir): + """The same thing as gconftool --all-dirs """ + if base_dir[0] == "/": + base_dir = base_dir[1:] + path = os.path.join(self.gconf_path, base_dir) + dirs = os.listdir(path) + dirs.remove("%gconf.xml") + return dirs + + + def set_key(self, key, value, override_type = False): + try: + success = True + #Get method according to incoming type + if isinstance(value, str): + method = self.client.set_string + elif isinstance(value, bool): + method = self.client.set_bool + elif isinstance(value, int): + method = self.client.set_int + else: + print type(value) + raise TypeError, "Unknown type" + if not override_type: + if not self.get_key_type(key) == type(value): + raise TypeError, "Type mismatch: use type_override to force your way or leave it the way it is!" + method(key, value) + except Exception, err: + print err + success = False + return success + + + + diff --git a/tests/gconftest.py b/tests/gconftest.py new file mode 100644 index 000000000..52f673abb --- /dev/null +++ b/tests/gconftest.py @@ -0,0 +1,39 @@ +import unittest +import sys +import os +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from lutris.gconfwrapper import GconfWrapper + + +class TestGConfWrapper(unittest.TestCase): + def __init__(self): + self.gconf = GconfWrapper() + + def runTest(self): + self.assertEqual(self.gconf.has_key('/apps/metacity/general/button_layout'), True) + self.assertEqual(self.gconf.has_key('/apps/metacity/general/bouton_disposition'), False) + self.assertEqual(self.gconf.has_key('/foo/bar'), False) + + self.assertEqual(self.gconf.get_key('/foo/bar'), None) + self.assertEqual(self.gconf.get_key('/apps/metacity/general/raise_on_click'), True) + self.assertTrue(self.gconf.set_key('/apps/metacity/general/auto_raise_delay', 500, override_type = True)) + self.assertEqual(self.gconf.get_key('/apps/metacity/general/auto_raise_delay'), 500) + + self.assertTrue(self.gconf.set_key('/apps/metacity/general/raise_on_click', False)) + self.assertEqual(self.gconf.get_key('/apps/metacity/general/raise_on_click'), False) + self.assertTrue(self.gconf.set_key('/apps/metacity/general/raise_on_click', True)) + self.assertEqual(self.gconf.get_key('/apps/metacity/general/raise_on_click'), True) + + self.assertTrue(self.gconf.set_key('/apps/metacity/general/auto_raise_delay', 499)) + self.assertEqual(self.gconf.get_key('/apps/metacity/general/auto_raise_delay'), 499) + self.assertFalse(self.gconf.set_key('/apps/metacity/general/auto_raise_delay', "Five hundred")) + self.assertTrue(self.gconf.set_key('/apps/metacity/general/auto_raise_delay', 500)) + + self.assertTrue(self.gconf.set_key('/apps/lutris/tests/foo', "dressed like pazuzu", override_type = True)) + self.assertEqual(self.gconf.get_key('/apps/lutris/tests/foo'), "dressed like pazuzu") + self.assertEqual(self.gconf.all_dirs('/apps/lutris'), ['tests']) + +if __name__ == '__main__': + test = TestGConfWrapper() + test.runTest() +