changeset 14:281d223aa361

attribution works now
author casties
date Mon, 01 Aug 2011 19:30:12 +0200
parents 5f48f956ffa3
children 41b90f09a1f2
files MetaData.py MetaDataFolder.py
diffstat 2 files changed, 79 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/MetaData.py	Fri Jul 29 20:35:10 2011 +0200
+++ b/MetaData.py	Mon Aug 01 19:30:12 2011 +0200
@@ -72,7 +72,7 @@
         return path
     
     def getSubDom(self, path=None, dom=None):
-        """returns the subtree of the dom rooted in this element"""
+        """returns the subtree (list) of the dom rooted in this element"""
         if dom is None:
             # get from server
             md = self.getDomFromPathOrUrl(path)
@@ -81,18 +81,20 @@
         # lets assume dom is rooted in the first element
         xpath = '.' + self.getXmlPath(omitRoot=True)
         logging.debug("getSubDom looking for %s in %s"%(xpath, dom))
-        elem = dom.find(xpath)
+        elem = dom.findall(xpath)
         return elem
         
-    def getData(self, path=None, dom=None, normalizeNames=True, recursive=0):
+    def getData(self, path=None, dom=None, normalizeNames=True, allOccurrences=False, allText=0):
         """returns dict with attributes and child elements from corresponding tag"""
+        logging.debug("getData(path=%s, dom=%s)"%(path,dom))
         if path is None and dom is None:
             return None
         
-        data = {}
-        attr = {}
-        elem = self.getSubDom(path=path, dom=dom)
-        if elem is not None:
+        dataList = []
+        elems = self.getSubDom(path=path, dom=dom)
+        for elem in elems:
+            data = {}
+            attr = {}
             # put attributes in @attr
             for attname in elem.keys():
                 attr[attname] = elem.get(attname)
@@ -107,12 +109,20 @@
             # put all subelements in dict
             if normalizeNames:
                 for e in elem:
-                    data[normalizeFieldName(e.tag)] = getText(e, recursive=recursive)
+                    data[normalizeFieldName(e.tag)] = getText(e, recursive=allText)
             else:
                 for e in elem:
-                    data[e.tag] = getText(e, recursive=recursive)
-            
-        return data
+                    data[e.tag] = getText(e, recursive=allText)
+                    
+            dataList.append(data)
+
+        if allOccurrences:
+            return dataList
+        
+        if dataList:
+            return dataList[0]
+        else:
+            return {}
 
     def getMapping(self, type):
         """returns MetaDataMapping for type"""
@@ -232,8 +242,11 @@
                     logging.error("getFormatted: no generic template either: %s"%(template))
                     return ""
             
-            # put field descriptions in mdHash        
-            fields = self.getMappedData(data, allFields=allFields)
+            if type:
+                # put field descriptions in mdHash        
+                fields = self.getMappedData(data, allFields=allFields)
+            else:
+                fields = {}
                 
             return tp(mdmap=fields, md=data)
 
--- a/MetaDataFolder.py	Fri Jul 29 20:35:10 2011 +0200
+++ b/MetaDataFolder.py	Mon Aug 01 19:30:12 2011 +0200
@@ -122,20 +122,52 @@
             
         return dom
 
-    def getXmlPathData(self, xmlpath, path=None, dom=None):
-        """returns contents of element at xmlpath as dict"""
-        logging.error("getXmlPathData(%s)"%xmlpath)
+    def getXmlPathObj(self, xmlpath):
+        """returns object at xmlpath"""
         # make xmlpath relative for Zope
         if xmlpath[0] == '/':
             xmlpath = xmlpath[1:]
             
-        mdObj = self.restrictedTraverse(xmlpath, None)
+        obj = self.restrictedTraverse(xmlpath, None)
+        return obj
+
+    def getXmlPathData(self, xmlpath, path=None, dom=None, allText=False, allOccurrences=False):
+        """returns contents of element at xmlpath as dict"""
+        logging.error("getXmlPathData(%s)"%xmlpath)
+        mdObj = self.getXmlPathObj(xmlpath)
         if mdObj is not None:
-            return mdObj.getData(path=path, dom=dom)
+            return mdObj.getData(path=path, dom=dom, allText=allText, allOccurrences=allOccurrences)
         else:
             logging.error("getXmlPathData: MetaData element at '%s' not found!"%xmlpath)
             return None
 
+    def getXmlPathFormatted(self, xmlpath, template, path=None, dom=None, data=None, allFields=False, allText=False, allOccurrences=False):
+        """returns contents of element at xmlpath as dict"""
+        logging.error("getXmlPathFormatted(xmlpath=%s, template=%s)"%(xmlpath,template))
+        mdObj = self.getXmlPathObj(xmlpath)
+        if mdObj is not None:
+            if data is None:
+                data = mdObj.getData(path=path, dom=dom, allText=allText, allOccurrences=allOccurrences)
+                
+            if data is None:
+                return ''
+            
+            fmt = ''
+            if allOccurrences:
+                # data is list of elements
+                for d in data:
+                    # concatenate formatted strings
+                    fmt += mdObj.getFormatted(template, path=path, dom=dom, data=d, allFields=allFields) + '\n'
+                    
+            else:
+                fmt = mdObj.getFormatted(template, path=path, dom=dom, data=data, allFields=allFields)
+                
+            return fmt
+        
+        else:
+            logging.error("getXmlPathFormatted: MetaData element at '%s' not found!"%xmlpath)
+            return ''
+
     def getResourceData(self, path=None, dom=None):
         """returns contents of resource tag as dict"""
         return self.getXmlPathData('resource', path=path, dom=dom)
@@ -148,6 +180,22 @@
         """returns contents of access tag as dict"""
         return self.getXmlPathData('resource/meta/access-conditions/access', path=path, dom=dom)
     
+    def getAttributionData(self, path=None, dom=None, allOccurrences=True):
+        """returns contents of attribution tag as dict"""
+        return self.getXmlPathData('resource/meta/access-conditions/attribution', path=path, dom=dom, allOccurrences=allOccurrences)
+    
+    def getAttributionFormatted(self, template, path=None, dom=None, data=None, allOccurrences=True):
+        """returns formatted contents of access tag"""
+        return self.getXmlPathFormatted('resource/meta/access-conditions/attribution', template, path=path, dom=dom, data=data, allOccurrences=allOccurrences)
+    
+    def getCopyrightData(self, path=None, dom=None, allOccurrences=True):
+        """returns contents of copyright tag as dict"""
+        return self.getXmlPathData('resource/meta/access-conditions/copyright', path=path, dom=dom, allText=True, allOccurrences=allOccurrences)
+    
+    def getCopyrightFormatted(self, template, path=None, dom=None, data=None, allOccurrences=True):
+        """returns formatted contents of access tag"""
+        return self.getXmlPathFormatted('resource/meta/access-conditions/copyright', template, path=path, dom=dom, data=data, allOccurrences=allOccurrences)
+    
     def getBibData(self, path=None, dom=None):
         """returns contents of bib tag as dict"""
         return self.resource.meta.bib.getData(path=path, dom=dom)