Package SmootLight :: Package operationscore :: Module SmootCoreObject
[hide private]
[frames] | no frames]

Source Code for Module SmootLight.operationscore.SmootCoreObject

 1  import pdb 
 2  import threading 
 3  import thread 
 4  import util.Config as configGetter 
 5  import types 
 6   
7 -class SmootCoreObject(object):
8 """SmootCoreObject is essentially a super-object class which grants us some niceties. It allows 9 us to use objects as if they are dictionaries -- we use this to store their arguments 10 convienently -- note that querying for a parameter that does not exist will return None. It 11 also offers some basic ThreadSafety."""
12 - def __init__(self, argDict, skipValidation = False):
13 self.dieListeners = [] 14 self.argDict = argDict 15 self.validateArgs(self.className()+'.params') 16 self.lock = thread.allocate_lock() 17 #put everything into attributes for speed 18 for key in argDict: 19 setattr(self, key, argDict[key]) 20 self.init() #call init of inheriting class
21
22 - def init(self):
23 pass
24
25 - def acquireLock(self):
26 self.lock = thread.allocate_lock() #TODO: fix. -- investigate this, it should only have to be run once in the initialization. 27 self.lock.acquire()
28
29 - def releaseLock(self):
30 self.lock.release()
31
32 - def className(self):
33 return self.__class__.__name__
34
35 - def __setitem__(self,k, item):
36 self.argDict[k] = item
37
38 - def __getitem__(self, key):
39 if key in self.argDict: 40 item = self.argDict[key] 41 if isinstance(item, types.FunctionType): 42 return item(self.argDict) #resolve the lambda function, if it exists 43 #elif isinstance(item, list): #if its a list of items 44 # pass #TODO: consider doing resolution of lambda funcs for items in lists 45 else: 46 return item 47 else: 48 return None
49 - def __contains__(self, item):
50 return item in self.argDict
51 - def __getiter__(self):
52 return self.argDict.__getiter__()
53
54 - def validateArgs(self, argFileName):
55 self.validateArgDict(configGetter.loadParamRequirementDict(argFileName))#util
56 #caches for us, woo! 57
58 - def validateArgDict(self, validationDict):
59 for item in validationDict: 60 if not item in self.argDict: 61 raise Exception(validationDict[item])
62
63 - def addDieListener(self, listener):
64 if listener not in self.dieListeners: 65 self.dieListeners.append(listener)
66
67 - def removeDieListener(self, listener):
68 if listener in self.dieListeners: 69 self.dieListeners.remove(listener)
70
71 - def die(self):
72 for listener in self.dieListeners: 73 listener.handleDie(self)
74