options/config management basically working

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@2520 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2008-04-12 14:00:13 +00:00
parent c8a881041b
commit d45477df93
5 changed files with 44 additions and 14 deletions

View file

@ -23,6 +23,9 @@ be created as elements within the Options object.
>>> config.i18n.languages >>> config.i18n.languages
['de', 'en', 'it'] ['de', 'en', 'it']
>>> config('i18n.languages')
['de', 'en', 'it']
Loading options as Python code Loading options as Python code
------------------------------ ------------------------------

View file

@ -22,8 +22,10 @@ Basic implementations for configuration options
$Id$ $Id$
""" """
import os
from zope.interface import implements from zope.interface import implements
from cybertools.meta.element import Element
from cybertools.meta.interfaces import IOptions, IConfigurator from cybertools.meta.interfaces import IOptions, IConfigurator
from cybertools.meta.namespace import AutoNamespace, Executor, ExecutionError from cybertools.meta.namespace import AutoNamespace, Executor, ExecutionError
@ -32,6 +34,30 @@ class Options(AutoNamespace):
implements(IOptions) implements(IOptions)
def __call__(self, key, default=None):
value = self
for part in key.split('.'):
value = getattr(value, part)
if isinstance(value, Element):
value = default
return value
class GlobalOptions(Options):
_filename = None
_lastChange = None
def __call__(self, key, default):
if self._filename is not None:
fn = self._filename
if os.path.exists(fn):
modified = os.path.getmtime(fn)
if self._lastChange is None or self._lastChange < modified:
Configurator(self).load(file=fn)
self._lastChange = modified
return super(GlobalOptions, self).__call__(key, default)
class Configurator(object): class Configurator(object):

View file

@ -73,7 +73,7 @@ class Element(dict):
return result return result
def __setattr__(self, key, value): def __setattr__(self, key, value):
if key in self.realAttributes: if key in self.realAttributes or key.startswith('_'):
super(Element, self).__setattr__(key, value) super(Element, self).__setattr__(key, value)
else: else:
self[key] = value self[key] = value
@ -116,6 +116,8 @@ class AutoElement(Element):
typeName = 'AutoElement' typeName = 'AutoElement'
def __getattr__(self, key): def __getattr__(self, key):
if key.startswith('_'): # no auto-creation for special attributes
raise AttributeError(key)
result = self.get(key, _not_found) result = self.get(key, _not_found)
if result is _not_found: if result is _not_found:
result = self.children.get(key, _not_found) result = self.children.get(key, _not_found)

View file

@ -31,27 +31,24 @@ class IOptions(Interface):
a file. a file.
""" """
def __contains__(key): def __call__(key):
""" Return True if this object provides the option identified """ Return the value belonging to the key given. The key may be a
by the key given. dotted name - it will be splitted on dots and attributes
""" will be looked up in turn on the resulting objects.
def __iter__(): Return None if no corresponding setting can be found.
""" Return an iterator over the option keys provided by this
object.
"""
def values(): The method may also provide some fallback mechanism when
""" Return an iterator over all settings. the element is not defined in the current object.
""" """
def __getitem__(key): def __getitem__(key):
""" Return the value belonging to the key given. """ Return the value belonging to the key given. The key
must not contain dots.
""" """
def __getattr__(key): def __getattr__(key):
""" Return the value belonging to the key given """ Return the value belonging to the key given.
(same as ``__getitem__()``).
""" """
def __str__(): def __str__():

View file

@ -87,6 +87,8 @@ class AutoNamespace(BaseNamespace):
return result return result
def __getattr__(self, key): def __getattr__(self, key):
if key.startswith('_'): # no auto-creation for special attributes
raise AttributeError(key)
return self[key] return self[key]