-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswissbibHash.py
204 lines (138 loc) · 6.12 KB
/
swissbibHash.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from xml.sax.handler import ContentHandler
import re
__author__ = 'swissbib'
class HashMarcContent(ContentHandler):
def __init__(self, harvestingConfigs):
ContentHandler.__init__(self)
self.tag001 = False
self.docid = '0'
self.numericDataField = False
self.validsubfield = False
self.marccontent = []
self.validOAIDC = False
self.numericControlField = False
self.harvestingConfigs = harvestingConfigs
def startElement(self, name, attrs):
if name.find('marc:datafield') != -1:
if attrs.get("tag").isnumeric() and not self.isDataTagExcluded(attrs.get("tag")) :
self.numericDataField = True
else:
self.numericDataField = False
elif name.find('marc:controlfield') != -1:
#b = attrs.get("tag").isnumeric()
testAlpha = attrs.get("tag")
if attrs.get("tag") == '001':
self.tag001 = True
elif attrs.get("tag").isnumeric() and testAlpha <> '005':
self.numericControlField = True
else:
self.numericControlField = False
def endElement(self, name):
if name.find('marc:datafield') != -1:
self.numericDataField = False
elif name.find('marc:controlfield') != -1:
self.numericControlField = False
def characters(self, content):
if self.numericDataField or self.numericControlField:
#remove EOL and whitespaces at the tail of the line
self.marccontent.append(content.rstrip())
elif self.tag001:
self.docid = content
self.tag001 = False
elif self.numericControlField:
self.marccontent.append(content.rstrip())
elif self.validOAIDC:
self.marccontent.append(content.rstrip())
def getMarccontent(self):
return "".join(self.marccontent)
def getDocid(self):
return self.docid
def isDataTagExcluded(self,tagValue):
if not self.harvestingConfigs.getDataTagExcludedFromHash() is None and tagValue in self.harvestingConfigs.getDataTagExcludedFromHash():
return True
else:
return False
class HashNebisMarcContent(HashMarcContent):
def __init__(self,harvestingConfigs):
HashMarcContent.__init__(self,harvestingConfigs)
def startElement(self, name, attrs):
#Nebis benutzt keine namespaces
if name.find('datafield') != -1 :
if attrs.get("tag").isnumeric() and not self.isDataTagExcluded(attrs.get("tag")):
self.numericDataField = True
else:
self.numericDataField = False
elif name.find('controlfield') != -1:
#b = attrs.get("tag").isnumeric()
testAlpha = attrs.get("tag")
if attrs.get("tag") == '001':
self.tag001 = True
elif attrs.get("tag").isnumeric() and testAlpha <> '005':
self.numericControlField = True
else:
self.numericControlField = False
def endElement(self, name):
if name.find('datafield') != -1:
self.numericDataField = False
elif name.find('controlfield') != -1:
self.numericControlField = False
class HashSwissBibMarcContent(HashMarcContent):
def __init__(self, harvestingConfigs):
HashMarcContent.__init__(self, harvestingConfigs)
def startElement(self, name, attrs):
if name.find('mx:datafield') != -1 :
if attrs.get("tag").isnumeric() and not self.isDataTagExcluded(attrs.get("tag")):
self.numericDataField = True
else:
self.numericDataField = False
elif name.find('mx:controlfield') != -1:
test = attrs.get("tag")
if attrs.get("tag") == '001':
self.tag001 = True
elif attrs.get("tag").isnumeric() and test <> '005':
self.numericControlField = True
else:
self.numericControlField = False
def endElement(self, name):
if name.find('mx:datafield') != -1:
self.numericDataField = False
elif name.find('mx:controlfield') != -1:
self.numericControlField = False
class HashDcContent(HashMarcContent):
#todo: sinnvolle Regel um content von DC auszuschliessen, im Moment begrenze ich das auf datafields
#evtl. sollte man die Ausschlussregel auch auf controlfields anwenden
#zur Zeit fuer die einzelnen Klassen ausprogrammiert
def __init__(self, harvestingConfigs):
HashMarcContent.__init__(self, harvestingConfigs)
self.pIdentifier = re.compile("^identifier$",re.UNICODE | re.DOTALL | re.IGNORECASE)
self.pdcField = re.compile("^dc:",re.UNICODE | re.DOTALL | re.IGNORECASE)
def startElement(self, name, attrs):
dcField = self.pdcField.search(name)
ident = self.pIdentifier.search(name)
if dcField :
self.validOAIDC = True
elif ident:
self.tag001 = True
def endElement(self, name):
if self.pdcField.search(name) or self.pIdentifier.search(name):
self.validOAIDC = False
class HashReroMarcContent(HashMarcContent):
def __init__(self, harvestingConfigs):
HashMarcContent.__init__(self, harvestingConfigs)
def startElement(self, name, attrs):
#rero Besonderheit:
#es wird nicht das Standardattribut tag sonder type fuer controlfields und datafields verwendet
if name.find('marc:datafield') != -1 and not self.isDataTagExcluded(attrs.get("type")):
if attrs.get("type").isnumeric():
self.numericDataField = True
else:
self.numericDataField = False
elif name.find('marc:controlfield') != -1:
#b = attrs.get("tag").isnumeric()
testAlpha = attrs.get("type")
if attrs.get("type") == '001':
self.tag001 = True
elif attrs.get("type").isnumeric() and testAlpha <> '005':
self.numericControlField = True
else:
self.numericControlField = False