Changeset 536

Show
Ignore:
Timestamp:
05/07/08 11:44:23 (2 months ago)
Author:
athomas
Message:
  • Added ape.version and removed "alpha".
  • Added Configuration.update()
  • One can now provide the Engine constructor with a logging URI.
  • Renamed self.engine to self.e
  • Command-line module changes:
    • Accepts argv.
    • Extracts the application name from argv[0].
    • Logging fixed.
  • Various other minor updates.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • ape/trunk/ape/commandline.py

    r535 r536  
    2222 
    2323 
     24import os 
    2425import sys 
    2526from optparse import OptionParser, Option 
     
    7374    assert type_name.endswith('Option') 
    7475    type_name = type_name[:-6].lower() 
     76    metavar = config_option.metavar 
     77    if metavar: 
     78        metavar = metavar.upper() 
    7579 
    7680    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, 
    7983        ) 
    8084 
     
    8791 
    8892 
    89 def bootstrap(cls, name, version=None, defer_startup=False, **kwargs): 
     93def bootstrap(cls, argv=None, name=None, version=None, defer_startup=False, **kwargs): 
    9094    """Bootstrap ape.Engine with command-line options. 
    9195 
    9296    Args: 
    9397        cls: ape.Engine subclass with identical call signature. 
     98        argv: sys.argv (or equivalent) 
    9499        name: Name of the application. 
    95100        version: Version number to provide to optparse. 
     
    100105        Instance of cls. 
    101106    """ 
    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) 
    103113    parser.add_option('--config', default=name + '.conf', 
    104114                      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') 
    107118 
    108     args = _extract_bootstrap_args(parser, sys.argv[1:]) 
     119    args = _extract_bootstrap_args(parser, argv[1:]) 
    109120    options, args = parser.parse_args(args) 
    110121 
    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        ) 
    113126 
    114127    for name, option in ape.Option.registry.iteritems(): 
     
    116129        parser.add_option(optparse_option) 
    117130 
    118     options, args = parser.parse_args(sys.argv[1:]) 
     131    options, args = parser.parse_args(argv[1:]) 
    119132    if not defer_startup: 
    120133        app.startup() 
  • ape/trunk/ape/config.py

    r535 r536  
    5757        return sorted(self.data.items()) 
    5858 
    59     def has_option(self, name): 
     59    def __contains__(self, name): 
    6060        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 
    6168 
    6269    def get(self, name, default=None): 
     
    126133        if instance is None: 
    127134            return self 
    128         engine = getattr(instance, 'engine', None) 
     135        engine = getattr(instance, 'e', None) 
    129136        if engine is not None: 
    130             config = instance.engine.config 
     137            config = instance.e.config 
    131138            return self.accessor(config, self.name, self.default) 
    132139        else: 
     
    134141 
    135142    def __set__(self, instance, value): 
    136         config = instance.engine.config 
     143        config = instance.e.config 
    137144        config.set(self.name, unicode(value)) 
    138145 
  • ape/trunk/ape/engine.py

    r535 r536  
    6363    _active_engine = None 
    6464 
    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): 
    6767        """Construct a new Engine. 
    6868 
     
    7272            defer_startup: Defer full initialisation. Logging and configuration 
    7373                           are loaded regardless of the state of this flag. 
     74            logging_uri: Logging URI. 
    7475            debug: Debug level as an integer. 
    7576        """ 
     
    7778        ComponentManager.__init__(self) 
    7879 
    79         self.engine = self 
    80         self.application_name = application_name or self.application_name 
    8180        self.config = Configuration(config_file or self.config_file) 
    82         self.log = self.initialise_logging(self.logging_uri) 
    8381 
    8482        if debug is not None: 
    8583            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) 
    8691 
    8792        if not defer_startup: 
     
    9095    def startup(self): 
    9196        """Start the engine.""" 
    92         self.log.debug('Starting %s', self.application_name) 
     97        self.log.debug('APE: Starting %s', self.application_name) 
    9398        self.load_plugins(plugin_paths=self.plugin_paths) 
    9499        for listener in self.lifecycle_listeners: 
     
    99104        """Shutdown the engine.""" 
    100105        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) 
    102107            for listener in self.lifecycle_listeners: 
    103108                listener.engine_shutdown() 
     
    105110        self.config = None 
    106111        self.components = None 
    107         self.engine = None 
     112        self.e = None 
    108113        Engine._active_engine = None 
    109114 
     
    246251 
    247252    def component_activated(self, component): 
    248         component.engine = self 
     253        component.e = self 
    249254        component.log = self.log 
    250255 
     
    254259 
    255260    @classmethod 
    256     def from_commandline(cls, name=None, *args, **kwargs): 
     261    def from_commandline(cls, *args, **kwargs): 
    257262        """Build and return the Engine from the command-line. 
    258263 
     
    265270        """ 
    266271        from ape.commandline import bootstrap 
    267         return bootstrap(cls, name or 'application', *args, **kwargs) 
     272        return bootstrap(cls, *args, **kwargs) 
    268273 
    269274 
  • ape/trunk/ape/__init__.py

    r535 r536  
    77 
    88__author__ = 'Alec Thomas <alec@swapoff.org>' 
    9  
     9try: 
     10    __version__ = __import__('pkg_resources').get_distribution('Pollute').version 
     11except ImportError: 
     12    pass 
    1013 
    1114from ape import engine, config, component 
  • ape/trunk/ape/tests/config.py

    r533 r536  
    132132        self.assertEquals([('a.option', 'x'), ('b.option', 'y')], config.options()) 
    133133 
    134     def test_has_option(self): 
     134    def test_contains(self): 
    135135        config = self._read() 
    136         self.assertEquals(False, config.has_option('option')
     136        self.assertEquals(False, 'option' in config
    137137        self._write(['option = x']) 
    138138        config = self._read() 
    139         self.assertEquals(True, config.has_option('option')
     139        self.assertEquals(True, 'option' in config
    140140 
    141141 
  • ape/trunk/setup.py

    r533 r536  
    44setup( 
    55    name='APE', 
    6     version='0.0.1alpha', 
     6    version='0.0.1', 
    77    description='A Python Application Engine.', 
    88    author='Alec Thomas',