Mercurial > hg > MetaDataProvider
comparison OSAS_helpers.py @ 20:9a1e75e708e1
small bugs
in metadata context field in metadata docinfo added
osa_system2 nicht mehr noetig
author | dwinter |
---|---|
date | Sun, 23 Oct 2011 21:29:02 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
19:bd3025a6a6c0 | 20:9a1e75e708e1 |
---|---|
1 # hilfsfunktionen | |
2 import os.path | |
3 from types import * | |
4 import xml.dom.minidom | |
5 import logging | |
6 | |
7 #ersetzt logging | |
8 def logger(txt,method,txt2): | |
9 """logging""" | |
10 logging.info(txt+ txt2) | |
11 | |
12 | |
13 | |
14 OSASObjectTypes=['OSAS_metaDataFile','OSAS_dir','Osas_file','OSAS_imageFolder','OSAS_dir_archive','OSAS_file_archive','OSAS_videoFolder','OSAS_videoFile','OSAS_videoFolder'] | |
15 OSASDirObjects=['OSAS_dir','OSAS_imageFolder',] | |
16 OSASExcludedFiles=['TheVolumeSettingsFolder','TheFindByContentFolder'] | |
17 | |
18 | |
19 def getText(nodelist): | |
20 """gibt text aus nodelist""" | |
21 rc = "" | |
22 for node in nodelist: | |
23 if node.nodeType == node.TEXT_NODE: | |
24 rc = rc + node.data | |
25 return rc | |
26 | |
27 def getMetaInfoFromXML(path,xmlInfo): | |
28 """get index.meta and translate it to HTML""" | |
29 html=[] | |
30 | |
31 dom = xml.dom.minidom.parseString(xmlInfo) | |
32 try: | |
33 name=getText(dom.getElementsByTagName("name")[0].childNodes) | |
34 except: | |
35 name="NOT_DEFINED!!!" | |
36 try: | |
37 creator=getText(dom.getElementsByTagName("creator")[0].childNodes) | |
38 except: | |
39 creator="NOT_DEFINED!!!" | |
40 | |
41 try: | |
42 creation_date=getText(dom.getElementsByTagName("archive-creation-date")[0].childNodes) | |
43 except: | |
44 creation_date="NOT_DEFINED!!!" | |
45 | |
46 try: | |
47 description=getText(dom.getElementsByTagName("description")[0].childNodes) | |
48 except: | |
49 description="NOT_DEFINED!!!" | |
50 | |
51 try: | |
52 type=getText(dom.getElementsByTagName("content-type")[0].childNodes) | |
53 except: | |
54 type="" | |
55 if type=="scanned document": | |
56 html="<h3>Document: "+name+"</h3>" | |
57 elif type=="folder": | |
58 html="<h3>Folder: "+name+"</h3>" | |
59 else: | |
60 html="<h3>Document: "+name+"</h3>" | |
61 | |
62 html=html+"<p><i>created by: "+creator+" at: "+creation_date+"</i></p>" | |
63 html=html+"<h4>Description</h4><p>"+description+"</p>" | |
64 try: | |
65 bib = dom.getElementsByTagName("meta")[0].getElementsByTagName("bib")[0] | |
66 if bib.attributes.has_key('type'): | |
67 html=html+"<h4>Info ("+bib.attributes['type'].value+")</h4>" | |
68 else: | |
69 html=html+"<h4>Info</h4>" | |
70 html=html+getBib(bib.childNodes) | |
71 | |
72 except: | |
73 """none""" | |
74 | |
75 # html=html.encode('utf-8','replace')+getBib(bib.childNodes).encode('utf-8','replace') | |
76 | |
77 | |
78 return html | |
79 | |
80 | |
81 def getBib(nodelist): | |
82 """ translate bibliographical entries """ | |
83 rc= "<table border='0'>" | |
84 | |
85 for node in nodelist: | |
86 | |
87 if node.nodeType == node.ELEMENT_NODE: | |
88 """nothing""" | |
89 | |
90 rc = rc+"<tr><td valign='right'>"+str(node.nodeName)+":</td><td> "+getText(node.childNodes)+"</td></tr>" | |
91 | |
92 #print rc | |
93 return rc+"</table>" | |
94 | |
95 def getPropertyOfDirs(indexMeta,propertyName): | |
96 """gibt Eigenschaften der directorys gemaess Index.metas file in path aus | |
97 @param indexMeta: index.meta file | |
98 @param propertyName: Property die ausgegebenwerden soll | |
99 @return: Hash mit key name des Directories and Value von property, None if error. | |
100 """ | |
101 ret={} | |
102 | |
103 try: | |
104 dom=xml.dom.minidom.parseString(indexMeta) | |
105 | |
106 rootProp=xml.xpath.Evaluate("/resource/%s"%propertyName,dom) | |
107 if len(rootProp)==1: | |
108 property= getText(rootProp[0].childNodes) | |
109 ret["."]=property | |
110 | |
111 for node in dom.getElementsByTagName("dir"): | |
112 try: | |
113 property= getText(node.getElementsByTagName(propertyName)[0].childNodes) | |
114 dirName=getText(node.getElementsByTagName("name")[0].childNodes) | |
115 ret[dirName]=property | |
116 except: | |
117 ret[dirName]=none | |
118 return ret | |
119 except: | |
120 | |
121 return ret | |
122 | |
123 | |
124 def dirHasProperty(path,indexMeta,propertyName,propertyValue): | |
125 """gibt 1 zurueck falls path hat propertyName und properName=propertyValue | |
126 @param propertyName:Property von path in index.meta des parent folders von path | |
127 @param propertyValue:Wert der Property | |
128 @param path: Pfad der getestet werden soll | |
129 """ | |
130 if getPropertyOfDirs(indexMeta,propertyName).get(os.path.split(path)[1],None)==propertyValue: | |
131 return 1 | |
132 else: | |
133 return 0 | |
134 | |
135 def isImageFolder(path,indexMeta): | |
136 """check if folder contains images without metadata or with metadata""" | |
137 # metadaten ergeben imagefolder | |
138 if dirHasProperty(path,indexMeta,'content-type','images'): | |
139 return 1 | |
140 | |
141 def isVideoFolder(path,indexMeta): | |
142 """check if folder contains images without metadata or with metadata""" | |
143 # metadaten ergeben imagefolder | |
144 | |
145 if dirHasProperty(path,indexMeta,'media-type','video'): | |
146 return 1 | |
147 | |
148 def isVideoStream(path,indexMeta): | |
149 if dirHasProperty(path,indexMeta,'container-format','quicktime'): | |
150 return 1 | |
151 | |
152 def isImageFolderGuess(path): | |
153 """check if folder contains images without metadata or with metadata""" | |
154 #sonst rate ob folder images enthaelten durch test nach suffix | |
155 try: | |
156 dir=os.listdir(path) | |
157 | |
158 imagesuffixes=['.gif','.jpg','.jpeg','.png','.tiff','.tif'] | |
159 ret="" | |
160 for a in dir: | |
161 | |
162 suffix=os.path.splitext(a)[1].lower() | |
163 | |
164 if suffix in imagesuffixes: | |
165 return 1 | |
166 | |
167 return 0 | |
168 | |
169 except: | |
170 return 0 | |
171 | |
172 def checkOSASFileType(object): | |
173 """checke filetype | |
174 nicht erkannte type auf werden auf none gesetzt | |
175 """ | |
176 if os.path.split(object)[1][0]=="." or os.path.split(object)[1] in OSASExcludedFiles: | |
177 return None | |
178 elif isImageFolderGuess(object): | |
179 return 'OSAS_imageFolder' | |
180 elif os.path.split(object)[1]=="index.meta": | |
181 return 'OSAS_metaDataFile' | |
182 elif os.path.isdir(object): | |
183 return 'OSAS_dir' | |
184 elif os.path.isfile(object): | |
185 return 'OSAS_file' | |
186 | |
187 def toList(fields): | |
188 """Einzelfeld in Liste umwandeln | |
189 @param fields: String oder Array | |
190 @return: gibt liste zurueck | |
191 | |
192 """ | |
193 if type(fields)==StringType: | |
194 return [fields] | |
195 else: | |
196 return fields | |
197 | |
198 | |
199 def localDate(): | |
200 """gives formatted local date""" | |
201 return strftime("%d.%m.%Y",localtime()) | |
202 | |
203 |