Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
key_to_right = StringOption('Tab')
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)
def get_value(self, value):
"""Gets the value of ``value``"""
if 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()
elif value.type == gconf.VALUE_BOOL:
return value.get_bool()
elif value.type == gconf.VALUE_LIST:
_list = value.get_list()
return [self.get_value(v) for v in _list]
else:
msg = "Unsupported type %s for %s"
raise TypeError(msg % (type(value), value))
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)
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)
# Autogenerate classes StringOption, IntOption etc. with SimpleOption
# same as:
# class StringOption(SimpleOption):
# gconf_type = gconf.VALUE_STRING
#
for gconf_type in GCONF_TYPES:
class_name = '%sOption' % gconf_type.value_nick.capitalize()
new_class = type(class_name, (SimpleOption,), {'gconf_type': gconf_type})
locals()[class_name] = new_class
class ListOption(SimpleOption):
gconf_type = gconf.VALUE_LIST
def _py_to_gconf(self, t):
return {
int: gconf.VALUE_INT,
float: gconf.VALUE_FLOAT,
str: gconf.VALUE_STRING,
unicode: gconf.VALUE_STRING,
bool: gconf.VALUE_BOOL
}[type(t)]
def __init__(self, default_value=None, **kwargs):
if default_value is not None:
assert hasattr(default_value, '__iter__'), 'Default value of ListOption must be iterable'
if len(default_value):
self.list_type = self._py_to_gconf(default_value[0])
# -*- 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']