Skip to content

Instantly share code, notes, and snippets.

@graven
Created December 14, 2011 06:27

Revisions

  1. Stan Klimoff renamed this gist Dec 14, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. Stan Klimoff revised this gist Dec 14, 2011. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions example output
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    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']
  3. Stan Klimoff revised this gist Dec 14, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions example.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # coding=utf-8

    from binding import Ingress, Egress
    from CMDB import Ingress, Egress

    class MySQL:
    def __init__(self, connectionString):
    @@ -23,5 +23,5 @@ def updateConnectionString(self, newConnectionString):
    pohape = Pohape()
    mysql.updateConnectionString("jdbc://localhost")

    from binding import CMDB
    from CMDB import CMDB
    print CMDB.getValueHistory("database.connectionString")
  4. Stan Klimoff revised this gist Dec 14, 2011. 1 changed file with 27 additions and 0 deletions.
    27 changes: 27 additions & 0 deletions example.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    # coding=utf-8

    from binding 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 binding import CMDB
    print CMDB.getValueHistory("database.connectionString")
  5. Stan Klimoff created this gist Dec 14, 2011.
    62 changes: 62 additions & 0 deletions CMDB.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    # 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")