Changeset 535
- Timestamp:
- 05/06/08 11:24:51 (2 months ago)
- Files:
-
- ape/trunk/ape/commandline.py (added)
- ape/trunk/ape/config.py (modified) (7 diffs)
- ape/trunk/ape/engine.py (modified) (4 diffs)
- ape/trunk/ape/__init__.py (modified) (1 diff)
- ape/trunk/ape/util.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
ape/trunk/ape/config.py
r533 r535 18 18 import os 19 19 from ape.component import * 20 from ape.util import to_ boolean, URI20 from ape.util import to_list, to_boolean, URI 21 21 22 22 … … 85 85 86 86 def get_bool(self, name, default=None): 87 value = self.get(name, default) 88 return to_boolean(value or 'false') 87 return to_boolean(self.get(name, default)) 89 88 90 89 def get_int(self, name, default=None): … … 95 94 96 95 def get_list(self, name, default=None, sep=',', keep_empty=False): 97 value = self.get(name, default) 98 if isinstance(value, basestring): 99 items = [item.strip() for item in value.split(sep)] 96 return to_list(self.get(name, default), sep, keep_empty) 97 98 99 class Option(object): 100 """"A convenience property for accessing configuration entries. 101 102 Options are also mapped to commandline flags via the ape.commandline module. 103 """ 104 105 registry = {} 106 107 def __init__(self, name, default=None, doc='', metavar=None): 108 """Create a new Option. 109 110 Args: 111 name: Name of the option. 112 default: Default value. 113 doc: Documentation string. 114 metavar: Name of variable to display in help. 115 """ 116 self.name = name 117 if default is not None: 118 self.default = self.cast(default) 100 119 else: 101 items = list(value or []) 102 if not keep_empty: 103 items = filter(None, items) 104 return items 105 106 107 108 class Option(object): 109 110 accessor = Configuration.get 111 registry = {} 112 113 def __init__(self, name, default=None, doc=''): 114 self.name = name 115 self.default = default 120 self.default = default 116 121 self.__doc__ = doc 122 self.metavar = metavar 117 123 self.registry[name] = self 118 124 … … 131 137 config.set(self.name, unicode(value)) 132 138 139 def accessor(self, config, name, default): 140 return self.cast(config.get(name, default)) 141 142 def cast(self, value): 143 return str(value) 144 133 145 134 146 class BoolOption(Option): 135 accessor = Configuration.get_bool 147 def cast(self, value): 148 return to_boolean(value) 136 149 137 150 138 151 class IntOption(Option): 139 accessor = Configuration.get_int 152 def cast(self, value): 153 return int(value) 140 154 141 155 142 156 class FloatOption(Option): 143 accessor = Configuration.get_float 157 def cast(self, value): 158 return float(value) 144 159 145 160 146 161 class ListOption(Option): 147 def __init__(self, name, default=None, sep=',', keep_empty=False, doc=''):148 Option.__init__(self, name, default, doc)162 def __init__(self, name, default=None, doc='', metavar=None, sep=',', 163 keep_empty=False): 149 164 self.sep = sep 150 165 self.keep_empty = keep_empty 151 152 def accessor(self, config, name, default): 153 return config.get_list(name, default, self.sep, self.keep_empty) 166 Option.__init__(self, name, default, doc, metavar) 167 168 def cast(self, value): 169 return to_list(value, self.sep, self.keep_empty) 154 170 155 171 156 172 class URIOption(Option): 157 def accessor(self, config, name, default):158 return URI( config.get(name, default))173 def cast(self, value): 174 return URI(value) 159 175 160 176 161 177 class ExtensionOption(Option): 162 def __init__(self, name, interface, default=None, doc='' ):163 Option.__init__(self, name, default, doc )178 def __init__(self, name, interface, default=None, doc='', metavar=None): 179 Option.__init__(self, name, default, doc, metavar) 164 180 self.xtnpt = ExtensionPoint(interface) 165 181 … … 168 184 return self 169 185 value = Option.__get__(self, instance, owner) 170 for impl in self.xtnpt. Extensions(instance):186 for impl in self.xtnpt.extensions(instance): 171 187 if impl.__class__.__name__ == value: 172 188 return impl … … 184 200 185 201 def __init__(self, name, interface, default=None, 186 include_missing=True, doc=''):202 doc='', include_missing=True): 187 203 ListOption.__init__(self, name, default, doc=doc) 188 204 self.xtnpt = ExtensionPoint(interface) … … 194 210 order = ListOption.__get__(self, instance, owner) 195 211 components = [] 196 for impl in self.xtnpt. Extensions(instance):212 for impl in self.xtnpt.extensions(instance): 197 213 if self.include_missing or impl.__class__.__name__ in order: 198 214 components.append(impl) ape/trunk/ape/engine.py
r533 r535 26 26 27 27 # Note: Error is re-exported from ape.component. 28 __all__ = ['Error', 'Engine', ' get_active_engine']28 __all__ = ['Error', 'Engine', 'IEngineLifecycleListener', 'get_active_engine'] 29 29 30 30 … … 43 43 44 44 class Engine(Component, ComponentManager): 45 """The application engine. 46 47 This class provides fundamental application services, including 48 access to a configuration file, logging, and a plugin system. 49 50 """ 45 51 46 52 lifecycle_listeners = ExtensionPoint(IEngineLifecycleListener) 47 53 48 logging_uri = URIOption('logging', 'stderr://', 'Logging URI') 49 debug = IntOption('debug', 0, 'Debug level') 50 plugin_paths = ListOption('plugins.paths', [], 'Plugin search paths') 54 logging_uri = URIOption('logging', 'stderr://', 'logging URI', 'uri') 55 debug = IntOption('debug', 0, 'debug level', 'level') 56 plugin_paths = ListOption('plugin-paths', [], 'plugin search paths', 57 'paths', sep=':') 58 59 # Class defaults 60 application_name = None 61 config_file = None 51 62 52 63 _active_engine = None … … 54 65 def __init__(self, application_name='app', config_file=None, 55 66 defer_startup=False, debug=None): 67 """Construct a new Engine. 68 69 Args: 70 application_name: Name of the application. 71 config_file: Configuration filename. 72 defer_startup: Defer full initialisation. Logging and configuration 73 are loaded regardless of the state of this flag. 74 debug: Debug level as an integer. 75 """ 56 76 Component.__init__(self) 57 77 ComponentManager.__init__(self) 58 78 59 79 self.engine = self 60 self.application_name = application_name 61 self.config = Configuration(config_file )80 self.application_name = application_name or self.application_name 81 self.config = Configuration(config_file or self.config_file) 62 82 self.log = self.initialise_logging(self.logging_uri) 63 83 … … 233 253 return True 234 254 255 @classmethod 256 def from_commandline(cls, name=None, *args, **kwargs): 257 """Build and return the Engine from the command-line. 258 259 Relies on the commandline module. 260 261 Args: 262 name: Application name. 263 args: Passed through to commandline.bootstrap() 264 kwargs: Passed through to commandline.bootstrap() 265 """ 266 from ape.commandline import bootstrap 267 return bootstrap(cls, name or 'application', *args, **kwargs) 268 235 269 236 270 def get_active_engine(): ape/trunk/ape/__init__.py
r533 r535 1 from ape import engine, config, component, util 1 # -*- coding: utf-8 -*- 2 # 3 # Copyright 2008 Alec Thomas <alec@swapoff.org> 4 # All rights reserved. 5 # 6 7 8 __author__ = 'Alec Thomas <alec@swapoff.org>' 9 10 11 from ape import engine, config, component 2 12 from ape.engine import * 3 13 from ape.config import * 4 14 from ape.component import * 5 from ape.util import *6 15 7 16 ape/trunk/ape/util.py
r533 r535 162 162 >>> u == v 163 163 True 164 >>> v.host = 'www. google.com'164 >>> v.host = 'www.example.com' 165 165 >>> u == v 166 166 False … … 306 306 307 307 308 def to_list(value, sep=',', keep_empty=False): 309 """Convert a token-separated string to a list.""" 310 if isinstance(value, basestring): 311 items = [item.strip() for item in value.split(sep)] 312 else: 313 items = list(value or []) 314 if not keep_empty: 315 items = filter(None, items) 316 return items 317 318 308 319 def get_last_traceback(): 309 320 """Write current exception traceback to a string.
