# HG changeset patch # User casties # Date 1312285081 -7200 # Node ID 5d41c350dd2be17aa7042af6ffa3e51984f83584 # Parent 41b90f09a1f2e2927c7fbe5ee1af01e729d71446 new getdata works now diff -r 41b90f09a1f2 -r 5d41c350dd2b MetaData.py --- a/MetaData.py Tue Aug 02 12:34:11 2011 +0200 +++ b/MetaData.py Tue Aug 02 13:38:01 2011 +0200 @@ -19,6 +19,7 @@ def putAppend(hash, key, value): """puts value in dict hash at key if it doesn't exist or adds value to a list""" + #logging.debug("putAppend(hash=%s, key=%s, value=%s)"%(hash,key,value)) if key in hash: # key exists oldval = hash[key] @@ -34,8 +35,10 @@ # key doesn't exist hash[key] = value + #logging.debug("putAppend returns hash=%s"%(hash)) return hash + class MetaData(Folder): """provides basic methods for managing metadata structures""" meta_type='MetaData' @@ -108,7 +111,8 @@ def _getData(self, elem, recursive, normalizeNames=False, all=False, allText=False): - logging.debug("_getDataFromDom(dom=%s, recursive=%s)"%(elem,recursive)) + """helper method for getData()""" + #logging.debug("_getDataFromDom(dom=%s, recursive=%s)"%(elem,recursive)) data = {} attr = {} # put attributes in @attr @@ -118,13 +122,6 @@ if attr: data['@attr'] = attr - # TODO: should this be here? - if self.mappingSelectAttribute: - # put type in @type - type = attr.get(self.mappingSelectAttribute, None) - if type is not None: - data['@type'] = normalizeFieldName(type) - for e in elem: # put all child elements in data if normalizeNames: @@ -145,13 +142,22 @@ else: data[key] = val - logging.debug("_getDataFromDom: returns %s"%repr(data)) + #logging.debug("_getDataFromDom: returns %s"%repr(data)) return data def getData(self, path=None, dom=None, normalizeNames=True, all=False, recursive=0, allText=0): - """returns dict with attributes and child elements from corresponding tag""" - logging.debug("getData(path=%s, dom=%s)"%(path,dom)) + """Returns dict with attributes and child elements from corresponding tag. + + @param path: file or url path to metadata file + @param dom: dom of metadata + @param normalizeNames: normalize tag names + @param all: put contents of tags with the same name in list value + @param recurse: number of recursions. 0=just children + @param allText: get text content of all subelements + @returns: dict with attributes (key=@attr) and child elements (key=tag) + """ + #logging.debug("getData(path=%s, dom=%s)"%(path,dom)) if path is None and dom is None: return None @@ -160,12 +166,20 @@ # subdom is list - return list data = [] for e in elem: - data.append(self._getData(e, recursive=recursive, normalizeNames=normalizeNames, allText=allText)) + data.append(self._getData(e, recursive=recursive, normalizeNames=normalizeNames, all=all, allText=allText)) else: # subdom is element - data = self._getData(elem, recursive=recursive, normalizeNames=normalizeNames, allText=allText) + data = self._getData(elem, recursive=recursive, normalizeNames=normalizeNames, all=all, allText=allText) + if self.mappingSelectAttribute: + # put type in @type + attr = data.get('@attr', None) + if attr: + type = attr.get(self.mappingSelectAttribute, None) + if type is not None: + data['@type'] = normalizeFieldName(type) + return data