added RecordSpec class
[sfa.git] / geni / util / rspec.py
index b5e4844..0831d91 100644 (file)
@@ -1,22 +1,39 @@
 import sys
 import pprint
 import os
+import httplib
 from xml.dom import minidom
 from types import StringTypes, ListType
 
 class Rspec():
 
-    def __init__(self, xml = None, xsd = None):
-        self.xsd = xsd # schema
-        self.rootNode = None # root of the dom
-        self.dict = {} # dict of the rspec.
+    def __init__(self, xml = None, xsd = None, NSURL = None):
+        '''
+        Class to manipulate RSpecs.  Reads and parses rspec xml into python dicts
+        and reads python dicts and writes rspec xml
+
+        self.xsd = # Schema.  Can be local or remote file.
+        self.NSURL = # If schema is remote, Name Space URL to query (full path minus filename)
+        self.rootNode = # root of the DOM
+        self.dict = # dict of the RSpec.
+        self.schemaDict = {} # dict of the Schema
+        '''
+        self.xsd = xsd
+        self.rootNode = None
+        self.dict = {}
+        self.schemaDict = {}
+        self.NSURL = NSURL 
         if xml: 
             if type(xml) == file:
                 self.parseFile(xml)
             if type(xml) == str:
                 self.parseString(xml)
             self.dict = self.toDict() 
-  
+        if xsd:
+            self._parseXSD(self.NSURL + self.xsd)
+
+
     def _getText(self, nodelist):
         rc = ""
         for node in nodelist:
@@ -99,8 +116,8 @@ class Rspec():
         """
         dom = minidom.parse(filename)
         self.rootNode = dom.childNodes[0]
-  
-  
+
+
     def parseString(self, xml):
         """
         read an xml string and store it as a dom object.
@@ -110,25 +127,72 @@ class Rspec():
         self.rootNode = dom.childNodes[0]
 
  
+    def _httpGetXSD(self, xsdURI):
+        # split the URI into relevant parts
+        host = xsdURI.split("/")[2]
+        if xsdURI.startswith("https"):
+            conn = httplib.HTTPSConnection(host,
+                httplib.HTTPSConnection.default_port)
+        elif xsdURI.startswith("http"):
+            conn = httplib.HTTPConnection(host,
+                httplib.HTTPConnection.default_port)
+        conn.request("GET", xsdURI)
+        # If we can't download the schema, raise an exception
+        r1 = conn.getresponse()
+        if r1.status != 200: 
+            raise Exception
+        return r1.read().replace('\n', '').replace('\t', '').strip() 
+
+
+    def _parseXSD(self, xsdURI):
+        """
+        Download XSD from URL, or if file, read local xsd file and set schemaDict
+        """
+        # Since the schema definiton is a global namespace shared by and agreed upon by
+        # others, this should probably be a URL.  Check for URL, download xsd, parse, or 
+        # if local file, use local file.
+        schemaDom = None
+        if xsdURI.startswith("http"):
+            try: 
+                schemaDom = minidom.parseString(self._httpGetXSD(xsdURI))
+            except Exception, e:
+                # logging.debug("%s: web file not found" % xsdURI)
+                # logging.debug("Using local file %s" % self.xsd")
+                print e
+                print "Can't find %s on the web. Continuing." % xsdURI
+        if not schemaDom:
+            if os.path.exists(xsdURI):
+                # logging.debug("using local copy.")
+                print "Using local %s" % xsdURI
+                schemaDom = minidom.parse(xsdURI)
+            else:
+                raise Exception("Can't find xsd locally")
+        self.schemaDict = self.toDict(schemaDom.childNodes[0])
+
+
     def dict2dom(self, rdict, include_doc = False):
         """
         convert a dict object into a dom object.
         """
      
         def elementNode(tagname, rd):
-            element = minidom.Element(tagname)   
+            element = minidom.Element(tagname)
             for key in rd.keys():
-                if isinstance(rd[key], StringTypes):
-                    element.setAttribute(key, rd[key])
+                if isinstance(rd[key], StringTypes) or isinstance(rd[key], int):
+                    element.setAttribute(key, str(rd[key]))
                 elif isinstance(rd[key], dict):
                     child = elementNode(key, rd[key])
                     element.appendChild(child)
                 elif isinstance(rd[key], list):
                     for item in rd[key]:
-                        child = elementNode(key, item)
+                        child = elementNode(key, {key:item})
                         element.appendChild(child)
             return element
-                     
+        
+        # Minidom does not allow documents to have more then one
+        # child, but elements may have many children. Because of
+        # this, the document's root node will be the first key/value
+        # pair in the dictionary.  
         node = elementNode(rdict.keys()[0], rdict.values()[0])
         if include_doc:
             rootNode = minidom.Document()
@@ -199,4 +263,28 @@ class Rspec():
             for child in dom.childNodes:
                 self.filter(tagname, attribute, blacklist, whitelist, child) 
 
+
+    def validateDicts(self):
+        types = {
+            'EInt' : int,
+            'EString' : str,
+            'EByteArray' : list,
+            'EBoolean' : bool,
+            'EFloat' : float,
+            'EDate' : date}
+
+
+
+class RecordSpec(Rspec):
+
+    def parseDict(self, rdict, include_doc = False):
+        """
+        Convert a dictionary into a dom object and store it.
+        """
+        self.rootNode = self.dict2dom(rdict, include_doc)
+
+    def dict2dom(self, rdict, include_doc = False):
+        record_dict = {'record': rdict}
+        return Rspec.dict2dom(self, record_dict, include_doc)
+        
 # vim:ts=4:expandtab