/usr/share/pyshared/gpyconf/gpyconf.py is in python-gpyconf 0.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | # coding: utf-8
# %FILEHEADER%
"""
The :mod:`gpyconf` module
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The :mod:`gpyconf` module contains gpyconf's default Controller, the
:class:`Configuration` class. It takes care of communication between
frontend and backend and offers an interface to "the developer".
The API is very minimalistic and easy to use. To get information on how to
define configuration models and access fields and fields' values, refer to
the :doc:`/usage` section.
API documentation
-----------------
"""
import weakref
from . import fields, backends, frontends
from .mvc import MVCComponent
from ._internal import logging, dicts
from ._internal import exceptions
from ._internal.exceptions import InvalidOptionError
__all__ = ('fields', 'backends', 'frontends', 'exceptions', 'Configuration')
class Proxy(object):
def __setattr__(self, attribute, value):
if attribute == '_proxy_obj':
object.__setattr__(self, attribute, value)
else:
self._proxy_obj.__setattr__(attribute, value)
def __getattr__(self, attribute):
return self._proxy_obj.__getattribute__(attribute)
class DefaultBackend(Proxy):
def __init__(self, *args, **kwargs):
from .backends import configparser
self._proxy_obj = configparser.ConfigParserBackend(*args, **kwargs)
class DefaultFrontend(Proxy):
def __init__(self, *args, **kwargs):
from .frontends import gtk
self._proxy_obj = gtk.ConfigurationDialog(*args, **kwargs)
class ConfigurationMeta(type):
""" Metaclass for the :class:`Configuration` class """
def __new__(cls, cls_name, cls_bases, cls_dict):
super_new = super(ConfigurationMeta, cls).__new__
parents = tuple(base for base in cls_bases
if isinstance(base, ConfigurationMeta))
if not parents:
# This isn't a subclass of ConfigurationMeta, don't do anything special
return super_new(cls, cls_name, cls_bases, cls_dict)
class_fields = cls_dict['fields'] = dicts.FieldsDict()
for superclass in parents:
for name, field in superclass.fields.iteritems():
class_fields[name] = field
field.field_var = name
new_fields = list()
for name, obj in cls_dict.items():
if isinstance(obj, fields.Field):
new_fields.append((name, cls_dict.pop(name)))
new_fields.sort(key=lambda item:item[1].creation_counter)
for name, field in new_fields:
class_fields[name] = field
field.field_var = name
return super_new(cls, cls_name, cls_bases, cls_dict)
class Configuration(MVCComponent):
"""
gpyconf's controller class. The :class:`Configuration` class acts between
the backend and the frontend; it makes the backend load its stored
values and passes them to the frontend and the other way round.
All keyword arguments passed will result in attributes, so calling the
call like this ::
conf_instance = MyConfiguration(foo=42, bar='bazz')
would set the :attr:`foo` attribute to 42 and the :attr:`bar` to 'bazz'.
With this, you can also change the used frontend or backend *at runtime*::
conf_instance = MyConfiguration(frontend=MyGreatWebInterface,
backend=MyGreatSQLiteBackend)
The signals :signal:`field-value-changed`, :signal:`frontend-initialized`,
:signal:`pre-read`, :signal:`pre-save`, :signal:`pre-reset` and
:signal:`initialized` should be self-speaking.
The signature for a callback connecting to :signal:`field-value-changed` is
the following::
def callback(sender_instance, field_instance, new_field_value):
...
"""
__metaclass__ = ConfigurationMeta
fields = dict()
frontend_instance = None
initially_read = False
logger = None
logging_level = 'warning'
#: The :doc:`backend <backends>` to use
backend = DefaultBackend
#: The :doc:`frontend <frontends>` to use
frontend = DefaultFrontend
__events__ = (
'field-value-changed',
'frontend-initialized',
'pre-read',
'pre-save',
'pre-reset',
'initialized'
)
def __init__(self, read=True, **kwargs):
MVCComponent.__init__(self)
for key, value in kwargs.iteritems():
setattr(self, key, value)
if self.logger is None:
self.logger = logging.Logger(self._class_name, self.logging_level)
self.logger.info("Logger initialized (%s)" % self.logger)
if not hasattr(self, 'backend_instance'):
self.backend_instance = self.backend(weakref.ref(self))
self.backend_instance.connect('log', self.backend_log)
self.logger.info("Backend initialized (%s)" % self.backend)
for name, instance in self.fields.iteritems():
instance.connect('value-changed', self.on_field_value_changed)
self.emit('initialized')
if read:
self.read()
# read the config andd set it to the fields.
def on_field_value_changed(self, sender, field, new_value):
self.emit('field-value-changed', field.field_var, new_value)
# MAGIC/API:
def __setattr__(self, attr, value):
# if ``attr`` is a field, don't overwrite the field but its value
if attr in self.fields:
return self.fields[attr].set_value(value)
else:
super(Configuration, self).__setattr__(attr, value)
def __getattr__(self, name):
try:
return self.fields[name].value
except KeyError:
raise AttributeError("No such attribute '%s'" % name)
# BACKEND:
def save(self, save=True):
"""
Checks for every field wether it's value is valid and not emtpy;
if the value is invalid or empty and the field was not marked to allow
blank values, an
:exc:`InvalidOptionError <gpyconf._internal.exceptions.InvalidOptionError>`
will be raised.
Otherwise, passes the fields' values to the backend. If the ``save``
argument is set :const:`True`, makes the backend store the values
permanently.
"""
self.logger.debug("Saving option values...")
if self.backend_instance.compatibility_mode:
self.logger.info("Backend runs in compatibility mode")
for name, field in self.fields.iteritems():
if not field.editable:
# not editable, ignore
continue
if field.isblank():
self.logger.info('Is blank', field=field)
if not field.blank:
# blank, but blank values are not allowed, raise error
raise InvalidOptionError("The option '%s' wasn't set yet" \
% name + " (is None). Use blank=True to safe anyway.")
value = None
else:
# not blank, validate
if not field.isvalid():
self.logger.error("Invalid option '%s'" % field.value,
field=field)
field.validation_error(field.value)
value = field.value
# if backend runs in compatibility mode, convert to str type:
if self.backend_instance.compatibility_mode:
if value is None:
value = u''
else:
value = field.python_to_conf(value)
if not isinstance(value, unicode):
self.logger.warning("Wrong datatype conversion: "
"Got %s, not unicode" % type(value), field=field)
self.backend_instance.set_option(name, value)
if save:
self._save()
def _save(self):
self.emit('pre-save')
self.backend_instance.save()
def read(self):
"""
Reads the configuration options from the backend and updates the
fields' values
"""
self.logger.debug("Reading option values...")
self.emit('pre-read')
if not self.initially_read:
self.backend_instance.read()
self.initially_read = True
for field, value in self.backend_instance.tree.iteritems():
try:
if self.backend_instance.compatibility_mode:
self.logger.info("Datatype conversion of '%s'" % field)
self.fields[field].setfromconf(value)
else:
self.fields[field].value = value
except KeyError:
self.logger.warning("Got an unexpected option name '%s' "
"(No field according to configuration option '%s')" % \
(field, field))
def reset(self):
""" Resets all configuration options """
self.logger.debug("Resetting option values...")
self.emit('pre-reset')
self.backend_instance.reset_all()
map(lambda field:field.reset_value(), self.fields.values())
self.read()
# FRONTEND:
def get_frontend(self):
"""
Returns a (new) instance of the specified :attr:`frontend`
"""
if self.frontend_instance is None:
self.logger.info("Using '%s' as frontend" % (self.frontend.__name__))
# initialize the frontend:
self._init_frontend(self.fields)
self.frontend_instance.connect('save', self.frontend_save)
self.frontend_instance.connect('log', self.frontend_log)
self.logger.info("Initialized frontend (%s)" % self.frontend)
self.frontend_instance.connect('field-value-changed',
self.frontend_field_value_changed)
self.emit('frontend-initialized')
return self.frontend_instance
def _init_frontend(self, fields):
"""
Instantiates the :attr:`frontend` passing all ``fields`` as parameter
and stores it the instance in the :attr:`frontend_instance` attribute.
Only interesting for developers who want to overwrite the default
:class:`Configuration` class.
"""
self.frontend_instance = self.frontend(weakref.ref(self), fields)
def run_frontend(self):
"""
Runs (shows) the frontend, waits for the frontend to quit, reads
values and saves them if the frontend tells to.
"""
self.logger.debug("Running frontend...")
self.get_frontend().run()
def frontend_field_value_changed(self, sender, field_name, new_value):
setattr(self, field_name, new_value)
def frontend_log(self, sender, msg, level):
getattr(self.logger, level)("Frontend: %s" % msg)
def frontend_save(self, sender):
self.save()
# BACKEND:
def backend_log(self, sender, msg, level):
getattr(self.logger, level)("Backend: %s" % msg)
|