-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswissbibMongoHarvesting.py
987 lines (674 loc) · 35.8 KB
/
swissbibMongoHarvesting.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
from pymongo import MongoClient
from datetime import datetime
import sys
import hashlib
import zlib
import os
import re
from bson.binary import Binary
#from pymongo.binary import Binary
import time
from swissbibUtilities import ErrorMongoProcessing
__author__ = 'swissbib'
class MongoDBHarvestingWrapper():
def __init__(self,applicationContext=None):
self.dbConnections = {}
self.appContext = applicationContext
for key in self.appContext.getConfiguration().getMongoHosts():
host = self.appContext.getConfiguration().getMongoHosts() [key]
sConnection = host.getConnection()
dbConnection = {}
client = MongoClient(sConnection)
#connection = Connection(sConnection)
dbConnection["connection"] = client
#dbConnection["dbname"] = host.getDB()
dbConnection["db"] = client[host.getDB()]
collections = {}
for key in host.getCollections():
name = host.getCollections()[key]
if not name is None:
collections[key] = dbConnection["db"][name]
dbConnection["collections"] = collections
self.dbConnections[host.getName()] = dbConnection
#self.host = harvestingConfig.getMongoHost()
#self.port = harvestingConfig.getMongoPort()
#self.dbName = harvestingConfig.getMongoDB()
#self.collectionName = harvestingConfig.getMongoCollection()
#there is a bug in pymongo related to MongoDB >= 2.1.x
#therefor we use so far the admin user
#https://jira.mongodb.org/browse/PYTHON-371
#I have to practice at first how to update python libraries
#self.connection = Connection(self.host)
#info = self.connection.server_info()
#{u'ok': 1.0,
# u'sysInfo': u'Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_49',
# u'version': u'2.2.1', u'versionArray': [2, 2, 1, 0],
# u'debug': False,
# u'maxBsonObjectSize': 16777216,
# u'bits': 64,
# u'gitVersion': u'd6764bf8dfe0685521b8bc7b98fd1fab8cfeb5ae'}
#self.db = self.connection[self.dbName]
self.collectedIds = {}
#if not self.collectionName is None:
# self.collectionRecords = self.db[self.collectionName]
def getDBConnections(self):
return self.dbConnections
def processRecord(self,hashType,configuration):
tCollection = self.dbConnections["harvesting"]["collections"]["repository"]
##otherwise varibale docId might not be defined in exception
docId = ""
try:
docId = "(" + configuration.getNetworkPrefix() + ")" + hashType.getDocid()
#tobehashed = hashType.getMarccontent()
#attention only name hash shadows an internal name
hashValue = hashlib.md5(hashType.getMarccontent().encode("utf8")).hexdigest()
mongoRecord = tCollection.find_one({"_id": docId})
#mongoRecord = self.collectionRecords.find_one({"_id": docId})
status = None
#todo: remove hashcontent after testing!
if not mongoRecord:
newrecord = {"_id":docId,
"hash":hashValue,
"lastdate":str(datetime.now())[:10],
"laststatus":"insertedToCBS",
"sizeHistory": 1,
"history" : [
{
"datum": datetime.now(),
"status": "insertedToCBS",
"hash" : hashValue
#"hashcontent" : hashType.getMarccontent()
}
]
}
tCollection.insert(newrecord)
#self.collectionRecords.insert(newrecord)
status = "writeToFileInserted"
elif mongoRecord and mongoRecord["hash"] == hashValue:
newHistoryItem ={
"datum":datetime.now(),
"status": "skipped",
"hash":hashValue
#"hashcontent" : hashType.getMarccontent()
}
mongoRecord["history"].append(newHistoryItem)
mongoRecord["sizeHistory"] = len(mongoRecord["history"])
mongoRecord["lastdate"] = str(datetime.now())[:10]
mongoRecord["laststatus"] = "skipped"
#tCollection.save(mongoRecord,safe=True)
tCollection.replace_one({"_id": docId}, mongoRecord)
#self.db[self.collectionName].save(mongoRecord)
#self.collectionRecords.save(mongoRecord,safe=True)
status = "skip"
elif mongoRecord and mongoRecord["hash"] != hashValue:
newHistoryItem ={
"datum":datetime.now(),
"status": "updatedToCBS",
"hash":hashValue
#"hashcontent" : hashType.getMarccontent()
}
mongoRecord["history"].append(newHistoryItem)
mongoRecord["sizeHistory"] = len(mongoRecord["history"])
mongoRecord["lastdate"] = str(datetime.now())[:10]
mongoRecord["laststatus"] = "updatedToCBS"
mongoRecord["hash"] = hashValue
#self.db[self.collectionName].save(mongoRecord)
#tCollection.save(mongoRecord,safe=True)
tCollection.replace_one({"_id": docId}, mongoRecord)
#self.collectionRecords.save(mongoRecord,safe=True)
status = "writeToFileUpdated"
return status
except:
message = ["error while processing reord against Mongo\n",
"docid: ", docId,
"SytemInfo: ", sys.exc_info()]
raise ErrorMongoProcessing(message)
def closeResources(self):
for connectionKey in self.dbConnections:
if not self.dbConnections[connectionKey] is None:
self.dbConnections[connectionKey]["connection"].close()
def storeResultOfProcessing(self,resultCollector,harvestingConfig):
tCollection = self.dbConnections["harvesting"]["collections"]["summary"]
if resultCollector.getHarvestingParameter() is None:
newSummary = {
"datum":datetime.now(),
"repository":harvestingConfig.getNetworkPrefix(),
"skipped":resultCollector.getRecordsSkipped(),
"tocbs" :resultCollector.getRecordsToCBS(),
"deleted":resultCollector.getRecordsDeleted(),
"parseError":resultCollector.getRecordsparseError()
}
else:
newSummary = {
"datumExact":datetime.now(),
"datum":datetime.now().strftime("%Y-%m-%d"),
"repository":harvestingConfig.getNetworkPrefix(),
"skipped":resultCollector.getRecordsSkipped(),
"tocbsInserted" :resultCollector.getRecordsToCBSInserted(),
"tocbsUpdated" :resultCollector.getRecordsToCBSUpdated(),
"deleted":resultCollector.getRecordsDeleted(),
"parseError":resultCollector.getRecordsparseError(),
"oaiParameter":resultCollector.getHarvestingParameter()
}
tCollection.insert(newSummary)
def storeResultOfNebisProcessing(self,resultCollector,harvestingConfig):
filenames = ""
#for filename in resultCollector.getProcessedFile():
# filenames +
tCollection = self.dbConnections["harvesting"]["collections"]["summary"]
newSummary = {
"datum":datetime.now(),
"repository":harvestingConfig.getNetworkPrefix(),
"skipped":resultCollector.getRecordsSkipped(),
"tocbs inserted" :resultCollector.getRecordsToCBSInserted(),
"tocbs updated" :resultCollector.getRecordsToCBSUpdated(),
"deleted":resultCollector.getRecordsDeleted(),
"parseError":resultCollector.getRecordsparseError(),
"processedFiles" : "#".join(resultCollector.getProcessedFile())
}
tCollection.insert(newSummary)
class MongoDBHarvestingWrapperAdmin(MongoDBHarvestingWrapper):
def __init__(self,applicationContext=None):
MongoDBHarvestingWrapper.__init__(self,applicationContext=applicationContext)
#Todo: der Wrapper sollte eine Refernz auf die configs behalten
#und nicht nur einfach Werte lesen und dann die iNstanz nicht weiter beachten
#Refactoring!
self.backupItemOpen = "<backupitem>"
self.backupItemClose = "</backupitem>"
self.idTagItemOpen = "<recordId>"
self.idTagItemClose = "</recordId>"
self.statusTagItemOpen = "<recordStatus>"
self.statusTagItemClose = "</recordStatus>"
self.dateTagItemOpen = "<recordDate>"
self.dateTagItemClose = "</recordDate>"
self.metadataTagItemOpen = "<recordMetadata>"
self.metadataTagItemClose = "</recordMetadata>"
self.ptagIdItem = re.compile("<recordId>(.*?)</recordId>",re.UNICODE | re.DOTALL | re.IGNORECASE)
self.ptagStatusItem = re.compile("<recordStatus>(.*?)</recordStatus>",re.UNICODE | re.DOTALL | re.IGNORECASE)
self.ptagDateItem = re.compile("<recordDate>(.*?)</recordDate>",re.UNICODE | re.DOTALL | re.IGNORECASE)
self.ptagMetadataItem = re.compile("<recordMetadata>(.*?)</recordMetadata>",re.UNICODE | re.DOTALL | re.IGNORECASE)
self.sourceOAIExtensions = {}
self.sourceMappings = {}
self.userDateStamp = None
self.pRecordUserDatestamp = re.compile("(.*?<datestamp>).*?(</datestamp>.*)",re.UNICODE | re.DOTALL | re.IGNORECASE)
self.pRecordDeleted = re.compile("deleted|newdeleted",re.UNICODE | re.DOTALL | re.IGNORECASE)
def readRecordsWithTimeStamp(self, startDate=None, endDate=None, outDir=None, fileSize=None, countToRead = None):
sourceCollection = self.getDBConnections()["nativeSources"]["collections"]["sourceDB"]
regExpdateTimeStamp = re.compile('<header>.*?<datestamp>(.*?)</datestamp>.*?</header>',re.UNICODE | re.DOTALL |
re.IGNORECASE)
if not startDate is None and not endDate is None:
tStartDate = datetime.strptime(startDate, "%Y-%m-%dT%H:%M:%SZ")
tEndDate = datetime.strptime(endDate, "%Y-%m-%dT%H:%M:%SZ")
clause = {
'$and': [{'recordTimestamp': {'$exists': True}},
{'recordTimestamp': {'$ne': None}},
{'recordTimestamp': {'$gte': tStartDate}},
{'recordTimestamp': {'$lte': tEndDate}}]
}
elif not startDate is None:
tStartDate = datetime.strptime(startDate, "%Y-%m-%dT%H:%M:%SZ")
clause = {
'$and': [{'recordTimestamp': {'$exists': True}},
{'recordTimestamp': {'$ne': None}},
{'recordTimestamp': {'$gte': tStartDate}}]
}
elif not endDate is None:
tEndDate = datetime.strptime(endDate, "%Y-%m-%dT%H:%M:%SZ")
clause = {
'$and': [{'recordTimestamp': {'$exists': True}},
{'recordTimestamp': {'$ne': None}},
{'recordTimestamp': {'$lte': tEndDate}}]
}
else:
clause = None
lastTimeStamp = "not defined so far"
if not clause is None:
outDir = self.checkAndCreateOutDir(outDir)
outfile = self.defineOutPutFile(outDir)
fileToWrite = open(outfile, "w")
self.writeHeader(fileToWrite)
#at the moment ascending sort order is the only possibility
print "".join(["number of total records in the current pipe: ", str(sourceCollection.find(clause).count()),"\n"])
if not countToRead is None:
print "number of max. records being processed: " + str(countToRead)
result = sourceCollection.find(clause).sort([('recordTimestamp', +1)])
numberOfRecordsAlreadyRead = 0
for document in result:
numberOfRecordsAlreadyRead += 1
#lese den letzten timestamp
if not countToRead is None and numberOfRecordsAlreadyRead > int(countToRead) :
break
r = zlib.decompress(document["record"])
currentRecordTimestamp = regExpdateTimeStamp.search(r)
if not currentRecordTimestamp:
#severe error, must not happen because of pre conditions
self.writeFooter(fileToWrite)
fileToWrite.close()
raise Exception("".join([ "record: ", r, " doesn't contain a timestamp which shouldn't be possible"] ))
else:
lastTimeStamp = currentRecordTimestamp.group(1)
fileToWrite.write(r + "\n\n")
fileToWrite.flush()
if (not fileSize is None):
statinfo = os.stat(outfile)
size = statinfo.st_size
forCompare = int(fileSize) * 1000000
if size > forCompare :
self.writeFooter(fileToWrite)
fileToWrite.close()
outfile = self.defineOutPutFile(outDir)
fileToWrite = open(outfile, "w")
self.writeHeader(fileToWrite)
if not fileToWrite is None:
self.writeFooter(fileToWrite)
fileToWrite.close()
fileLastTimestamp = open(os.getcwd() + os.sep + "lastTimestamp.txt","w")
fileLastTimestamp.write(lastTimeStamp)
fileLastTimestamp.flush()
fileLastTimestamp.close()
def readRecords(self,rId=None,countToRead=None,fileSize=None,outDir=None,condition=None,
inputFile=None,userDatestamp=None, docRecordField=None, purgeDeleted=False):
self.userDateStamp = userDatestamp
if not inputFile is None:
self.prepareMappings()
fileToRead = open(inputFile,"r")
outDir = self.checkAndCreateOutDir(outDir)
outfile = self.defineOutPutFile(outDir)
fileToWrite = open(outfile,"w")
self.writeHeader(fileToWrite)
pRecordPrefix = re.compile("(\((.*?)\))(.*)",re.UNICODE | re.DOTALL | re.IGNORECASE)
for line in fileToRead:
nCRline = line[:-1]
spRecordPrefix = pRecordPrefix.search(nCRline)
if spRecordPrefix:
try:
network = spRecordPrefix.group(2)
firstPart = spRecordPrefix.group(1)
secondPart = spRecordPrefix.group(3)
collection = self.sourceMappings[network]
dbConnection = self.getDBConnections()["nativeSources"]['db']
sourceCollection = dbConnection[collection]
searchedID = firstPart + self.sourceOAIExtensions[network] + secondPart
document = sourceCollection.find_one({"_id": searchedID})
if document:
r = document["record"]
#print zlib.decompress(r)
fileToWrite.write(self.setCustomDatestamp(zlib.decompress(r)) + "\n\n")
fileToWrite.flush()
if (not fileSize is None):
statinfo = os.stat(outfile)
size = statinfo.st_size
forCompare = int(fileSize) * 1000000
if (size > forCompare):
self.writeFooter(fileToWrite)
fileToWrite.close()
os.system("gzip " + self.currentOutFile)
outfile = self.defineOutPutFile(outDir)
fileToWrite = None
fileToWrite = open(outfile,"w")
self.writeHeader(fileToWrite)
else:
print >> sys.stderr, "".join(["ID: ", nCRline,"\n" ," transformed to: ", searchedID, " not found"])
except Exception as pythonBaseException:
print >> sys.stderr, "".join(["Exception: ", str(pythonBaseException), "\n" "ID: ", nCRline])
if (fileToWrite is not None):
self.writeFooter(fileToWrite)
fileToWrite.close()
os.system("gzip " + self.currentOutFile)
else:
sourceCollection = self.getDBConnections()["nativeSources"]["collections"]["sourceDB"]
if (not rId is None):
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
print "<" + self.appContext.getConfiguration().getRoottag() + ">"
document = sourceCollection.find_one({"_id": rId})
if document:
docFieldName = docRecordField is not None and docRecordField or "record"
r = document[docFieldName]
print self.setCustomDatestamp(zlib.decompress(r))
print "</" + self.appContext.getConfiguration().getRoottag() + ">"
elif (fileSize is None and outDir is not None):
outDir = self.checkAndCreateOutDir(outDir)
outfile = self.defineOutPutFile(outDir)
fileToWrite = open(outfile,"w")
self.writeHeader(fileToWrite)
if condition is None:
result = sourceCollection.find()
else:
result = sourceCollection.find(self.tokenizeCondition(condition))
alreadyRead = 0
for document in result:
if countToRead is not None and alreadyRead >= int(countToRead):
break
if purgeDeleted and self.pRecordDeleted.match(document['status']):
continue
docFieldName = docRecordField is not None and docRecordField or "record"
r = document[docFieldName]
fileToWrite.write(self.setCustomDatestamp(zlib.decompress(r) + "\n"))
alreadyRead +=1
self.writeFooter(fileToWrite)
fileToWrite.flush()
fileToWrite.close()
os.system("gzip " + self.currentOutFile)
else:
outDir = self.checkAndCreateOutDir(outDir)
outfile = self.defineOutPutFile(outDir)
fileToWrite = open(outfile,"w")
self.writeHeader(fileToWrite)
if condition is None:
result = sourceCollection.find()
else:
result = sourceCollection.find(self.tokenizeCondition(condition))
alreadyRead = 0
for document in result:
if countToRead is not None and alreadyRead >= int(countToRead):
break
if purgeDeleted and self.pRecordDeleted.match(document['status']):
continue
docFieldName = docRecordField is not None and docRecordField or "record"
r = document[docFieldName]
#wholeRecordUnzipped = zlib.decompress(r)
#we don't want to have any linebreaks because it's easier to work with
#rNoBreaks = "".join(wholeRecordUnzipped.splitlines())
#fileToWrite.write("".join(zlib.decompress(r).splitlines()) + "\n")
fileToWrite.write(self.setCustomDatestamp(zlib.decompress(r) + "\n"))
alreadyRead +=1
fileToWrite.flush()
statinfo = os.stat(outfile)
size = statinfo.st_size
forCompare = int(fileSize) * 1000000
if (size > forCompare):
self.writeFooter(fileToWrite)
fileToWrite.flush()
fileToWrite.close()
os.system("gzip " + self.currentOutFile)
outfile = self.defineOutPutFile(outDir)
fileToWrite = open(outfile,"w")
self.writeHeader(fileToWrite)
if (fileToWrite is not None):
self.writeFooter(fileToWrite)
fileToWrite.flush()
fileToWrite.close()
os.system("gzip " + self.currentOutFile)
def tokenizeCondition(self,condition):
pTokenCondition = re.compile(':',re.UNICODE | re.DOTALL | re.IGNORECASE)
searchColon = pTokenCondition.search(condition)
if searchColon:
splitted = condition.split(":")
dictCondition = {splitted[0] : splitted[1]}
else:
#we assume a date search with the operators $gt|$lt
splitted = condition.split("#")
if (len(splitted) == 2):
dictCondition = {'datum': {splitted[0] : splitted[1]}}
elif (len(splitted) == 3):
dictCondition = {
'$and': [
{splitted[0]: {splitted[1]: int(splitted[2])}}, # for example 'year#$lte#2015'
{'includedInNationalLicences': { '$not': { '$eq' : 'no' }} } #does not include articles which are not part of National Licences
]
}
else:
dictCondition = {}
return dictCondition
def writeBackupRecords(self,fileSize=None,outDir=None):
sourceCollection = self.getDBConnections()["nativeSources"]["collections"]["sourceDB"]
self.checkAndCreateOutDir(outDir)
outfile = self.defineOutPutFile(outDir)
outfile = self.defineOutPutFile(outDir)
fileToWrite = open(outfile,"w")
self.writeHeader(fileToWrite)
result = sourceCollection.find()
for document in result:
#decomp = zlib.decompress(document["record"])
_recordToWrite = [self.backupItemOpen,
self.idTagItemOpen,document["_id"].encode('utf-8'),self.idTagItemClose ,
self.statusTagItemOpen,document["status"].encode('utf-8'),self.statusTagItemClose,
self.metadataTagItemOpen,zlib.decompress(document["record"]),self.metadataTagItemClose,
self.dateTagItemOpen,document["datum"].encode('utf-8'), self.dateTagItemClose,
self.backupItemClose]
fileToWrite.write("".join(_recordToWrite) + os.linesep)
fileToWrite.flush()
statinfo = os.stat(outfile)
size = statinfo.st_size
forCompare = int(fileSize) * 1000000
if (size > forCompare):
self.writeFooter(fileToWrite)
fileToWrite.flush()
fileToWrite.close()
outfile = self.defineOutPutFile(outDir)
fileToWrite = None
fileToWrite = open(outfile,"w")
self.writeHeader(fileToWrite)
if (fileToWrite is not None):
self.writeFooter(fileToWrite)
fileToWrite.flush()
fileToWrite.close()
def restoreBackupRecords(self,outDir=None):
self.checkOutDir(outDir)
sourceCollection = self.getDBConnections()["nativeSources"]["collections"]["sourceDB"]
for fname in os.listdir(outDir):
itemLines = []
start = False
for line in open(outDir + os.sep + fname,"r"):
if line.find(self.backupItemOpen) != -1:
itemLines.append(line)
start = True
elif start == True and line.find(self.backupItemClose) == -1:
itemLines.append(line)
elif start == True and line.find(self.backupItemClose) != -1:
itemLines.append(line)
_values = self.valuesRestoredRecord("".join(itemLines))
mongoRecord = sourceCollection.find_one({"_id": _values["id"]})
binary = Binary( zlib.compress(_values["record"],9))
if not mongoRecord:
newRecord = {"_id":_values["id"],
"datum":_values["date"],
"record":binary,
"status":_values["status"]
}
sourceCollection.insert(newRecord)
else:
mongoRecord["record"] = binary
mongoRecord["status"] = _values["status"]
mongoRecord["datum"] = _values["date"]
sourceCollection.replace_one({"_id": _values["id"]}, mongoRecord)
#sourceCollection.save(mongoRecord,safe=True)
itemLines = []
start = False
def defineOutPutFile(self,outdir):
prefix = self.appContext.getConfiguration().getNetworkPrefix()
time.sleep(5)
outfile = "".join([outdir,os.sep,str(prefix),
"-","export.",
'{:%Y%m%d%H%M%S}'.format(datetime.now()),
".xml"])
self.currentOutFile = outfile
return outfile
def checkAndCreateOutDir(self,outdir):
if not outdir[-1] == os.sep:
outdir = "".join([outdir,os.sep])
outdir = "".join([outdir, '{:%Y%m%d%H%M%S}'.format(datetime.now())])
if not os.path.exists(outdir):
os.makedirs(outdir)
return outdir
def checkOutDir(self,outdir):
if not os.path.exists(outdir):
raise Exception("outDir doesn't exist")
def valuesRestoredRecord(self,structureRestoredRecord):
_values = {}
_id = self.ptagIdItem.search(structureRestoredRecord)
if _id:
_values ['id']= _id.group(1)
else:
raise Exception("restored record doesn't contain an recordID" + os.linesep + structureRestoredRecord)
_status = self.ptagStatusItem.search(structureRestoredRecord)
if _status:
_values ['status']= _status.group(1)
else:
raise Exception("restored record doesn't contain status" + os.linesep + structureRestoredRecord)
_date = self.ptagDateItem.search(structureRestoredRecord)
if _date:
_values ['date']= _date.group(1)
else:
raise Exception("restored record doesn't contain date field" + os.linesep + structureRestoredRecord)
_metaData = self.ptagMetadataItem.search(structureRestoredRecord)
if _metaData:
_values ['record']= _metaData.group(1)
else:
raise Exception("restored record doesn't contain metadata" + os.linesep + structureRestoredRecord)
return _values
def writeHeader (self,fileToWrite):
fileToWrite.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
fileToWrite.write("<" + self.appContext.getConfiguration().getRoottag() + ">\n")
fileToWrite.flush()
def writeFooter (self,fileToWrite):
fileToWrite.write("</" + self.appContext.getConfiguration().getRoottag() + ">\n")
fileToWrite.flush()
def prepareMappings(self):
definitionsPrefixMappings = self.appContext.getConfiguration().getSourcePrefixMapping()
definitionsSourceOAIExtensions = self.appContext.getConfiguration().getSourceOAIExtension()
if definitionsPrefixMappings is None or definitionsSourceOAIExtensions is None:
raise Exception("please provide sourceOAIExtension and sourcePrefixMapping in configuration file for export of multiple colletions")
prefixMappings = re.split('##',definitionsPrefixMappings)
self.sourceMappings = {}
for mapping in prefixMappings:
tm = re.split('=',mapping)
self.sourceMappings[tm[0]] = tm[1]
oaiExtensions = re.split('##',definitionsSourceOAIExtensions)
self.sourceOAIExtensions = {}
for mapping in oaiExtensions:
tOAI = re.split('=',mapping)
self.sourceOAIExtensions[tOAI[0]] = tOAI[1]
def setCustomDatestamp(self,record):
if (not self.userDateStamp is None):
sRecord = self.pRecordUserDatestamp.search(record)
if (sRecord):
return "".join([sRecord.group(1),self.userDateStamp, sRecord.group(2)])
else:
return record
class MongoDBHarvestingWrapperFixRecords(MongoDBHarvestingWrapperAdmin):
def __init__(self,applicationContext=None):
self.pReroDeleteRecord = re.compile("(<record>.*?</header>).*",re.UNICODE | re.DOTALL | re.IGNORECASE)
MongoDBHarvestingWrapper.__init__(self,applicationContext=applicationContext)
def fixReroDeleteRecord(self):
sourceCollection = self.getDBConnections()["nativeSources"]["collections"]["sourceDB"]
#numberDeleted = sourceCollection.find({"status":"deleted"}).count()
#numberNewDeleted = sourceCollection.find({"status":"newdeleted"}).count()
result = sourceCollection.find({"status":"newdeleted"})
for document in result:
recordToDeleteCompressed = document["record"]
docid = document["_id"]
recordToDelete = zlib.decompress(recordToDeleteCompressed)
spReroDeletedRecordsToSubstitutePattern = self.pReroDeleteRecord.search(recordToDelete)
if spReroDeletedRecordsToSubstitutePattern:
modifiedRecord= spReroDeletedRecordsToSubstitutePattern.group(1) + "</record>"
binarymodifiedRecord = Binary( zlib.compress(modifiedRecord,9))
document["record"] = binarymodifiedRecord
#attention: save is deprecated - question: is it possible to pass a document object to replace_one method??
sourceCollection.save(document)
#sourceCollection.replace_one({"_id": docid}, document)
else:
print "record " + recordToDelete + " konnte per regex nicht verarbeitet werden"
result = sourceCollection.find({"status":"deleted"})
for document in result:
recordToDeleteCompressed = document["record"]
docid = document["_id"]
recordToDelete = zlib.decompress(recordToDeleteCompressed)
spReroDeletedRecordsToSubstitutePattern = self.pReroDeleteRecord.search(recordToDelete)
if spReroDeletedRecordsToSubstitutePattern:
modifiedRecord= spReroDeletedRecordsToSubstitutePattern.group(1) + "</record>"
binarymodifiedRecord = Binary( zlib.compress(modifiedRecord,9))
document["record"] = binarymodifiedRecord
#attention: save is deprecated - see comment above
sourceCollection.save(document)
# sourceCollection.replace_one({"_id": docid}, document)
else:
print "record " + recordToDelete + " konnte per regex nicht verarbeitet werden"
class MongoDBHarvestingWrapperSearch502(MongoDBHarvestingWrapperAdmin):
def __init__(self,applicationContext=None):
self.pSearch502 = re.compile("<(marc:)?datafield *?tag=\"502\".*",re.UNICODE | re.DOTALL | re.IGNORECASE)
MongoDBHarvestingWrapper.__init__(self,applicationContext=applicationContext)
def read502Records(self,outDir=None,fileSize=None):
sourceCollection = self.getDBConnections()["nativeSources"]["collections"]["sourceDB"]
outDir = self.checkAndCreateOutDir(outDir)
outfile = self.defineOutPutFile(outDir)
fileToWrite = open(outfile,"w")
self.writeHeader(fileToWrite)
#numberDeleted = sourceCollection.find({"status":"deleted"}).count()
#numberNewDeleted = sourceCollection.find({"status":"newdeleted"}).count()
result = sourceCollection.find()
for document in result:
recordCompressed = document["record"]
record = zlib.decompress(recordCompressed)
#f = open("/home/swissbib/temp/trash/idsbb502.xml","r")
#text = f.readlines()
#f.close()
#text = "".join(text)
spSearch502 = self.pSearch502.search(record)
if spSearch502:
fileToWrite.write(record + "\n")
fileToWrite.flush()
statinfo = os.stat(outfile)
size = statinfo.st_size
forCompare = int(fileSize) * 1000000
if (size > forCompare):
self.writeFooter(fileToWrite)
fileToWrite.flush()
fileToWrite.close()
outfile = self.defineOutPutFile(outDir)
fileToWrite = None
fileToWrite = open(outfile,"w")
self.writeHeader(fileToWrite)
if (fileToWrite is not None):
self.writeFooter(fileToWrite)
fileToWrite.close()
class MongoDBHarvestingWrapperSearchDefinedGeneric(MongoDBHarvestingWrapperAdmin):
def __init__(self,applicationContext=None):
MongoDBHarvestingWrapper.__init__(self,applicationContext=applicationContext)
def setRegEx(self,regex = None):
if not regex is None:
self.pDefinedRegex = re.compile(regex,re.UNICODE | re.DOTALL | re.IGNORECASE | re.MULTILINE)
def setdocRecordField(self,docField = None):
self.docRecordField = docField
def readMatchingRecords(self,outDir=None,fileSize=None,userDatestamp=None):
self.userDateStamp = userDatestamp
if self.pDefinedRegex is None:
raise Exception("no Regex defined")
sourceCollection = self.getDBConnections()["nativeSources"]["collections"]["sourceDB"]
outDir = self.checkAndCreateOutDir(outDir)
outfile = self.defineOutPutFile(outDir)
fileToWrite = open(outfile,"w")
self.writeHeader(fileToWrite)
#numberDeleted = sourceCollection.find({"status":"deleted"}).count()
#numberNewDeleted = sourceCollection.find({"status":"newdeleted"}).count()
result = sourceCollection.find()
for document in result:
if not self.docRecordField is None:
recordCompressed = document[self.docRecordField]
else:
recordCompressed = document["record"]
record = zlib.decompress(recordCompressed)
record1 = ''.join(record.splitlines())
#f = open("/home/swissbib/temp/trash/idsbb502.xml","r")
#text = f.readlines()
#f.close()
#text = "".join(text)
spDefinedRegex = self.pDefinedRegex.search(record1)
if spDefinedRegex:
fileToWrite.write(self.setCustomDatestamp(record) + "\n")
fileToWrite.flush()
statinfo = os.stat(outfile)
size = statinfo.st_size
forCompare = int(fileSize) * 1000000
if (size > forCompare):
self.writeFooter(fileToWrite)
fileToWrite.flush()
fileToWrite.close()
outfile = self.defineOutPutFile(outDir)
fileToWrite = None
fileToWrite = open(outfile,"w")
self.writeHeader(fileToWrite)
if (fileToWrite is not None):
self.writeFooter(fileToWrite)
fileToWrite.close()
os.system("gzip " + outDir + os.sep + "*")