Package SmootLight :: Package util :: Module ComponentRegistry
[hide private]
[frames] | no frames]

Source Code for Module SmootLight.util.ComponentRegistry

 1  import hashlib  
 2  from logger import main_log 
 3  import thread 
 4  #TODO: make component registry a singleton 
5 -def initRegistry():
6 #TODO: don't overwrite existing registry 7 if not 'Registry' in globals(): 8 globals()['Registry'] = {} 9 makelock()
10 11
12 -def makelock():
13 global utilLock 14 utilLock = thread.allocate_lock()
15 -def clearRegistry():
16 initRegistry()
17
18 -def removeComponent(cid):
19 global Registry 20 Registry.pop(cid)
21
22 -def getLock():
23 global utilLock 24 return utilLock
25 -def getComponent(cid):
26 global Registry 27 return Registry[cid]
28 29 #Registry of all components of the light system 30 #TODO: pick a graceful failure behavior and implement it
31 -def registerComponent(component, cid=None):
32 global Registry 33 if cid != None: 34 Registry[cid] = component 35 else: 36 try: 37 cid = component['Id'] 38 except KeyError: 39 cid = getNewId() 40 component['Id'] = cid 41 main_log.debug(cid + 'automatically assigned') 42 if cid in Registry: 43 main_log.warn(cid + 'overwritten.') 44 Registry[cid] = component 45 return cid
46
47 -def verifyUniqueId(cid):
48 global Registry 49 return not cid in Registry
50
51 -def removeComponent(cid):
52 global Registry 53 Registry.pop(cid)
54
55 -def getNewId():
56 global Registry 57 trialKey = len(Registry) 58 trialId = hashlib.md5(str(trialKey)).hexdigest() 59 while trialId in Registry: 60 trialKey += 1 61 trialId = hashlib.md5(str(trialKey)).hexdigest() 62 return trialId
63