Changeset 535

Show
Ignore:
Timestamp:
05/06/08 11:24:51 (2 months ago)
Author:
athomas
Message:
  • Added util.to_list().
  • Added commandline module for bootstrapping applications from the commandline.
  • Added Engine class-level defaults for application_name and config_file.
Files:

Legend:

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

    r533 r535  
    1818import os 
    1919from ape.component import * 
    20 from ape.util import to_boolean, URI 
     20from ape.util import to_list, to_boolean, URI 
    2121 
    2222 
     
    8585 
    8686    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)) 
    8988 
    9089    def get_int(self, name, default=None): 
     
    9594 
    9695    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 
     99class 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) 
    100119        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 
    116121        self.__doc__ = doc 
     122        self.metavar = metavar 
    117123        self.registry[name] = self 
    118124 
     
    131137        config.set(self.name, unicode(value)) 
    132138 
     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 
    133145 
    134146class BoolOption(Option): 
    135     accessor = Configuration.get_bool 
     147    def cast(self, value): 
     148        return to_boolean(value) 
    136149 
    137150 
    138151class IntOption(Option): 
    139     accessor = Configuration.get_int 
     152    def cast(self, value): 
     153        return int(value) 
    140154 
    141155 
    142156class FloatOption(Option): 
    143     accessor = Configuration.get_float 
     157    def cast(self, value): 
     158        return float(value) 
    144159 
    145160 
    146161class 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): 
    149164        self.sep = sep 
    150165        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) 
    154170 
    155171 
    156172class 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
    159175 
    160176 
    161177class 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
    164180        self.xtnpt = ExtensionPoint(interface) 
    165181 
     
    168184            return self 
    169185        value = Option.__get__(self, instance, owner) 
    170         for impl in self.xtnpt.Extensions(instance): 
     186        for impl in self.xtnpt.extensions(instance): 
    171187            if impl.__class__.__name__ == value: 
    172188                return impl 
     
    184200 
    185201    def __init__(self, name, interface, default=None, 
    186                  include_missing=True, doc=''): 
     202                 doc='', include_missing=True): 
    187203        ListOption.__init__(self, name, default, doc=doc) 
    188204        self.xtnpt = ExtensionPoint(interface) 
     
    194210        order = ListOption.__get__(self, instance, owner) 
    195211        components = [] 
    196         for impl in self.xtnpt.Extensions(instance): 
     212        for impl in self.xtnpt.extensions(instance): 
    197213            if self.include_missing or impl.__class__.__name__ in order: 
    198214                components.append(impl) 
  • ape/trunk/ape/engine.py

    r533 r535  
    2626 
    2727# Note: Error is re-exported from ape.component. 
    28 __all__ = ['Error', 'Engine', 'get_active_engine'] 
     28__all__ = ['Error', 'Engine', 'IEngineLifecycleListener', 'get_active_engine'] 
    2929 
    3030 
     
    4343 
    4444class 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    """ 
    4551 
    4652    lifecycle_listeners = ExtensionPoint(IEngineLifecycleListener) 
    4753 
    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 
    5162 
    5263    _active_engine = None 
     
    5465    def __init__(self, application_name='app', config_file=None, 
    5566                 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        """ 
    5676        Component.__init__(self) 
    5777        ComponentManager.__init__(self) 
    5878 
    5979        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
    6282        self.log = self.initialise_logging(self.logging_uri) 
    6383 
     
    233253        return True 
    234254 
     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 
    235269 
    236270def 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 
     11from ape import engine, config, component 
    212from ape.engine import * 
    313from ape.config import * 
    414from ape.component import * 
    5 from ape.util import * 
    615 
    716 
  • ape/trunk/ape/util.py

    r533 r535  
    162162        >>> u == v 
    163163        True 
    164         >>> v.host = 'www.google.com' 
     164        >>> v.host = 'www.example.com' 
    165165        >>> u == v 
    166166        False 
     
    306306 
    307307 
     308def 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 
    308319def get_last_traceback(): 
    309320    """Write current exception traceback to a string.