annotate MetaData.py @ 14:281d223aa361

attribution works now
author casties
date Mon, 01 Aug 2011 19:30:12 +0200
parents 5f48f956ffa3
children 41b90f09a1f2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
1 from OFS.Folder import Folder
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
2 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
3 from Globals import package_home
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
4 from AccessControl import ClassSecurityInfo
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
5 import logging
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
6
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
7 import xml.etree.ElementTree as ET
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
8
2
ac8e119b25ec trying to make import from xml work
casties
parents: 1
diff changeset
9 from MetaDataMapping import MetaDataMapping
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
10 from SrvTxtUtils import getHttpData, getText
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
11
6
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
12 def normalizeFieldName(bt, underscore=True):
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
13 """returns normalised field type for looking up mappings"""
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
14 bt = bt.strip().replace(' ', '-').lower()
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
15 if underscore:
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
16 bt = bt.replace('_', '-')
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
17
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
18 return bt
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
19
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
20
6
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
21 class MetaData(Folder):
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
22 """provides basic methods for managing metadata structures"""
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
23 meta_type='MetaData'
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
24 security=ClassSecurityInfo()
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
25 manage_options = Folder.manage_options+(
5
c1dbf78cc036 more MetaDataFolder
casties
parents: 4
diff changeset
26 {'label':'Main Config','action':'changeMetaDataForm'},
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
27 {'label':'Import XML Schema','action':'importMetaDataExportXML'},
3
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
28 #{'label':'Select Fields for Display','action':'indicateDisplayFieldsForm'},
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
29 )
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
30
11
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
31 mappingSelectAttribute = None
6
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
32 """the name of the attribute that can be used to select a mapping (if applicable)"""
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
33
11
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
34 def __init__(self,id,title=None,shortDescription='',description='',fields=''):
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
35 """initialize a new instance"""
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
36 self.id = id
11
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
37 # title is tag name
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
38 if title:
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
39 self.title = title
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
40 else:
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
41 # assume id is tag name
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
42 self.title = id
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
43
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
44 self.shortDescription =shortDescription #label fuer link auf add page
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
45 self.description=description #description of the method for link page
13
5f48f956ffa3 more resistant to empty data
casties
parents: 12
diff changeset
46 if fields:
5f48f956ffa3 more resistant to empty data
casties
parents: 12
diff changeset
47 self.fieldList=fields.split(",")[0:]
6
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
48 #self.metaDataServerUrl="" # muss mit change metadata gesetzt werden
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
49
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
50
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
51 def getFieldList(self):
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
52 """returns fieldList"""
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
53 return ','.join(self.fieldList)
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
54
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
55 def getTagName(self):
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
56 """returns the tag name of this element"""
11
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
57 if self.title:
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
58 return self.title
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
59 else:
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
60 return self.shortDescription
6
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
61
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
62 def getXmlPath(self, omitRoot=False):
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
63 """returns the xpath to this element"""
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
64 path = '/%s'%self.getTagName()
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
65 parent = self.aq_parent
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
66 if parent.meta_type == self.meta_type:
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
67 # add parent
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
68 path = parent.getXmlPath(omitRoot=omitRoot) + path
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
69 elif omitRoot:
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
70 return ''
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
71
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
72 return path
7
e959bc6bf2a7 starting use for other metadata elements
casties
parents: 6
diff changeset
73
8
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
74 def getSubDom(self, path=None, dom=None):
14
281d223aa361 attribution works now
casties
parents: 13
diff changeset
75 """returns the subtree (list) of the dom rooted in this element"""
8
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
76 if dom is None:
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
77 # get from server
12
7f0e2b656e5c more work for non-bib metadata
casties
parents: 11
diff changeset
78 md = self.getDomFromPathOrUrl(path)
8
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
79
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
80 # ElementTree doesn't like absolute paths
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
81 # lets assume dom is rooted in the first element
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
82 xpath = '.' + self.getXmlPath(omitRoot=True)
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
83 logging.debug("getSubDom looking for %s in %s"%(xpath, dom))
14
281d223aa361 attribution works now
casties
parents: 13
diff changeset
84 elem = dom.findall(xpath)
8
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
85 return elem
7
e959bc6bf2a7 starting use for other metadata elements
casties
parents: 6
diff changeset
86
14
281d223aa361 attribution works now
casties
parents: 13
diff changeset
87 def getData(self, path=None, dom=None, normalizeNames=True, allOccurrences=False, allText=0):
8
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
88 """returns dict with attributes and child elements from corresponding tag"""
14
281d223aa361 attribution works now
casties
parents: 13
diff changeset
89 logging.debug("getData(path=%s, dom=%s)"%(path,dom))
13
5f48f956ffa3 more resistant to empty data
casties
parents: 12
diff changeset
90 if path is None and dom is None:
5f48f956ffa3 more resistant to empty data
casties
parents: 12
diff changeset
91 return None
5f48f956ffa3 more resistant to empty data
casties
parents: 12
diff changeset
92
14
281d223aa361 attribution works now
casties
parents: 13
diff changeset
93 dataList = []
281d223aa361 attribution works now
casties
parents: 13
diff changeset
94 elems = self.getSubDom(path=path, dom=dom)
281d223aa361 attribution works now
casties
parents: 13
diff changeset
95 for elem in elems:
281d223aa361 attribution works now
casties
parents: 13
diff changeset
96 data = {}
281d223aa361 attribution works now
casties
parents: 13
diff changeset
97 attr = {}
8
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
98 # put attributes in @attr
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
99 for attname in elem.keys():
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
100 attr[attname] = elem.get(attname)
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
101
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
102 data['@attr'] = attr
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
103 if self.mappingSelectAttribute:
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
104 # put type in @type
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
105 type = attr.get(self.mappingSelectAttribute, None)
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
106 if type is not None:
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
107 data['@type'] = normalizeFieldName(type)
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
108
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
109 # put all subelements in dict
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
110 if normalizeNames:
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
111 for e in elem:
14
281d223aa361 attribution works now
casties
parents: 13
diff changeset
112 data[normalizeFieldName(e.tag)] = getText(e, recursive=allText)
8
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
113 else:
4cd862bf37a3 more renovation
casties
parents: 7
diff changeset
114 for e in elem:
14
281d223aa361 attribution works now
casties
parents: 13
diff changeset
115 data[e.tag] = getText(e, recursive=allText)
281d223aa361 attribution works now
casties
parents: 13
diff changeset
116
281d223aa361 attribution works now
casties
parents: 13
diff changeset
117 dataList.append(data)
281d223aa361 attribution works now
casties
parents: 13
diff changeset
118
281d223aa361 attribution works now
casties
parents: 13
diff changeset
119 if allOccurrences:
281d223aa361 attribution works now
casties
parents: 13
diff changeset
120 return dataList
281d223aa361 attribution works now
casties
parents: 13
diff changeset
121
281d223aa361 attribution works now
casties
parents: 13
diff changeset
122 if dataList:
281d223aa361 attribution works now
casties
parents: 13
diff changeset
123 return dataList[0]
281d223aa361 attribution works now
casties
parents: 13
diff changeset
124 else:
281d223aa361 attribution works now
casties
parents: 13
diff changeset
125 return {}
6
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
126
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
127 def getMapping(self, type):
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
128 """returns MetaDataMapping for type"""
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
129 # try type as id
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
130 mapping = getattr(self, type, None)
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
131 if mapping is None:
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
132 # try manually
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
133 mapFolder = self
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
134 for obj in mapFolder.objectValues():
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
135 if obj.meta_type == "MetadataMapping":
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
136 # real type is in title
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
137 mapType = obj.title
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
138 if mapType == type:
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
139 # try type as is
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
140 return obj
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
141
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
142 if normalizeFieldName(mapType, underscore=True) == normalizeFieldName(type, underscore=True):
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
143 # try normalized type without underscore
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
144 return obj
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
145
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
146 return mapping
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
147
6
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
148 def getMapFields(self, data):
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
149 """returns dict with metadata description for data"""
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
150 fields = {}
11
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
151 type = data.get('@type', None)
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
152 if not type:
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
153 logging.error("getMapFields: no @type!")
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
154 return fields
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
155
6
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
156 # get mapping from main/meta/bib
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
157 mapping = self.getMapping(type)
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
158 if mapping is None:
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
159 logging.error("getMapFields: no mapping for type: %s"%type)
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
160 return fields
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
161
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
162 # get field descriptions (copy so we can change it)
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
163 fields = mapping.getFields().copy()
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
164 # add field list
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
165 fields['@fieldList'] = mapping.getFieldList()
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
166
6
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
167 return fields
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
168
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
169 def getMappedData(self, data, allFields=False):
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
170 """returns dict with metadata descriptions and data for data"""
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
171 fields = self.getMapFields(data)
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
172 fieldList = fields['@fieldList']
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
173 mappedData = {}
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
174 mappedList = []
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
175 for bk in fieldList:
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
176 # ignore descriptions without data
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
177 if not data.get(bk, None):
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
178 continue
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
179
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
180 # field description (copy so we can change it)
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
181 bf = fields[bk].copy()
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
182 # add value
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
183 bf['value'] = data[bk]
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
184 mappedData[bk] = bf
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
185 mappedList.append(bk)
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
186
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
187 if allFields and len(mappedData) < len(data):
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
188 # add fields that were not in fields
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
189 for bk in data.keys():
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
190 if bk in mappedData or not data[bk]:
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
191 continue
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
192
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
193 mappedData[bk] = {'tag':bk, 'label':bk, 'value':data[bk]}
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
194 mappedList.append(bk)
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
195
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
196 mappedData['@fieldList'] = mappedList
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
197 return mappedData
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
198
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
199 def getDCMappedData(self, data, allFields=False):
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
200 """returns dict with DC keys and data form data"""
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
201 fields = self.getMapFields(data)
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
202 dcData = {}
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
203 for bk in fields.keys():
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
204 # ignore descriptions without data
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
205 if not data.get(bk, None):
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
206 continue
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
207
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
208 # field description
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
209 dc = fields[bk].get('dcmap', None)
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
210 if dc:
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
211 # add value
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
212 if dcData.get('dc',None):
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
213 # key exists - append
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
214 dcData[dc] += '/' + data[bk]
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
215 else:
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
216 dcData[dc] = data[bk]
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
217
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
218 return dcData
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
219
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
220 def getFormatted(self, template, path=None, dom=None, data=None, allFields=False):
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
221 """returns string with document data formatted according to template.
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
222 gets data from server or dom or pre-parsed data."""
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
223 logging.debug("getFormatted(template=%s)"%(template))
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
224
13
5f48f956ffa3 more resistant to empty data
casties
parents: 12
diff changeset
225 # get contents of tag
6
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
226 if data is None:
9
eeaad777d3d7 more work for non-bib metadata
casties
parents: 8
diff changeset
227 data = self.getData(path=path, dom=dom)
13
5f48f956ffa3 more resistant to empty data
casties
parents: 12
diff changeset
228 if data is None:
5f48f956ffa3 more resistant to empty data
casties
parents: 12
diff changeset
229 # no data
5f48f956ffa3 more resistant to empty data
casties
parents: 12
diff changeset
230 logging.error("getFormatted: no data for template: %s"%(template))
5f48f956ffa3 more resistant to empty data
casties
parents: 12
diff changeset
231 return ""
6
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
232
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
233 type = data.get('@type', '')
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
234
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
235 # get template
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
236 tp=getattr(self,"%s_%s"%(template, normalizeFieldName(type)), None)
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
237 if tp is None:
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
238 logging.warning("getFormatted: no template for: %s_%s"%(template, type))
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
239 # try generic
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
240 tp=getattr(self,"%s_generic"%(template), None)
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
241 if tp is None:
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
242 logging.error("getFormatted: no generic template either: %s"%(template))
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
243 return ""
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
244
14
281d223aa361 attribution works now
casties
parents: 13
diff changeset
245 if type:
281d223aa361 attribution works now
casties
parents: 13
diff changeset
246 # put field descriptions in mdHash
281d223aa361 attribution works now
casties
parents: 13
diff changeset
247 fields = self.getMappedData(data, allFields=allFields)
281d223aa361 attribution works now
casties
parents: 13
diff changeset
248 else:
281d223aa361 attribution works now
casties
parents: 13
diff changeset
249 fields = {}
6
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
250
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
251 return tp(mdmap=fields, md=data)
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
252
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
253
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
254
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
255 def correctPath(self,path,remove=None,prefix=None,cut=0):
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
256 """convinience method um einen pfad zu veraendern"""
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
257 if remove is not None:
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
258 path=path.replace(remove,'')
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
259 if prefix is not None:
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
260 path=os.path.join(prefix,path)
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
261
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
262 if cut>0:
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
263 splitted=path.split("/")
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
264 path="/".join(splitted[0:len(splitted)-cut])
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
265 return path
6
00147a1ab4ac division between MetaDataFolder and Metadata looks good now (to me :-)
casties
parents: 5
diff changeset
266
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
267
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
268 def importMetaDataExportXML(self,importFile=None,RESPONSE=None):
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
269 """imports metadata from the metadataexportxml file"""
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
270
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
271 if importFile is None:
3
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
272 pt=PageTemplateFile('zpt/importMetaDataExportXML', globals()).__of__(self)
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
273 return pt()
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
274
3
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
275 dom=ET.parse(importFile)
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
276 node = dom.getroot()
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
277 if node.tag != 'metadataExport':
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
278 node = dom.find("metadataExport")
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
279
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
280 self.createMappingFromDom(node)
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
281
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
282 if RESPONSE is not None:
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
283 RESPONSE.redirect('manage_main')
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
284
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
285
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
286 def createMappingFromDom(self,metadatanode,metadata=None):
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
287 """erzeuge ein Mapping aus dem der metadatanode des xmlformats, metadata ist ein metadataobject"""
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
288
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
289 if metadata is None:
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
290 metadata=self
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
291
3
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
292 nodes=metadatanode
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
293
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
294 for node in nodes:
2
ac8e119b25ec trying to make import from xml work
casties
parents: 1
diff changeset
295 logging.debug("node: %s"%repr(node))
3
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
296 if node.tag=="set":
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
297 set=node
3
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
298 id=set.get('name')
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
299 list=[]
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
300 argList={}
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
301 for entry in set:
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
302 genericName=entry.get('genericName')
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
303 if set.get('name')=='generic':
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
304 # generic mapping doesn't have labels
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
305 tag = genericName
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
306 label = genericName
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
307 else:
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
308 tag=entry.get('tag')
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
309 label=entry.get('label')
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
310
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
311 if not tag:
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
312 # ignore empty tags
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
313 continue
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
314
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
315 description=getText(entry)
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
316 argList[tag]={'tag':tag,'label':label,'explanation':description,'status':'optional'}
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
317
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
318 logging.debug("createMappingFromDom: new mapping=%s"%repr(argList))
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
319 metadata._setObject(id,MetaDataMapping(id,id,argList))
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
320
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
321 elif node.tag=="metadata":
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
322 mn=node
3
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
323 name=mn.get('name')
3dadf0d89261 more renovation
casties
parents: 2
diff changeset
324 logging.debug("createMappingFromDom: new metadata=%s"%repr(name))
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
325 metadata._setObject(name,MetaData(name,name))
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
326 mdObj=getattr(metadata,name)
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
327 mdObj.createMappingFromDom(mn)
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
328
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
329
5
c1dbf78cc036 more MetaDataFolder
casties
parents: 4
diff changeset
330 security.declarePublic('changeMetaDataForm')
9
eeaad777d3d7 more work for non-bib metadata
casties
parents: 8
diff changeset
331 changeMetaDataForm = PageTemplateFile('zpt/changeMetadata', globals())
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
332
5
c1dbf78cc036 more MetaDataFolder
casties
parents: 4
diff changeset
333 security.declarePublic('changeMetaData')
11
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
334 def changeMetaData(self,title=None,shortDescription=None,description=None,mappingSelectAttribute=None,fields=None,metaDataServerUrl=None,RESPONSE=None):
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
335 """Change Metadata"""
11
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
336 self.title = title
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
337 self.shortDescription=shortDescription
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
338 self.description=description
11
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
339 self.mappingSelectAttribute=mappingSelectAttribute
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
340 if fields:
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
341 self.fieldList=fields.split(",")[0:]
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
342
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
343 self.metaDataServerUrl=metaDataServerUrl
11
a29665fa9c62 more work for non-bib metadata
casties
parents: 9
diff changeset
344
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
345 if RESPONSE is not None:
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
346 RESPONSE.redirect('manage_main')
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
347
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
348
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
349 def manage_addMetaDataForm(self):
9
eeaad777d3d7 more work for non-bib metadata
casties
parents: 8
diff changeset
350 """interface for adding the Metadata"""
eeaad777d3d7 more work for non-bib metadata
casties
parents: 8
diff changeset
351 pt=PageTemplateFile('zpt/addMetadataForm', globals()).__of__(self)
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
352 return pt()
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
353
13
5f48f956ffa3 more resistant to empty data
casties
parents: 12
diff changeset
354 def manage_addMetaData(self,id,title=None,shortDescription=None,description=None,fields=None,RESPONSE=None):
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
355 """a metadata objekt"""
13
5f48f956ffa3 more resistant to empty data
casties
parents: 12
diff changeset
356 newObj = MetaData(id,title=title,shortDescription=shortDescription,description=description,fields=fields)
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
357 self.Destination()._setObject(id,newObj)
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
358 if RESPONSE is not None:
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
359 RESPONSE.redirect('manage_main')
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
360