comparison MetaData.py @ 15:41b90f09a1f2

new getdata
author casties
date Tue, 02 Aug 2011 12:34:11 +0200
parents 281d223aa361
children 5d41c350dd2b
comparison
equal deleted inserted replaced
14:281d223aa361 15:41b90f09a1f2
15 if underscore: 15 if underscore:
16 bt = bt.replace('_', '-') 16 bt = bt.replace('_', '-')
17 17
18 return bt 18 return bt
19 19
20 def putAppend(hash, key, value):
21 """puts value in dict hash at key if it doesn't exist or adds value to a list"""
22 if key in hash:
23 # key exists
24 oldval = hash[key]
25 if isinstance(oldval, list):
26 # is list already - append
27 oldval.append(value)
28 else:
29 # needs list
30 val = [oldval, value]
31 hash[key] = val
32
33 else:
34 # key doesn't exist
35 hash[key] = value
36
37 return hash
20 38
21 class MetaData(Folder): 39 class MetaData(Folder):
22 """provides basic methods for managing metadata structures""" 40 """provides basic methods for managing metadata structures"""
23 meta_type='MetaData' 41 meta_type='MetaData'
24 security=ClassSecurityInfo() 42 security=ClassSecurityInfo()
69 elif omitRoot: 87 elif omitRoot:
70 return '' 88 return ''
71 89
72 return path 90 return path
73 91
74 def getSubDom(self, path=None, dom=None): 92 def getSubDom(self, path=None, dom=None, all=False):
75 """returns the subtree (list) of the dom rooted in this element""" 93 """returns the subtree (list) of the dom rooted in this element"""
76 if dom is None: 94 if dom is None:
77 # get from server 95 # get from server
78 md = self.getDomFromPathOrUrl(path) 96 md = self.getDomFromPathOrUrl(path)
79 97
80 # ElementTree doesn't like absolute paths 98 # ElementTree doesn't like absolute paths
81 # lets assume dom is rooted in the first element 99 # lets assume dom is rooted in the first element
82 xpath = '.' + self.getXmlPath(omitRoot=True) 100 xpath = '.' + self.getXmlPath(omitRoot=True)
83 logging.debug("getSubDom looking for %s in %s"%(xpath, dom)) 101 logging.debug("getSubDom looking for %s in %s"%(xpath, dom))
84 elem = dom.findall(xpath) 102 if all:
103 elem = dom.findall(xpath)
104 else:
105 elem = dom.find(xpath)
106
85 return elem 107 return elem
86 108
87 def getData(self, path=None, dom=None, normalizeNames=True, allOccurrences=False, allText=0): 109
110 def _getData(self, elem, recursive, normalizeNames=False, all=False, allText=False):
111 logging.debug("_getDataFromDom(dom=%s, recursive=%s)"%(elem,recursive))
112 data = {}
113 attr = {}
114 # put attributes in @attr
115 for attname in elem.keys():
116 attr[attname] = elem.get(attname)
117
118 if attr:
119 data['@attr'] = attr
120
121 # TODO: should this be here?
122 if self.mappingSelectAttribute:
123 # put type in @type
124 type = attr.get(self.mappingSelectAttribute, None)
125 if type is not None:
126 data['@type'] = normalizeFieldName(type)
127
128 for e in elem:
129 # put all child elements in data
130 if normalizeNames:
131 # normalize key names
132 key = normalizeFieldName(e.tag)
133 else:
134 key = e.tag
135
136 if recursive > 0:
137 # more recursive - call _getData on elements
138 val = self._getData(e, recursive=recursive-1, normalizeNames=normalizeNames, all=all, allText=allText)
139 else:
140 val = getText(e, recursive=allText)
141
142 if all:
143 # add multiple tags as list
144 putAppend(data, key, val)
145 else:
146 data[key] = val
147
148 logging.debug("_getDataFromDom: returns %s"%repr(data))
149 return data
150
151
152 def getData(self, path=None, dom=None, normalizeNames=True, all=False, recursive=0, allText=0):
88 """returns dict with attributes and child elements from corresponding tag""" 153 """returns dict with attributes and child elements from corresponding tag"""
89 logging.debug("getData(path=%s, dom=%s)"%(path,dom)) 154 logging.debug("getData(path=%s, dom=%s)"%(path,dom))
90 if path is None and dom is None: 155 if path is None and dom is None:
91 return None 156 return None
92 157
93 dataList = [] 158 elem = self.getSubDom(path=path, dom=dom, all=all)
94 elems = self.getSubDom(path=path, dom=dom) 159 if all:
95 for elem in elems: 160 # subdom is list - return list
96 data = {} 161 data = []
97 attr = {} 162 for e in elem:
98 # put attributes in @attr 163 data.append(self._getData(e, recursive=recursive, normalizeNames=normalizeNames, allText=allText))
99 for attname in elem.keys(): 164
100 attr[attname] = elem.get(attname)
101
102 data['@attr'] = attr
103 if self.mappingSelectAttribute:
104 # put type in @type
105 type = attr.get(self.mappingSelectAttribute, None)
106 if type is not None:
107 data['@type'] = normalizeFieldName(type)
108
109 # put all subelements in dict
110 if normalizeNames:
111 for e in elem:
112 data[normalizeFieldName(e.tag)] = getText(e, recursive=allText)
113 else:
114 for e in elem:
115 data[e.tag] = getText(e, recursive=allText)
116
117 dataList.append(data)
118
119 if allOccurrences:
120 return dataList
121
122 if dataList:
123 return dataList[0]
124 else: 165 else:
125 return {} 166 # subdom is element
167 data = self._getData(elem, recursive=recursive, normalizeNames=normalizeNames, allText=allText)
168
169 return data
170
126 171
127 def getMapping(self, type): 172 def getMapping(self, type):
128 """returns MetaDataMapping for type""" 173 """returns MetaDataMapping for type"""
129 # try type as id 174 # try type as id
130 mapping = getattr(self, type, None) 175 mapping = getattr(self, type, None)