File: process.py

package info (click to toggle)
synopsis 0.8.0-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 10,112 kB
  • ctags: 12,996
  • sloc: cpp: 34,254; ansic: 33,620; python: 10,975; sh: 7,261; xml: 6,369; makefile: 773; asm: 445
file content (80 lines) | stat: -rw-r--r-- 2,445 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#
# Copyright (C) 2003 Stefan Seefeld
# All rights reserved.
# Licensed to the public under the terms of the GNU LGPL (>= 2),
# see the file COPYING for details.
#

from Processor import Processor, Error
import AST
from getoptions import get_options

import sys

def error(msg):
   """Write an error message and exit."""
   sys.stderr.write(msg)
   sys.stderr.write('\n')
   sys.exit(-1)

def process(argv = sys.argv, **commands):
   """Accept a set of commands and process according to command line options.
   The typical call will start with the name of the processor to be executed,
   followed by a set of parameters, followed by non-parameter arguments.
   All parameters are either of the form 'name=value', or '--name=value'.
   The first form expects 'value' to be valid python, the second a string.
   The remaining non-parameter arguments are associated with the 'input'
   parameter.
   Once this initialization is done, the named command's 'process' method
   is executed.
   """

   #first make sure the function was called with the correct argument types
   for c in commands:
      if not isinstance(commands[c], Processor):
         error("command '%s' isn't a valid processor"%c)

   if len(argv) < 2:
      error("Usage : %s <command> [args] [input files]"%argv[0])

   elif argv[1] == '--help':
      print "Usage: %s --help"%argv[0]
      print "   or: %s <command> --help"%argv[0]
      print "   or: %s <command> [parameters]"%argv[0]
      print ""
      print "Available commands:"
      for c in commands:
         print "   %s"%c
      sys.exit(0)

   command = argv[1]
   args = argv[2:]

   if '--help' in args:
      print "Parameters for command '%s'"%command
      parameters = commands[command].get_parameters()
      tab = max(map(lambda x:len(x), parameters.keys()))
      for p in parameters:
         print "   %-*s     %s"%(tab, p, parameters[p].doc)
      sys.exit(0)

   props = {}
   # process all option arguments...
   for o, a in get_options(args): props[o] = a

   # ...and keep remaining (non-option) arguments as 'input'
   if args: props['input'] = args

   if command in commands:
      ast = AST.AST()
      try:
         commands[command].process(ast, **props)
      except KeyError, e:
         error('missing argument "%s"'%e)
      except Error, e:
         error(str(e))
      except KeyboardInterrupt, e:
         print 'KeyboardInterrupt'
   else:
      error('no command "%s"'%command)