-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwca.py
executable file
·182 lines (156 loc) · 5.8 KB
/
dwca.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import zipfile
from lxml import etree
import sys
from collections import deque
import xmlDictTools
import pprint
FNF_ERROR = "File {0} not present in archive."
NAMESPACES = { "http://rs.tdwg.org/dwc/terms/": "dwc:",
"http://purl.org/dc/terms/": "dcterms:",
"http://rs.tdwg.org/ac/terms/": "ac:",
"http://ns.adobe.com/xap/1.0/rights/": "xmpRights:",
"http://ns.adobe.com/xap/1.0/": "xmp:",
"http://iptc.org/std/Iptc4xmpExt/1.0/xmlns/": "Iptc4xmpExt:",
}
class Dwca:
"""
Internal representation of a Darwin Core Archive file.
"""
archdict = None
archive = None
metadata = None
core = None
extensions = None
def __init__(self,name="dwca.zip"):
self.archive = zipfile.ZipFile(name, 'r')
meta = self.archive.open('meta.xml','r')
root = etree.parse(meta).getroot()
rdict = xmlDictTools.xml2d(root)
self.archdict = rdict["archive"]
metadata = self.archdict["#metadata"]
mf = self.archive.open(metadata,'r')
mdtree = etree.parse(mf).getroot()
self.metadata = xmlDictTools.xml2d(mdtree)
corefile = self.archdict["core"]["files"]["location"]
self.core = DwcaRecordFile(self.archdict["core"], self.archive.open(corefile,'r'))
self.extensions = []
if "extension" in self.archdict:
if isinstance(self.archdict["extension"],list):
for x in self.archdict["extension"]:
extfile = x["files"]["location"]
self.extensions.append(DwcaRecordFile(x, self.archive.open(extfile,'r')))
else:
extfile = self.archdict["extension"]["files"]["location"]
self.extensions.append(DwcaRecordFile(self.archdict["extension"], self.archive.open(extfile,'r')))
class DwcaRecordFile:
"""
Internal representation of a darwin core archive record data file.
"""
name = ""
filehandle = None
closed = True
namespace = ""
filetype = ""
rowtype = ""
encoding = "utf-8"
linesplit = "\n"
fieldsplit = "\t"
fieldenc = ""
ignoreheader = 0
defaults = None
# Don't instantiate objects here
fields = None
linebuf = None
def __init__(self,filedict,fh):
"""
Construct a DwcaRecordFile from a xml tree pointer to the <location> tag containing the data file name
and a file handle pointing to the data file.
"""
self.name = filedict['files']['location']
self.filehandle = fh
# Instantiate objects here or we get cross talk between classes.
self.fields = {}
self.linebuf = deque()
closed = False
idtag = "id"
if 'id' in filedict:
self.filetype = "core"
else:
idtag = "coreid"
self.filetype = "extension"
self.rowtype = filedict["#rowType"]
self.encoding = filedict["#encoding"]
self.linesplit = filedict["#linesTerminatedBy"].decode('string_escape')
self.fieldsplit = filedict["#fieldsTerminatedBy"].decode('string_escape')
self.fieldenc = filedict["#fieldsEnclosedBy"].decode('string_escape')
self.ignoreheader = int(filedict["#ignoreHeaderLines"])
idfld = filedict[idtag]
self.fields[int(idfld['#index'])] = idtag
self.defaults = {}
for fld in filedict['field']:
term = fld['#term']
for ns in NAMESPACES:
if term.startswith(ns):
term = term.replace(ns,NAMESPACES[ns])
break
if '#index' in fld:
self.fields[int(fld['#index'])] = term
elif '#default' in fld:
self.defaults[term] = fld['#default']
else:
raise Exception("Field {0} has neither index nor default in {1}".format(term,self.name))
def __iter__(self):
"""
Returns the object itself, as per spec.
"""
return self
def close(self):
"""
Closes the internally maintained filehandle
"""
self.filehandle.close()
closed = filehandle.closed
def next(self):
"""
Returns the next line in the record file, used for iteration
"""
r = self.readline()
if len(r) > 0:
return r
else:
raise StopIteration
def readline(self,size=None):
"""
Returns a parsed record line from a DWCA file as an dictionary.
"""
while len(self.linebuf) == 0:
fileLine = self.filehandle.readline().decode(self.encoding)
if len(fileLine) == 0:
return {}
else:
fileLineArr = fileLine.split(self.linesplit)
for potLine in fileLineArr:
if len(potLine) > 0:
if self.ignoreheader == 0:
self.linebuf.append(potLine)
else:
self.ignoreheader -= 1
line = self.linebuf.popleft()
lineArr = line.split(self.fieldsplit)
lineDict = {}
for k in self.fields:
try:
if lineArr[k] != "":
lineDict[self.fields[k]] = lineArr[k]
except IndexError:
print "Line missing fields: ", line
lineDict.update(self.defaults)
return lineDict
def readlines(self,sizehint=None):
"""
Returns all lines in the file. Cheats off readline.
"""
lines = []
for line in self:
lines.append(self.readline())
return lines