Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _convert_gconf_value(value):
if value.type == gconf.VALUE_STRING:
return value.get_string()
if value.type == gconf.VALUE_INT:
return value.get_int()
if value.type == gconf.VALUE_BOOL:
return value.get_bool()
if value.type == gconf.VALUE_FLOAT:
return value.get_float()
if value.type == gconf.VALUE_LIST:
return [_convert_gconf_value(v) for v in value.get_list()]
raise TypeError("unknown gconf type %s" % value.type)
key_to_left = StringOption('ISO_Left_Tab')
key_to_1 = StringOption()
...
options = KeyOptions("/apps/gedit-2/plugins/tabswitch")
print options.key_to_right
"""
import os
import gconf
GCONF_TYPES = (
gconf.VALUE_STRING,
gconf.VALUE_INT,
gconf.VALUE_BOOL,
gconf.VALUE_LIST,
gconf.VALUE_FLOAT,
gconf.VALUE_SCHEMA
)
class OptionsType(type):
"""
Metaclass for options class.
Add capability for get all options list.
"""
def __new__(cls, base, name, attrs):
attrs['options'] = {}
for key, attr in attrs.iteritems():
if isinstance(attr, Option):
attrs['options'][key] = attr
attr.default_key = key
new_class = super(OptionsType, cls).__new__(cls, base, name, attrs)
return new_class
def get_value (self, key):
'''returns the value of key `key' ''' #'
#if '/' in key:
# raise 'ConfError', 'key must not contain /'
value = self._gconf_client.get (self._domain + key)
value_type = value.type
if value_type == VALUE_BOOL:
return value.get_bool ()
elif value_type == VALUE_INT:
return value.get_int ()
elif value_type == VALUE_STRING:
return value.get_string ()
elif value_type == VALUE_FLOAT:
return value.get_float ()
# -*- coding: utf-8 -*-
import os
import gconf
GCONF_TYPES = (
gconf.VALUE_STRING,
gconf.VALUE_INT,
gconf.VALUE_BOOL,
gconf.VALUE_LIST,
gconf.VALUE_FLOAT,
gconf.VALUE_SCHEMA
)
class OptionsContainerType(type):
"""
Metaclass for any options container class.
"""
def __new__(cls, base, name, attrs):
attrs['options'] = {}
for key, attr in attrs.iteritems():
if isinstance(attr, Option):
attrs['options'][key] = attr
attr.default_key = key
attr._storage_client = '_storage' in attrs and attrs['_storage'] or Options._storage
attr._base_uri = attrs['_uri']
client.set_string(fullkey, value)
elif isinstance(value, bool):
client.set_bool(fullkey, value)
elif isinstance(value, int):
client.set_int(fullkey, value)
elif isinstance(value, float):
client.set_float(fullkey, value)
elif isinstance(value, list):
# this is lame, but there isn't enough information to
# figure it out another way
if len(value) == 0 or isinstance(value[0], str):
list_type = gconf.VALUE_STRING
elif isinstance(value[0], int):
list_type = gconf.VALUE_INT
elif isinstance(value[0], float):
list_type = gconf.VALUE_FLOAT
elif isinstance(value[0], bool):
list_type = gconf.VALUE_BOOL
else:
raise TypeError("unknown gconf type %s" % type(value[0]))
client.set_list(fullkey, list_type, value)
else:
raise TypeError()
finally:
gconf_lock.release()
client.set_string(fullkey, value)
elif isinstance(value, bool):
client.set_bool(fullkey, value)
elif isinstance(value, int):
client.set_int(fullkey, value)
elif isinstance(value, float):
client.set_float(fullkey, value)
elif isinstance(value, list):
# this is lame, but there isn't enough information to
# figure it out another way
if len(value) == 0 or isinstance(value[0], str):
list_type = gconf.VALUE_STRING
elif isinstance(value[0], int):
list_type = gconf.VALUE_INT
elif isinstance(value[0], float):
list_type = gconf.VALUE_FLOAT
elif isinstance(value[0], bool):
list_type = gconf.VALUE_BOOL
else:
raise TypeError("unknown gconf type %s" % type(value[0]))
client.set_list(fullkey, list_type, value)
else:
raise TypeError()
finally:
gconf_lock.release()
def get_schema_value(self):
value = self.__client.get_default_from_schema(self.__key)
if value:
if value.type == gconf.VALUE_BOOL:
return value.get_bool()
elif value.type == gconf.VALUE_STRING:
return value.get_string()
elif value.type == gconf.VALUE_INT:
return value.get_int()
elif value.type == gconf.VALUE_FLOAT:
return value.get_float()
else:
raise Exception("No schema value for %s" % self.__key)
class Spec:
"""
The spec is an adapter between a GConfValue and a Python value,
simplifying the conversion and the integrity.
You should use L{Spec.STRING}, L{Spec.FLOAT}, L{Spec.INT} and L{Spec.BOOL}
instead.
"""
def __init__(self, name, gconf_type, py_type, default):
self.gconf_type = gconf_type
self.py_type = py_type
self.default = default
self.name = name
Spec.STRING = Spec("string", gconf.VALUE_STRING, str, '')
Spec.FLOAT = Spec("float", gconf.VALUE_FLOAT, float, 0.0)
Spec.INT = Spec("int", gconf.VALUE_INT, int, 0)
Spec.BOOL = Spec("bool", gconf.VALUE_BOOL, bool, True)
class GConfValue(object):
"""
The GConfValue represents the GConf key's data. You define a certain schema
(or type of data) and GConfValue keeps track of its integrity. It adds the
possibility to define a default value to be used when the key is inexistent
or contains an invalid data type. You can also define callbacks that notify
you when the key is altered.
Taken from U{GAW Introduction }::
import gwp, gconf, gtk
gconf.client_get_default().add_dir("/apps/gwp", gconf.CLIENT_PRELOAD_NONE)
def set_num(self, num):
self.value = self.client.get(self.key)
if self.value:
if self.value.type == gconf.VALUE_INT:
self.client.set_int(self.key, int(num))
elif self.value.type == gconf.VALUE_FLOAT:
self.client.set_float(self.key, num)
else:
self.client.set_float(self.key, num)
import gconf, gobject, gtk
class Spec (object):
def __init__ (self, name, gconf_type, py_type, default):
self.__gconf_type = gconf_type
self.__py_type = py_type
self.__default = default
self.__name = name
gconf_type = property (lambda self: self.__gconf_type)
py_type = property (lambda self: self.__py_type)
default = property (lambda self: self.__default)
name = property (lambda self: self.__name)
Spec.STRING = Spec ("string", gconf.VALUE_STRING, str, '')
Spec.FLOAT = Spec ("float", gconf.VALUE_FLOAT, float, 0.0)
Spec.INT = Spec ("int", gconf.VALUE_INT, int, 0)
Spec.BOOL = Spec ("bool", gconf.VALUE_BOOL, bool, True)
def data_file_chooser (button, key, use_directory = False, use_uri = True, default = None, client = None):
"""
Returns a gaw.Data.
use_directory - boolean variable setting if it's we're using files or directories.
use_uri - boolean variable setting if we're using URI's or normal filenames.
Associates a gaw.Data to a gtk.FileChooserButton.
"""
if not use_directory and not use_uri:
getter = button.get_filename
setter = button.set_filename