root/branches/Carsten_PtrWork2/Tools/convScript/OptionHandler.py

Revision 151, 4.7 kB (checked in by cneumann, 2 years ago)

added: script for mechanical code conversions

Line 
1
2 import optparse;
3 import sys;
4 from OperationRegistry import OperationRegistry;
5 from Operations import OperationBase;
6 from Operations import GroupingOperation;
7
8 class OptionHandler:
9     """ Support for command line options.
10     """
11    
12     #### member
13     #  options -- option object (returned by optparse.parse_args)
14     #  args -- arguments list (returned by optparse.parse_args)
15     
16     options = None;
17     args = None;
18    
19     @staticmethod
20     def getOptionActive(optionName):
21         """ Returns true if the given option is active
22         """
23         #print "getOptionActive: %s" % optionName;
24         if optionName in OptionHandler.options.__dict__:
25             optionVal = OptionHandler.options.__dict__[optionName];
26            
27             if (optionVal != None) and (optionVal != False):
28                 return True;
29            
30         return False;
31        
32     @staticmethod
33     def getOptionArg(optionName):
34         """ Returns the argument of an option
35         """
36         if optionName in OptionHandler.options.__dict__:
37             return OptionHandler.options.__dict__[optionName];
38        
39         return None;
40            
41     @staticmethod
42     def getOptionList():
43         """ Low level access to the options dictionary.
44         """
45         return OptionHandler.options;
46    
47     @staticmethod
48     def getArgList():
49         """ Low level access to the arguments list.
50         """
51         return OptionHandler.args;
52    
53     @staticmethod
54     def showOpsCallback(option, opt, value, parser, *args):
55         """ Callback that prints the names of all registered operations.
56         """
57         ## begin local function for recursion
58         def printOpList(opList, indent):
59             opList.sort(key=lambda op : op.getName().lower());
60             for op in opList:
61                 if isinstance(op, GroupingOperation):
62                     opNameList = op.getOpNameList();
63                     subOpList = [OperationRegistry.getRegisteredOp(subOp)
64                          for subOp in opNameList];
65                     print indent * " " + "\"" + op.getName() + "\" <group>:";
66                     printOpList(subOpList, indent + 2);
67                 else:
68                     print indent * " " + "\"" + op.getName() + "\"";
69                
70         print "Registered operations are:";
71         ## end local function
72         
73         printOpList(OperationRegistry.getRegisteredOpList(), 2);
74         sys.exit(0);
75    
76     @staticmethod
77     def setup(args):
78         """ Setup option parser and parse arguments
79         """
80         parser = optparse.OptionParser();
81        
82         # setup the option parser
83         parser.add_option(
84             "-d", "--diff",
85             action="store",
86             type="string",
87             dest="diffFile",
88             help="write a unified diff to DIFFFILE. [default: write no diff].",
89             metavar="DIFFFILE");
90        
91         parser.add_option(
92             "-f", "--followsymlink",
93             action="store_true",
94             dest="followSymlink",
95             help="follow symbolic links to directories. [default: false].");
96        
97         parser.add_option(
98             "-l", "--logfile",
99             action="store",
100             type="string",
101             dest="logFile",
102             help="write a log to LOGFILE. [default: write no log].",
103             metavar="LOGFILE");
104        
105         parser.add_option(
106             "-n", "--noop",
107             action="store_true",
108             dest="noOp",
109             help="do not modify files on disk. [default: false].");
110        
111         parser.add_option(
112             "-o", "--operations",
113             action="append",
114             type="string",
115             dest="operations",
116             help="only perform the listed operations \"OP1:OP2:...\". [default: all].",
117             metavar="\"OP1:OP2:...\"");
118        
119         parser.add_option(
120             "-p", "--pattern",
121             action="store",
122             type="string",
123             dest="filePattern",
124             help="only operate on files matching the REGEX. [default: operate on all files].",
125             metavar="REGEX");
126        
127         parser.add_option(
128             "-s", "--show-ops",
129             action="callback",
130             callback=OptionHandler.showOpsCallback,
131             help="display a list of operation names for use with the -o option and exit.");
132            
133         parser.add_option(
134             "-v", "--verbose",
135             action="count",
136             dest="verbose",
137             help="print verbose log, can be used multiple times to increase verbosity.");
138        
139         parser.set_defaults(followsymlink=False);
140         parser.set_defaults(noop=False);
141         parser.set_defaults(verbose=0);
142        
143         (options, args) = parser.parse_args(args);
144        
145         OptionHandler.options = options;
146         OptionHandler.args = args;
Note: See TracBrowser for help on using the browser.