-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpull.py
704 lines (655 loc) · 34.4 KB
/
pull.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
"""Usage:
pull.py set
pull.py auto <to> [--date=<MMDDYY>]
pull.py move <from> <to> [--date=<MMDDYY>]
pull.py (-h | --help)
pull.py (-v | --version)
Options:
set Executes routine with set directories and today's date
auto Executes routine with specified directory and date
move Copies and combines SGM files from source to dest.
--date=<MMDDYY> Optional date of the files to pull
-h --help Show this screen.
-v --version Show version.
"""
__author__ = 'sjohnson'
import sys
import os
import shutil
import glob
import re
import collections
from docopt import docopt
from datetime import date, datetime
from collections import defaultdict
try:
from schema import Schema, And, Or, Use, SchemaError
except ImportError:
exit('This example requires that `schema` data-validation library'
' is installed: \n pip install schema\n'
'https://github.com/halst/schema')
# This class provides the switch functionality we want. You only need to look at
# this if you want to know how this works. It only needs to be defined
# once, no need to muck around with its internals.
class switch(object):
def __init__(self, value):
self.value = value
self.fall = False
def __iter__(self):
"""Return the match method once, then stop"""
yield self.match
raise StopIteration
def match(self, *args):
"""Indicate whether or not to enter a case suite"""
if self.fall or not args:
return True
elif self.value in args: # changed for v1.5, see below
self.fall = True
return True
else:
return False
def alpha_array():
"""Array of compiled replacement patterns for use with "alpha" function
@rtype : list
"""
list_regex = [
[re.compile("\"", re.DOTALL), "'"],
[re.compile(re.escape(" "), re.MULTILINE), " "],
[re.compile(re.escape(" "), re.MULTILINE), " "],
[re.compile(re.escape("</ACT>"), re.MULTILINE), ""],
[re.compile(re.escape("</AGENCY>"), re.MULTILINE), ""],
[re.compile(re.escape("</AGY>"), re.MULTILINE), ""],
[re.compile(re.escape("</AMDPAR>"), re.MULTILINE), ""],
[re.compile(re.escape("</APPENDIX>"), re.MULTILINE), ""],
[re.compile(re.escape("</AUTH>"), re.MULTILINE), ""],
[re.compile(re.escape("</BILCOD>"), re.MULTILINE), ""],
[re.compile(re.escape("</BOXHD>"), re.MULTILINE), ""],
[re.compile(re.escape("</CFR>"), re.MULTILINE), ""],
[re.compile(re.escape("</CHED>"), re.MULTILINE), ""],
[re.compile(re.escape("</DATE>"), re.MULTILINE), ""],
[re.compile(re.escape("</DEPDOC>"), re.MULTILINE), ""],
[re.compile(re.escape("</ENT>"), re.MULTILINE), ""],
[re.compile(re.escape("</FEDREG>"), re.MULTILINE), ""],
[re.compile(re.escape("</FP>"), re.MULTILINE), ""],
[re.compile(re.escape("</FP-1>"), re.MULTILINE), ""],
[re.compile(re.escape("</FP-2>"), re.MULTILINE), ""],
[re.compile(re.escape("</GID>"), re.MULTILINE), ""],
[re.compile(re.escape("</HD1>"), re.MULTILINE), ""],
[re.compile(re.escape("</HD2>"), re.MULTILINE), ""],
[re.compile(re.escape("</HD3>"), re.MULTILINE), ""],
[re.compile(re.escape("</HED>"), re.MULTILINE), ""],
[re.compile(re.escape("</LI>"), re.MULTILINE), ""],
[re.compile(re.escape("</MID>"), re.MULTILINE), ""],
[re.compile(re.escape("</NAME>"), re.MULTILINE), ""],
[re.compile(re.escape("</NEWPART>"), re.MULTILINE), ""],
[re.compile(re.escape("</NO>"), re.MULTILINE), ""],
[re.compile(re.escape("</P>"), re.MULTILINE), ""],
[re.compile(re.escape("</PART>"), re.MULTILINE), ""],
[re.compile(re.escape("</PARTNO>"), re.MULTILINE), ""],
[re.compile(re.escape("</RIN>"), re.MULTILINE), ""],
[re.compile(re.escape("</ROW>"), re.MULTILINE), ""],
[re.compile(re.escape("</RULE>"), re.MULTILINE), ""],
[re.compile(re.escape("</RULES>"), re.MULTILINE), ""],
[re.compile(re.escape("</SECAUTH>"), re.MULTILINE), ""],
[re.compile(re.escape("</SECHD>"), re.MULTILINE), ""],
[re.compile(re.escape("</SECTION>"), re.MULTILINE), ""],
[re.compile(re.escape("</SECTNO>"), re.MULTILINE), ""],
[re.compile(re.escape("</SIG>"), re.MULTILINE), ""],
[re.compile(re.escape("<STARS/ >"), re.MULTILINE), "<STARS>"],
[re.compile(re.escape("<STARS />"), re.MULTILINE), "<STARS>"],
[re.compile(re.escape("<STARS/>"), re.MULTILINE), "<STARS>"],
[re.compile(re.escape("</STARS>"), re.MULTILINE), ""],
[re.compile(re.escape("</SUBAGY>"), re.MULTILINE), ""],
[re.compile(re.escape("</SUBJECT>"), re.MULTILINE), ""],
[re.compile(re.escape("</SUBPART>"), re.MULTILINE), ""],
[re.compile(re.escape("</TDESC>"), re.MULTILINE), ""],
[re.compile(re.escape("</TITLE>"), re.MULTILINE), ""],
[re.compile(re.escape("</TTITLE>"), re.MULTILINE), ""],
[re.compile(re.escape("</UNITNAME>"), re.MULTILINE), ""],
[re.compile(re.escape("</VOL>"), re.MULTILINE), ""],
[re.compile("\"", re.DOTALL), "'"],
[re.compile("\n\n", re.DOTALL), "\n"],
[re.compile("\s*\n\s*", re.DOTALL), "\n"],
[re.compile("\n\s\n", re.DOTALL), "\n"],
[re.compile("\n\?>", re.DOTALL), "?>"],
[re.compile("\:\s\n<", re.DOTALL), ":\n<"],
[re.compile(re.escape("<RULE>"), re.MULTILINE), "\n\n<RULE>"],
[re.compile("<\?USGPO Galley End:\s*\?>", re.MULTILINE), ""],
[re.compile(re.escape("<PART>"), re.MULTILINE), "\n<PART>"],
[re.compile("\n\n<PART>", re.MULTILINE), "\n<PART>"],
[re.compile(re.escape("<SECTION>"), re.MULTILINE), "\n<SECTION>"],
[re.compile("\n\n<SECTION>", re.MULTILINE), "\n<SECTION>"],
[re.compile(re.escape(" <SUBJECT>"), re.MULTILINE), "<SUBJECT>"],
[re.compile(re.escape("<SUBJECT> "), re.MULTILINE), "<SUBJECT>"],
[re.compile("\n<SUBJECT>", re.MULTILINE), "<SUBJECT>"],
[re.compile("\n</", re.DOTALL), "</"],
[re.compile("\.\s\n<", re.MULTILINE), ".\n<"],
[re.compile(re.escape(" &thnsp;"), re.MULTILINE), "&thnsp;"],
[re.compile(re.escape("&thnsp; "), re.MULTILINE), "&thnsp;"],
[re.compile("\. \n\n", re.MULTILINE), ".\n"],
[re.compile(re.escape(". </EXTRACT>"), re.MULTILINE), ".</EXTRACT>"],
[re.compile(re.escape(". </NOTE>"), re.MULTILINE), ".</NOTE>"],
[re.compile(re.escape(" </REGTEXT>"), re.MULTILINE), "</REGTEXT>"],
[re.compile("\n</REGTEXT>", re.MULTILINE), "</REGTEXT>"],
[re.compile(re.escape("<Q P='04'></REGTEXT>"), re.MULTILINE), "</REGTEXT>"],
[re.compile(re.escape("<Q P='03'></REGTEXT>"), re.MULTILINE), "</REGTEXT>"],
[re.compile(re.escape("<Q P='02'></REGTEXT>"), re.MULTILINE), "</REGTEXT>"],
[re.compile(re.escape("<STARS> "), re.MULTILINE), "<STARS>"],
[re.compile("\n<STARS>", re.DOTALL), "<STARS>"],
[re.compile(re.escape("</REGTEXT><STARS>"), re.MULTILINE), "<STARS></REGTEXT>"],
[re.compile(re.escape("</GPOTABLE></REGTEXT>"), re.MULTILINE), "</GPOTABLE>\n</REGTEXT>"],
[re.compile("\n</CONTENTS>", re.MULTILINE), "</CONTENTS>"],
[re.compile(re.escape(". </CONTENTS>"), re.MULTILINE), ".</CONTENTS>"],
[re.compile(re.escape("<STARS>"), re.MULTILINE), "\n<STARS>"],
[re.compile("\n\n<STARS>", re.MULTILINE), "\n<STARS>"],
[re.compile("<STARS>\n</REGTEXT>", re.MULTILINE), "<STARS></REGTEXT>"],
[re.compile(re.escape("<REGTEXT"), re.MULTILINE), "\n<REGTEXT"],
[re.compile(re.escape("<LSTSUB>"), re.MULTILINE), "<LSTSUB>"],
[re.compile(re.escape("<EFFDATE>"), re.MULTILINE), "<EFFDATE>"],
[re.compile(":\n<P>", re.MULTILINE), ":<P>"],
[re.compile("^<PART><HED>.*\n\n{0,2}(?=<AMDPAR>)", re.MULTILINE), ""],
[re.compile("^<SUBPART><HED>.*\n\n{0,2}(?=<AMDPAR>)", re.MULTILINE), ""],
[re.compile("^<Q P=.*\n\n{0,2}(?=<AMDPAR>)", re.MULTILINE), ""],
[re.compile("<SUBCHAP><HED>.*\n\n{0,2}(?=<AMDPAR>)", re.MULTILINE), ""],
[re.compile("\s*<AMDPAR>", re.MULTILINE), "\n<P>"],
# [re.compile("\n\n<AMDPAR>", re.MULTILINE), "\n<P>"],
[re.compile("<EXTRACT>\n", re.DOTALL), "<EXTRACT>"],
[re.compile(re.escape("<SECTNO>&"), re.MULTILINE), "\n<SECTNO>&"],
[re.compile("\n\n<SECTNO>&", re.MULTILINE), "\n<SECTNO>&"],
[re.compile(" \n<HED>", re.MULTILINE), "\n<HED>"],
[re.compile("\n<HED>", re.MULTILINE), "<HED>"],
[re.compile(re.escape(" <P>"), re.MULTILINE), "<P>"],
[re.compile(re.escape("<P>"), re.MULTILINE), "\n<P>"],
[re.compile("\n\n<P>", re.MULTILINE), "\n<P>"],
[re.compile(re.escape("><E T='04'>Authority:</E>"), re.MULTILINE), ">Authority:"],
[re.compile(re.escape("″"), re.MULTILINE), "&sec;"],
[re.compile(re.escape("′"), re.MULTILINE), "&min;"],
[re.compile(re.escape("&fnl;"), re.MULTILINE), ""],
[re.compile(re.escape("<ROW"), re.MULTILINE), "\n<ROW"],
[re.compile("\n\n<ROW", re.MULTILINE), "\n<ROW"],
[re.compile(re.escape("<LI>"), re.MULTILINE), "\n<LI>"],
[re.compile("\n\n<LI>", re.MULTILINE), "\n<LI>"],
[re.compile("\"", re.MULTILINE), "'"],
[re.compile(re.escape(",tp0,i1"), re.MULTILINE), ""],
[re.compile(re.escape(",i1'"), re.MULTILINE), "'"],
[re.compile(re.escape("L1,i1"), re.MULTILINE), "L1"],
[re.compile(re.escape("L2,i1"), re.MULTILINE), "L2"],
[re.compile(re.escape("``"), re.MULTILINE), "“"],
[re.compile(re.escape("''"), re.MULTILINE), "&rdquo"],
[re.compile(re.escape("<E T='7462'>"), re.MULTILINE), "<E T='03'>"],
[re.compile(re.escape("€"), re.MULTILINE), "!l*f"],
[re.compile(re.escape("##"), re.MULTILINE), "\n\n"],
[re.compile(re.escape("<E T='22'>"), re.MULTILINE), "<E T='52'>"],
[re.compile(re.escape(", </E>"), re.MULTILINE), ",</E> "],
[re.compile(re.escape(" </E> "), re.MULTILINE), "</E> "],
[re.compile(re.escape(". </E>"), re.MULTILINE), ".</E> "],
[re.compile(re.escape(" </E>"), re.MULTILINE), "</E> "],
[re.compile(re.escape("  "), re.MULTILINE), " "],
[re.compile(re.escape("  "), re.MULTILINE), " "],
[re.compile(re.escape("  "), re.MULTILINE), " "],
[re.compile(re.escape("  "), re.MULTILINE), " "],
[re.compile("\n<HED", re.MULTILINE), "<HED"],
[re.compile(" \n<P>", re.MULTILINE), "\n<P>"],
[re.compile(re.escape(" <P>"), re.MULTILINE), "<P>"],
[re.compile(re.escape("<P> "), re.MULTILINE), "<P>"],
[re.compile("\n<P>\n", re.MULTILINE), "\n<P>"],
[re.compile(re.escape(":<P>("), re.MULTILINE), ":\n<P>("],
[re.compile(re.escape("+"), re.MULTILINE), "+"],
[re.compile(re.escape("&equal;"), re.MULTILINE), "="],
[re.compile(re.escape("="), re.MULTILINE), "="],
[re.compile(re.escape("/"), re.MULTILINE), "/"],
[re.compile(re.escape("+/−"), re.MULTILINE), "±"],
[re.compile(re.escape("+/-"), re.MULTILINE), "±"],
[re.compile(re.escape("@"), re.MULTILINE), "@"],
[re.compile(re.escape("'"), re.MULTILINE), "'"],
[re.compile(re.escape("*"), re.MULTILINE), "*"],
[re.compile(re.escape("%"), re.MULTILINE), "%"],
[re.compile(re.escape("′"), re.MULTILINE), "&min;"],
[re.compile(re.escape("&agr;"), re.MULTILINE), "α"],
[re.compile(re.escape("  "), re.MULTILINE), "&thnsp;"],
[re.compile(re.escape(" "), re.MULTILINE), ""],
[re.compile(re.escape("$"), re.MULTILINE), "$"],
[re.compile(re.escape("‐"), re.MULTILINE), "-"],
[re.compile(re.escape("μ"), re.MULTILINE), "µ"],
[re.compile("&fnl;\n<FNP>", re.MULTILINE), " "],
[re.compile("&fnl;\n<FP>", re.MULTILINE), " "],
[re.compile(re.escape("<FNP>"), re.MULTILINE), ""],
[re.compile(re.escape("'L2,tp0,i1'"), re.MULTILINE), "'L2'"],
[re.compile(re.escape(",i1'"), re.MULTILINE), "'"],
[re.compile("\n<ENT", re.MULTILINE), "<ENT"],
[re.compile("<ENT>\s\n", re.MULTILINE), "<ENT>\n"],
[re.compile("<ENT>\n", re.MULTILINE), ""],
[re.compile(re.escape("<FNC>"), re.MULTILINE), ""],
[re.compile(re.escape("&fnl;"), re.MULTILINE), ""],
[re.compile(re.escape("’"), re.MULTILINE), "'"],
[re.compile(re.escape("‘"), re.MULTILINE), "`"],
[re.compile(re.escape("<Q"), re.MULTILINE), "\n<Q"],
[re.compile("\n\n<Q", re.MULTILINE), "\n<Q"],
[re.compile(re.escape("_"), re.MULTILINE), "&lowbarm;"],
[re.compile(re.escape("<E T='72'>&lowbarm;"), re.MULTILINE), "&lowbarm;"],
[re.compile(re.escape("&lowbarm;</E>"), re.MULTILINE), "&lowbarm;"],
[re.compile(re.escape("&lowbarm;"), re.MULTILINE), "_"],
[re.compile(re.escape("&llddash;"), re.MULTILINE), "_"],
[re.compile(re.escape("[Reserved.],"), re.MULTILINE), "[Reserved],"],
[re.compile(re.escape("[Reserved],."), re.MULTILINE), "[Reserved],"],
[re.compile(re.escape("—[Reserved],"), re.MULTILINE), " [Reserved],"],
[re.compile(re.escape("—[RESERVED],"), re.MULTILINE), " [RESERVED],"],
[re.compile(re.escape(" [Reserved],"), re.MULTILINE), " [Reserved],"],
[re.compile(re.escape(" [RESERVED],"), re.MULTILINE), " [RESERVED],"],
[re.compile(re.escape("‐"), re.MULTILINE), "-"],
[re.compile(re.escape("<HD4>"), re.MULTILINE), "<HD1>"],
[re.compile(re.escape("<HD6>"), re.MULTILINE), "<HD2>"],
[re.compile(re.escape("—</E>"), re.MULTILINE), "</E>—"],
[re.compile("\n<FTREF>", re.MULTILINE), "<FTREF>"],
[re.compile("<FTNT>\n", re.MULTILINE), "<FTNT>"],
[re.compile("\n</FTNT>", re.MULTILINE), "</FTNT>"],
[re.compile("\n<SU>", re.MULTILINE), "<SU>"],
[re.compile("\n<FR>", re.MULTILINE), "<FR>"],
[re.compile(re.escape("<E T='52'>x</E>"), re.MULTILINE), "<E T='52'>X</E>"],
[re.compile(re.escape("<AMDPAR>"), re.MULTILINE), "<P>"],
[re.compile(re.escape("<TTITLE>"), re.MULTILINE), "\n<TTITLE>"],
[re.compile("\n\n<TTITLE>", re.MULTILINE), "\n<TTITLE>"],
[re.compile(re.escape("<TDESC>"), re.MULTILINE), "\n<TDESC>"],
[re.compile("\n\n<TDESC>", re.MULTILINE), "\n<TDESC>"],
[re.compile("<TTITLE> \n", re.MULTILINE), ""],
[re.compile("<TTITLE>  \n", re.MULTILINE), ""],
[re.compile("<TTITLE> \n", re.MULTILINE), ""],
[re.compile("<TTITLE>  \n", re.MULTILINE), ""],
[re.compile(re.escape("<BOXHD> "), re.MULTILINE), "<BOXHD>"],
[re.compile("<BOXHD>\n", re.MULTILINE), "<BOXHD>"],
[re.compile("\.\n\n<BOXHD>", re.MULTILINE), "\n<BOXHD>"],
[re.compile(re.escape("<ENT> "), re.MULTILINE), "<ENT>"],
[re.compile("\n<ENT", re.MULTILINE), "<ENT"],
[re.compile("\n<ENT", re.MULTILINE), "<ENT"],
[re.compile(re.escape(" <ENT"), re.MULTILINE), "<ENT"],
[re.compile(re.escape("<ROW"), re.MULTILINE), "\n<ROW"],
[re.compile("\n\n<ROW", re.MULTILINE), "\n<ROW"],
[re.compile("\n</GPO", re.MULTILINE), "</GPO"],
[re.compile(re.escape(" </GPO"), re.MULTILINE), "</GPO"],
[re.compile(re.escape("&qdrt;"), re.MULTILINE), "&qdrt;"],
[re.compile(re.escape(" & "), re.MULTILINE), " & "],
[re.compile(re.escape(" < "), re.MULTILINE), " < "],
[re.compile("<EXTRACT>\n", re.MULTILINE), "<EXTRACT>"],
[re.compile(re.escape(" &thnsp;"), re.MULTILINE), "&thnsp;"],
[re.compile(re.escape("&thnsp; "), re.MULTILINE), "&thnsp;"],
[re.compile(re.escape("  "), re.MULTILINE), "&thnsp;"],
[re.compile(re.escape("<E T='61'>±</E>"), re.MULTILINE), "±"],
[re.compile(re.escape("<E T='61'>×</E>"), re.MULTILINE), "×"],
[re.compile(re.escape("<E T='61'>#</E>"), re.MULTILINE), "#"],
[re.compile(re.escape("<E T='61'>&sec;</E>"), re.MULTILINE), "&sec;"],
[re.compile(re.escape("<E T='61'>·</E>"), re.MULTILINE), "·"],
[re.compile(re.escape("<E T='61'>ω</E>"), re.MULTILINE), "ω"],
[re.compile(re.escape("<E T='61'>#</E>"), re.MULTILINE), "#"],
[re.compile(re.escape("<E T='61'>μ</E>"), re.MULTILINE), "μ"],
[re.compile(re.escape("<E T='61'>°</E>"), re.MULTILINE), "°"],
[re.compile(re.escape("° F"), re.MULTILINE), "°F"],
[re.compile(re.escape(" °F"), re.MULTILINE), "°F"],
[re.compile(re.escape("°F"), re.MULTILINE), " °F"],
[re.compile(re.escape("° C"), re.MULTILINE), "°C"],
[re.compile(re.escape(" °C"), re.MULTILINE), "°C"],
[re.compile(re.escape("°C"), re.MULTILINE), " °C"],
[re.compile(re.escape("^"), re.MULTILINE), "−"],
[re.compile(re.escape("Register</E>. "), re.MULTILINE), "Register.</E> "],
[re.compile("Register</E>\. \n", re.MULTILINE), "Register.</E>\n"],
[re.compile(re.escape("Register</E>, "), re.MULTILINE), "Register,</E> "],
[re.compile(re.escape("r of the <E T='04'>Federal Register.</E>"), re.MULTILINE), "r of the Federal Register."],
[re.compile(re.escape("r of the <E T='04'>Federal Register,</E>"), re.MULTILINE), "r of the Federal Register,"],
[re.compile(re.escape("ce of the <E T='04'>Federal Register.</E>"), re.MULTILINE),
"ce of the Federal Register."],
[re.compile(re.escape("ce of the <E T='04'>Federal Register,</E>"), re.MULTILINE),
"ce of the Federal Register,"],
[re.compile(re.escape("<FP1-2>"), re.MULTILINE), "<P-2>"],
[re.compile(re.escape("<FP2-2>"), re.MULTILINE), "<FP2>"],
[re.compile(re.escape("<CITA TYPE='N'>"), re.MULTILINE), "<CITA>"],
[re.compile(re.escape(" </E> "), re.MULTILINE), "</E> "],
[re.compile(re.escape("!l*f"), re.MULTILINE), "&euro"],
[re.compile(re.escape("<Q P='02'/>"), re.MULTILINE), ""],
[re.compile(re.escape("<Q P='04'/>"), re.MULTILINE), ""],
[re.compile(re.escape("<Q"), re.MULTILINE), "\n<Q"],
[re.compile("\n\n<Q", re.MULTILINE), "\n<Q"],
[re.compile(re.escape(". </EXAMPLE>"), re.MULTILINE), ".</EXAMPLE>"],
[re.compile(re.escape(" POSITION='NOFLOAT'"), re.MULTILINE), ""],
[re.compile(re.escape(" BORDER='NODRAW'"), re.MULTILINE), ""],
[re.compile(re.escape(" STRIP='YES'"), re.MULTILINE), ""],
[re.compile(re.escape(" HTYPE='CENTER'"), re.MULTILINE), ""],
[re.compile(re.escape(" ROTATION='P'"), re.MULTILINE), ""],
[re.compile("<\?USGPO Galley Info Start\:.*?Galley Info End\?>", re.DOTALL), ""],
[re.compile("\r", re.MULTILINE), "\n"],
[re.compile(re.escape("'PART '"), re.DOTALL), "' PART='"],
[re.compile(re.escape("TITLE=' "), re.DOTALL), "TITLE='"],
[re.compile(re.escape(" ' PART="), re.DOTALL), "' PART="],
[re.compile(re.escape("PART=' "), re.DOTALL), "PART='"],
[re.compile(re.escape(" '>"), re.DOTALL), "'>"],
]
return list_regex
def omega_array():
"""
Array of compiled replacement patterns for use with "omega" function
@return: list of compiled substitution regular expressions
"""
list_regex = [
[re.compile(re.escape(" "), re.DOTALL), " "],
[re.compile("\n \n", re.DOTALL), "\n"],
[re.compile("\. \n</", re.DOTALL), ".</"],
[re.compile(" \n</", re.DOTALL), "</"],
[re.compile("\n</", re.DOTALL), "</"],
[re.compile("(?<=Authority:)\n<P>|(?<=Note:)\n<P>|(?<=Source:)\n<P>|(?<=Example:)\n<P>", re.MULTILINE), "<P>"],
[re.compile("\n\n", re.DOTALL), "\n"],
[re.compile("(?<=\S)(?<!<STARS>)</REGTEXT>", re.DOTALL), "\n</REGTEXT>"],
[re.compile("</REGTEXT>\s+<REGTEXT", re.DOTALL), "</REGTEXT>\n\n<REGTEXT"],
[re.compile("<REGTEXT", re.DOTALL), "\n<REGTEXT"],
[re.compile(re.escape("<E T='51'>1</E>"), re.DOTALL), "<SU>1</SU>"],
[re.compile(re.escape("<E T='51'>2</E>"), re.DOTALL), "<SU>2</SU>"],
[re.compile(re.escape("<E T='51'>3</E>"), re.DOTALL), "<SU>3</SU>"],
[re.compile(re.escape("<E T='51'>4</E>"), re.DOTALL), "<SU>4</SU>"],
[re.compile(re.escape("<E T='51'>5</E>"), re.DOTALL), "<SU>5</SU>"],
[re.compile(re.escape("<E T='51'>6</E>"), re.DOTALL), "<SU>6</SU>"],
[re.compile(re.escape("<E T='51'>7</E>"), re.DOTALL), "<SU>7</SU>"],
[re.compile(re.escape("<E T='51'>8</E>"), re.DOTALL), "<SU>8</SU>"],
[re.compile(re.escape("<E T='51'>9</E>"), re.DOTALL), "<SU>9</SU>"],
[re.compile(re.escape("<E T='51'>10</E>"), re.DOTALL), "<SU>10</SU>"],
[re.compile(re.escape("<E T='51'>11</E>"), re.DOTALL), "<SU>11</SU>"],
[re.compile(re.escape("<E T='51'>12</E>"), re.DOTALL), "<SU>12</SU>"],
[re.compile(re.escape("<E T='51'>13</E>"), re.DOTALL), "<SU>13</SU>"],
[re.compile(re.escape("<E T='51'>14</E>"), re.DOTALL), "<SU>14</SU>"],
[re.compile(re.escape("<E T='51'>15</E>"), re.DOTALL), "<SU>15</SU>"],
[re.compile(re.escape("<E T='51'>16</E>"), re.DOTALL), "<SU>16</SU>"],
[re.compile(re.escape("<E T='51'>17</E>"), re.DOTALL), "<SU>17</SU>"],
[re.compile(re.escape("<SUBJECT>"), re.MULTILINE), "\n<SUBJECT>"],
[re.compile(re.escape("</CFRDOC>"), re.DOTALL), "\n\n</CFRDOC>"],
[re.compile("(?<!\n\n)<SECTION>", re.DOTALL), "\n<SECTION>"],
[re.compile("<PRTPAG.*?>\n?", re.MULTILINE), ""],
[re.compile("\n{0,2}^(<SUBPART>.*\n|<PART>.*\n|<HD1>.*\n)?(<SECTION>.*\n)?.*\n.*?<SUBJECT>\[?Removed.*\]?\.?",
re.MULTILINE), ""],
[re.compile("\n{0,2}^(<SUBPART>.*\n|<PART>.*\n|<HD1>.*\n)?(<SECTION>.*\n)?.*\n.*?<SUBJECT>\[?Amended.*\]?\.?",
re.MULTILINE), ""],
[re.compile("\n{0,2}^(<SUBPART>.*\n|<PART>.*\n|<HD1>.*\n)?(<SECTION>.*\n)?.*\n.*?<SUBJECT>\[?Corrected.*\]?\.?",
re.MULTILINE), ""],
[re.compile(
"\n{0,2}^(<SUBPART>.*\n|<PART>.*\n|<HD1>.*\n)?(<SECTION>.*\n)?.*\n.*?<SUBJECT>\[?Redesignated.*\]?\.?",
re.MULTILINE), ""],
[re.compile("<HD1>.*?\[Removed.*\]\n|<HD1>.*?\[?Amended.*\]?\n|<HD1>.*?\[?Corrected.*\]?\n", re.MULTILINE), ""],
[re.compile("<(GPH.*?)>\s*?(<GID>.*?</GPH>)\s*?<!--(GPH.*?)-->", re.MULTILINE), "<\g<3>>\n\g<2>\n"],
[re.compile("<(MATH.*?)>\s*?(<MID>.*?</MATH>)\s*?<!--(MATH.*?)-->", re.MULTILINE), "<\g<3>>\n\g<2>\n"],
[re.compile("<BILCOD>.*\n", re.MULTILINE), ""],
]
return alpha_array() + list_regex
def get_from_dict(val, my_dict, hilo="prev"):
"""
Searches a dictionary with positions:value to return either previous or next value to the specified "val" position
@rtype : str
@param val: location to search either up or down
@param my_dict: dictionary of all occurance locations (key = location, value = searched string)
@param hilo: count up to the next or down to previous
@return: value of that location
"""
ret_value = None
od = collections.OrderedDict(sorted(my_dict.items(), key=lambda t: t[0]))
for case in switch(hilo):
if case('prev'):
for k, v in od.items():
if int(k) < val:
ret_value = v
break
if case('next'):
for k, v in od.items():
if int(k) > val:
ret_value = val
return ret_value
def replace(filename, regexes):
"""
Replaces occurences of patterns in a dict with corresponding string in a given file
@type filename: str
@type regexes: list
@param filename: filename where replacements should take place
@param regexes: list of compiled replacement patterns [search pattern, replacement string]
"""
with open(filename, 'rb') as content_file:
file_string = content_file.read().decode()
# Use RE package to allow for replacement (also allowing for (multiline) REGEX)
for pattern in regexes:
try:
file_string = pattern[0].sub(pattern[1], file_string)
except Exception as e:
exit("Bad regular expression: " + pattern)
# Write contents to file.
# Using mode 'w' truncates the file.
with open(filename, 'w') as file_handle:
file_handle.write(file_string)
return
def move_files(from_dir, to_dir, file_date):
"""Moves the files from one dir to another. Optional date specifies particular date, otherwise today's used
@type from_dir: str
@type to_dir: str
@type file_date: str
@param from_dir: Where to get files from
@param to_dir: Where to move files to
@param file_date: Date of the files to move
"""
if file_date is None:
file_date = date.today().strftime("%m%d%y")
sMM = file_date[0:2]
sDD = file_date[2:4]
sYY = file_date[4:6]
sMMM = ''
for case in switch(sMM):
if case('01'):
sMM = "JA"
sMMM = "JAN"
break
if case('02'):
sMM = "FE"
sMMM = "FEB"
break
if case('03'):
sMM = "MR"
sMMM = "MAR"
break
if case('04'):
sMM = "AP"
sMMM = "APR"
break
if case('05'):
sMM = "MY"
sMMM = "MAY"
break
if case('06'):
sMM = "JN"
sMMM = "JUN"
break
if case('07'):
sMM = "JY"
sMMM = "JUL"
break
if case('08'):
sMM = "AU"
sMMM = "AUG"
break
if case('09'):
sMM = "SE"
sMMM = "SEP"
break
if case('10'):
sMM = "OC"
sMMM = "OCT"
break
if case('11'):
sMM = "NO"
sMMM = "NOV"
break
if case('12'):
sMM = "DE"
sMMM = "DEC"
break
if case():
sys.exit("Missing month!!!")
file_set = glob.glob(os.path.join(from_dir, sDD + sMM + 'R*.SGM'))
if file_set:
dest_file = open(os.path.join(to_dir, sYY + sMMM + sDD), 'wb')
for filename in file_set:
if os.path.isfile(filename):
if not os.path.exists(to_dir):
os.makedirs(to_dir)
shutil.copyfileobj(open(filename, 'rb'), dest_file)
else:
sys.exit("Input is not a file!!! -> " + filename)
dest_file.close()
else:
sys.exit("No input files located for a specified date!!!")
return dest_file
def alpha(tmp_file):
"""Applies replacement pattern to data files
@type tmp_file: str
@param tmp_file: combined file
"""
if os.path.isfile(tmp_file):
replace(tmp_file, alpha_array()),
return
def omega(tmp_file):
"""Applied final replacement patterns to data file
@rtype : object
@param tmp_file: str
@return:
"""
if os.path.isfile(tmp_file):
replace(tmp_file, omega_array()),
return
def partext(temp_file, file_date):
"""
Extract REGTEXT blocks and expend their date, etc. properties
@rtype : str
@param temp_file: Input file to process
@param file_date: Date of the file
@return: Processed file
"""
file_string = ''
if file_date is None:
eff_date = date.today().strftime("%Y%m%d")
else:
eff_date = datetime.strptime(file_date, "%m%d%y").strftime("%Y%m%d")
if os.path.isfile(temp_file):
# Read contents from file as a single string
with open(temp_file, 'rb') as content_file:
file_string = content_file.read().decode()
new_file_string = ''
vol_num = re.findall('<VOL>(\d*)', file_string)[0]
# Get a dictionary of Effective dates and their location for attaching to REGTEXT
effdates_info = defaultdict(list)
effdates_info[0].append(datetime.strptime(eff_date, "%Y%m%d"))
effdates_info[0].append("Pull date: {0:%B} {0.day}, {0:%Y}".format(datetime.strptime(eff_date, "%Y%m%d")))
for eff_date_itr in re.finditer(re.compile("DATE.?><HED>DATES.*\n?<P>(.*)", re.MULTILINE), file_string):
if eff_date_itr.group():
eff_date_str = re.findall("(\w*) (\d{1,2}), (\d{4})", eff_date_itr.group())
if len(eff_date_str) == 1:
try:
effdates_info[eff_date_itr.start()].append(
datetime.strptime(" ".join(str(i) for i in eff_date_str[0]), "%B %d %Y"))
effdates_info[eff_date_itr.start()].append("{0:%B} {0.day}, {0:%Y}".format(
datetime.strptime(" ".join(str(i) for i in eff_date_str[0]), "%B %d %Y")))
except ValueError as err:
print(err)
print("Please correct Effective Dates in a final file if/where December 31, 1969 appears!!!")
effdates_info[eff_date_itr.start()].append(datetime.fromtimestamp(0))
effdates_info[eff_date_itr.start()].append(
"{0:%B} {0.day}, {0:%Y}".format(datetime.fromtimestamp(0)))
elif len(eff_date_str) > 1:
try:
effdates_info[eff_date_itr.start()].append(
datetime.strptime(" ".join(str(i) for i in eff_date_str[0]), "%B %d %Y"))
except ValueError as err:
print(err)
print("Please correct Effective Dates in a final file where December 31, 1969 appears!!!")
effdates_info[eff_date_itr.start()].append(datetime.fromtimestamp(0))
effdates_info[eff_date_itr.start()].append(eff_date_itr.group(1))
else:
effdates_info[eff_date_itr.start()].append(None)
effdates_info[eff_date_itr.start()].append(eff_date_itr.group(1))
else:
effdates_info[eff_date_itr.start()].append(None)
effdates_info[eff_date_itr.start()].append(eff_date_itr.group(1))
# Get a dictionary of PRTPAGE tag numbers and their location for attaching to REGTEXT
prtpage_info = dict()
prtpage_info[0] = ['00000']
for prt_page_itr in re.finditer(re.compile("\s*<PRTPAGE P=\'(\d+)\'>\s*"), file_string):
if prt_page_itr.group():
pg_num = re.findall("\d+", prt_page_itr.group())
prtpage_info[prt_page_itr.start()] = pg_num
else:
prtpage_info[prt_page_itr.start()] = '00000'
# Retrieve REGTEXT clauses and attach dates and page number tags and attributes to them
id_seq = 0
for reg in re.finditer('(<REGTEXT TITLE.*?</REGTEXT>)', file_string, re.S):
# Eliminate certain specific REGTEXT buckets
if re.search("continues to read", reg.group(0)) and not re.search("revised|revising|amend|remove|add",
reg.group(0)):
continue
id_seq += 1
reg_eff_date = get_from_dict(reg.start(), effdates_info)
# Warn when Pull date is substituted
if reg_eff_date[1].find("Pull date:") >= 0:
print("Warning: valid date is not found! Look for 'Pull date:' in the final file!")
if reg_eff_date[0]:
effdate_attrib = reg_eff_date[0].strftime("%Y%m%d")
effdate_element = reg_eff_date[1]
else:
effdate_attrib = "{0:%Y}0000".format(datetime.strptime(eff_date, "%Y%m%d"))
effdate_element = reg_eff_date[1]
reg_prt_num = get_from_dict(reg.start(), prtpage_info)
regtxt_attrb = ' EFFDATE=\'' + effdate_attrib + '\' ID=\'' + eff_date + '-' + str(id_seq) + \
'\' FRPAGE=\'' + vol_num + 'FR' + reg_prt_num[
0] + '\'><EFFDATES>' + effdate_element
reg_txt = re.sub(">", regtxt_attrb, reg.group(0), count=1)
new_file_string += reg_txt
new_file_string = "<CFRDOC ED='XX' REV='XX'>\n\n" + new_file_string + "\n</CFRDOC>"
new_file_name = os.path.join(os.path.dirname(temp_file), eff_date + ".AMD")
with open(new_file_name, "w") as text_file:
text_file.write(new_file_string)
return new_file_name
if __name__ == "__main__":
args = docopt(__doc__, version='\nPULL 2.3.13')
if args['set']:
from_dir = r'\\hqnapdcm0734\ofr\ofr_gpo\TOOFR'
to_dir = r'\\hqnapdcm0734\ofr\e_cfr\Regtext'
if os.path.exists(from_dir) and os.path.exists(to_dir):
temp_file = move_files(from_dir, to_dir, None)
alpha(temp_file.name)
final_file = partext(temp_file.name, None)
omega(final_file)
print("\n*** Auto Processing Completed! File is located here: " + final_file + " ***")
else:
print(
"\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
"!!! Either <from> or <to> or both directories are invalid !!!\n"
"!!! Make sure 'M:\Toofr' and 'L:\Regtext' are present !!!\n"
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
elif args['auto']:
from_dir = r'\\hqnapdcm0734\ofr\ofr_gpo\TOOFR'
schema = Schema({
'<to>': And(os.path.exists, error='\n<to> directory must exist!!!'),
'--date': Or(None, And(lambda n: datetime.strptime(n, "%m%d%y")),
error='\n--date= must be in a <MMDDYY> format!!!'),
str: object
})
try:
args = schema.validate(args)
except SchemaError as e:
sys.exit(e)
temp_file = move_files(from_dir, args['<to>'], args['--date'])
alpha(temp_file.name)
final_file = partext(temp_file.name, args['--date'])
omega(final_file)
print("\n*** Auto Processing Completed! File is located here: " + final_file + " ***")
elif args['move']:
schema = Schema({
'<from>': And(os.path.exists, error='\n<from> directory must exist!!!'),
'<to>': And(os.path.exists, error='\n<to> directory must exist!!!'),
'--date': Or(None, And(lambda n: datetime.strptime(n, "%m%d%y")),
error='\n--date= must be in a <MMDDYY> format!!!'),
str: object
})
try:
args = schema.validate(args)
except SchemaError as e:
sys.exit(e)
temp_file = move_files(args['<from>'], args['<to>'], args['--date'])
print("\n*** Files Moved & Combined! Destination file is located here: " + temp_file.name + " ***")