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:
		
							parent
							
								
									c8a881041b
								
							
						
					
					
						commit
						d45477df93
					
				
					 5 changed files with 44 additions and 14 deletions
				
			
		|  | @ -23,6 +23,9 @@ be created as elements within the Options object. | |||
|   >>> config.i18n.languages | ||||
|   ['de', 'en', 'it'] | ||||
| 
 | ||||
|   >>> config('i18n.languages') | ||||
|   ['de', 'en', 'it'] | ||||
| 
 | ||||
| Loading options as Python code | ||||
| ------------------------------ | ||||
| 
 | ||||
|  |  | |||
|  | @ -22,8 +22,10 @@ Basic implementations for configuration options | |||
| $Id$ | ||||
| """ | ||||
| 
 | ||||
| import os | ||||
| from zope.interface import implements | ||||
| 
 | ||||
| from cybertools.meta.element import Element | ||||
| from cybertools.meta.interfaces import IOptions, IConfigurator | ||||
| from cybertools.meta.namespace import AutoNamespace, Executor, ExecutionError | ||||
| 
 | ||||
|  | @ -32,6 +34,30 @@ class Options(AutoNamespace): | |||
| 
 | ||||
|     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): | ||||
| 
 | ||||
|  |  | |||
|  | @ -73,7 +73,7 @@ class Element(dict): | |||
|         return result | ||||
| 
 | ||||
|     def __setattr__(self, key, value): | ||||
|         if key in self.realAttributes: | ||||
|         if key in self.realAttributes or key.startswith('_'): | ||||
|             super(Element, self).__setattr__(key, value) | ||||
|         else: | ||||
|             self[key] = value | ||||
|  | @ -116,6 +116,8 @@ class AutoElement(Element): | |||
|     typeName = 'AutoElement' | ||||
| 
 | ||||
|     def __getattr__(self, key): | ||||
|         if key.startswith('_'):     # no auto-creation for special attributes | ||||
|             raise AttributeError(key) | ||||
|         result = self.get(key, _not_found) | ||||
|         if result is _not_found: | ||||
|             result = self.children.get(key, _not_found) | ||||
|  |  | |||
|  | @ -31,27 +31,24 @@ class IOptions(Interface): | |||
|         a file. | ||||
|     """ | ||||
| 
 | ||||
|     def __contains__(key): | ||||
|         """ Return True if this object provides the option identified | ||||
|             by the key given. | ||||
|         """ | ||||
|     def __call__(key): | ||||
|         """ Return the value belonging to the key given. The key may be a | ||||
|             dotted name - it will be splitted on dots and attributes | ||||
|             will be looked up in turn on the resulting objects. | ||||
| 
 | ||||
|     def __iter__(): | ||||
|         """ Return an iterator over the option keys provided by this | ||||
|             object. | ||||
|         """ | ||||
|             Return None if no corresponding setting can be found. | ||||
| 
 | ||||
|     def values(): | ||||
|         """ Return an iterator over all settings. | ||||
|             The method may also provide some fallback mechanism when | ||||
|             the element is not defined in the current object. | ||||
|         """ | ||||
| 
 | ||||
|     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): | ||||
|         """ Return the value belonging to the key given | ||||
|             (same as ``__getitem__()``). | ||||
|         """ Return the value belonging to the key given. | ||||
|         """ | ||||
| 
 | ||||
|     def __str__(): | ||||
|  |  | |||
|  | @ -87,6 +87,8 @@ class AutoNamespace(BaseNamespace): | |||
|         return result | ||||
| 
 | ||||
|     def __getattr__(self, key): | ||||
|         if key.startswith('_'):     # no auto-creation for special attributes | ||||
|             raise AttributeError(key) | ||||
|         return self[key] | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 helmutm
						helmutm