comparison MetaData.py @ 24:d036de7fd78d

getDRI added
author dwinter
date Fri, 27 Jul 2012 12:46:00 +0200
parents 9a1e75e708e1
children a19575be96e8
comparison
equal deleted inserted replaced
20:9a1e75e708e1 24:d036de7fd78d
148 148
149 #logging.debug("_getDataFromDom: returns %s"%repr(data)) 149 #logging.debug("_getDataFromDom: returns %s"%repr(data))
150 return data 150 return data
151 151
152 152
153 def getDRI(self,path=None,type="escidoc",dom=None):
154 """returns the DRI of an document, i.e. the content of the dri tag with the given type."""
155
156 #logging.debug("getData(path=%s, dom=%s)"%(path,dom))
157
158
159 if path is None and dom is None:
160 return None
161
162
163 if dom is None:
164 # get from server
165 dom = self.getDomFromPathOrUrl(path)
166
167 # ElementTree doesn't like absolute paths
168 # lets assume dom is rooted in the first element
169
170 xpath = '.' + self.getXmlPath(omitRoot=True)+'/dri[@type="%s"]'%type
171 logging.debug("getSubDom looking for %s in %s"%(xpath, dom))
172
173
174 elem = dom.find(xpath)
175
176
177 if elem is None:
178 return None
179
180
181 return getText(elem)
182
153 def getData(self, path=None, dom=None, normalizeNames=True, all=False, recursive=0, allText=0): 183 def getData(self, path=None, dom=None, normalizeNames=True, all=False, recursive=0, allText=0):
154 """Returns dict with attributes and child elements from corresponding tag. 184 """Returns dict with attributes and child elements from corresponding tag.
155 185
156 @param path: file or url path to metadata file 186 @param path: file or url path to metadata file
157 @param dom: dom of metadata 187 @param dom: dom of metadata
287 317
288 def getFormatted(self, template, path=None, dom=None, data=None, allFields=False): 318 def getFormatted(self, template, path=None, dom=None, data=None, allFields=False):
289 """returns string with document data formatted according to template. 319 """returns string with document data formatted according to template.
290 gets data from server or dom or pre-parsed data.""" 320 gets data from server or dom or pre-parsed data."""
291 logging.debug("getFormatted(template=%s)"%(template)) 321 logging.debug("getFormatted(template=%s)"%(template))
292 322 logging.debug(self.absolute_url())
323 logging.debug(self.__dict__)
324
293 # get contents of tag 325 # get contents of tag
294 if data is None: 326 if data is None:
295 data = self.getData(path=path, dom=dom) 327 data = self.getData(path=path, dom=dom)
296 if data is None: 328 if data is None:
297 # no data 329 # no data
299 return "" 331 return ""
300 332
301 type = data.get('@type', '') 333 type = data.get('@type', '')
302 334
303 # get template 335 # get template
336
304 tp=getattr(self,"%s_%s"%(template, normalizeFieldName(type)), None) 337 tp=getattr(self,"%s_%s"%(template, normalizeFieldName(type)), None)
338
305 if tp is None: 339 if tp is None:
306 logging.warning("getFormatted: no template for: %s_%s"%(template, type)) 340 logging.warning("getFormatted: no template for: %s_%s!"%(template, normalizeFieldName(type)))
307 # try generic 341 # try generic
308 tp=getattr(self,"%s_generic"%(template), None) 342 tp=getattr(self,"%s_generic"%(template), None)
309 if tp is None: 343 if tp is None:
310 logging.error("getFormatted: no generic template either: %s"%(template)) 344 logging.error("getFormatted: no generic template either: %s"%(template))
311 return "" 345 return ""
313 if type: 347 if type:
314 # put field descriptions in mdHash 348 # put field descriptions in mdHash
315 fields = self.getMappedData(data, allFields=allFields) 349 fields = self.getMappedData(data, allFields=allFields)
316 else: 350 else:
317 fields = {} 351 fields = {}
318 352
353 logging.debug("XXXX")
354 logging.debug(tp)
319 return tp(mdmap=fields, md=data) 355 return tp(mdmap=fields, md=data)
320 356
321 357
322 358
323 def correctPath(self,path,remove=None,prefix=None,cut=0): 359 def correctPath(self,path,remove=None,prefix=None,cut=0):