Mercurial > hg > MetaDataProvider
changeset 8:4cd862bf37a3
more renovation
author | casties |
---|---|
date | Thu, 28 Jul 2011 14:54:54 +0200 |
parents | e959bc6bf2a7 |
children | eeaad777d3d7 |
files | MetaData.py MetaDataFolder.py MetaDataMapping.py |
diffstat | 3 files changed, 53 insertions(+), 37 deletions(-) [+] |
line wrap: on
line diff
--- a/MetaData.py Thu Jul 28 10:36:39 2011 +0200 +++ b/MetaData.py Thu Jul 28 14:54:54 2011 +0200 @@ -75,11 +75,46 @@ return path - def getSubDom(self, dom): + def getSubDom(self, path=None, dom=None): """returns the subtree of the dom rooted in this element""" + if dom is None: + # get from server + md = self.getMDFromPathOrUrl(path) + dom = ET.fromstring(md) + + # ElementTree doesn't like absolute paths + # 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) + return elem - def getSubElements(self, dom): - """returns a dict with names and text values of direct child elements""" + def getData(self, path=None, dom=None, normalizeNames=True): + """returns dict with attributes and child elements from corresponding tag""" + data = {} + attr = {} + elem = self.getSubDom(path=path, dom=dom) + if elem is not None: + # put attributes in @attr + for attname in elem.keys(): + attr[attname] = elem.get(attname) + + data['@attr'] = attr + if self.mappingSelectAttribute: + # put type in @type + type = attr.get(self.mappingSelectAttribute, None) + if type is not None: + data['@type'] = normalizeFieldName(type) + + # put all subelements in dict + if normalizeNames: + for e in elem: + data[normalizeFieldName(e.tag)] = getText(e) + else: + for e in elem: + data[e.tag] = getText(e) + + return data def getMapping(self, type): """returns MetaDataMapping for type""" @@ -202,27 +237,6 @@ return tp(mdmap=fields, md=data) - def getDataFromDom(self, dom): - """returns dict with all elements from corresponding tag""" - info = {} - # ElementTree doesn't like absolute paths - # lets assume dom is rooted in the first element - xpath = '.' + self.getXmlPath(omitRoot=True) - logging.debug("getDataFromDom looking for %s in %s"%(xpath, dom)) - elem = dom.find(xpath) - if elem is not None: - # put type in @type - type = elem.get(self.mappingSelectAttribute, None) - if type is not None: - info['@type'] = normalizeFieldName(type) - - # put all subelements in dict - for e in elem: - info[normalizeFieldName(e.tag)] = getText(e) - - return info - - def correctPath(self,path,remove=None,prefix=None,cut=0): """convinience method um einen pfad zu veraendern"""
--- a/MetaDataFolder.py Thu Jul 28 10:36:39 2011 +0200 +++ b/MetaDataFolder.py Thu Jul 28 14:54:54 2011 +0200 @@ -90,18 +90,26 @@ url=path else: # path only + path = path.replace('/mpiwg/online/', '') if path.endswith("index.meta"): - url =self.metaDataServerUrl%path + url = self.metaDataServerUrl%path else: - url=os.path.join(self.metaDataServerUrl%path,'index.meta') + url = os.path.join(self.metaDataServerUrl%path,'index.meta') #logging.debug("get Metadata: %s"%url) md = getHttpData(url) return md - def getBibdataFromDom(self, dom): + def getTexttoolData(self, path=None, dom=None): + """returns contents of texttool tag as dict""" + return self.resource.meta.texttool.getData(path=path, dom=dom) + + + + + def getBibdata(self, path=None, dom=None): """returns contents of bib tag as dict""" - return self.resource.meta.bib.getDataFromDom(dom) + return self.resource.meta.bib.getData(path=path, dom=dom) def getBibMapping(self, bibtype): """returns MetaDataMapping for resource/meta/bib of bibtype""" @@ -118,7 +126,6 @@ def getDCMappedData(self, bibdata): """returns dict with DC keys and data from bibdata""" return self.resource.meta.bib.getDCMappedData(bibdata) - def getBibFormattedMetaData(self, path=None, dom=None, bibdata=None): """get the metadafrom server""" @@ -135,10 +142,12 @@ logging.debug("getBibFormattedLabel(%s)"%path) return self.resource.meta.bib.getFormatted('label_template', path=path, dom=dom, data=bibdata) + # compatibility getFormattedMetaData = getBibFormattedMetaData getFormattedMetaDataShort = getBibFormattedMetaData getFormattedMetaDataExtended = getBibFormattedMetaDataExtended getFormattedLabel = getBibFormattedLabel + def getDCFormatted(self,path): """get the metadata as dc set"""
--- a/MetaDataMapping.py Thu Jul 28 10:36:39 2011 +0200 +++ b/MetaDataMapping.py Thu Jul 28 14:54:54 2011 +0200 @@ -37,7 +37,7 @@ """dict containing element descriptions""" fieldList = [] - """list of element names in preferred order""" + """list of field names in preferred order""" def __init__(self,id,title,fields,fieldList=None): """init @@ -65,13 +65,6 @@ """returns ordered list of fields""" return self.fieldList - - - - - - - # convenience method def getAt(self, array, idx, default=None): """returns element from array or default"""