Skip to content

Instantly share code, notes, and snippets.

@graven
Created December 14, 2011 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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")
# coding=utf-8
from CMDB import Ingress, Egress
class MySQL:
def __init__(self, connectionString):
self.connectionString = Egress("database.connectionString", connectionString)
def updateConnectionString(self, newConnectionString):
print "MySQL: New connection string is", newConnectionString
self.connectionString.update(newConnectionString)
class Pohape:
def __init__(self):
self.connectionString = Ingress("database.connectionString", self.updateConnectionString)
def updateConnectionString(self, newConnectionString):
print "Pohape: database connection string changed to", newConnectionString
print "Pohape: Recycling"
return True
mysql = MySQL("jdbc://10.8.0.1")
pohape = Pohape()
mysql.updateConnectionString("jdbc://localhost")
from CMDB import CMDB
print CMDB.getValueHistory("database.connectionString")
graven@grace ~/Projects/cobalt » python example.py
Links: Updating database.connectionString: None —> jdbc://10.8.0.1
Links: Updated database.connectionString without errors
Links: Registering subscriber for database.connectionString
Links: Last known value for database.connectionString is jdbc://10.8.0.1
MySQL: New connection string is jdbc://localhost
Links: Updating database.connectionString: jdbc://10.8.0.1 —> jdbc://localhost
Pohape: database connection string changed to jdbc://localhost
Pohape: Recycling
Links: Updated database.connectionString without errors
['jdbc://10.8.0.1', 'jdbc://localhost']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment