Changeset 537

Show
Ignore:
Timestamp:
05/08/2008 03:59:31 AM (3 months ago)
Author:
athomas
Message:
  • Refactored command-line API.
Files:

Legend:

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

    r536 r537  
    2424import os 
    2525import sys 
     26import traceback 
    2627from optparse import OptionParser, Option 
    2728import ape 
     29from ape.component import Component 
    2830from ape.util import URI, to_boolean 
    2931 
    3032 
    31 __all__ = ['bootstrap'] 
     33__all__ = ['CommandLine', 'bootstrap'] 
     34 
     35 
     36class CommandLine(Component): 
     37    abstract = True 
     38 
     39    def run(self): 
     40        """Command-line applications should override this method.""" 
     41        raise NotImplementedError 
     42 
     43    @classmethod 
     44    def bootstrap(cls, engine_cls, **kwargs): 
     45        """Start and run command-line with an Engine.""" 
     46 
     47        try: 
     48            e = bootstrap(engine_cls, **kwargs) 
     49            self = cls(e) 
     50        except ape.Error, e: 
     51            print >> sys.stderr, 'fatal:', str(e) 
     52            sys.exit(1) 
     53 
     54        try: 
     55            try: 
     56                self.run() 
     57            except ape.Error, e: 
     58                print >> sys.stderr, 'fatal:', str(e) 
     59                sys.exit(1) 
     60            except KeyboardInterrupt: 
     61                print '^C' 
     62        finally: 
     63            try: 
     64                self.e.shutdown() 
     65            except Exception, e: 
     66                print >> sys.stderr, 'Engine shutdown failed!' 
     67                traceback.print_exc() 
     68                sys.exit(-1) 
     69 
     70        sys.exit(0) 
    3271 
    3372 
  • ape/trunk/ape/__init__.py

    r536 r537  
    1212    pass 
    1313 
    14 from ape import engine, config, component 
     14from ape import engine, config, component, commandline 
    1515from ape.engine import * 
    1616from ape.config import * 
    1717from ape.component import * 
     18from ape.commandline import * 
    1819 
    1920 
    20 __all__ = engine.__all__ + config.__all__ + component.__all__ 
     21__all__ = engine.__all__ + config.__all__ + component.__all__ + commandline.__all__ 
  • ape/trunk/ape/util.py

    r535 r537  
    170170    def __repr__(self): 
    171171        return "URI(u'%s')" % unicode(self) 
     172 
     173    def __nonzero__(self): 
     174        return len(str(self)) 
    172175 
    173176    def __str__(self):