aboutsummaryrefslogtreecommitdiff
path: root/operationscore/SmootCoreObject.py
blob: 6addb9c9fe8d3c8d5c4d26e15c538ed26516f266 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import pdb
import threading
import thread
import util.Config as configGetter
import types

class SmootCoreObject(object):
    """SmootCoreObject is essentially a super-object class which grants us some niceties.  It allows
    us to use objects as if they are dictionaries -- we use this to store their arguments
    convienently -- note that querying for a parameter that does not exist will return None.  It
    also offers some basic ThreadSafety."""
    def __init__(self, argDict, skipValidation = False):
        self.dieListeners = []
        self.argDict = argDict
        self.validateArgs(self.className()+'.params') 
        self.lock = thread.allocate_lock()
        #put everything into attributes for speed
        for key in argDict:
            setattr(self, key, argDict[key])
        self.init() #call init of inheriting class
    
    def init(self):
        pass
        
    def acquireLock(self):
        self.lock = thread.allocate_lock() #TODO: fix. -- investigate this, it should only have to be run once in the initialization.
        self.lock.acquire()
        
    def releaseLock(self):
        self.lock.release()
        
    def className(self):
        return self.__class__.__name__
        
    def __setitem__(self,k, item):
        self.argDict[k] = item
        
    def __getitem__(self, key):
        if key in self.argDict:
            item = self.argDict[key]
            if isinstance(item, types.FunctionType):
                return item(self.argDict) #resolve the lambda function, if it exists
            #elif isinstance(item, list): #if its a list of items
            #    pass #TODO: consider doing resolution of lambda funcs for items in lists
            else:
                return item
        else:
            return None
    def __contains__(self, item):
        return item in self.argDict
    def __getiter__(self):
        return self.argDict.__getiter__()
        
    def validateArgs(self, argFileName):
        self.validateArgDict(configGetter.loadParamRequirementDict(argFileName))#util
        #caches for us, woo!
        
    def validateArgDict(self, validationDict):
        for item in validationDict:
            if not item in self.argDict:
                raise Exception(validationDict[item])
    
    def addDieListener(self, listener):
        if listener not in self.dieListeners:
            self.dieListeners.append(listener)

    def removeDieListener(self, listener):
        if listener in self.dieListeners:
            self.dieListeners.remove(listener)    
        
    def die(self):
        for listener in self.dieListeners:
            listener.handleDie(self)