Statistics
| Branch: | Revision:

wcn_emulator / parameters_parser.py @ f26348de

History | View | Annotate | Download (4.4 KB)

1
import sys
2
import getopt
3

    
4

    
5

    
6
class parameters():
7
    """ configuration parameters storage class."""
8

    
9
    def __init__(self, programName, neededParams, optionalParams):
10
        """ initialize the parameter class. needeParams/optionalParams are lists
11
        of couples in the form: 
12
        "command line option"->[optionName, wantsValue, 
13
                   defaultValue, usageMessage, type]
14
        optional parameters should always use False as default value, so they 
15
        return False on getParam().
16

17
        ***Use only one-letter options***
18
        """
19

    
20
        self.neededParamsNames = {}
21
        self.optionalParamsNames = {}
22
        self.neededParams = {}
23
        self.optionalParams = {}
24

    
25
        self.programName = programName
26
        
27
        optionalParams.append(("-h", ["help", False, False, "show this help",
28
            int]))
29
        self.setParams(neededParams, optionalParams)
30

    
31
    def setParams(self, neededParams, optionalParams):
32
        self.parserString = ""
33
        for p in neededParams:
34
            self.neededParamsNames[p[0]] = p[1]
35
            if p[1][1] == True:
36
                self.parserString += p[0][1]+":"
37
            else:
38
                self.parserString += p[0][1]
39
        for p in optionalParams:
40
            self.optionalParamsNames[p[0]] = p[1]
41
            if p[1][1] == True:
42
                self.parserString += p[0][1]+":"
43
            else:
44
                self.parserString += p[0][1]
45

    
46
    def checkNeededParams(self):
47
        """ check if all needed params have been set """
48
        for clp,value in self.neededParamsNames.items():
49
            if value[0] not in self.neededParams:
50
                print >> sys.stderr, clp+" is a mandatory parameter "
51
                self.printUsage()
52
                sys.exit(1)
53
 
54
    def checkCorrectness(self):
55
        """ do some consistence checks here for the configuration parameters """
56
        self.checkNeededParams()
57
        if self.getParam("help") == True:
58
            return False
59
        return True
60

    
61
    def printUsage(self):
62
        """ print the usage of the program """
63
        print >> sys.stderr
64
        print >> sys.stderr, "usage ", self.programName+":"
65
        for pname, pvalue in self.neededParamsNames.items():
66
            print >> sys.stderr, " ", pname, pvalue[3]
67
        for pname, pvalue in self.optionalParamsNames.items():
68
            print >> sys.stderr, " [",pname, pvalue[3], "]"
69

    
70
    def getParam(self, paramName):
71
        """ return a configuration parameter """
72
        for pname, pvalue in self.neededParamsNames.items():
73
            if pvalue[0] == paramName:
74
                if paramName in self.neededParams:
75
                    return self.neededParams[paramName]
76
        for pname, pvalue in self.optionalParamsNames.items():
77
            if pvalue[0] == paramName:
78
                if paramName in self.optionalParams:
79
                    return self.optionalParams[paramName]
80
                else:
81
                    return pvalue[2]
82
        print >> sys.stderr, "Coding error: param", paramName,"is not",\
83
            "among the available options"
84
        sys.exit(1)
85

    
86
    def printConf(self):
87
        """ just print all the configuration for debug """
88
        print ""
89
        for pname, pvalue in self.neededParams.items():
90
            print pname, pvalue
91
        for pname, pvalue in self.optionalParams.items():
92
            print pname, pvalue
93

    
94
    def parseArgs(self):
95
        """ argument parser """
96
        try:
97
            opts, args = getopt.getopt(sys.argv[1:], self.parserString)
98
        except getopt.GetoptError, err:
99
            print >> sys.stderr,  str(err)
100
            self.printUsage()
101
            sys.exit(2)
102
        for option,v in opts:
103
            if option == "-h":
104
                self.printUsage()
105
                sys.exit(2)
106
            if option in self.neededParamsNames.keys():
107
                optionValue = self.neededParamsNames[option]
108
                if optionValue[1] == True:
109
                    self.neededParams[optionValue[0]] = optionValue[4](v)
110
                else:
111
                    self.neededParams[optionValue[0]] = True
112
            elif option in self.optionalParamsNames.keys():
113
                optionValue = self.optionalParamsNames[option]
114
                if optionValue[1] == True:
115
                    self.optionalParams[optionValue[0]] = optionValue[4](v)
116
                else:
117
                    self.optionalParams[optionValue[0]] = True
118
            else:
119
                assert False, "unhandled option"
120
    
121

    
122