Changeset 536
- Timestamp:
- 05/07/08 11:44:23 (2 months ago)
- Files:
-
- ape/trunk/ape/commandline.py (modified) (5 diffs)
- ape/trunk/ape/config.py (modified) (3 diffs)
- ape/trunk/ape/engine.py (modified) (9 diffs)
- ape/trunk/ape/__init__.py (modified) (1 diff)
- ape/trunk/ape/tests/config.py (modified) (1 diff)
- ape/trunk/setup.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
ape/trunk/ape/commandline.py
r535 r536 22 22 23 23 24 import os 24 25 import sys 25 26 from optparse import OptionParser, Option … … 73 74 assert type_name.endswith('Option') 74 75 type_name = type_name[:-6].lower() 76 metavar = config_option.metavar 77 if metavar: 78 metavar = metavar.upper() 75 79 76 80 args = dict( 77 help=config_option.__doc__ , action='callback', default=default,78 metavar= config_option.metavar, dest=config_option.name,81 help=config_option.__doc__ + ' (%default)', action='callback', default=default, 82 metavar=metavar, dest=config_option.name, 79 83 ) 80 84 … … 87 91 88 92 89 def bootstrap(cls, name, version=None, defer_startup=False, **kwargs):93 def bootstrap(cls, argv=None, name=None, version=None, defer_startup=False, **kwargs): 90 94 """Bootstrap ape.Engine with command-line options. 91 95 92 96 Args: 93 97 cls: ape.Engine subclass with identical call signature. 98 argv: sys.argv (or equivalent) 94 99 name: Name of the application. 95 100 version: Version number to provide to optparse. … … 100 105 Instance of cls. 101 106 """ 102 parser = OptionParser(conflict_handler='resolve', version=version) 107 argv = argv or sys.argv 108 name = name or os.path.basename(argv[0]) or cls.application_name 109 110 # Extract bootstrap arguments from commandline 111 parser = OptionParser(conflict_handler='resolve', 112 version=version or cls.application_version) 103 113 parser.add_option('--config', default=name + '.conf', 104 114 help='configuration file for %s' % name, 105 metavar='FILE') 106 parser.add_option('--debug', default=0, type='int', help='debug level') 115 metavar='file') 116 parser.add_option('--debug', default=0, type='int') 117 parser.add_option('--logging', default=None, type='string') 107 118 108 args = _extract_bootstrap_args(parser, sys.argv[1:])119 args = _extract_bootstrap_args(parser, argv[1:]) 109 120 options, args = parser.parse_args(args) 110 121 111 app = cls(application_name=name, config_file=options.config, 112 defer_startup=True, **kwargs) 122 app = cls( 123 application_name=name, config_file=options.config, defer_startup=True, 124 logging_uri=options.logging, debug=options.debug, **kwargs 125 ) 113 126 114 127 for name, option in ape.Option.registry.iteritems(): … … 116 129 parser.add_option(optparse_option) 117 130 118 options, args = parser.parse_args( sys.argv[1:])131 options, args = parser.parse_args(argv[1:]) 119 132 if not defer_startup: 120 133 app.startup() ape/trunk/ape/config.py
r535 r536 57 57 return sorted(self.data.items()) 58 58 59 def has_option(self, name):59 def __contains__(self, name): 60 60 return name in self.data 61 62 def update(self, data, transient=False): 63 """Bulk update configuration options from a dictionary.""" 64 self.data.update(data) 65 if not transient and self.filename: 66 # TODO Reflect this back to the configuration file 67 raise NotImplementedError 61 68 62 69 def get(self, name, default=None): … … 126 133 if instance is None: 127 134 return self 128 engine = getattr(instance, 'e ngine', None)135 engine = getattr(instance, 'e', None) 129 136 if engine is not None: 130 config = instance.e ngine.config137 config = instance.e.config 131 138 return self.accessor(config, self.name, self.default) 132 139 else: … … 134 141 135 142 def __set__(self, instance, value): 136 config = instance.e ngine.config143 config = instance.e.config 137 144 config.set(self.name, unicode(value)) 138 145 ape/trunk/ape/engine.py
r535 r536 63 63 _active_engine = None 64 64 65 def __init__(self, application_name= 'app', config_file=None,66 defer_startup=False, debug=None):65 def __init__(self, application_name=None, config_file=None, 66 defer_startup=False, logging_uri=None, debug=None): 67 67 """Construct a new Engine. 68 68 … … 72 72 defer_startup: Defer full initialisation. Logging and configuration 73 73 are loaded regardless of the state of this flag. 74 logging_uri: Logging URI. 74 75 debug: Debug level as an integer. 75 76 """ … … 77 78 ComponentManager.__init__(self) 78 79 79 self.engine = self80 self.application_name = application_name or self.application_name81 80 self.config = Configuration(config_file or self.config_file) 82 self.log = self.initialise_logging(self.logging_uri)83 81 84 82 if debug is not None: 85 83 self.config.set('debug', debug, transient=True) 84 if logging_uri is not None: 85 self.config.set('logging', logging_uri, transient=True) 86 87 88 self.e = self 89 self.application_name = application_name or self.application_name or 'app' 90 self.log = self.initialise_logging(self.logging_uri, self.debug) 86 91 87 92 if not defer_startup: … … 90 95 def startup(self): 91 96 """Start the engine.""" 92 self.log.debug(' Starting %s', self.application_name)97 self.log.debug('APE: Starting %s', self.application_name) 93 98 self.load_plugins(plugin_paths=self.plugin_paths) 94 99 for listener in self.lifecycle_listeners: … … 99 104 """Shutdown the engine.""" 100 105 if Engine._active_engine: 101 self.log.debug(' Shutting down %s', self.application_name)106 self.log.debug('APE: Shutting down %s', self.application_name) 102 107 for listener in self.lifecycle_listeners: 103 108 listener.engine_shutdown() … … 105 110 self.config = None 106 111 self.components = None 107 self.e ngine= None112 self.e = None 108 113 Engine._active_engine = None 109 114 … … 246 251 247 252 def component_activated(self, component): 248 component.e ngine= self253 component.e = self 249 254 component.log = self.log 250 255 … … 254 259 255 260 @classmethod 256 def from_commandline(cls, name=None,*args, **kwargs):261 def from_commandline(cls, *args, **kwargs): 257 262 """Build and return the Engine from the command-line. 258 263 … … 265 270 """ 266 271 from ape.commandline import bootstrap 267 return bootstrap(cls, name or 'application',*args, **kwargs)272 return bootstrap(cls, *args, **kwargs) 268 273 269 274 ape/trunk/ape/__init__.py
r535 r536 7 7 8 8 __author__ = 'Alec Thomas <alec@swapoff.org>' 9 9 try: 10 __version__ = __import__('pkg_resources').get_distribution('Pollute').version 11 except ImportError: 12 pass 10 13 11 14 from ape import engine, config, component ape/trunk/ape/tests/config.py
r533 r536 132 132 self.assertEquals([('a.option', 'x'), ('b.option', 'y')], config.options()) 133 133 134 def test_ has_option(self):134 def test_contains(self): 135 135 config = self._read() 136 self.assertEquals(False, config.has_option('option'))136 self.assertEquals(False, 'option' in config) 137 137 self._write(['option = x']) 138 138 config = self._read() 139 self.assertEquals(True, config.has_option('option'))139 self.assertEquals(True, 'option' in config) 140 140 141 141 ape/trunk/setup.py
r533 r536 4 4 setup( 5 5 name='APE', 6 version='0.0.1 alpha',6 version='0.0.1', 7 7 description='A Python Application Engine.', 8 8 author='Alec Thomas',
