#! /usr/bin/env python # # # Copyright (C) 2003 Michael Urman # # 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 2 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 pygtk #pygtk.require('2.0') import gconf import posixpath class GConfDir(object): def __init__(self, name): self.__dict__['[name]'] = name self.__dict__['[client]'] = gconf.client_get_default() def __repr__(self): return "GConfDir('%s')" % self.__dict__['[name]'] def __getattr__(self, attr): name = posixpath.join(self.__dict__['[name]'], attr) conf = self.__dict__['[client]'] maps = { gconf.VALUE_STRING : 'get_string', gconf.VALUE_INT : 'get_int', gconf.VALUE_FLOAT : 'get_float', gconf.VALUE_FLOAT : 'get_bool', } if conf.dir_exists(name): return GConfDir(name) else: val = conf.get(name) if val is None: raise AttributeError("GConf Key '%s' does not exist" % name) vtype = val.type if vtype == gconf.VALUE_INVALID: raise AttributeError("GConf Key '%s' is invalid" % name) elif vtype == gconf.VALUE_LIST: values = val.get_list() ltype = val.get_list_type() if len(values) == 0: value = [] else: attr = maps.get(ltype) if attr is None: raise ValueError("GConf List Key '%s' holds unknown type" % name) else: value = [] for val in values: if val.get_type() == ltype: value.append(getattr(val, attr)()) else: value.append(None) #value = [getattr(val, attr)() for val in values] elif vtype == gconf.VALUE_SCHEMA: value = GConfSchema() elif vtype == gconf.VALUE_PAIR: raise NotImplementedError("GConf Key '%s' is a pair" % name) else: attr = maps.get(vtype) if attr is None: raise AttributeError("GConf Key '%s' is an unknown type" % name) else: value = getattr(val, attr)() return value def __setattr__(self, attr, val): print 'setattr', attr, val name = posixpath.join(self.__dict__['[name]'], attr) conf = self.__dict__['[client]'] vtype = type(val) if val is GConfDir: raise NotImplementedError("GConf.client.add_dir(...)?") conf.add_dir() elif vtype == str: conf.set_string(name, val) elif vtype == unicode: conf.set_string(name, val.encode('utf8')) elif vtype == int: conf.set_int(name, val) elif vtype == float: conf.set_float(name, val) elif vtype == bool: conf.set_bool(name, val) elif vtype == tuple or vtype == list: ltypes = dict(map(None, [type(v) for v in val], [])).keys() if len(ltypes) == len(val) == 2: raise NotImplementedError("GConf: set pair") elif len(ltypes) != 1: raise ValueError("GConf lists can only contain one type") ltype = ltypes[0] raise NotImplementedError("GConf: set list") #elif vtype def __iter__(self): name = self.__dict__['[name]'] conf = self.__dict__['[client]'] strip = len(name) if not name.endswith('/'): strip += 1 entries = tuple([e.key for e in conf.all_entries(name)]) dirs = tuple(conf.all_dirs(name)) keys = [ key[strip:] for key in (entries+dirs) ] for key in keys: #print "" continue #print ">" yield key, value def __contains__(self, key): name = posixpath.join(self.__dict__['[name]'], key) conf = self.__dict__['[client]'] exists = conf.dir_exists(name) or conf.get(name) is not None return exists class GConfSchema(object): def __repr__(self): return 'GConfSchema()' if __name__ == '__main__': def show(conf, depth=0): for k, v in conf: if type(v) == GConfDir: print ' '*depth + k show(v, depth+2) else: print ' '*depth + k, '=', repr(v) show(GConfDir('/'))