[rhmessaging-commits] rhmessaging commits: r1708 - in mgmt: basil and 4 other directories.

rhmessaging-commits at lists.jboss.org rhmessaging-commits at lists.jboss.org
Tue Feb 19 23:38:03 EST 2008


Author: justi9
Date: 2008-02-19 23:38:03 -0500 (Tue, 19 Feb 2008)
New Revision: 1708

Added:
   mgmt/basil/
   mgmt/basil/bin/
   mgmt/basil/bin/basil
   mgmt/basil/python/
   mgmt/basil/python/basil/
   mgmt/basil/python/basil/__init__.py
   mgmt/basil/python/basil/__init__.strings
   mgmt/basil/python/basil/util.py
Modified:
   mgmt/etc/devel.profile
Log:
Introduces a new module, basil, for generating client-side mgmt models.

Added: mgmt/basil/bin/basil
===================================================================
--- mgmt/basil/bin/basil	                        (rev 0)
+++ mgmt/basil/bin/basil	2008-02-20 04:38:03 UTC (rev 1708)
@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+
+import sys
+
+from basil import *
+
+def usage():
+    print "Usage: basil SCHEMA-XML"
+    sys.exit(1)
+
+def main():
+    if len(sys.argv) != 2:
+        usage()
+
+    xml = sys.argv[1]
+    maker = ClassMaker(xml)
+
+    print maker.make()
+
+if __name__ == "__main__":
+    main()


Property changes on: mgmt/basil/bin/basil
___________________________________________________________________
Name: svn:executable
   + *

Added: mgmt/basil/python/basil/__init__.py
===================================================================
--- mgmt/basil/python/basil/__init__.py	                        (rev 0)
+++ mgmt/basil/python/basil/__init__.py	2008-02-20 04:38:03 UTC (rev 1708)
@@ -0,0 +1,74 @@
+import mllib
+from string import Template
+
+from util import *
+
+strings = StringCatalog(__file__)
+
+class ClassMaker(object):
+    def __init__(self, schema_xml):
+        self.schema_xml = schema_xml
+        self.main_tmpl = Template(strings.get("main"))
+        self.class_tmpl = Template(strings.get("class"))
+        self.constructor_tmpl = Template(strings.get("constructor"))
+        self.attribute_tmpl = Template(strings.get("attribute"))
+        self.method_tmpl = Template(strings.get("method"))
+
+    def make(self):
+        schema = mllib.xml_parse(self.schema_xml)
+
+        args = dict()
+        
+        l = list()
+        for cls in schema.query["schema/class"]:
+          l.append(self.make_class(cls))
+        args["classes"] = "\n\n".join(l)
+
+        return self.main_tmpl.substitute(args)
+
+    def make_class(self, node):
+        args = dict()
+        args["name"] = node["@name"].capitalize()
+        args["constructor"] = self.make_constructor(node)
+
+        l = list()
+        for meth in node.query["method"]:
+            l.append(self.make_method(meth))
+        args["methods"] = "\n\n".join(l)
+
+        return self.class_tmpl.substitute(args)
+
+    def make_constructor(self, node):
+        args = dict()
+        args["mgmt_name"] = node["@name"]
+
+        l = list()
+        for attr in node.query["configElement"]:
+            l.append(self.make_attribute(attr))
+        for attr in node.query["instElement"]:
+            l.append(self.make_attribute(attr))
+        args["attributes"] = "\n".join(l)
+
+        return self.constructor_tmpl.substitute(args)
+
+    def make_attribute(self, node):
+        args = dict()
+        args["name"] = node["@name"]
+
+        return self.attribute_tmpl.substitute(args)
+
+    def make_method(self, node):
+        args = dict()
+        args["name"] = node["@name"]
+
+        pl, al = list(), list()
+        pl.append("self")
+        for arg in node.query["arg"]:
+            name = arg["@name"]
+            pl.append(name)
+            al.append("\"%s\": %s" % (name, name))
+        pl.append("callback")
+        args["parameters"] = ", ".join(pl)
+        args["args"] = ", ".join(al)
+
+        return self.method_tmpl.substitute(args)

