-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
renBuilder.py
192 lines (163 loc) · 6.65 KB
/
renBuilder.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
import base64
import csv
from datetime import datetime
from github import Github
from io import StringIO
import os
import re
from sqlalchemy.orm.exc import MultipleResultsFound
from model.renewal import Renewal
from model.registration import Registration
class CCRReader():
def __init__(self, manager):
self.git = Github(os.environ['ACCESS_TOKEN'])
self.repo = self.git.get_repo(os.environ['CCR_REPO'])
self.ccrYears = {}
self.dbManager = manager
def loadYears(self, selectedYear, loadFromTime):
for year in self.repo.get_contents('/data'):
yearMatch = re.match(r'^([0-9]{4}).*\.tsv$', year.name)
if not yearMatch: continue
fileYear = yearMatch.group(1)
if selectedYear is not None and selectedYear != fileYear: continue
fileCommit = self.repo.get_commits(path=year.path)[0]
commitDate = fileCommit.commit.committer.date
if loadFromTime is not None and commitDate < loadFromTime: continue
yearInfo = {
'path': year.path,
'filename': year.name,
'sha': year.sha
}
self.ccrYears[fileYear] = yearInfo
def importYears(self):
for year in self.ccrYears.keys(): self.importYear(year)
def importYear(self, year):
yearInfo = self.ccrYears[year]
print('Loading Year {}'.format(year))
cceFile = CCRFile(self.repo, yearInfo, self.dbManager.session)
cceFile.loadFileTSV()
cceFile.readRows()
self.dbManager.commitChanges()
class CCRFile():
def __init__(self, repo, ccrFile, session):
self.repo = repo
self.ccrFile = ccrFile
self.session = session
self.rows = []
def loadFileTSV(self):
yearBlob = self.repo.get_git_blob(self.ccrFile['sha'])
tsvString = base64.b64decode(yearBlob.content).decode('utf-8')
tsvFile = StringIO(tsvString)
self.rows = csv.DictReader(tsvFile, delimiter='\t', quotechar='"')
def readRows(self):
for row in self.rows: self.parseRow(row)
def parseRow(self, row):
rec = self.matchRenewal(row['entry_id'])
if rec: self.updateRenewal(rec, row)
else: self.createRenewal(row)
def createRenewal(self, row):
title = CCRFile.cascadeFieldNameLoad('title', 'titl', row=row)
renewalDateText = CCRFile.cascadeFieldNameLoad('rdat', 'dreg', row=row)
source = CCRFile.cascadeFieldNameLoad('source', 'full_text', row=row)
author = CCRFile.cascadeFieldNameLoad('author', 'auth', row=row)
notes = CCRFile.cascadeFieldNameLoad('notes', 'note', row=row)
try:
renDate = datetime.strptime(renewalDateText, '%Y-%m-%d')
except ValueError:
renDate = None
renRec = Renewal(
uuid=row['entry_id'],
author=author,
title=title,
reg_data='{}|{}'.format(row['oreg'], row['odat']),
renewal_num=row['id'],
renewal_date=renDate,
renewal_date_text=renewalDateText,
new_matter=row['new_matter'],
see_also_regs=row['see_also_reg'],
see_also_rens=row['see_also_ren'],
notes=notes,
source=source
)
for numField in ['volume', 'part', 'number', 'page']:
setattr(
renRec,
numField,
row[numField] if row[numField] != '' else None
)
self.matchRegistrations(renRec, row['oreg'], row['odat'])
renRec.addClaimants(row['claimants'])
self.session.add(renRec)
print('INSERT', renRec)
def updateRenewal(self, rec, row):
rec.uuid = row['entry_id']
rec.title = CCRFile.cascadeFieldNameLoad('title', 'titl', row=row)
rec.source = CCRFile.cascadeFieldNameLoad('source', 'full_text', row=row)
rec.author = CCRFile.cascadeFieldNameLoad('author', 'auth', row=row)
rec.notes = CCRFile.cascadeFieldNameLoad('notes', 'note', row=row)
rec.reg_data = '{}|{}'.format(row['oreg'], row['odat'])
rec.renewal_num = row['id']
rec.new_matter = row['new_matter']
if row['see_also_reg']:
rec.see_also_regs = row['see_also_reg']
if row['see_also_ren']:
rec.see_also_rens = row['see_also_ren']
rec.renewal_date_text = CCRFile.cascadeFieldNameLoad('rdat', 'dreg', row=row)
try:
rec.renewal_date = datetime.strptime(rec.renewal_date_text, '%Y-%m-%d')
except ValueError:
rec.renewal_date = None
for numField in ['volume', 'part', 'number', 'page']:
setattr(
rec,
numField,
row[numField] if row[numField] != '' else None
)
self.matchRegistrations(rec, row['oreg'], row['odat'])
rec.updateClaimants(row['claimants'])
print('UPDATE', rec)
def matchRenewal(self, uuid):
return self.session.query(Renewal).filter(Renewal.uuid == uuid).one_or_none()
def matchRegistrations(self, renRec, regnum, origDate):
if regnum is None or regnum.strip() == '': return
try:
checkDate = datetime.strptime(origDate, '%Y-%m-%d')
except ValueError:
checkDate = None
regnumQuery = self.session.query(Registration)\
.filter(Registration.regnum == regnum)\
.filter(Registration.reg_date == checkDate)
try:
origReg = regnumQuery.one_or_none()
except MultipleResultsFound:
origRegs = regnumQuery.all()
if len(origRegs) < 1:
origReg = None
seeAlsoRegs = regnumQuery.all()
renRec.see_also_regs = '{}|{}'.format(
renRec.see_also_regs,
'|'.join([ r.regnum for r in seeAlsoRegs ])
)
else:
origReg = origRegs[0]
if len(origRegs) > 1:
renRec.see_also_regs = '{}|{}'.format(
renRec.see_also_regs,
'|'.join([ r.regnum for r in origRegs[1:] ])
)
if origReg:
renRec.registrations.append(origReg)
renRec.orphan = False
else:
print('Matching Registration not found!')
if len(renRec.registrations) < 1:
renRec.orphan = True
@staticmethod
def cascadeFieldNameLoad(*fields, row=None):
for field in fields:
try:
return row[field]
except KeyError:
pass
print('No matching field found!')
raise KeyError