work in progress: cybertools.meta package for managing general meta-information, e.g. for configuration settings and preferences
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@2516 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
97af36aea8
commit
c2df42555e
7 changed files with 177 additions and 62 deletions
|
@ -10,3 +10,36 @@ Configuration Options, Settings, Preferences
|
||||||
|
|
||||||
>>> from cybertools.meta.config import Options
|
>>> from cybertools.meta.config import Options
|
||||||
|
|
||||||
|
>>> config = Options()
|
||||||
|
|
||||||
|
The Options object allows us to access arbitrary attributes that will
|
||||||
|
be created as elements within the Options object.
|
||||||
|
|
||||||
|
>>> config.storage
|
||||||
|
<AutoElement 'storage'>
|
||||||
|
|
||||||
|
>>> config.i18n.languages = ['de', 'en', 'it']
|
||||||
|
|
||||||
|
>>> config.i18n.languages
|
||||||
|
['de', 'en', 'it']
|
||||||
|
|
||||||
|
Loading options as Python code
|
||||||
|
------------------------------
|
||||||
|
|
||||||
|
>>> from cybertools.meta.namespace import Executor
|
||||||
|
>>> config = Options()
|
||||||
|
>>> ex = Executor(config)
|
||||||
|
|
||||||
|
>>> code = """
|
||||||
|
... controller(names=['cmdline', 'telnet'])
|
||||||
|
... controller.telnet.port = 5001
|
||||||
|
... scheduler(name='core')
|
||||||
|
... logger(name='default', standard=30)
|
||||||
|
... """
|
||||||
|
|
||||||
|
>>> result = ex.execute(code)
|
||||||
|
|
||||||
|
>>> config.scheduler.name
|
||||||
|
'core'
|
||||||
|
>>> config.logger.standard
|
||||||
|
30
|
||||||
|
|
|
@ -22,17 +22,16 @@ Basic implementations for configuration options
|
||||||
$Id$
|
$Id$
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from zope.component import adapts
|
from zope.interface import implements
|
||||||
from zope.interface import implements, Interface
|
|
||||||
|
|
||||||
from cybertools.meta.interfaces import IOptions
|
from cybertools.meta.interfaces import IOptions
|
||||||
|
from cybertools.meta.namespace import AutoNamespace
|
||||||
|
|
||||||
|
|
||||||
class Options(object):
|
class Options(AutoNamespace):
|
||||||
|
|
||||||
implements(IOptions)
|
implements(IOptions)
|
||||||
adapts(Interface)
|
|
||||||
|
|
||||||
def __init__(self, context):
|
def __init__(self, context=None):
|
||||||
self.context = context
|
self.context = context
|
||||||
|
|
||||||
|
|
120
meta/element.py
Normal file
120
meta/element.py
Normal file
|
@ -0,0 +1,120 @@
|
||||||
|
#
|
||||||
|
# Copyright (c) 2008 Helmut Merz helmutm@cy55.de
|
||||||
|
#
|
||||||
|
# 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
|
||||||
|
#
|
||||||
|
|
||||||
|
"""
|
||||||
|
Building flexible hierarchical object structures with XML-like
|
||||||
|
elements.
|
||||||
|
|
||||||
|
$Id$
|
||||||
|
"""
|
||||||
|
|
||||||
|
from cybertools.util.jeep import Jeep
|
||||||
|
|
||||||
|
_not_found = object()
|
||||||
|
|
||||||
|
|
||||||
|
class Element(dict):
|
||||||
|
|
||||||
|
typeName = 'Element'
|
||||||
|
posArgs = ('name',)
|
||||||
|
|
||||||
|
def __init__(self, namespace, name, collection=None, parent=None):
|
||||||
|
self.namespace = namespace
|
||||||
|
self.name = name
|
||||||
|
self.collection = collection
|
||||||
|
self.parent = parent
|
||||||
|
self.children = Jeep()
|
||||||
|
|
||||||
|
def __call__(self, *args, **kw):
|
||||||
|
elem = self.__class__(self.namespace, '')
|
||||||
|
for idx, v in enumerate(args):
|
||||||
|
if idx < len(self.posArgs):
|
||||||
|
elem[self.posArgs[idx]] = v
|
||||||
|
for k, v in kw.items():
|
||||||
|
elem[k] = v
|
||||||
|
elem.name = elem.get('name')
|
||||||
|
if not elem.name:
|
||||||
|
elem.name = self.name
|
||||||
|
if self.collection is not None:
|
||||||
|
self.collection.append(elem)
|
||||||
|
return elem
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
if isinstance(key, (list, tuple)):
|
||||||
|
return tuple(self[k] for k in key)
|
||||||
|
elif isinstance(key, Element):
|
||||||
|
self.children.append(key)
|
||||||
|
return key
|
||||||
|
elif isinstance(key, (int, long, basestring)):
|
||||||
|
return self.children[key]
|
||||||
|
else:
|
||||||
|
raise KeyError(key)
|
||||||
|
|
||||||
|
def __getattr__(self, key):
|
||||||
|
result = self.get(key, _not_found)
|
||||||
|
if result is _not_found:
|
||||||
|
raise AttributeError(key)
|
||||||
|
return result
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return iter(self.children)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "<%s '%s'>" % (self.typeName, self.name)
|
||||||
|
|
||||||
|
|
||||||
|
class AutoElement(Element):
|
||||||
|
|
||||||
|
typeName = 'AutoElement'
|
||||||
|
|
||||||
|
def __call__(self, *args, **kw):
|
||||||
|
if self.collection is None:
|
||||||
|
elem = self
|
||||||
|
else:
|
||||||
|
elem = self.__class__(self.namespace, '')
|
||||||
|
self.collection.append(elem)
|
||||||
|
for idx, v in enumerate(args):
|
||||||
|
if idx < len(self.posArgs):
|
||||||
|
elem[self.posArgs[idx]] = v
|
||||||
|
for k, v in kw.items():
|
||||||
|
elem[k] = v
|
||||||
|
elem.name = elem.get('name')
|
||||||
|
if not elem.name:
|
||||||
|
elem.name = self.name
|
||||||
|
return elem
|
||||||
|
|
||||||
|
def __getattr__(self, key):
|
||||||
|
result = self.get(key, _not_found)
|
||||||
|
if result is _not_found:
|
||||||
|
result = self.__class__(self.namespace, key, parent=self)
|
||||||
|
self[key] = result
|
||||||
|
return result
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
try:
|
||||||
|
return super(AutoElement, self).__getitem__(key)
|
||||||
|
except KeyError:
|
||||||
|
if isinstance(key, basestring):
|
||||||
|
result = self.__class__(self.namespace, key, parent=self)
|
||||||
|
self.children[key] = result
|
||||||
|
return result
|
||||||
|
else:
|
||||||
|
raise KeyError(key)
|
|
@ -26,6 +26,7 @@ $Id$
|
||||||
|
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
|
from cybertools.meta.element import Element, AutoElement
|
||||||
from cybertools.util.jeep import Jeep
|
from cybertools.util.jeep import Jeep
|
||||||
|
|
||||||
_not_found = object()
|
_not_found = object()
|
||||||
|
@ -62,60 +63,21 @@ class BaseNamespace(dict):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
class Element(object):
|
|
||||||
|
|
||||||
posArgs = ('name',)
|
|
||||||
|
|
||||||
def __init__(self, namespace, name, collection=None, parent=None):
|
|
||||||
self.namespace = namespace
|
|
||||||
self.name = name
|
|
||||||
self.collection = collection
|
|
||||||
self.parent = parent
|
|
||||||
self.subElements = Jeep()
|
|
||||||
|
|
||||||
def __call__(self, *args, **kw):
|
|
||||||
elem = self.__class__(self.namespace, '', parent=self)
|
|
||||||
for idx, v in enumerate(args):
|
|
||||||
if idx < len(self.posArgs):
|
|
||||||
setattr(elem, self.posArgs[idx], v)
|
|
||||||
for k, v in kw.items():
|
|
||||||
setattr(elem, k, v)
|
|
||||||
if not elem.name:
|
|
||||||
elem.name = self.name
|
|
||||||
if self.collection is not None:
|
|
||||||
self.collection.append(elem)
|
|
||||||
return elem
|
|
||||||
|
|
||||||
def __getitem__(self, key):
|
|
||||||
if isinstance(key, (list, tuple)):
|
|
||||||
return tuple(self[k] for k in key)
|
|
||||||
elif isinstance(key, Element):
|
|
||||||
self.subElements.append(key)
|
|
||||||
return key
|
|
||||||
elif isinstance(key, (int, long, basestring)):
|
|
||||||
return self.subElements[key]
|
|
||||||
else:
|
|
||||||
print '*** Error', key
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return self.name
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return "<Element '%s'>" % self.name
|
|
||||||
|
|
||||||
|
|
||||||
class AutoNamespace(BaseNamespace):
|
class AutoNamespace(BaseNamespace):
|
||||||
|
|
||||||
elementFactory = Element
|
elementFactory = AutoElement
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
result = self.get(key, _not_found)
|
result = self.get(key, _not_found)
|
||||||
if result is _not_found:
|
if result is _not_found:
|
||||||
result = getattr(self, key, _not_found)
|
result = getattr(self, key)
|
||||||
if result is _not_found:
|
return result
|
||||||
elem = Element(self, key)
|
|
||||||
self[key] = elem
|
def __getattr__(self, key):
|
||||||
return elem
|
result = self.get(key, _not_found)
|
||||||
|
if result is _not_found:
|
||||||
|
result = self.elementFactory(self, key)
|
||||||
|
self[key] = result
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -50,8 +50,8 @@ namespace provides a secure restricted execution environment.
|
||||||
...
|
...
|
||||||
NameError: open
|
NameError: open
|
||||||
|
|
||||||
Elements and sub-elements
|
Elements and sub-elements (children)
|
||||||
-------------------------
|
------------------------------------
|
||||||
|
|
||||||
>>> code = """
|
>>> code = """
|
||||||
... topic('zope3', title='Zope 3')[
|
... topic('zope3', title='Zope 3')[
|
||||||
|
@ -62,18 +62,19 @@ Elements and sub-elements
|
||||||
... """
|
... """
|
||||||
|
|
||||||
>>> from cybertools.util.jeep import Jeep
|
>>> from cybertools.util.jeep import Jeep
|
||||||
|
>>> from cybertools.meta.element import Element
|
||||||
>>> topics = Jeep()
|
>>> topics = Jeep()
|
||||||
>>> symbols = namespace.BaseNamespace()
|
>>> symbols = namespace.BaseNamespace()
|
||||||
>>> symbols['topic'] = namespace.Element(symbols, 'topic', topics)
|
>>> symbols['topic'] = Element(symbols, 'topic', topics)
|
||||||
>>> symbols['annotation'] = namespace.Element(symbols, 'annotation')
|
>>> symbols['annotation'] = Element(symbols, 'annotation')
|
||||||
>>> symbols['child'] = namespace.Element(symbols, 'child')
|
>>> symbols['child'] = Element(symbols, 'child')
|
||||||
|
|
||||||
>>> exec code in symbols
|
>>> exec code in symbols
|
||||||
|
|
||||||
>>> dict(topics)
|
>>> dict(topics)
|
||||||
{'python': <Element 'python'>, 'zope3': <Element 'zope3'>}
|
{'python': <Element 'python'>, 'zope3': <Element 'zope3'>}
|
||||||
|
|
||||||
>>> dict(topics['python'].subElements)
|
>>> dict(topics['python'].children)
|
||||||
{'zope3': <Element 'zope3'>, 'annotation': <Element 'annotation'>}
|
{'zope3': <Element 'zope3'>, 'annotation': <Element 'annotation'>}
|
||||||
|
|
||||||
|
|
||||||
|
@ -86,7 +87,7 @@ A Namespace Automatically Generating Elements
|
||||||
something
|
something
|
||||||
|
|
||||||
>>> auto.something
|
>>> auto.something
|
||||||
<Element 'something'>
|
<AutoElement 'something'>
|
||||||
|
|
||||||
|
|
||||||
Execution of Python Code in a Namespace
|
Execution of Python Code in a Namespace
|
||||||
|
|
|
@ -11,7 +11,7 @@ from zope.testing.doctestunit import DocFileSuite
|
||||||
|
|
||||||
|
|
||||||
class Test(unittest.TestCase):
|
class Test(unittest.TestCase):
|
||||||
"Basic tests for the storage package."
|
"Basic tests for the meta package."
|
||||||
|
|
||||||
def testBasicStuff(self):
|
def testBasicStuff(self):
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -111,7 +111,7 @@ class Jeep(object):
|
||||||
if key is _notfound:
|
if key is _notfound:
|
||||||
raise AttributeError("No name attribute present")
|
raise AttributeError("No name attribute present")
|
||||||
if key in self.keys():
|
if key in self.keys():
|
||||||
raise ValueError("Object already present")
|
raise ValueError("Object '%s' already present" % key)
|
||||||
self._sequence.insert(idx, key)
|
self._sequence.insert(idx, key)
|
||||||
object.__setattr__(self, key, obj)
|
object.__setattr__(self, key, obj)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue