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