gconfwrapper

This commit is contained in:
Mathieu Comandon 2010-08-03 04:09:19 +02:00
parent 16bb0c4ecc
commit 1a2cb9a6c8
6 changed files with 228 additions and 25 deletions

View file

@ -1,2 +1,5 @@
nbproject
build
.project
.pydevproject
.settings

33
lutris/configure.py Normal file
View file

@ -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

View file

@ -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 <dir>"""
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()
#print ldc.get_resolutions()

26
lutris/exceptions.py Normal file
View file

@ -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)

99
lutris/gconfwrapper.py Normal file
View file

@ -0,0 +1,99 @@
# -*- coding:Utf-8 -*-
###############################################################################
## GConfWrapper.py
##
## Copyright (c) 2010 Mathieu Comandon <strycore@gmail.com>
##
## Author: 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
###############################################################################
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 <dir>"""
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

39
tests/gconftest.py Normal file
View file

@ -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()