Remove trailing whitespace errors
[panucci.git] / src / panucci / simplegconf.py
blob09ddf0ab4de48c47f0592939d749ce80e776720f
1 #!/usr/bin/env python
3 # This file is part of Panucci.
4 # Copyright (c) 2008-2010 The Panucci Audiobook and Podcast Player Project
6 # Panucci is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # Panucci is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Panucci. If not, see <http://www.gnu.org/licenses/>.
20 from __future__ import absolute_import
22 import os.path
23 import logging
25 try:
26 import gconf
27 except:
28 # on the tablet, it's probably in "gnome"
29 from gnome import gconf
32 gconf_dir = '/apps/panucci'
34 class SimpleGConfClient(gconf.Client):
35 """ A simplified wrapper around gconf.Client
36 GConf docs: http://library.gnome.org/devel/gconf/stable/
37 """
39 __type_mapping = { int: 'int', long: 'float', float: 'float',
40 str: 'string', bool: 'bool', list: 'list', }
42 def __init__(self, directory):
43 """ directory is the base directory that we're working in """
44 self.__log = logging.getLogger('panucci.simplegconf.SimpleGConfClient')
45 self.__directory = directory
46 gconf.Client.__init__(self)
48 self.add_dir( self.__directory, gconf.CLIENT_PRELOAD_NONE )
50 def __get_manipulator_method( self, data_type, operation ):
51 """ data_type must be a vaild "type"
52 operation is either 'set' or 'get' """
54 if self.__type_mapping.has_key( data_type ):
55 method = operation + '_' + self.__type_mapping[data_type]
56 return getattr( self, method )
57 else:
58 self.__log.warn('Data type "%s" is not supported.', data_type)
59 return lambda x,y=None: None
61 def sset( self, key, value ):
62 """ A simple set function, no type is required, it is determined
63 automatically. 'key' is relative to self.__directory """
65 return self.__get_manipulator_method(type(value), 'set')(
66 os.path.join(self.__directory, key), value )
68 def sget( self, key, data_type=None, default=None ):
69 """ A simple get function, data_type or default value is required,
70 if default value is given data type will be guessed from it,
71 'key' is relative to self.__directory """
73 dtype = type(default) if data_type is None else data_type
74 if dtype is None:
75 self.__log.warn('sget error: data_type or default must be set')
76 return
78 if self.get( os.path.join(self.__directory, key) ) is None:
79 return default
80 else:
81 return self.__get_manipulator_method(dtype, 'get')(
82 os.path.join(self.__directory, key) )
84 def snotify( self, callback ):
85 """ Set a callback to watch self.__directory """
86 return self.notify_add( self.__directory, callback )
88 gconf = SimpleGConfClient( gconf_dir )