-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransform.py
executable file
·244 lines (200 loc) · 8.44 KB
/
transform.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
#!/usr/bin/env python3
# -*- coding: utf8 -*-
import argparse
import codecs
import csv
from io import BytesIO
import json
import logging
import lxml.etree as ET
import os
import re
import sys
import xml.parsers.expat as xerr
from classes.ead import Ead as Ead
encodings = ['ascii', 'utf-8', 'windows-1252', 'latin-1']
#========================================
# Get list of EAD files (input or output)
#========================================
def get_files_in_path(rootdir, recursive):
result = []
if recursive is True:
print('Traversing recursively...')
for (root, dir, files) in os.walk(rootdir):
result.extend(
[os.path.join(root, f) for f in files if not f.startswith('.')])
else:
print('Searching top folder only...')
result = [os.path.join(rootdir, f) for f in os.listdir(
rootdir) if not f.startswith('.') and os.path.isfile(
os.path.join(rootdir, f))]
print('Found {0} files to process.'.format(len(result)))
return result
#====================================================
# Verify file decoding and return utf8-encoded bytes
#====================================================
def verify_decoding(f, encodings):
print(" Checking encoding...")
for encoding in encodings:
bytes = codecs.open(f, mode='r', encoding=encoding, errors='strict')
try:
b = bytes.read()
print(' - {0} OK.'.format(encoding))
return b.encode('utf8')
except UnicodeDecodeError:
print(' - {0} Error!'.format(encoding))
return False
#=========================
# Load handles from a file
#=========================
def load_handles(handle_file):
result = {}
with open(handle_file, "r") as f:
for line in csv.DictReader(f):
id = line['identifier']
handle = line['handlehttp']
if id not in result:
result[id] = handle
return result
#===============================================================
# Main function: Parse command line arguments and run main loop
#===============================================================
def main():
'''The main wrapper for the transformation code -- parsing arguments,
reading all the files in the specified path, attempting to decode from
various encodings, applying transrormations, and writing out both files and
reports.'''
# user greeting
border = "=" * 19
print("\n".join(['', border, "| EAD Transformer |", border, '']))
handles = load_handles('data/handles.csv')
missing_handles = []
# set up message logging to record actions on files
logger = logging.basicConfig(
format='%(asctime)s %(levelname)s %(message)s',
filename='data/reports/transform.log',
filemode='w',
level=logging.INFO
)
#-----------------------------
# Parse command line arguments
#-----------------------------
parser = argparse.ArgumentParser(description='Process and validate EAD.')
parser.add_argument('-e', '--encoding', action='store_true',
help='check encoding only of files in input path')
parser.add_argument('-i', '--input',
help='input path of files to be transformed')
parser.add_argument('-o', '--output', required=True,
help='ouput path for transformed files')
parser.add_argument('-r', '--resume', action='store_true',
help='resume job, skipping files that already exist in outpath')
parser.add_argument('-R', '--recursive', action='store_true',
help='recursively process files starting at rootdirectory')
parser.add_argument('-v', '--validate', action='store_true',
help='validate that xml is well formed')
parser.add_argument('-s', '--schema',
help='XSD to validate against')
parser.add_argument('files', nargs='*',
help='files to check')
args = parser.parse_args()
# notify that resume flag is set
if args.resume is True:
print("Resume flag (-r) is set, will skip existing files")
# notify that encoding-check flag is set
if args.encoding is True:
print("Encoding flag (-e) flag is set, checking encoding ...")
# notify that validation-check flag is set
if args.validate is True:
print("Validation flag (-v) flag is set, checking well-formedness ...")
# load XSD to validate against
if args.schema:
schema_xml = ET.parse(args.schema)
ead_schema = ET.XMLSchema(schema_xml)
ead_parser = ET.XMLParser(schema=ead_schema)
# get files from inpath
if args.input:
input_dir = args.input
print("Checking files in folder '{0}'...".format(input_dir))
files_to_check = get_files_in_path(input_dir, recursive=args.recursive)
# otherwise, use arguments for files to check
else:
input_dir = os.path.dirname(args.files[0])
print(
"No input path specified; processing files from arguments...")
files_to_check = [f for f in args.files]
# set path for output
output_dir = args.output
#---------------------------------------
# Main loop for processing each EAD XML
#---------------------------------------
for n, f in enumerate(files_to_check):
# set up output paths and create directories if needed
output_path = os.path.join(output_dir, os.path.relpath(f, input_dir))
basename = os.path.basename(f)
parent_dir = os.path.dirname(output_path)
if not os.path.isdir(parent_dir):
os.makedirs(parent_dir)
# summarize file paths to screen
print("\n{0}. Processing EAD file: {1}".format(n+1, f))
print(" IN => {0}".format(f))
print(" OUT => {0}".format(output_path))
# if the resume flag is set, skip files for which output file exists
if args.resume:
if os.path.exists(output_path) and os.path.isfile(output_path):
print(" Skipping {0}: output file exists".format(f))
continue
# attempt strict decoding of file according to common schemes
ead_bytes = verify_decoding(f, encodings)
if not ead_bytes:
print(" Could not reliably decode {0}, skipping...".format(f))
logging.error("{0} could not be decoded.".format(f))
continue
if args.encoding is True:
# validate XML and write to file
if args.validate is True:
file_like_obj = BytesIO(ead_bytes)
try:
ead_tree = ET.parse(file_like_obj)
# ead_schema.assertValid(ead_tree)
ead_tree.write(output_path)
except:
# logging.error(xmlschema.error_log.last_error)
print(" Could not parse XML in {0}, skipping...".format(f))
logging.error("{0} is malformed XML.".format(f))
# write decoded bytes to file without validation
else:
with open(output_path, 'wb') as outfile:
outfile.write(ead_bytes)
continue
else:
if basename in handles.keys():
handle = handles[basename]
else:
missing_handles.append(basename)
handle = ''
# create an EAD object
print(" Parsing XML...")
ead = Ead(basename, handle, BytesIO(ead_bytes))
# add missing elements
ead.add_missing_extents()
ead.correct_text_in_extents()
ead.add_missing_box_containers()
ead.insert_handle()
ead.add_title_to_dao()
# remove duplicate, empty, and unneeded elements
ead.remove_multiple_abstracts()
ead.remove_empty_elements()
ead.remove_opening_of_title()
# fix errors and rearrange
ead.fix_box_number_discrepancies()
ead.move_scopecontent()
ead.sort_containers()
# write out result
ead.tree.write(output_path,
pretty_print=True,
encoding='utf-8',
xml_declaration=True
)
# print(missing_handles)
if __name__ == '__main__':
main()