Added: mgmt/basil/python/basil/__init__.strings
===================================================================
--- mgmt/basil/python/basil/__init__.strings	                        (rev 0)
+++ mgmt/basil/python/basil/__init__.strings	2008-02-20 04:38:03 UTC (rev 1708)
@@ -0,0 +1,68 @@
+[main]
+import sys, os
+
+class Model(object):
+    def __init__(self, broker):
+        self.broker = broker
+        self.broker_id = "%s:%i" % (broker.host, broker.port)
+        self.broker.configListener(self.broker_id, self.__config_callback)
+
+        self.method_sequence = 1
+        self.outstanding_calls = dict()
+
+        self.classes = list()
+        self.classes_by_mgmt_name = dict()
+
+    def call_method(self, obj, name, args, callback):
+        method_sequence += 1
+        self.outstanding_calls[self.method_sequence] = callback
+
+        self.broker.method(id, obj.mgmt_id, obj.mgmt_name, name, args=args,
+                           packageName=obj.mgmt_package)
+
+    def __config_callback(self, broker_id, object_name, values, timestamps):
+        if broker_id != self.broker_id:
+            raise Exception("Unexpected broker")
+
+        print broker_id, object_name, values, timestamps
+
+${classes}
+
+if __name__ == "__main__":
+    from qpid.management import ManagedBroker
+    from time import sleep
+
+    host = "localhost"
+    port = 5672
+    spec = os.environ.get("AMQP_SPEC", "/usr/share/amqp/amqp.0-10-preview.xml")
+
+    broker = ManagedBroker(host, port, spec)
+    model = Model(broker)
+
+    broker.start()
+
+    while True:
+        sleep(5)
+
+[class]
+class ${name}(object):
+${constructor}
+
+${methods}
+
+[constructor]
+    def __init__(self, model, id):
+        self.mgmt_model = model
+        self.mgmt_id = id
+        self.mgmt_name = "${mgmt_name}"
+        self.mgmt_package = "qpid"
+
+${attributes}
+
+[attribute]
+        self.${name} = None
+
+[method]
+    def ${name}(${parameters}):
+        args = {${args}}
+        self.mgmt_model.call_method(self, ${name}, args, callback)

Added: mgmt/basil/python/basil/util.py
===================================================================
--- mgmt/basil/python/basil/util.py	                        (rev 0)
+++ mgmt/basil/python/basil/util.py	2008-02-20 04:38:03 UTC (rev 1708)
@@ -0,0 +1,39 @@
+import os
+from StringIO import StringIO
+
+class StringCatalog(object):
+    def __init__(self, filename):
+        self.__strings = dict()
+
+        path = os.path.splitext(filename)[0] + ".strings"
+
+        try:
+            file = open(path)
+            self.parse(file)
+        finally:
+            file.close()
+
+    def get(self, key):
+        return self.__strings.get(key)
+
+    def parse(self, file):
+        key = None
+        writer = StringIO()
+
+        for line in file:
+            line = line[0:-1]
+
+            if line.startswith("[") and line.endswith("]"):
+                if key:
+                    self.__strings[key] = writer.getvalue().rstrip()
+
+                writer = StringIO()
+
+                key = line[1:-1]
+
+                continue
+
+            writer.write(line)
+            writer.write("\n")
+
+        self.__strings[key] = writer.getvalue().rstrip()

Modified: mgmt/etc/devel.profile
===================================================================
--- mgmt/etc/devel.profile	2008-02-19 22:44:00 UTC (rev 1707)
+++ mgmt/etc/devel.profile	2008-02-20 04:38:03 UTC (rev 1708)
@@ -2,7 +2,7 @@
     export DEVEL_HOME="$PWD"
 fi
 
-export DEVEL_MODULES="mint cumin"
+export DEVEL_MODULES="basil mint cumin"
 
 # PYTHONPATH
 




More information about the rhmessaging-commits mailing list