root/gaphor/tags/gaphor-0.12.5/gaphor/misc/conf.py

Revision 465, 4.4 kB (checked in by arjanmol, 4 years ago)

*** empty log message ***

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 # GConf wrapper
2 import gconf
3 from gconf import VALUE_BOOL, VALUE_INT, VALUE_STRING, VALUE_FLOAT
4 from types import StringType, IntType, FloatType
5
6 class Conf:
7     def __init__ (self, appname, allowed={}):
8         self._domain = '/apps/%s/' % appname
9         self._allowed = allowed
10         self._gconf_client = gconf.client_get_default ()
11        
12     def __getitem__ (self, attr):
13         return self.get_value (attr)
14
15     def __setitem__ (self, key, val):
16         allowed = self._allowed
17         if allowed.has_key (key):
18             if not key in allowed[key]:
19                 good = ', '.join (allowed[key])
20                 raise 'ConfError', '%s must be one of: (%s)' % (key, good)
21         self.set_value (key, val)
22
23     def _get_type (self, key):
24         KeyType = type (key)
25         if KeyType == StringType:
26             return 'string'
27         elif KeyType == IntType:
28             return 'int'
29         elif KeyType == FloatType:
30             return 'float'
31         else:
32             raise 'ConfError', 'unsupported type: %s' % str (KeyType)
33        
34     # Public functions
35
36     def set_allowed (self, allowed):
37         self._allowed = allowed
38        
39     def set_domain (self, domain):
40         self._domain = domain
41        
42     def get_domain (self):
43         return self._domain
44    
45     def get_gconf_client (self):
46         return self._gconf_client
47        
48     def get_value (self, key):
49         '''returns the value of key `key' ''' #'
50         #if '/' in key:
51         #    raise 'ConfError', 'key must not contain /'
52        
53         value = self._gconf_client.get (self._domain + key)
54         value_type = value.type
55         if value_type == VALUE_BOOL:
56             return value.get_bool ()
57         elif value_type == VALUE_INT:
58             return value.get_int ()
59         elif value_type == VALUE_STRING:
60             return value.get_string ()
61         elif value_type == VALUE_FLOAT:
62             return value.get_float ()
63    
64     def set_value (self, key, value):
65         '''sets the value of key `key' to `value' '''
66         value_type = self._get_type (value)
67        
68         #if '/' in key:
69         #    raise 'ConfError', 'key must not contain /'
70        
71         func = getattr (self._gconf_client, 'set_' + value_type)
72         apply (func, (self._domain + key, value))
73        
74     def get_string (self, key):
75         #if '/' in key:
76         #    raise 'ConfError', 'key must not contain /'
77        
78         return self._gconf_client.get_string (self._domain + key)
79    
80     def set_string (self, key, value):
81         if type (value) != StringType:
82             raise 'ConfError', 'value must be a string'
83         #if '/' in key:
84         #    raise 'ConfError', 'key must not contain /'
85        
86         self._gconf_client.set_string (self._domain + key, value)
87        
88     def get_bool (self, key):
89         #if '/' in key:
90         #    raise 'ConfError', 'key must not contain /'
91        
92         return self._gconf_client.get_bool (self._domain + key)
93    
94     def set_bool (self, key, value):
95         if type (value) != IntType and \
96            (key != 0 or key != 1):
97             raise 'ConfError', 'value must be a boolean'
98         #if '/' in key:
99         #    raise 'ConfError', 'key must not contain /'
100        
101         self._gconf_client.set_bool (self._domain + key, value)
102        
103     def get_int (self, key):
104         #if '/' in key:
105         #    raise 'ConfError', 'key must not contain /'
106        
107         return self._gconf_client.get_int (self._domain + key)
108    
109     def set_int (self, key, value):
110         if type (value) != IntType:
111             raise 'ConfError', 'value must be an int'
112         #if '/' in key:
113         #    raise 'ConfError', 'key must not contain /'
114        
115         self._gconf_client.set_int (self._domain + key, value)
116        
117     def get_float (self, key):
118         #if '/' in key:
119         #    raise 'ConfError', 'key must not contain /'
120        
121         return self._gconf_client.get_float (self._domain + key)
122    
123     def set_float (self, key, value):
124         if type (value) != FloatType:
125             raise 'ConfError', 'value must be an float'
126        
127         #if '/' in key:
128         #    raise 'ConfError', 'key must not contain /'
129        
130         self._gconf_client.set_float (self._domain + key, value)
131
132 def test():
133     c = Conf ('test-gconf')
134     c['foo'] = '1'
135     c['bar'] = 2
136     c['baz'] = 3.0
137
138     print c['foo'], c['bar'], c['baz']
139    
140 if __name__ == '__main__':
141     test()
Note: See TracBrowser for help on using the browser.