add a global version of getListFromFile()
[monitor.git] / config.py
1 #!/usr/bin/python
2 import pickle
3 import os
4 import getopt
5 import sys
6 import __main__
7 from optparse import OptionParser
8
9 config_command = False
10
11 XMLRPC_SERVER="https://boot.planet-lab.org/PLCAPI/"
12 def parse_bool(option, opt_str, value, parser):
13         if opt_str in ["--debug"]:
14                 parser.values.debug = int(int(value))
15         elif opt_str in ["--mail"]:
16                 parser.values.mail = int(int(value))
17         elif opt_str in ["--bcc"]:
18                 parser.values.bcc = int(int(value))
19         elif opt_str in ["--policysavedb"]:
20                 parser.values.policysavedb = int(int(value))
21         elif opt_str in ["--squeeze"]:
22                 parser.values.squeeze = int(int(value))
23         else:
24                 print "blue"
25
26 def getListFromFile(file):
27         f = open(file, 'r')
28         list = []
29         for line in f:
30                 line = line.strip()
31                 list += [line]
32         return list
33
34 class config:
35         debug=0
36         mail=0
37         bcc=0
38         email="soltesz@cs.utk.edu"
39         run=False
40         checkopt=False
41         squeeze=0
42         policysavedb=0
43         __file = ".config"
44
45         def __init__(self, parser=None):
46                 if os.path.exists(self.__file): # file exists, read that.
47                         f = open(self.__file, 'r')
48                         o = pickle.load(f)
49                         self.__dict__.update(o)
50                         f.close()
51
52                 if parser == None:
53                         self.parser = OptionParser()
54                 else:
55                         self.parser = parser
56
57                 self.parser.set_defaults(debug = self.debug,
58                                                                 mail = self.mail,
59                                                                 bcc  = self.bcc,
60                                                                 email = self.email,
61                                                                 run = self.run,
62                                                                 checkopt = False,
63                                                                 squeeze = self.squeeze,
64                                                                 policysavedb = self.policysavedb)
65
66                 self.parser.add_option("", "--debug", dest="debug",
67                                                   help="Enable debugging", 
68                                                   type="int",
69                                                   metavar="[0|1]",
70                                                   action="callback", 
71                                                   callback=parse_bool)
72                 self.parser.add_option("", "--mail", dest="mail",
73                                                   help="Enable sending email",
74                                                   type="int",
75                                                   metavar="[0|1]",
76                                                   action="callback", 
77                                                   callback=parse_bool)
78                 self.parser.add_option("", "--bcc", dest="bcc",
79                                                   help="Include BCC to user",
80                                                   type="int",
81                                                   metavar="[0|1]",
82                                                   action="callback", 
83                                                   callback=parse_bool)
84                 self.parser.add_option("", "--squeeze", dest="squeeze",
85                                                   help="Squeeze sites or not",
86                                                   type="int",
87                                                   metavar="[0|1]",
88                                                   action="callback", 
89                                                   callback=parse_bool)
90                 self.parser.add_option("", "--policysavedb", dest="policysavedb",
91                                                   help="Save the policy event database after a run",
92                                                   type="int",
93                                                   metavar="[0|1]",
94                                                   action="callback", 
95                                                   callback=parse_bool)
96                 self.parser.add_option("", "--checkopt", dest="checkopt", 
97                                                   action="store_true",
98                                                   help="print current options")
99                 self.parser.add_option("", "--run", dest="run", 
100                                                   action="store_true",
101                                                   help="Perform monitor or print configs")
102                 self.parser.add_option("", "--email", dest="email",
103                                                   help="Specify an email address to use for mail when "+\
104                                                                  "debug is enabled or for bcc when it is not")
105                 
106                 # config_command is needed to keep subsequent loads of config() from
107                 # trying to parse the arguments that have already been parsed by
108                 # the new main().
109                 if parser == None and config_command:
110                         print "calling parse_args"
111                         self.parse_args()
112
113         def parse_args(self):
114                 #print "self: %s" % self
115                 #import traceback
116                 #print traceback.print_stack()
117                 #print "Ccalling parse_args"
118                 (options, args) = self.parser.parse_args()
119                 #for o in options.__dict__:
120                 #       print "optin: %s == %s" % (o, options.__dict__[o])
121                 self.__dict__.update(options.__dict__)
122                 self.__dict__['args'] = args
123                 self.save(options)
124                 if options.checkopt:
125                         self.usage()
126                 #       print "\nAdd --run to actually perform the command"
127                         sys.exit(1)
128
129         def getListFromFile(self, file):
130                 f = open(file, 'r')
131                 list = []
132                 for line in f:
133                         line = line.strip()
134                         list += [line]
135                 return list
136
137         def print_values(self):
138                 exclude = ['parser']
139                 for key in self.__dict__.keys():
140                         if key not in exclude:
141                                 print "%20s == %s" % (key, self.__dict__[key])
142                 
143         def save(self, options=None):
144                 f = open(self.__file, 'w')
145                 if options == None:
146                         o = {'debug': self.debug, 
147                                  'mail': self.mail, 
148                                  'bcc': self.bcc, 
149                                  'email':self.email,
150                                  'squeeze':self.squeeze,
151                                  'policysavedb':self.policysavedb}
152                 else:
153                         o = options.__dict__
154
155                 pickle.dump(o, f)
156                 f.close()
157
158         def usage(self):
159                 self.print_values()
160                 self.parser.print_help()
161
162
163 def main():
164         """ Start threads, do some housekeeping, then daemonize. """
165         # Defaults
166         global config_command
167         config_command = True
168         config = __main__.config()
169
170         try:
171                 print "acalling parse_args"
172                 config.parse_args()
173                 
174         except Exception, err:
175                 print "Error: %s " %  err
176                 config.usage()
177                 sys.exit(1)
178
179         config.usage()
180
181
182 if __name__ == '__main__':
183         main()