Skip to content

Instantly share code, notes, and snippets.

@graven
Created December 14, 2011 06:27
Show Gist options
  • Save graven/1475505 to your computer and use it in GitHub Desktop.
Save graven/1475505 to your computer and use it in GitHub Desktop.
# coding=utf-8
class CMDB:
values, valueHistory, publishers, subscribers = {}, {}, {}, {}
@staticmethod
def getValue(id):
return CMDB.values.get(id, None)
@staticmethod
def getValueHistory(id):
''' happy downloading '''
return CMDB.valueHistory.get(id, [])
@staticmethod
def setValue(id, val):
if id not in CMDB.valueHistory: CMDB.valueHistory[id] = []
CMDB.valueHistory[id].append(val)
CMDB.values[id] = val
@staticmethod
def getSubscribers(id):
return CMDB.subscribers.get(id, [])
@staticmethod
def addSubscriber(subscriber):
if id not in CMDB.subscribers: CMDB.subscribers[subscriber.id] = []
CMDB.subscribers[subscriber.id].append(subscriber)
return CMDB.getValue(subscriber.id)
@staticmethod
def getPublisher(id):
return CMDB.publishers.get(id, None)
@staticmethod
def setPublisher(publisher):
CMDB.publisher[publisher.id] = publisher
class Ingress:
def __init__(self, id, f):
self.id = id
self.f = f
print "Links: Registering subscriber for %s" % (self.id)
value = CMDB.addSubscriber(self)
print "Links: Last known value for %s is %s" % (self.id, value)
def update(self, newVal):
# do a remote call, send a message, write to event journal — whatever... here just call f
self.f(newVal)
return True
class Egress:
def __init__(self, id, initVal):
self.id = id
self.update(initVal)
def update(self, newVal):
print "Links: Updating %s: %s —> %s" % (self.id, CMDB.getValue(self.id), newVal)
CMDB.setValue(self.id, newVal)
retList = [subscriber.update(newVal) for subscriber in CMDB.getSubscribers(self.id)]
print "Links: Updated %s %s errors" % (self.id, (False in retList) and "with" or "without")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment