Author: nunofsantos
Date: 2007-11-12 17:37:37 -0500 (Mon, 12 Nov 2007)
New Revision: 1291
Added:
mgmt/mint/model.py
Modified:
mgmt/mint/config.xml
mgmt/mint/schemaparser.py
Log:
refactored test code into model.py
Modified: mgmt/mint/config.xml
===================================================================
--- mgmt/mint/config.xml 2007-11-12 21:55:30 UTC (rev 1290)
+++ mgmt/mint/config.xml 2007-11-12 22:37:37 UTC (rev 1291)
@@ -1,6 +1,6 @@
<config>
<configOption name="dsn" value="postgresql://localhost/" />
- <configOption name="pythonOutput" value="schema.out.py" />
- <configOption name="sqlOutput" value="schema.out.sql" />
+ <configOption name="pythonOutput" value="schema.py" />
+ <configOption name="sqlOutput" value="schema.sql" />
<configOption name="schemaXML" value="xml/MgmtSchema.xml" />
</config>
Added: mgmt/mint/model.py
===================================================================
--- mgmt/mint/model.py (rev 0)
+++ mgmt/mint/model.py 2007-11-12 22:37:37 UTC (rev 1291)
@@ -0,0 +1,184 @@
+from qpid.management import ManagedBroker
+from schema import *
+from time import sleep
+from datetime import *
+from sqlobject import *
+
+class Model:
+ currentMethodId = None
+ outstandingMethodCalls = None
+
+ def __init__(self):
+ self.currentMethodId = 1
+ self.outstandingMethodCalls = dict()
+
+ def getQueueByOriginalId(self, id, create=False):
+ queue = None
+ try:
+ queues = MgmtQueue.selectBy(idOriginal=id)[:1]
+ queue = queues[0]
+ except IndexError:
+ if (create): queue = MgmtQueue(idOriginal=id)
+ return queue
+
+ def getQueueByName(self, name, vhost, create=False):
+ queue = None
+ try:
+ queues = MgmtQueue.selectBy(name=name, mgmtVhost=vhost)[:1]
+ queue = queues[0]
+ except IndexError:
+ if (create): queue = MgmtQueue(name=name, mgmtVhost=vhost)
+ return queue
+
+ def getVhostByName(self, name, broker, create=False):
+ vhost = None
+ try:
+ vhosts = MgmtVhost.selectBy(name=name, mgmtBroker=broker)[:1]
+ vhost = vhosts[0]
+ except IndexError:
+ if (create): vhost = MgmtVhost(name=name, mgmtBroker=broker)
+ return vhost
+
+ def getVhostByOriginalId(self, id, create=False):
+ vhost = None
+ try:
+ vhosts = MgmtVhost.selectBy(idOriginal=id)[:1]
+ vhost = vhosts[0]
+ except IndexError:
+ if (create): vhost = MgmtVhost(idOriginal=id)
+ return vhost
+
+ def getBrokerByPort(self, port, system, create=False):
+ broker = None
+ try:
+ brokers = MgmtBroker.selectBy(port=port, mgmtSystem=system)[:1]
+ broker = brokers[0]
+ except IndexError:
+ if (create): broker = MgmtBroker(port=port, mgmtSystem=system)
+ return broker
+
+ def getBrokerByOriginalId(self, id, create=False):
+ broker = None
+ try:
+ brokers = MgmtBroker.selectBy(idOriginal=id)[:1]
+ broker = brokers[0]
+ except IndexError:
+ if (create): broker = MgmtBroker(idOriginal=id)
+ return broker
+
+ def getSystemByOriginalId(self, id, create=False):
+ system = None
+ try:
+ systems = MgmtSystem.selectBy(idOriginal=id)[:1]
+ system = systems[0]
+ except IndexError:
+ if (create): system = MgmtSystem(idOriginal=id)
+ return system
+
+ def sanitizeDict(self, d):
+ for k in d.iterkeys():
+ if (k.endswith("Id")):
+ d[self.convertKey(k)] = d.pop(k)
+ elif (k == "id"):
+ d[self.convertKey(k)] = d.pop(k)
+ for k in d.iterkeys():
+ if (k.endswith("Ref")):
+ d[self.convertKey(k)] = d.pop(k)
+ return d
+
+ def convertKey(self, k):
+ if (k == "id"):
+ return k + "Original"
+ if (k.endswith("Id")):
+ return k + "ent"
+ elif (k.endswith("Ref")):
+ oldK = k
+ k = k[0].upper() + k[1:]
+ return "mgmt" + k.replace("Ref", "ID")
+
+ def configCallback(self, broker, objectName, list, timestamps):
+ print "\nCONFIG---------------------------------------------------"
+ print "broker=" + broker
+ print objectName
+ print list
+ result = None
+ d = self.sanitizeDict(dict(list))
+
+ print d
+
+ d["recTime"] = datetime.fromtimestamp(timestamps[0]/1000000000)
+ d["creationTime"] = datetime.fromtimestamp(timestamps[1]/1000000000)
+ if (objectName == "Queue"):
+ print "* QUEUE"
+ queue = self.getQueueByName(d["name"],
self.getVhostByOriginalId(d.pop(self.convertKey("vhostRef"))), True)
+ queue.set(**d)
+ print queue.id
+ print " -> " + queue.name
+ result = queue
+ elif (objectName == "Vhost"):
+ print "* VHOST"
+ vhost = self.getVhostByName(d["name"],
self.getBrokerByOriginalId(d.pop(self.convertKey("brokerRef"))), True)
+ vhost.set(**d)
+ print vhost.id
+ print " -> " + vhost.name
+ result = vhost
+ elif (objectName == "Broker"):
+ print "* BROKER"
+ d.pop(self.convertKey("systemRef"))
+ broker = self.getBrokerByPort(d["port"],
self.getSystemByOriginalId("123456789"), True)
+ broker.set(**d)
+ print broker.id
+ result = broker
+ print "END CONFIG---------------------------------------------------\n"
+ return result
+
+ def instCallback(self, broker, objectName, list, timestamps):
+ print "\nINST---------------------------------------------------"
+ print "broker=" + broker
+ print objectName
+ print list
+ result = None
+ d = self.sanitizeDict(dict(list))
+ if (objectName == "Queue"):
+ print "* QUEUE"
+ queue = self.getQueueByOriginalId(d[self.convertKey("id")])
+ d["mgmtQueue"] = queue.id
+ d["recTime"] = datetime.fromtimestamp(timestamps[0]/1000000000)
+ queueStats = MgmtQueueStats()
+ queueStats.set(**d)
+ d = dict()
+ if (timestamps[2] != 0):
+ d["deletionTime"] = datetime.fromtimestamp(timestamps[2]/1000000000)
+ d["mgmtQueueStats"] = queueStats
+ queue.set(**d)
+ print queue.id
+ result = queueStats
+ elif (objectName == "Vhost"):
+ print "* VHOST"
+ elif (objectName == "Broker"):
+ print "* BROKER"
+ print "END INST---------------------------------------------------\n"
+ return result
+
+ def methodCallback(self, broker, methodId, error, args):
+ print "\nMETHOD---------------------------------------------------"
+ print "broker=" + broker
+ print methodId
+ print error
+ print args
+ methodCallback = self.outstandingMethodCalls.pop(methodId)
+ print methodCallback
+ eval(methodCallback)
+ print "END METHOD---------------------------------------------------\n"
+
+ def addManagedBroker(self, broker, label):
+ broker.configListener(label, self.configCallback)
+ broker.instrumentationListener (label, self.instCallback)
+ broker.methodListener (label, self.methodCallback)
+
+ def registerCallback(self, callback):
+ self.currentMethodId += 1
+ methodId = self.currentMethodId
+ self.outstandingMethodCalls[methodId] = callback
+ return methodId
+
Modified: mgmt/mint/schemaparser.py
===================================================================
--- mgmt/mint/schemaparser.py 2007-11-12 21:55:30 UTC (rev 1290)
+++ mgmt/mint/schemaparser.py 2007-11-12 22:37:37 UTC (rev 1291)
@@ -1,4 +1,4 @@
-import qpid.mllib
+import mllib
from sqlobject import connectionForURI, sqlhub, MixedCaseUnderscoreStyle
@@ -26,9 +26,6 @@
self.dataTypesMap["bool"] = "BOOLEAN"
self.dataTypesMap["sstr"] = "VARCHAR(1000)"
self.dataTypesMap["lstr"] = "VARCHAR(4000)"
- #self.dataTypesMap["ipAddress"] = "VARCHAR(100)"
- #self.dataTypesMap["enum"] = "VARCHAR(100) CHECK ("
- #self.dataTypesMap["fieldTable"] = "VARCHAR(2000)"
def generate(self, name, elements, type):
self.startTable(name, type)
@@ -42,23 +39,23 @@
else:
actualName = name + "_stats"
self.currentTable = actualName
- self.sqlOutput += "\nCREATE TABLE " + actualName + " (\n"
+ self.sqlOutput += "\nCREATE TABLE %s (\n" % (actualName)
self.sqlOutput += " id BIGSERIAL PRIMARY KEY,\n"
self.sqlOutput += " id_original BIGINT,\n"
if (type == "config"):
- self.sqlOutput += " " + name + "_stats_id BIGINT,\n"
+ self.sqlOutput += " %s_stats_id BIGINT,\n" % (name)
else:
- self.sqlOutput += " " + name + "_id BIGINT REFERENCES " +
name + " ,\n"
+ self.sqlOutput += " %s_id BIGINT REFERENCES %s ,\n" % (name, name)
self.generateTimestampColumn("rec")
if (type == "config"):
self.generateTimestampColumn("creation")
self.generateTimestampColumn("deletion")
def generateTimestampColumn(self, col):
- self.sqlOutput += " " + col + "_time TIMESTAMP,\n"
+ self.sqlOutput += " %s_time TIMESTAMP,\n" % (col)
def generateIndex(self, name):
- self.indexes += "\nCREATE INDEX " + self.currentTable + "_" +
name + "_index ON " + self.currentTable + "(" + name +
");\n"
+ self.indexes += "\nCREATE INDEX %s_%s_index ON %s(%s);\n" %
(self.currentTable, name, self.currentTable, name)
def generateColumn(self, elem, suffix=""):
actualName = self.style.pythonAttrToDBColumn(elem["@name"] + suffix)
@@ -76,9 +73,9 @@
params = ""
if (elem["(a)name"].endswith("Ref")):
foreignKeyTable = "mgmt_" + actualName.replace("_ref",
"")
- self.sqlOutput += " " + foreignKeyTable + "_id BIGINT REFERENCES
" + foreignKeyTable + ",\n"
+ self.sqlOutput += " %s_id BIGINT REFERENCES %s,\n" % (foreignKeyTable,
foreignKeyTable)
else:
- self.sqlOutput += " " + actualName + " " +
self.dataTypesMap[actualType] + params + ",\n"
+ self.sqlOutput += " %s %s %s,\n" % (actualName,
self.dataTypesMap[actualType], params)
if (elem["(a)type"].startswith("hilo") and suffix ==
""):
self.generateColumn(elem, "High")
self.generateColumn(elem, "Low")
@@ -88,7 +85,7 @@
def endTable(self, name, type):
self.sqlOutput = self.sqlOutput[:-2] + "\n);\n" + self.indexes
if (type == "inst"):
- self.sqlOutput += "\nALTER TABLE " + name + " ADD FOREIGN KEY
(" + name + "_stats_id) REFERENCES " + name + "_stats;\n"
+ self.sqlOutput += "\nALTER TABLE %s ADD FOREIGN KEY (%s_stats_id) REFERENCES
%s_stats;\n" % (name, name, name)
self.currentTable = ""
self.indexes = ""
@@ -105,9 +102,9 @@
syle = None
def __init__(self, style, dsn):
- self.pythonOutput = "from sqlobject import *\n\n" + \
- "conn = connectionForURI('" + dsn +
"')\n" + \
- "sqlhub.processConnection = conn\n\n"
+ self.pythonOutput = "from sqlobject import *\n\n"
+ self.pythonOutput += "conn = connectionForURI(\"%s\")\n" % (dsn)
+ self.pythonOutput += "sqlhub.processConnection = conn\n\n"
self.additional = ""
self.style = style
@@ -116,14 +113,14 @@
name = prefix + name[0].upper() + name[1:]
return name
- def generate(self, name, elements):
- name = self.attrNameFromDbColumn(name)
- self.startClass(name + "Stats")
+ def generate(self, name, schemaName, schemaId, elements):
+ pythonName = self.attrNameFromDbColumn(name)
+ self.startClass(pythonName + "Stats")
self.endClass()
- self.startClass(name)
+ self.startClass(pythonName, schemaName, schemaId)
for elem in elements:
self.generateMethod(elem)
- self.endClass(name)
+ self.endClass(pythonName)
def generateAdditionalAttribs(self, name, elements):
name = self.attrNameFromDbColumn(name)
@@ -133,12 +130,15 @@
# generate foreign keys
refName = self.attrNameFromDbColumn(refName, "Mgmt", "Ref")
# add missing attribute (not added correctly with SqlObject 0.7.7; may need to be
removed in later versions)
- self.pythonOutput += " _SO_class_" +
self.style.pythonAttrToDBColumn(refName).capitalize() + " = " + refName +
"()\n"
- self.additional += "\n" + refName +
".sqlmeta.addJoin(MultipleJoin('" + name + \
- "', joinMethodName='all" +
name.replace("Mgmt", "") + "s'))"
+ self.pythonOutput += " _SO_class_%s = %s()\n" %
(self.style.pythonAttrToDBColumn(refName).capitalize(), refName)
+ self.additional += "\n%s.sqlmeta.addJoin(MultipleJoin('%s" %
(refName, name)
+ self.additional += "', joinMethodName='all%ss'))" %
(name.replace("Mgmt", ""))
- def startClass(self, name):
- self.pythonOutput += "\nclass " + name + "(SQLObject):\n"
+ def startClass(self, pythonName, schemaName="", schemaId=""):
+ self.pythonOutput += "\nclass %s(SQLObject):\n" % (pythonName)
+ if (schemaName != ""):
+ self.pythonOutput += " schemaId = %d\n" % int(schemaId)
+ self.pythonOutput += " schemaName = \"%s\"\n" % schemaName
self.pythonOutput += " class sqlmeta:\n"
self.pythonOutput += " fromDatabase = True\n"
@@ -147,9 +147,11 @@
comment = ' """' + elem["@desc"] +
'"""\n'
else:
comment = ""
- self.pythonOutput += " def " + elem["@name"] +
"():\n"
+ self.pythonOutput += " def %s(self, managedBroker, model,
callbackMethod):\n" % (elem["@name"])
self.pythonOutput += comment
- self.pythonOutput += " pass\n"
+ self.pythonOutput += " methodId =
model.registerCallback(callbackMethod)\n"
+ self.pythonOutput += " managedBroker.method(self.schemaId, methodId,
self.schemaName, \"%s\", packageName=\"qpid\", " %
(elem["@name"])
+ self.pythonOutput += "args=())\n"
def endClass(self, name=""):
if (self.additional != ""):
@@ -157,7 +159,7 @@
self.additional = ""
if (name != "" and not name.endswith("Stats")):
# add missing attribute (not added correctly with SqlObject 0.7.7; may need to be
removed in later versions)
- self.pythonOutput += " _SO_class_" +
self.style.pythonAttrToDBColumn(name).capitalize() + "_stats = " + name +
"Stats()\n"
+ self.pythonOutput += " _SO_class_%s_stats = %sStats()\n" %
(self.style.pythonAttrToDBColumn(name).capitalize(), name)
def getCode(self):
return self.pythonOutput
@@ -180,7 +182,7 @@
self.sqlGen = SqlGenerator(self.style)
def parseConfigFile(self):
- config = qpid.mllib.xml_parse("config.xml")
+ config = mllib.xml_parse("config.xml")
configOptions = config.query["config/configOption"]
for opt in configOptions:
self.options[opt["@name"]] = opt["@value"]
@@ -190,13 +192,13 @@
sqlhub.processConnection = conn
sqlFile = open(self.options["sqlOutput"], "w")
pythonFile = open(self.options["pythonOutput"], "w")
- schema = qpid.mllib.xml_parse(self.options["schemaXML"])
+ schema = mllib.xml_parse(self.options["schemaXML"])
classes = schema.query["schema/class"]
for cls in classes:
actualName = "mgmt_" + cls["@name"]
self.sqlGen.generate(actualName, cls.query["configElement"],
"config")
self.sqlGen.generate(actualName, cls.query["instElement"],
"inst")
- self.pythonGen.generate(actualName, cls.query["method"])
+ self.pythonGen.generate(actualName, cls["@name"],
cls["@schemaId"], cls.query["method"])
self.pythonGen.generateAdditionalAttribs(actualName,
cls.query["configElement"])
sqlFile.write(self.sqlGen.getCode())
pythonFile.write(self.pythonGen.getCode())