Author: nunofsantos
Date: 2007-10-30 17:49:06 -0400 (Tue, 30 Oct 2007)
New Revision: 1189
Modified:
mgmt/mint/schemaparser.py
Log:
refactor code into separate classes
Modified: mgmt/mint/schemaparser.py
===================================================================
--- mgmt/mint/schemaparser.py 2007-10-30 20:59:27 UTC (rev 1188)
+++ mgmt/mint/schemaparser.py 2007-10-30 21:49:06 UTC (rev 1189)
@@ -1,128 +1,175 @@
-import qpid.mllib
+import mllib
from sqlobject import connectionForURI, sqlhub, MixedCaseUnderscoreStyle
-# mapping between xml schema types and database column types
-dataTypesMap = {}
-dataTypesMap["string"] = "VARCHAR(2000)"
-dataTypesMap["bool"] = "BOOLEAN"
-dataTypesMap["uint16"] = "INT2"
-dataTypesMap["uint32"] = "INT4"
-dataTypesMap["uint32_wm"] = "INT4"
-dataTypesMap["uint64"] = "INT8"
-dataTypesMap["ipAddress"] = "VARCHAR(100)"
-dataTypesMap["enum"] = "VARCHAR(100) CHECK ("
-dataTypesMap["fieldTable"] = "VARCHAR(2000)"
-# Generate Sql
-def generateSql(name, elements, type):
- sql = startTable(name, type)
- for elem in elements:
- sql += generateColumn(elem)
- return endTable(sql)
+class SqlGenerator:
+ """generates SQL code from broker XML schema"""
-def startTable(name, type):
- if (type == "config"):
- actualName = name
- else:
- actualName = name + "_stats"
- table = "CREATE TABLE " + actualName + " (\n"
- table += " id BIGSERIAL PRIMARY KEY,\n"
- if (type == "inst"):
- table += " " + name + "_id BIGINT REFERENCES " + name + "
,\n"
- table += generateTimestampColumn("rec")
- if (type == "config"):
- table += generateTimestampColumn("creation")
- table += generateTimestampColumn("deletion")
- return table
+ # mapping between xml schema types and database column types
+ dataTypesMap = {}
+ sqlOutput = ""
+ syle = None
+
+ def __init__(self, style):
+ self.sqlOutput = ""
+ self.style = style
+ self.dataTypesMap["string"] = "VARCHAR(2000)"
+ self.dataTypesMap["bool"] = "BOOLEAN"
+ self.dataTypesMap["uint16"] = "INT2"
+ self.dataTypesMap["uint32"] = "INT4"
+ self.dataTypesMap["uint32_wm"] = "INT4"
+ self.dataTypesMap["uint64"] = "INT8"
+ self.dataTypesMap["ipAddress"] = "VARCHAR(100)"
+ self.dataTypesMap["enum"] = "VARCHAR(100) CHECK ("
+ self.dataTypesMap["fieldTable"] = "VARCHAR(2000)"
-def generateTimestampColumn(col):
- column = " " + col + "_time TIMESTAMP,\n"
- return column
+ def generate(self, name, elements, type):
+ self.startTable(name, type)
+ for elem in elements:
+ self.generateColumn(elem)
+ self.endTable()
-def generateColumn(elem, suffix=""):
- actualName = style.pythonAttrToDBColumn(elem["@name"] + suffix)
- if (elem["(a)type"].startswith("enum")):
- actualType = "enum"
- params = actualName + " IN ("
- for i in elem["(a)type"].replace("enum(",
"").replace(")", "").split(","):
- params += "'" + i + "',"
- params = params[:-1] + "))"
- else:
- actualType = elem["@type"]
- params = ""
- column = " " + actualName + " " + dataTypesMap[actualType] +
params + ",\n"
- if (elem["(a)type"].endswith("_wm") and suffix == ""):
- column += generateColumn(elem, "High")
- column += generateColumn(elem, "Low")
- return column
+ def startTable(self, name, type):
+ if (type == "config"):
+ actualName = name
+ else:
+ actualName = name + "_stats"
+ self.sqlOutput += "CREATE TABLE " + actualName + " (\n"
+ self.sqlOutput += " id BIGSERIAL PRIMARY KEY,\n"
+ if (type == "inst"):
+ self.sqlOutput += " " + name + "_id BIGINT REFERENCES " +
name + " ,\n"
+ self.generateTimestampColumn("rec")
+ if (type == "config"):
+ self.generateTimestampColumn("creation")
+ self.generateTimestampColumn("deletion")
-def endTable(sql):
- return sql[:-2] + "\n);\n\n"
+ def generateTimestampColumn(self, col):
+ self.sqlOutput += " " + col + "_time TIMESTAMP,\n"
+
+ def generateColumn(self, elem, suffix=""):
+ actualName = self.style.pythonAttrToDBColumn(elem["@name"] + suffix)
+ if (elem["(a)type"].startswith("enum")):
+ actualType = "enum"
+ params = actualName + " IN ("
+ for i in elem["(a)type"].replace("enum(",
"").replace(")", "").split(","):
+ params += "'" + i + "',"
+ params = params[:-1] + "))"
+ else:
+ actualType = elem["@type"]
+ params = ""
+ if (elem["(a)name"].endswith("Ref")):
+ foreignKeyTable = "mgmt_" + actualName.replace("_ref",
"")
+ self.sqlOutput += " " + foreignKeyTable + "_id BIGINT REFERENCES
" + foreignKeyTable + " ,\n"
+ else:
+ self.sqlOutput += " " + actualName + " " +
self.dataTypesMap[actualType] + params + ",\n"
+ if (elem["(a)type"].endswith("_wm") and suffix == ""):
+ self.generateColumn(elem, "High")
+ self.generateColumn(elem, "Low")
+ def endTable(self):
+ self.sqlOutput = self.sqlOutput[:-2] + "\n);\n\n"
-# Generate Pyhon
-def generatePython(name, elements):
- name = style.dbColumnToPythonAttr(name)
- name = name[0].upper() + name[1:]
- python = startClass(name)
- for elem in elements:
- python += generateMethod(elem)
- python = endClass(python)
- python += startClass(name + "Stats")
- python = endClass(python)
- return python
+ def getCode(self):
+ return self.sqlOutput
-def startClass(name):
- result = "class " + name + "(SQLObject):\n"
- result += " class sqlmeta:\n"
- result += " fromDatabase = True\n"
- return result
-def generateMethod(elem):
- if (elem["@desc"] != None):
- comment = ' """' + elem["@desc"] +
'"""\n'
- else:
- comment = ""
- result = " def " + elem["@name"] + "():\n"
- result += comment
- result +=" pass\n\n"
- return result
-def endClass(python):
- return python + "\n"
+class PythonGenerator:
+ """generates Python code from broker XML schema"""
+ pythonOutput = ""
+ syle = None
-# parse configuration file
-config = qpid.mllib.xml_parse("config.xml")
-configOptions = config.query["config/configOption"]
-options = dict()
-for opt in configOptions:
- options[opt["@name"]] = opt["@value"]
+ def __init__(self, style, dsn):
+ self.pythonOutput = "from sqlobject import *\n\n" + \
+ "conn = connectionForURI('" + dsn +
"')\n" + \
+ "sqlhub.processConnection = conn\n\n"
+ self.style = style
+
+ def attrNameFromDbColumn(self, name, prefix="", removeSuffix=""):
+ name = self.style.dbColumnToPythonAttr(name.replace(removeSuffix, ""))
+ name = prefix + name[0].upper() + name[1:]
+ return name
+
+ def generate(self, name, elements):
+ name = self.attrNameFromDbColumn(name)
+ self.startClass(name)
+ for elem in elements:
+ self.generateMethod(elem)
+ self.endClass()
+ self.startClass(name + "Stats")
+ self.endClass()
+ def generateForeignKeys(self, name, elements):
+ name = self.attrNameFromDbColumn(name)
+ for elem in elements:
+ refName = elem["@name"]
+ if (refName.endswith("Ref")):
+ refName = self.attrNameFromDbColumn(refName, "Mgmt", "Ref")
+ self.pythonOutput += "\n" + refName +
".sqlmeta.addJoin(MultipleJoin('" + name + \
+ "', joinMethodName='all" +
name.replace("Mgmt", "") + "s'))\n\n"
+ def startClass(self, name):
+ self.pythonOutput += "class " + name + "(SQLObject):\n"
+ self.pythonOutput += " class sqlmeta:\n"
+ self.pythonOutput += " fromDatabase = True\n"
-sqlFile = open(options["sqlOutput"], "w")
-pythonFile = open(options["pythonOutput"], "w")
+ def generateMethod(self, elem):
+ if (elem["@desc"] != None):
+ comment = ' """' + elem["@desc"] +
'"""\n'
+ else:
+ comment = ""
+ self.pythonOutput += " def " + elem["@name"] +
"():\n"
+ self.pythonOutput += comment
+ self.pythonOutput += " pass\n\n"
-conn = connectionForURI(options["dsn"])
-sqlhub.processConnection = conn
+ def endClass(self):
+ self.pythonOutput += "\n"
-schema = qpid.mllib.xml_parse(options["schemaXML"])
-objects = schema.query["schema/object"]
+ def getCode(self):
+ return self.pythonOutput
-style = MixedCaseUnderscoreStyle()
-pythonFile.write("from sqlobject import *\n\n" + \
- "conn = connectionForURI('" + options["dsn"] +
"')\n" + \
- "sqlhub.processConnection = conn\n\n")
-for obj in objects:
- actualName = "mgmt_" + obj["@name"]
- sql = generateSql(actualName, obj.query["configElement"],
"config")
- sql += generateSql(actualName, obj.query["instElement"], "inst")
- python = generatePython(actualName, obj.query["method"])
- sqlFile.write(sql)
- pythonFile.write(python)
+class SchemaParser:
+ """parses broker XML schema"""
-sqlFile.close()
-pythonFile.close()
+ options = dict()
+ schema = None
+ style = None
+ pythonGen = None
+ sqlGen = None
+
+ def __init__(self):
+ self.parseConfigFile()
+ self.style = MixedCaseUnderscoreStyle()
+ self.pythonGen = PythonGenerator(self.style, self.options["dsn"])
+ self.sqlGen = SqlGenerator(self.style)
+
+ def parseConfigFile(self):
+ config = mllib.xml_parse("config.xml")
+ configOptions = config.query["config/configOption"]
+ for opt in configOptions:
+ self.options[opt["@name"]] = opt["@value"]
+
+ def generateCode(self):
+ conn = connectionForURI(self.options["dsn"])
+ sqlhub.processConnection = conn
+ sqlFile = open(self.options["sqlOutput"], "w")
+ pythonFile = open(self.options["pythonOutput"], "w")
+ schema = mllib.xml_parse(self.options["schemaXML"])
+ objects = schema.query["schema/object"]
+ for obj in objects:
+ actualName = "mgmt_" + obj["@name"]
+ self.sqlGen.generate(actualName, obj.query["configElement"],
"config")
+ self.sqlGen.generate(actualName, obj.query["instElement"],
"inst")
+ self.pythonGen.generate(actualName, obj.query["method"])
+ self.pythonGen.generateForeignKeys(actualName,
obj.query["configElement"])
+ sqlFile.write(self.sqlGen.getCode())
+ pythonFile.write(self.pythonGen.getCode())
+ sqlFile.close()
+ pythonFile.close()
+
+
+# parser = SchemaParser()
+# parser.generateCode()