Synopsis - Synopsis/process.py

File: Synopsis/process.py

     1#
     2# Copyright (C) 2003 Stefan Seefeld
     3# All rights reserved.
     4# Licensed to the public under the terms of the GNU LGPL (>= 2),
     5# see the file COPYING for details.
     6#
     7
     8from Processor import Processor, Error
     9import IR
    10from getoptions import get_options
    11
    12import sys
    13
    14def error(msg):
    15    """Write an error message and exit."""
    16    sys.stderr.write(msg)
    17    sys.stderr.write('\n')
    18    sys.exit(-1)
    19
    20def process(argv = sys.argv, **commands):
    21    """Accept a set of commands and process according to command line options.
    22    The typical call will start with the name of the processor to be executed,
    23    followed by a set of parameters, followed by non-parameter arguments.
    24    All parameters are either of the form 'name=value', or '--name=value'.
    25    The first form expects 'value' to be valid python, the second a string.
    26    The remaining non-parameter arguments are associated with the 'input'
    27    parameter.
    28    Once this initialization is done, the named command's 'process' method
    29    is executed.
    30    """
    31
    32    #first make sure the function was called with the correct argument types
    33    for c in commands:
    34        if not isinstance(commands[c], Processor):
    35            error("command '%s' isn't a valid processor"%c)
    36
    37    if len(argv) < 2:
    38        error("Usage : %s <command> [args] [input files]"%argv[0])
    39
    40    elif argv[1] == '--help':
    41        print "Usage: %s --help"%argv[0]
    42        print "   or: %s <command> --help"%argv[0]
    43        print "   or: %s <command> [parameters]"%argv[0]
    44        print ""
    45        print "Available commands:"
    46        for c in commands:
    47            print "   %s"%c
    48        sys.exit(0)
    49
    50    command = argv[1]
    51    args = argv[2:]
    52
    53    if '--help' in args:
    54        print "Parameters for command '%s'"%command
    55        parameters = commands[command].get_parameters()
    56        tab = max(map(lambda x:len(x), parameters.keys()))
    57        for p in parameters:
    58            print "   %-*s     %s"%(tab, p, parameters[p].doc)
    59        sys.exit(0)
    60
    61    props = {}
    62    # process all option arguments...
    63    for o, a in get_options(args): props[o.replace('-', '_')] = a
    64
    65    # ...and keep remaining (non-option) arguments as 'input'
    66    if args: props['input'] = args
    67
    68    if command in commands:
    69        ir = IR.IR()
    70        try:
    71            commands[command].process(ir, **props)
    72        except Error, e:
    73            error(str(e))
    74        except KeyboardInterrupt, e:
    75            print 'KeyboardInterrupt'
    76        except IOError, e:
    77            error(str(e))
    78    else:
    79        error('no command "%s"'%command)
    80
    81

Generated on Thu Apr 16 16:27:16 2009 by
synopsis (version devel)