-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContext.py
548 lines (339 loc) · 17.9 KB
/
Context.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
import os
from datetime import datetime, timedelta
import types
import re
from swissbibUtilities import SwissbibUtilities
__author__ = 'swissbib'
class ApplicationContext:
def __init__(self):
pass
def setMongoWrapper(self,wrapper):
self.mongoWrapper = wrapper
def getMongoWrapper(self):
if hasattr(self,'mongoWrapper'):
return self.mongoWrapper
else:
return None
def setConfiguration(self,configuration):
self.configuration = configuration
def getConfiguration(self):
if hasattr(self,'configuration'):
return self.configuration
return None
def setResultCollector(self,collector):
self.rCollector = collector
def getResultCollector(self):
if hasattr(self,'rCollector'):
return self.rCollector
else:
return None
def setWriteContext(self,writeContext):
self.wContext = writeContext
def getWriteContext(self):
if hasattr(self,'wContext'):
return self.wContext
else:
return None
class NLApplicationContext(ApplicationContext):
def __init__(self):
ApplicationContext.__init__(self)
def setModsTransformation(self,modsTransformation):
self.mTransformation = modsTransformation
def getModsTransformation(self):
if hasattr(self,'mTransformation'):
return self.mTransformation
else:
return None
class WriteContext:
def __init__(self,context):
self.appContext = context
self.granularityPattern = re.compile('Thh:mm:ssZ',re.UNICODE | re.DOTALL | re.IGNORECASE)
self.nextTimeStamp = self.getNextTimestamp()
self.footerWritten = False
self.fileClosed = True
def setOutFileName(self,fileName):
self.outFileName = fileName
self.wholeContentFile = open(self.outFileName,"w")
self.fileClosed = False
self.writeHeader()
def getOutFileName(self):
return self.outFileName
def writeItem(self,item):
tList = ['\n',item,"\n"]
if self.appContext.getConfiguration().getWriteHarvestedFiles():
self.wholeContentFile.writelines(tList)
def closeWriteContext(self):
if not self.wholeContentFile is None:
self.writeFooter()
self.flushContent()
self.wholeContentFile.close()
self.fileClosed = True
self.wholeContentFile = None
#todo: verschiebe file nach results
def writeHeader(self):
self.wholeContentFile.write("<collection>\n")
def writeFooter(self):
self.wholeContentFile.write("</collection>\n")
def flushContent(self):
self.wholeContentFile.flush()
def moveContentFile(self):
if not self.fileClosed is True:
self.closeWriteContext()
numberProcessedRecords = self.appContext.getResultCollector().getIncrementProcessedRecordNoFurtherDetails()
maxDocuments = self.appContext.getConfiguration().getMaxDocuments()
blocked = self.appContext.getConfiguration().getBlocked()
#in case the configuration is set to blocked a
if (not maxDocuments is None and not numberProcessedRecords is None
and int(numberProcessedRecords) > int(maxDocuments)) \
or blocked:
os.chdir(self.appContext.getConfiguration().getDumpDir())
#collected content shouldn't be sent to CBS because number of collected records is too large
os.system("mv " + self.appContext.getConfiguration().getSummaryContentFile() + " " + self.appContext.getConfiguration().getArchiveNotSent())
tCommand = "gzip -9 " + self.appContext.getConfiguration().getArchiveNotSent() + os.sep + self.appContext.getConfiguration().getSummaryContentFile()
os.system(tCommand)
if not blocked:
self.appContext.getConfiguration().setBlocked('true')
os.chdir(self.appContext.getConfiguration().getResultDir())
SwissbibUtilities.sendNotificationMail(receivers=self.appContext.getConfiguration().getEMailNotifification(),
network=self.appContext.getConfiguration().getNetworkPrefix(),
numberDocuments=numberProcessedRecords,
mailserver=self.appContext.getConfiguration().getMailServer())
else:
os.chdir(self.appContext.getConfiguration().getDumpDir())
os.system("mv " + self.appContext.getConfiguration().getSummaryContentFile() + " " + self.appContext.getConfiguration().getArchiveDir())
tCommand = "gzip -9 " + self.appContext.getConfiguration().getArchiveDir() + os.sep + self.appContext.getConfiguration().getSummaryContentFile()
os.system(tCommand)
os.chdir(self.appContext.getConfiguration().getResultDir())
tCommand = "ln -s " + self.appContext.getConfiguration().getArchiveDir() + os.sep +\
self.appContext.getConfiguration().getSummaryContentFile() + ".gz " +\
self.appContext.getConfiguration().getSummaryContentFile() + ".gz"
os.system(tCommand)
if os.path.isdir(self.appContext.getConfiguration().getDumpDir()):
os.system("rm -r " + self.appContext.getConfiguration().getDumpDir())
def setAndWriteConfigAfterError(self):
if not self.appContext is None and not self.appContext.getConfiguration() is None:
cwd = os.getcwd()
#we change at first directory because configfile-path might be relative
os.chdir(self.appContext.getConfiguration().getApplicationDir())
self.appContext.getConfiguration().setActionFinished("no")
self.appContext.getConfiguration().setStoppageTime(str(datetime.now()))
newConf = open(self.appContext.getConfiguration().getConfigFilename(),"w")
newConf.write(self.appContext.getConfiguration().getXMLasString())
newConf.close()
os.chdir(cwd)
def setAndWriteConfigAfterSuccess(self, setTimeStamp = True):
if not self.appContext is None and not self.appContext.getConfiguration() is None:
cwd = os.getcwd()
#we change at first directory because configfile-path might be relative
os.chdir(self.appContext.getConfiguration().getApplicationDir())
#we had the problem with manually provided delete messages. In this case we must not
#set any new time
if (setTimeStamp):
self.appContext.getConfiguration().setTimestampUTC(self.nextTimeStamp)
self.appContext.getConfiguration().setActionFinished("yes")
self.appContext.getConfiguration().setCompleteListSize(str(self.appContext.getResultCollector().getNumberAllProcessedRecords()))
self.appContext.getConfiguration().setStoppageTime(str(datetime.now()))
conffilename = self.appContext.getConfiguration().getConfdir() + os.sep + os.path.basename(self.appContext.getConfiguration().getConfigFilename())
newConf = open(conffilename,"w")
newConf.write(self.appContext.getConfiguration().getXMLasString())
newConf.close()
os.chdir(cwd)
else:
print "writing of configuration file after successful processing wasn't possible - why is Appcontext or write Context None??"
def handleOperationAfterError(self,exType=None, message="", additionalText=""):
self.setAndWriteConfigAfterError()
if not self.appContext.getResultCollector() is None and (
self.appContext.getResultCollector().getRecordsDeleted() > 0 or
self.appContext.getResultCollector().getRecordsToCBSInserted() > 0 or
self.appContext.getResultCollector().getRecordsToCBSNoSkip() > 0 or
self.appContext.getResultCollector().getRecordsToCBSUpdated() > 0 or
self.appContext.getResultCollector().getRecordsSkipped() > 0 ) :
self.moveContentFile()
label = "an error occured - already collected content was moved"
else:
label = "an error occured - no already collected content to move available"
if not exType is None:
self.writeErrorLog(header="an error occured - already collected content was moved", message=[message,additionalText,exType])
else:
self.writeErrorLog(header="an error occured - already collected content was moved", message=[message,additionalText])
def writeErrorLog(self, header=None, message= None):
if header is None:
header = [str(datetime.now()) + " - error message"]
elif isinstance(header,types.ListType):
header.insert(0,str(datetime.now()) )
else:
header = [str(datetime.now()) + " " + str(header)]
if message is None:
message = ["no error message provided"]
elif isinstance(message,types.StringType):
message = [message]
elif isinstance(message,types.ListType):
pass
else:
message = ["no proper error-message-type was provided"]
if not self.appContext is None and not self.appContext.getConfiguration() is None:
#logTyp 2 = error message
self._writeLog(header,message,logType=2)
else:
self._writeLog(header,message,logType=2, writeToStdOut=True)
def writeLog(self, header=None, message= None):
if header is None:
header = [str(datetime.now()) + " - log message"]
elif isinstance(header,types.ListType):
header.insert(0,str(datetime.now()) )
else:
header = [str(datetime.now()) + " " + str(header)]
if message is None:
message = ["no log message provided"]
elif isinstance(message,types.StringType):
message = [message]
elif isinstance(message,types.ListType):
pass
else:
message = ["no proper log-message-type was provided"]
if not self.appContext is None and not self.appContext.getConfiguration() is None:
#logTyp 1 = log message standard
self._writeLog(header,message)
else:
self._writeLog(header,message,writeToStdOut=True)
def _writeLog(self, header, message, logType=1,writeToStdOut=False):
try:
if writeToStdOut is False:
if logType == 1: #log-message
fileName = self.appContext.getConfiguration().getProcessLogDir() + os.sep + self.appContext.getConfiguration().getProcessLogFile()
else: #error-message
fileName = self.appContext.getConfiguration().getErrorLogDir() + os.sep + self.appContext.getConfiguration().getErrorLogFile()
procLog = open(fileName,"a")
procLog.write('-'*60 + "\n")
procLog.write("\n".join(header) + "\n")
for value in message:
procLog.write(str(value) + "\n")
procLog.write('-'*60 + "\n")
procLog.close()
else:
print '-'*60 + "\n"
print "\n".join(header) + "\n"
for value in message:
print str(value) + "\n"
print '-'*60 + "\n"
except Exception as exInWrite:
procLog = open(self.appContext.getConfiguration().getErrorLogDir() + os.sep + self.appContext.getConfiguration().getErrorLogFile(),"a")
procLog.write('-'*60 + "\n")
procLog.write("error while writing error log")
procLog.write(str(exInWrite))
procLog.write('-'*60 + "\n")
procLog.close()
def getNextTimestamp(self):
granularity = self.appContext.getConfiguration().getGranularity()
if not granularity is None and self.granularityPattern.search(granularity):
cTimeUTC = datetime.utcnow()
nTList = [str(cTimeUTC.date()),"T",str(cTimeUTC.hour),":",str(cTimeUTC.minute),":",str(cTimeUTC.second),"Z"]
return "".join(nTList)
else:
return datetime.utcnow().strftime("%Y-%m-%d")
class HarvestingWriteContext(WriteContext):
def __init__(self,context):
WriteContext.__init__(self,context)
self.setOutFileName("".join([self.appContext.getConfiguration().getDumpDir(),os.sep, self.appContext.getConfiguration().getSummaryContentFile()]))
def getOutFileName(self):
return self.outFileName
class FilePushWriteContext(WriteContext):
def __init__(self,context):
WriteContext.__init__(self,context)
def closeWriteContext(self):
#kann ich den pfad eines files abfragen
self.writeFooter()
self.wholeContentFile.close()
os.system("mv " + self.getOutFileName() + " " + self.appContext.getConfiguration().getArchiveDir())
tCommand = "gzip -9 --force " + self.appContext.getConfiguration().getArchiveDir() + os.sep + os.path.basename(self.getOutFileName())
os.system(tCommand)
resultDir = self.appContext.getConfiguration().getResultDir()
os.chdir(resultDir)
tCommand = "ln -s " + self.appContext.getConfiguration().getArchiveDir() + os.sep + \
os.path.basename(self.getOutFileName()) + ".gz " + \
self.appContext.getConfiguration().getResultDir() + \
os.sep + os.path.basename(self.getOutFileName()) + ".gz"
os.system(tCommand)
self.appContext.getResultCollector().setCollectedArchiveFiles(os.path.basename(self.getOutFileName()) + '.gz',self.appContext.getConfiguration().getArchiveDir() )
#os.chdir(self.appContext.getConfiguration().getClusteringDir())
#os.system("rm -r " + self.appContext.getConfiguration().getCollectedDir())
def handleOperationAfterError(self,exType=None, message="", additionalText=""):
self.setAndWriteConfigAfterError()
label = "an error occured in Nebis File Processig: "
if not exType is None:
self.writeErrorLog(header=label, message=[message,additionalText,exType])
else:
self.writeErrorLog(header=label, message=[message,additionalText])
class FileWebdavWriteContext(WriteContext):
def __init__(self,context):
WriteContext.__init__(self,context)
def setOutFileName(self,fileName):
self.outFileName = fileName
self.outFileNameWithPath = self.appContext.getConfiguration().getDumpDir() + os.sep + self.outFileName
self.wholeContentFile = open(self.outFileNameWithPath,"w")
self.writeHeader()
def closeWriteContext(self):
#kann ich den pfad eines files abfragen
self.writeFooter()
self.wholeContentFile.close()
os.system("mv " + self.outFileNameWithPath + " " + self.appContext.getConfiguration().getArchiveDir())
fileInArchive = self.appContext.getConfiguration().getArchiveDir() + os.sep + self.outFileName
tCommand = "gzip -9 --force " + fileInArchive
os.system(tCommand)
fileInArchiveZipped = fileInArchive + ".gz"
os.chdir(self.appContext.getConfiguration().getResultDir())
tCommand = "ln -s " + fileInArchiveZipped + " " +self.outFileName + ".gz"
os.system(tCommand)
self.appContext.getResultCollector().setCollectedArchiveFiles(self.outFileName + ".gz",self.appContext.getConfiguration().getArchiveDir() )
class TaskContext:
def __init__(self,appContext=None,defaultRecord=None):
self.appContext = appContext
self.defaultRecord = defaultRecord
def getDBWrapper(self):
return self.appContext.getMongoWrapper()
def getConfiguration(self):
return self.appContext.getConfiguration()
def getResultCollector (self):
return self.appContext.getResultCollector()
def getWriteContext(self):
return self.appContext.getWriteContext()
class StoreNativeRecordContext(TaskContext):
def __init__(self,appContext=None,rID=None,singleRecord="",deleted=False):
TaskContext.__init__(self,appContext=appContext)
self.dateTimeStamp = re.compile('<header.*?>.*?<datestamp>(.*?)</datestamp>.*?</header>',re.UNICODE | re.DOTALL | re.IGNORECASE)
self.rID = rID
self.singleRecord = singleRecord
self.deleted = deleted
def getID(self):
return self.rID
#todo: necessary only for Initial GND??
def setID(self,rID):
self.rID = rID
def getRecordTimestamp(self):
recordTimestamp = None
if not self.singleRecord is None and self.appContext.getConfiguration().getAddRecordTimeStamp():
existTimestamp = self.dateTimeStamp.search(self.singleRecord)
if existTimestamp:
recordTimestamp = existTimestamp.group(1)
return recordTimestamp
def getRecord(self):
return self.singleRecord
#we need this method because in relation to GND (initial loading)
#sentences are going to be prepared before storing into Mongo
#this is done 'after' creation of the object
def setRecord(self, record):
self.singleRecord = record
def isDeleted(self):
return self.deleted
class StoreNativeNLRecordContext(StoreNativeRecordContext):
def __init__(self, appContext=None, rID=None, jatsRecord="", deleted=False,
modsRecord=""):
StoreNativeRecordContext.__init__(self,appContext,rID,jatsRecord,deleted)
self.modsRecord = modsRecord
def getModsRecord(self):
return self.modsRecord
# we need this method because in relation to GND (initial loading)
# sentences are going to be prepared before storing into Mongo
# this is done 'after' creation of the object
def setModsRecord(self, record):
self.modsRecord = record