comparison MetaData.py @ 14:281d223aa361

attribution works now
author casties
date Mon, 01 Aug 2011 19:30:12 +0200
parents 5f48f956ffa3
children 41b90f09a1f2
comparison
equal deleted inserted replaced
13:5f48f956ffa3 14:281d223aa361
70 return '' 70 return ''
71 71
72 return path 72 return path
73 73
74 def getSubDom(self, path=None, dom=None): 74 def getSubDom(self, path=None, dom=None):
75 """returns the subtree of the dom rooted in this element""" 75 """returns the subtree (list) of the dom rooted in this element"""
76 if dom is None: 76 if dom is None:
77 # get from server 77 # get from server
78 md = self.getDomFromPathOrUrl(path) 78 md = self.getDomFromPathOrUrl(path)
79 79
80 # ElementTree doesn't like absolute paths 80 # ElementTree doesn't like absolute paths
81 # lets assume dom is rooted in the first element 81 # lets assume dom is rooted in the first element
82 xpath = '.' + self.getXmlPath(omitRoot=True) 82 xpath = '.' + self.getXmlPath(omitRoot=True)
83 logging.debug("getSubDom looking for %s in %s"%(xpath, dom)) 83 logging.debug("getSubDom looking for %s in %s"%(xpath, dom))
84 elem = dom.find(xpath) 84 elem = dom.findall(xpath)
85 return elem 85 return elem
86 86
87 def getData(self, path=None, dom=None, normalizeNames=True, recursive=0): 87 def getData(self, path=None, dom=None, normalizeNames=True, allOccurrences=False, allText=0):
88 """returns dict with attributes and child elements from corresponding tag""" 88 """returns dict with attributes and child elements from corresponding tag"""
89 logging.debug("getData(path=%s, dom=%s)"%(path,dom))
89 if path is None and dom is None: 90 if path is None and dom is None:
90 return None 91 return None
91 92
92 data = {} 93 dataList = []
93 attr = {} 94 elems = self.getSubDom(path=path, dom=dom)
94 elem = self.getSubDom(path=path, dom=dom) 95 for elem in elems:
95 if elem is not None: 96 data = {}
97 attr = {}
96 # put attributes in @attr 98 # put attributes in @attr
97 for attname in elem.keys(): 99 for attname in elem.keys():
98 attr[attname] = elem.get(attname) 100 attr[attname] = elem.get(attname)
99 101
100 data['@attr'] = attr 102 data['@attr'] = attr
105 data['@type'] = normalizeFieldName(type) 107 data['@type'] = normalizeFieldName(type)
106 108
107 # put all subelements in dict 109 # put all subelements in dict
108 if normalizeNames: 110 if normalizeNames:
109 for e in elem: 111 for e in elem:
110 data[normalizeFieldName(e.tag)] = getText(e, recursive=recursive) 112 data[normalizeFieldName(e.tag)] = getText(e, recursive=allText)
111 else: 113 else:
112 for e in elem: 114 for e in elem:
113 data[e.tag] = getText(e, recursive=recursive) 115 data[e.tag] = getText(e, recursive=allText)
114 116
115 return data 117 dataList.append(data)
118
119 if allOccurrences:
120 return dataList
121
122 if dataList:
123 return dataList[0]
124 else:
125 return {}
116 126
117 def getMapping(self, type): 127 def getMapping(self, type):
118 """returns MetaDataMapping for type""" 128 """returns MetaDataMapping for type"""
119 # try type as id 129 # try type as id
120 mapping = getattr(self, type, None) 130 mapping = getattr(self, type, None)
230 tp=getattr(self,"%s_generic"%(template), None) 240 tp=getattr(self,"%s_generic"%(template), None)
231 if tp is None: 241 if tp is None:
232 logging.error("getFormatted: no generic template either: %s"%(template)) 242 logging.error("getFormatted: no generic template either: %s"%(template))
233 return "" 243 return ""
234 244
235 # put field descriptions in mdHash 245 if type:
236 fields = self.getMappedData(data, allFields=allFields) 246 # put field descriptions in mdHash
247 fields = self.getMappedData(data, allFields=allFields)
248 else:
249 fields = {}
237 250
238 return tp(mdmap=fields, md=data) 251 return tp(mdmap=fields, md=data)
239 252
240 253
241 254