1 | # -*- coding: utf-8 -*-
|
---|
2 | # $Id: config_classes.py 12290 2011-02-10 14:02:38Z pstorz $
|
---|
3 | import re
|
---|
4 | import logging
|
---|
5 | import copy
|
---|
6 | logger = logging.getLogger("directive")
|
---|
7 | import scheduleparser # schedule class
|
---|
8 | from auto_types import *
|
---|
9 |
|
---|
10 | from prettynames import *
|
---|
11 |
|
---|
12 | RXP_SIMPLE_ITEM = re.compile(r'''
|
---|
13 | (^|;)(\s*(?P<itemname>[\w ]+?)?\s*=\s*(?P<value>"?[^;^\n]+"?)(?P<comment>\s+\#.*)?) # xxx = yyy # comment
|
---|
14 | ''',re.M| re.X)
|
---|
15 | #(^|;)(\s*(?P<itemname>[\w ]+?)?\s*=\s*(?P<value>"?[^{^}^;^\n]+"?)(?P<comment>\s+\#.*)?) # xxx = yyy # comment
|
---|
16 |
|
---|
17 | #(^|;)(\s*(?P<itemname>[\w ]+?)?\s*=\s*(?P<value>"?[^{^}^;^\n^\s]+"?)\s*(?P<comment>\s+\#.*)?) # xxx = yyy # comment
|
---|
18 |
|
---|
19 | RXP_RESTYPE = re.compile(r'''
|
---|
20 | ^\s*(?P<resourcetype>\w[\w ]+?\s*(=\s+)?\s?)$
|
---|
21 | ''', re.M|re.X|re.S)
|
---|
22 |
|
---|
23 | # TODO: allow spaces in resourcetypes like sd addresses!
|
---|
24 | # this does not work now
|
---|
25 | # SD Addresses = <
|
---|
26 |
|
---|
27 | RXP_NRTEXT = re.compile(r'''
|
---|
28 | (?P<number>\d+)\s?(?P<text>[A-Za-z]+)
|
---|
29 | ''', re.M|re.X|re.S)
|
---|
30 |
|
---|
31 | #RXP_TEXTNR = re.compile(r'''
|
---|
32 | #(?P<text>\w+)\s?(?P<number>\d+)
|
---|
33 | #''', re.M|re.X|re.S)
|
---|
34 |
|
---|
35 | # what resources can appear multiple times in director configs?
|
---|
36 | MULTIPLE_RESOURCES_IN_DIRD = set([
|
---|
37 | 'client',
|
---|
38 | 'console',
|
---|
39 | 'fileset',
|
---|
40 | 'job',
|
---|
41 | 'jobdefs',
|
---|
42 | 'messages',
|
---|
43 | 'pool',
|
---|
44 | 'schedule',
|
---|
45 | 'storage',
|
---|
46 | ])
|
---|
47 |
|
---|
48 | INTERNALLY_REFERENCED_ITEMS = set(['fileset',
|
---|
49 | 'client' ,
|
---|
50 | 'catalog',
|
---|
51 | 'messages',
|
---|
52 | 'schedule',
|
---|
53 | 'storage',
|
---|
54 | 'jobdefs',
|
---|
55 | 'pool',
|
---|
56 | 'incrementalbackuppool',
|
---|
57 | 'fullbackuppool',
|
---|
58 | 'differentialbackuppool',
|
---|
59 | 'job',
|
---|
60 | 'basejob',
|
---|
61 | 'device',
|
---|
62 | ])
|
---|
63 |
|
---|
64 | # translate full name to short name
|
---|
65 | res2shrtn = { 'catalog' :'cat',
|
---|
66 | 'client' :'cli',
|
---|
67 | 'console' :'con',
|
---|
68 | 'counter' :'counter',
|
---|
69 | 'director' :'dir',
|
---|
70 | 'fileset' :'fs',
|
---|
71 | 'filedaemon' :'cli', # filedaemon is the same as cli
|
---|
72 | 'job' :'job',
|
---|
73 | 'jobdefs' :'job', # jobdefs are the same as jobs
|
---|
74 | 'messages' :'msgs',
|
---|
75 | 'pool' :'pool',
|
---|
76 | 'runscript' :'runscript',
|
---|
77 | 'schedule' :'sch',
|
---|
78 | 'storage' :'store',
|
---|
79 | #'console' :'con',
|
---|
80 | 'autochanger' :'changer',
|
---|
81 | 'device' :'dev',
|
---|
82 | 'options' :'options',
|
---|
83 | #manually added
|
---|
84 | 'diraddresses' :'addresses',
|
---|
85 | 'fdaddresses' :'addresses',
|
---|
86 | 'sdaddresses' :'addresses',
|
---|
87 | 'ip' :'ip',
|
---|
88 | 'ipv4' :'ip',
|
---|
89 | 'ipv6' :'ip',
|
---|
90 | 'include' : 'include',
|
---|
91 | 'exclude' : 'include',
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | # which itemsnames can appear multiple times?
|
---|
96 | MULTIPLE_ALLOWED_ITEMS_SET = set([
|
---|
97 | 'file',
|
---|
98 | 'run',
|
---|
99 | ])
|
---|
100 |
|
---|
101 | #MULTIPLE_ALLOWED_RESOURCES_SET = set([
|
---|
102 | # 'include',
|
---|
103 | # 'exclude',
|
---|
104 | # 'ip',
|
---|
105 | # ])
|
---|
106 |
|
---|
107 |
|
---|
108 | timemultiplicators = {
|
---|
109 | "seconds" : 1,
|
---|
110 | "minutes" : 60,
|
---|
111 | "mins" : 60,
|
---|
112 | "hours" : 3600,
|
---|
113 | "days" : 3600*24,
|
---|
114 | "weeks" : 3600*24*7,
|
---|
115 | "quarters" : 3600*24*91,
|
---|
116 | "years" : 3600*24*365,
|
---|
117 | "months" : 60*60*24*30,
|
---|
118 | "n" : 60,
|
---|
119 | }
|
---|
120 |
|
---|
121 | datamultiplicators = {
|
---|
122 | "k" : 1024, #/* kilobyte */
|
---|
123 | "kb": 1000, #/* kb kilobyte */
|
---|
124 | "m" : 1048576, #/* megabyte */
|
---|
125 | "mb": 1000000, #/* mb megabyte */
|
---|
126 | "g" : 1073741824, #/* gigabyte */
|
---|
127 | "gb": 1000000000}; #/* gb gigabyte */
|
---|
128 |
|
---|
129 |
|
---|
130 | RESSOURCES_WITH_EQUALSIGN = set(['diraddresses',
|
---|
131 | 'fdaddresses',
|
---|
132 | 'sdaddresses',
|
---|
133 | 'ip',
|
---|
134 | 'ipv4',
|
---|
135 | 'ipv6',
|
---|
136 | ])
|
---|
137 |
|
---|
138 | #INDENTSTRING = '|--|'
|
---|
139 | INDENTSTRING = ' '
|
---|
140 |
|
---|
141 |
|
---|
142 |
|
---|
143 |
|
---|
144 | class Item(object):
|
---|
145 | def __init__(self, name, value, defaultvalue, default, type, required, parentresource = None, indentlevel = 0):
|
---|
146 | self.indentlevel = indentlevel
|
---|
147 | self.parentresource = parentresource
|
---|
148 | self.name = name.lower().replace(' ','')
|
---|
149 | self.defaultvalue = defaultvalue
|
---|
150 | self.default = default
|
---|
151 | self.value = value # real value lies in storage
|
---|
152 | self.type = type
|
---|
153 | self.required = required
|
---|
154 | self.printall = False
|
---|
155 | #TODO: try/except this eval
|
---|
156 | #print type, defaultvalue
|
---|
157 |
|
---|
158 | #storage1 = eval(type + '(' + str(defaultvalue) +')' )
|
---|
159 | #self.storage = copy.deepcopy(storage1)
|
---|
160 | #logger.debug('storage: ' +str(id(self.storage)) + ' storage1: ' + str(id(storage1)))
|
---|
161 |
|
---|
162 | self.storage = eval(type + '(' + str(defaultvalue) +')' )
|
---|
163 | self.storage.setValue(value)
|
---|
164 |
|
---|
165 |
|
---|
166 | self.storage.indentlevel = self.indentlevel + 1
|
---|
167 | self.storage.setParentItem(self)
|
---|
168 |
|
---|
169 |
|
---|
170 | def __lt__(self,other):
|
---|
171 | #print "comparing", self.name, other.name
|
---|
172 | if self.name == 'name': # name comes first
|
---|
173 | #print self.name,"<",other.name
|
---|
174 | return True
|
---|
175 | if self.name < other.name :
|
---|
176 | #print self.name,"<",other.name
|
---|
177 | return True
|
---|
178 | else:
|
---|
179 | #print self.name,">",other.name
|
---|
180 | return False
|
---|
181 |
|
---|
182 |
|
---|
183 | def __repr__(self):
|
---|
184 | string_types = set(['store_time'])
|
---|
185 | if self.type in string_types: # explicitly store this in a string
|
---|
186 | return "Item(\'%s\', \'%s\', \'%s\', %s, \'%s\', %s)" % (str(self.name),
|
---|
187 | self.value,
|
---|
188 | self.defaultvalue,
|
---|
189 | self.default,
|
---|
190 | str(self.type),
|
---|
191 | self.required)
|
---|
192 | else:
|
---|
193 | return "Item(\'%s\', %s, %s, %s, \'%s\', %s)" % (str(self.name),
|
---|
194 | self.value,
|
---|
195 | self.defaultvalue,
|
---|
196 | self.default,
|
---|
197 | str(self.type),
|
---|
198 | self.required)
|
---|
199 |
|
---|
200 |
|
---|
201 | ''' find out if this item is to be printed or not '''
|
---|
202 | def print_item(self):
|
---|
203 | printout = self.printall # override to print all out
|
---|
204 | if self.default:
|
---|
205 | if str(self.storage.value) != str(self.defaultvalue):
|
---|
206 | logger.debug( "printing " + self.name + " as self.storage.value != self.defaultvalue:"
|
---|
207 | + str(self.storage.value) + ' != ' + str(self.defaultvalue) + '<' )
|
---|
208 | printout = True
|
---|
209 | else:
|
---|
210 | #pass
|
---|
211 | logger.debug( "NOT printing " + self.name + " as self.storage.value == self.defaultvalue:"
|
---|
212 | + str(self.storage.value) + ' == ' + str(self.defaultvalue) + '<' )
|
---|
213 |
|
---|
214 | elif self.required:
|
---|
215 | # jobs can inherit their items from jobdefs, jobdefs do not need a client
|
---|
216 | # but a name is needed anyway
|
---|
217 | # TODO: merge jobs and jobdefs in order to check if they are complete after merge
|
---|
218 |
|
---|
219 | # jobs and jobdefs
|
---|
220 | if self.parentresource.resourcetype.startswith('job') and self.value == self.defaultvalue and str(self.storage.value) == str(self.defaultvalue):
|
---|
221 | printout = False
|
---|
222 | else:
|
---|
223 | logger.debug( "printing " + self.name + " as self.required is set")
|
---|
224 | printout = True
|
---|
225 |
|
---|
226 | elif self.storage.value is not None:
|
---|
227 |
|
---|
228 | if self.storage.value != self.defaultvalue:
|
---|
229 | logger.debug( "printing " + self.name + " as self.storage.value is set to :>"
|
---|
230 | + str( self.storage.value )+'<')
|
---|
231 | printout = True
|
---|
232 |
|
---|
233 | return printout
|
---|
234 |
|
---|
235 |
|
---|
236 | def __str__(self):
|
---|
237 | '''
|
---|
238 | standard behaviour:
|
---|
239 | only print items that are configured and have not a different value than the default value
|
---|
240 | '''
|
---|
241 |
|
---|
242 | if self.print_item():
|
---|
243 | return '%s%s = %s\n' % ( self.indentlevel * INDENTSTRING, prettyName(self.name), self.storage)
|
---|
244 | #return ' %s = %s\n' % (prettyName(self.name), self.storage)
|
---|
245 | else:
|
---|
246 | return ''
|
---|
247 |
|
---|
248 | def setValue(self, value):
|
---|
249 | '''set value item'''
|
---|
250 | self.storage.setValue(value)
|
---|
251 |
|
---|
252 |
|
---|
253 |
|
---|
254 |
|
---|
255 |
|
---|
256 | #class command(object):
|
---|
257 | # '''
|
---|
258 | # this class contains the information to a console command, also dot commands
|
---|
259 | # '''
|
---|
260 | # def __init__(self, commandtext, commandname, usage, help, inrunscript):
|
---|
261 | # self.commandtext = commandtext
|
---|
262 | # self.commandname = commandname
|
---|
263 | # self.usage = usage
|
---|
264 | # self.help = help
|
---|
265 | # self.inrunscript = inrunscript
|
---|
266 | #
|
---|
267 | # def __repr__(self):
|
---|
268 | # return "command('%s','%s','%s','%s','%s')" % (self.commandtext, self.commandname, self.usage, self.help, self.inrunscript)
|
---|
269 |
|
---|
270 |
|
---|
271 |
|
---|
272 |
|
---|
273 | # the bacula internal storage representations
|
---|
274 | #
|
---|
275 | # base classes for all storages
|
---|
276 | class Item_Storage(object):
|
---|
277 | """
|
---|
278 | base class for the representation of any item object
|
---|
279 | """
|
---|
280 |
|
---|
281 | def __init__(self,val):
|
---|
282 | self.value = val
|
---|
283 | self.comment = ''
|
---|
284 | self.possiblevalues = [] # what values are possible here ?
|
---|
285 | self.parentitem = None
|
---|
286 |
|
---|
287 | def __str__(self):
|
---|
288 | #TODO do this in a nicer way without a catch-all try-except
|
---|
289 | if self.comment != '':
|
---|
290 | comment = "\t# " + self.comment
|
---|
291 | else:
|
---|
292 | comment = ''
|
---|
293 | try:
|
---|
294 | #return self.value
|
---|
295 | return self.value + comment #+self.__class__.__name__ + ' id: '+ str(id(self))
|
---|
296 | except:
|
---|
297 | #return str(self.value)
|
---|
298 | return unicode(self.value) + comment #+self.__class__.__name__ + ' id: '+ str(id(self))
|
---|
299 |
|
---|
300 | def setParentItem(self,parentItem):
|
---|
301 | self.parentitem = parentItem
|
---|
302 |
|
---|
303 | def setValue(self,value):
|
---|
304 | logger.debug( self.__class__.__name__ +':' + 'called self.value with \''+ str(value) + '\'')
|
---|
305 | self.value = value
|
---|
306 | logger.debug("self.value is now" + str(self) )
|
---|
307 |
|
---|
308 | class Item_Storage_String (Item_Storage):
|
---|
309 | pass
|
---|
310 | #def __str__(self):
|
---|
311 | # return str(self.value)
|
---|
312 | #return '"' + str(self.value) + '"'# + '\t# '+ self.__class__.__name__
|
---|
313 |
|
---|
314 |
|
---|
315 | # generated
|
---|
316 | class store_pint32( Item_Storage ):
|
---|
317 | def __str__(self):
|
---|
318 | return str(self.value) #+ '\t# '+self.__class__.__name__ + 'id: '+ str(id(self))
|
---|
319 | #return str(self.value) + '\t# '+self.__class__.__name__ + 'id: '+ str(id(self))
|
---|
320 |
|
---|
321 |
|
---|
322 |
|
---|
323 |
|
---|
324 | class store_name( Item_Storage_String ):
|
---|
325 |
|
---|
326 | def setValue(self,value):
|
---|
327 | logger.debug( self.__class__.__name__ +':' + 'called self.value with \''+ unicode(value) + '\'')
|
---|
328 | if value != None and value != 0:
|
---|
329 | if type(value) == str:
|
---|
330 | value = value.strip('"')
|
---|
331 | else:
|
---|
332 | value = str(value)
|
---|
333 | self.value = '"' + value.strip('"') + '"'
|
---|
334 | else:
|
---|
335 | self.value = value
|
---|
336 | logger.debug("self.value is now " + unicode(self) )
|
---|
337 |
|
---|
338 |
|
---|
339 | class store_str( store_name ):
|
---|
340 | pass
|
---|
341 |
|
---|
342 | class store_time( Item_Storage ):
|
---|
343 | def __init__(self,val):
|
---|
344 | self.value = val
|
---|
345 | self.comment = ''
|
---|
346 | self.possiblevalues = [] # what values are possible here ?
|
---|
347 | self.parentitem = None
|
---|
348 |
|
---|
349 | def __str__(self):
|
---|
350 | #TODO do this in a nicer way without a catch-all try-except
|
---|
351 | if self.value is None:
|
---|
352 | return str(self.value)
|
---|
353 | outstring = ''
|
---|
354 | for mult in ["years", "quarters" ,"months" ,"weeks", "days","hours","minutes","seconds"]:
|
---|
355 | if self.value % timemultiplicators[mult] == 0:
|
---|
356 | outstring = "%d %s " % (self.value/timemultiplicators[mult], mult)
|
---|
357 | return outstring
|
---|
358 | return outstring
|
---|
359 |
|
---|
360 | def setParentItem(self,parentItem):
|
---|
361 | self.parentitem = parentItem
|
---|
362 |
|
---|
363 | def setValue(self,value):
|
---|
364 | logger.debug( self.__class__.__name__ +':' + 'called self.value with \''+ str(value) + '\'')
|
---|
365 | self.value = self.__parse_timeinput__(value)
|
---|
366 | logger.debug("self.value is now" + str(self) )
|
---|
367 |
|
---|
368 | def __partNumberKeywords__(self,string):
|
---|
369 | '''
|
---|
370 | inserts a space between numbers and keywords if there is none
|
---|
371 | '''
|
---|
372 | return re.sub(r'([0-9](?=[A-Za-z])|[A-Za-z](?=[0-9]))', "\\1 ", string)
|
---|
373 | #s = ''
|
---|
374 | #for match in RXP_NRTEXT.finditer(string):
|
---|
375 | # #print string,match.groups()
|
---|
376 | # s += ' '.join(match.groups()) + ' '
|
---|
377 | #return s
|
---|
378 |
|
---|
379 | def __parse_timeinput__(self,timestring):
|
---|
380 | '''
|
---|
381 | parses the input string and sets the value to the resulting seconds
|
---|
382 | '''
|
---|
383 | timeval = 0
|
---|
384 | if timestring is None or timestring == 'None':
|
---|
385 | return None
|
---|
386 | multiplicator = 1
|
---|
387 | # search for x*x*X*X...
|
---|
388 | index = timestring.find('*')
|
---|
389 | if index != -1:
|
---|
390 | timeval = eval(timestring)
|
---|
391 | #print timeval
|
---|
392 | return timeval
|
---|
393 | #timestring = str(timeval)
|
---|
394 |
|
---|
395 |
|
---|
396 | if timestring.isdigit():
|
---|
397 | return int(timestring)
|
---|
398 |
|
---|
399 |
|
---|
400 | # search for multiple pairs like "3 seconds 4 days"
|
---|
401 | timestring = self.__partNumberKeywords__(timestring)
|
---|
402 | valueUnitPairs = timestring.split()
|
---|
403 | valueUnitPairs.reverse()
|
---|
404 | #print valueUnitPairs
|
---|
405 | #print timestring+'<'
|
---|
406 | if len(valueUnitPairs)%2:
|
---|
407 | logger.error(valueUnitPairs)
|
---|
408 | logger.error("There should always be a pair of value-unit\n maybe you forgot a space between value and unit?")
|
---|
409 | while len(valueUnitPairs) > 1:
|
---|
410 | vustr = str(valueUnitPairs.pop()) + ' ' + str(valueUnitPairs.pop())
|
---|
411 | for cut in (range(8)):
|
---|
412 | if cut == 0: # extra, as range -0 leads to nothing instead of giving the whole string
|
---|
413 | cut = -8
|
---|
414 | for mult in ('months','seconds',
|
---|
415 | 'mins','minutes','hours',
|
---|
416 | 'days','weeks','quarters',
|
---|
417 | 'years'): #,'n'
|
---|
418 | index = vustr.lower().find(mult[:-cut])
|
---|
419 | #print mult[:-cut],vustr
|
---|
420 | if index != -1 and len(mult[:-cut])>0:
|
---|
421 | multiplicator = timemultiplicators[mult]
|
---|
422 | tmpstring = vustr.replace(mult[:-cut],'')
|
---|
423 | #print mult[:-cut],vustr
|
---|
424 | vustr = tmpstring
|
---|
425 |
|
---|
426 | timeval += float(vustr) * multiplicator
|
---|
427 | #print "%s -> %d " % (vustr,timeval)
|
---|
428 |
|
---|
429 |
|
---|
430 | return int(timeval)
|
---|
431 |
|
---|
432 |
|
---|
433 |
|
---|
434 |
|
---|
435 | class store_bool( Item_Storage ):
|
---|
436 | #pass
|
---|
437 | '''
|
---|
438 | also base class for store_bit
|
---|
439 | '''
|
---|
440 |
|
---|
441 | def __init__(self,val):
|
---|
442 | Item_Storage.__init__(self, val)
|
---|
443 | self.setValue(val)
|
---|
444 |
|
---|
445 | def setValue(self,value):
|
---|
446 | logger.debug( self.__class__.__name__ +':' + 'called self.value with \''+ str(value) + '\' type: ' + str(type(value)))
|
---|
447 | if type(value) is str:
|
---|
448 | logger.debug('value was string')
|
---|
449 | if value.lower() == 'yes' or value.lower() == '1':
|
---|
450 | self.value = True
|
---|
451 | else:
|
---|
452 | self.value = False
|
---|
453 |
|
---|
454 | elif type(value) is int: # type int
|
---|
455 | logger.debug('value was int')
|
---|
456 | if value == 1:
|
---|
457 | self.value = True
|
---|
458 | else:
|
---|
459 | self.value = False
|
---|
460 |
|
---|
461 | elif type(value) is bool: # type int
|
---|
462 | logger.debug('value was bool')
|
---|
463 | self.value = value
|
---|
464 | else:
|
---|
465 | logger.error( "value must be <int> or <str>, but is " + str(type(value)) )
|
---|
466 |
|
---|
467 | logger.debug('value is now ' + str(self.value))
|
---|
468 |
|
---|
469 | def __str__(self):
|
---|
470 | if self.comment != '':
|
---|
471 | comment = "\t# " + self.comment
|
---|
472 | else:
|
---|
473 | comment = ''
|
---|
474 | if self.value == True:
|
---|
475 | #return 'Yes'#+ '\t# '+self.__class__.__name__ + 'id: '+ str(id(self))
|
---|
476 | return 'Yes' + comment #+self.__class__.__name__ + 'id: '+ str(id(self))
|
---|
477 |
|
---|
478 | else:
|
---|
479 | #return 'No' #+ '\t# '+self.__class__.__name__ + 'id: '+ str(id(self))
|
---|
480 | return 'No' + comment #+self.__class__.__name__ + 'id: '+ str(id(self))
|
---|
481 |
|
---|
482 | class store_bit( store_bool ):
|
---|
483 | pass
|
---|
484 |
|
---|
485 |
|
---|
486 | class store_dir( store_name ):
|
---|
487 | pass
|
---|
488 |
|
---|
489 |
|
---|
490 | class store_alist_str( Item_Storage_String ):
|
---|
491 | pass
|
---|
492 |
|
---|
493 | class store_password( store_name ):
|
---|
494 | pass
|
---|
495 |
|
---|
496 | class store_res( Item_Storage_String ):
|
---|
497 | pass
|
---|
498 |
|
---|
499 |
|
---|
500 |
|
---|
501 | class store_acl( Item_Storage_String ):
|
---|
502 |
|
---|
503 | def __init__(self,val):
|
---|
504 | self.value = val
|
---|
505 | self.comment = ''
|
---|
506 | self.possiblevalues = [] # what values are possible here ?
|
---|
507 | self.parentitem = None
|
---|
508 | self.ACLDict = {}
|
---|
509 |
|
---|
510 | def __str__(self):
|
---|
511 | return str(self.value)
|
---|
512 |
|
---|
513 |
|
---|
514 | def setParentItem(self,parentItem):
|
---|
515 | self.parentitem = parentItem
|
---|
516 |
|
---|
517 | def setValue(self,value):
|
---|
518 | logger.debug( self.__class__.__name__ +':' + 'called self.setvalue with \''+ str(value) + '\'')
|
---|
519 | self.value = value
|
---|
520 | self.__Value2ACLDict__()
|
---|
521 | self.__ACLDict2value__()
|
---|
522 | #self.__parse_ACLinput__(value)
|
---|
523 | #self.ACLDict2value()
|
---|
524 | logger.debug("self.value is now >%s<" %( self.value ))
|
---|
525 |
|
---|
526 | def __Value2ACLDict__(self):
|
---|
527 | '''
|
---|
528 | parses the input string and sets the ACL Dictionary
|
---|
529 | '''
|
---|
530 | #print "parsing the following value: %s " % (self.value)
|
---|
531 |
|
---|
532 | if self.value is None or self.value == 'None' or self.value == u'' or self.value == '':
|
---|
533 | for k in self.ACLDict.keys():
|
---|
534 | self.ACLDict[k]= False
|
---|
535 | self.value = None
|
---|
536 | else:
|
---|
537 | for acl in self.value.replace(' ','').split(','):
|
---|
538 | self.ACLDict[acl]=True
|
---|
539 | self.__ACLDict2value__()
|
---|
540 | #print self.ACLDict
|
---|
541 |
|
---|
542 |
|
---|
543 | def __ACLDict2value__(self):
|
---|
544 | '''
|
---|
545 | reads the acl Dictionary and produces the correct output in the value field
|
---|
546 | '''
|
---|
547 | s = ''
|
---|
548 |
|
---|
549 | for msg,val in self.ACLDict.iteritems():
|
---|
550 | if val == True:
|
---|
551 | s += '%s,' % (msg)
|
---|
552 | #self.setValue(s[:-1])
|
---|
553 | #self.value = s[:-1]
|
---|
554 | if (len(s[:-1])) == 0:
|
---|
555 | self.value = None
|
---|
556 | else:
|
---|
557 | self.value = s[:-1]# cut last comma
|
---|
558 |
|
---|
559 |
|
---|
560 |
|
---|
561 |
|
---|
562 |
|
---|
563 |
|
---|
564 |
|
---|
565 |
|
---|
566 | class store_int32( Item_Storage ):
|
---|
567 | pass
|
---|
568 |
|
---|
569 | class store_addresses_port( Item_Storage ):
|
---|
570 | pass
|
---|
571 |
|
---|
572 | class store_addresses( Item_Storage ):
|
---|
573 | pass
|
---|
574 |
|
---|
575 | class store_addresses_address( Item_Storage ):
|
---|
576 | pass
|
---|
577 |
|
---|
578 | class store_inc( Item_Storage ):
|
---|
579 | pass
|
---|
580 |
|
---|
581 | class store_migtype( Item_Storage ):
|
---|
582 |
|
---|
583 | def __init__(self,val):
|
---|
584 | Item_Storage.__init__(self, val)
|
---|
585 | self.possiblevalues = migtypes
|
---|
586 | if val == 0:
|
---|
587 | Item_Storage.setValue(self, 'smallestvolume')
|
---|
588 |
|
---|
589 | class store_short_runscript( Item_Storage ):
|
---|
590 | pass
|
---|
591 |
|
---|
592 | class store_runscript( Item_Storage ):
|
---|
593 | pass
|
---|
594 |
|
---|
595 | class store_alist_res( Item_Storage_String ):
|
---|
596 | pass
|
---|
597 |
|
---|
598 | class store_jobtype( Item_Storage ):
|
---|
599 |
|
---|
600 | def __init__(self,val):
|
---|
601 | Item_Storage.__init__(self, val)
|
---|
602 | self.possiblevalues = jobtypes
|
---|
603 | if val == 0:
|
---|
604 | Item_Storage.setValue(self, 'backup')
|
---|
605 |
|
---|
606 | class store_replace( Item_Storage ):
|
---|
607 |
|
---|
608 | def __init__(self,val):
|
---|
609 | Item_Storage.__init__(self, val)
|
---|
610 | self.possiblevalues = ReplaceOptions
|
---|
611 | if val == 0:
|
---|
612 | Item_Storage.setValue(self, 'always')
|
---|
613 |
|
---|
614 | class store_level( Item_Storage ):
|
---|
615 |
|
---|
616 | def __init__(self,val):
|
---|
617 | Item_Storage.__init__(self, val)
|
---|
618 | self.possiblevalues = joblevels
|
---|
619 | if val == 0:
|
---|
620 | Item_Storage.setValue(self, 'full')
|
---|
621 |
|
---|
622 |
|
---|
623 | class store_size64( Item_Storage ):
|
---|
624 | def __init__(self,val):
|
---|
625 | self.value = val
|
---|
626 | self.comment = ''
|
---|
627 | self.possiblevalues = [] # what values are possible here ?
|
---|
628 | self.parentitem = None
|
---|
629 |
|
---|
630 | def __str__(self):
|
---|
631 | #TODO do this in a nicer way without a catch-all try-except
|
---|
632 | if self.value is None:
|
---|
633 | return str(self.value)
|
---|
634 | outstring = ''
|
---|
635 | for mult in ["gb","mb","kb","g","m","k"]:
|
---|
636 | if self.value % datamultiplicators[mult] == 0:
|
---|
637 | outstring = "%d %s " % (self.value/datamultiplicators[mult], mult)
|
---|
638 | return outstring
|
---|
639 | return str(self.value)
|
---|
640 |
|
---|
641 | #outstring = ''
|
---|
642 | #for mult in ["years", "quarters" ,"months" ,"weeks", "days","hours","minutes","seconds"]:
|
---|
643 | # if self.value % timemultiplicators[mult] == 0:
|
---|
644 | # outstring = "%d %s " % (self.value/timemultiplicators[mult], mult)
|
---|
645 | # return outstring
|
---|
646 | #return outstring
|
---|
647 |
|
---|
648 | def setParentItem(self,parentItem):
|
---|
649 | self.parentitem = parentItem
|
---|
650 |
|
---|
651 | def setValue(self,value):
|
---|
652 | logger.debug( self.__class__.__name__ +':' + 'called self.value with \''+ str(value) + '\'')
|
---|
653 | self.value = self.__parse_datainput__(value)
|
---|
654 | logger.debug("self.value is now" + str(self) )
|
---|
655 |
|
---|
656 | def __parse_datainput__(self,datastring):
|
---|
657 | '''
|
---|
658 | parses the input string and sets the value to the resulting seconds
|
---|
659 | '''
|
---|
660 | if datastring is None or datastring == 'None':
|
---|
661 | return None
|
---|
662 |
|
---|
663 | datastring = str(datastring).lower()
|
---|
664 |
|
---|
665 | multiplicator = 1
|
---|
666 | dstring = datastring
|
---|
667 | for mult in ["gb","mb","kb","g","m","k"]:
|
---|
668 | index = dstring.lower().find(mult)
|
---|
669 | if index != -1:
|
---|
670 | multiplicator = datamultiplicators[mult]
|
---|
671 | tmpstring = datastring.replace(mult,'')
|
---|
672 | #print datastring
|
---|
673 | dstring = tmpstring
|
---|
674 | dataval = float(dstring) * multiplicator
|
---|
675 | return dataval
|
---|
676 |
|
---|
677 |
|
---|
678 | # search for x*x*X*X...
|
---|
679 | #index = timestring.find('*')
|
---|
680 | #if index != -1:
|
---|
681 | # timeval = eval(timestring)
|
---|
682 | # print timeval
|
---|
683 | # timestring = str(timeval)#####
|
---|
684 |
|
---|
685 | #print timestring+'<'
|
---|
686 | #for mult in timemultiplicators:
|
---|
687 | # for cut in (range(8)):
|
---|
688 | # if cut == 0: # extra, as range -0 leads to nothing instead of giving the whole string
|
---|
689 | # cut = -8
|
---|
690 | # index = timestring.find(mult[:-cut])
|
---|
691 | # if index != -1 and len(mult[:-cut])>0:
|
---|
692 | # multiplicator = timemultiplicators[mult]
|
---|
693 | # tmpstring = timestring.replace(mult[:-cut],'')
|
---|
694 | # #print timestring
|
---|
695 | # timestring = tmpstring
|
---|
696 | #timeval = float(timestring) * multiplicator
|
---|
697 | #return timeval
|
---|
698 |
|
---|
699 |
|
---|
700 | class store_strname( Item_Storage_String ):
|
---|
701 | pass
|
---|
702 |
|
---|
703 | class store_label( Item_Storage_String ):
|
---|
704 | pass
|
---|
705 |
|
---|
706 | class store_actiononpurge( Item_Storage ):
|
---|
707 | pass
|
---|
708 |
|
---|
709 | class store_runscript_bool( Item_Storage ):
|
---|
710 | pass
|
---|
711 |
|
---|
712 | class store_runscript_cmd( Item_Storage ):
|
---|
713 | pass
|
---|
714 |
|
---|
715 | class store_runscript_target( Item_Storage ):
|
---|
716 | pass
|
---|
717 |
|
---|
718 | class store_runscript_when( Item_Storage ):
|
---|
719 | pass
|
---|
720 |
|
---|
721 | class store_run( Item_Storage_String ):
|
---|
722 | def __init__(self,val):
|
---|
723 | self.value = val
|
---|
724 | logger.debug('store_run: called with ' + str(val))
|
---|
725 | self.runentry = scheduleparser.RunEntry(val)
|
---|
726 | self.possiblevalues = []
|
---|
727 |
|
---|
728 | def setValue(self,value):
|
---|
729 | self.runentry.setValue(value)
|
---|
730 | # set value to None if resutling string contains only whitespace,
|
---|
731 | # in order to have the default value and not print it
|
---|
732 | if len(str(self.runentry).strip()) == 0:
|
---|
733 | self.value = None
|
---|
734 | else:
|
---|
735 | self.value = str(self.runentry)
|
---|
736 |
|
---|
737 |
|
---|
738 | def __str__(self):
|
---|
739 | self.value = str(self.runentry) # update value string
|
---|
740 | return str(self.runentry)
|
---|
741 |
|
---|
742 | class store_device( Item_Storage_String ):
|
---|
743 | pass
|
---|
744 |
|
---|
745 | class store_maxblocksize( Item_Storage ):
|
---|
746 | pass
|
---|
747 |
|
---|
748 | class store_devtype( Item_Storage ):
|
---|
749 | pass
|
---|
750 |
|
---|
751 |
|
---|
752 |
|
---|
753 | class store_msgs( Item_Storage ):
|
---|
754 |
|
---|
755 | def __init__(self,val):
|
---|
756 | self.value = val
|
---|
757 | self.comment = ''
|
---|
758 | self.possiblevalues = [] # what values are possible here ?
|
---|
759 | self.parentitem = None
|
---|
760 | self.messageDict = {}
|
---|
761 | self.parameter = None
|
---|
762 | self.setValue(val)
|
---|
763 |
|
---|
764 | def __str__(self):
|
---|
765 | s = ''
|
---|
766 |
|
---|
767 | if self.parameter is not None:
|
---|
768 | s += self.parameter + ' = '
|
---|
769 |
|
---|
770 | for msg,val in self.messageDict.iteritems():
|
---|
771 | if msg == 'all':
|
---|
772 | if self.messageDict['all']:
|
---|
773 | s += 'all,'
|
---|
774 | continue
|
---|
775 |
|
---|
776 | if self.messageDict['all']:
|
---|
777 | if val == False:
|
---|
778 | s += '!%s,' % (msg)
|
---|
779 | else:# not all, so write all that are true
|
---|
780 | if val == True:
|
---|
781 | s += '%s,' % (msg)
|
---|
782 | return s[:-1] # cut last comma
|
---|
783 |
|
---|
784 | def getParameter(self):
|
---|
785 | '''
|
---|
786 | this returns the target of the messages entry
|
---|
787 | '''
|
---|
788 | return self.parameter
|
---|
789 |
|
---|
790 | def setParameter(self, parameter):
|
---|
791 | self.parameter = parameter
|
---|
792 |
|
---|
793 | def setParentItem(self,parentItem):
|
---|
794 | self.parentitem = parentItem
|
---|
795 |
|
---|
796 | def setValue(self,value):
|
---|
797 | logger.debug( self.__class__.__name__ +':' + 'called self.setvalue with \''+ str(value) + '\'')
|
---|
798 | self.value = self.__parse_messageinput__(value)
|
---|
799 | logger.debug("self.value is now" + str(self) )
|
---|
800 |
|
---|
801 | def __parse_messageinput__(self,msgsstring):
|
---|
802 | '''
|
---|
803 | parses the input string and sets the value
|
---|
804 | '''
|
---|
805 | if msgsstring is None or msgsstring == 'None':
|
---|
806 | self.parameter = None
|
---|
807 | return None
|
---|
808 |
|
---|
809 | splitted = msgsstring.split('=') # do we have a message string with parameter
|
---|
810 | if len(splitted) > 1: # (like root@localhost = all, !skipped)
|
---|
811 | self.parameter = splitted[0].rstrip(' ')
|
---|
812 | msgsstring = splitted[1]
|
---|
813 |
|
---|
814 |
|
---|
815 | messagestring = msgsstring.lower()
|
---|
816 | for message in msg_types:
|
---|
817 | self.messageDict[message] = None
|
---|
818 | index = messagestring.find('!'+message)
|
---|
819 | if index != -1:
|
---|
820 | logger.debug( "found NOT %s" % (message) )
|
---|
821 | self.messageDict[message] = False
|
---|
822 | else:
|
---|
823 | index = messagestring.find(message)
|
---|
824 | if index != -1:
|
---|
825 | logger.debug("found %s" % (message))
|
---|
826 | self.messageDict[message] = True
|
---|
827 | return self.__str__()
|
---|
828 |
|
---|
829 |
|
---|
830 |
|
---|
831 |
|
---|
832 |
|
---|
833 | class store_opts( Item_Storage ):
|
---|
834 | def __init__(self,val):
|
---|
835 | Item_Storage.__init__(self, val)
|
---|
836 | #if val == 0:
|
---|
837 | # Item_Storage.setValue(self, 'full')
|
---|
838 | def setParentItem(self,parentItem):
|
---|
839 | self.parentitem = parentItem
|
---|
840 | try:
|
---|
841 | self.possiblevalues = FS_option_kw[self.parentitem.name]
|
---|
842 | logger.debug(self.possiblevalues)
|
---|
843 | except:
|
---|
844 | logger.error('keyerror in FS_option_kw for ' + self.parentitem.name)
|
---|
845 |
|
---|
846 |
|
---|
847 | class store_drivetype( Item_Storage ):
|
---|
848 | pass
|
---|
849 |
|
---|
850 | class store_regex( Item_Storage ):
|
---|
851 | pass
|
---|
852 |
|
---|
853 | class store_wild( Item_Storage ):
|
---|
854 | pass
|
---|
855 |
|
---|
856 | class store_fstype( Item_Storage ):
|
---|
857 | pass
|
---|
858 |
|
---|
859 | class store_base( Item_Storage ):
|
---|
860 | pass
|
---|
861 |
|
---|
862 | class store_plugin( Item_Storage ):
|
---|
863 | pass
|
---|
864 |
|
---|
865 | class store_excludedir( Item_Storage ):
|
---|
866 | pass
|
---|
867 |
|
---|
868 | class store_fname( store_name ):
|
---|
869 | pass
|
---|
870 |
|
---|
871 |
|
---|
872 | class options_res( Item_Storage ):
|
---|
873 | pass
|
---|
874 |
|
---|
875 |
|
---|
876 | class store_plugin_name( Item_Storage ):
|
---|
877 | pass
|
---|
878 |
|
---|
879 |
|
---|
880 |
|
---|
881 | if __name__ == "__main__":
|
---|
882 | #msgsstore = store_msgs("root@localhost = mount")
|
---|
883 | tmp = store_time("1 hour")
|
---|
884 |
|
---|
885 | #for mult in ('months','seconds','n',
|
---|
886 | # 'mins','minutes','hours',
|
---|
887 | # 'days','weeks','quarters',
|
---|
888 | # 'years'):
|
---|
889 | # for val in range(9):
|
---|
890 | # value = str(val) + ' ' + mult
|
---|
891 | # tmp.setValue(value)
|
---|
892 | # print str(value) + '->' + str(tmp)
|
---|
893 | #
|
---|
894 | tmp.setValue("1month30minutes12seconds")
|
---|
895 | print tmp |
---|