2021-03-26 09:46:17 +01:00
|
|
|
#!/usr/bin/env python3.7
|
|
|
|
# coding=utf-8
|
|
|
|
|
|
|
|
from argparse import ArgumentParser
|
2021-08-06 16:50:22 +02:00
|
|
|
from stand_off_data import StandOffData
|
2021-04-30 09:44:35 +02:00
|
|
|
import hashlib
|
2021-03-26 09:46:17 +01:00
|
|
|
import json
|
|
|
|
|
|
|
|
|
2021-04-30 09:44:35 +02:00
|
|
|
def main():
|
|
|
|
# Parse the given arguments
|
|
|
|
parser = ArgumentParser(description='Create a vrt from JSON and txt')
|
2021-05-18 10:26:03 +02:00
|
|
|
parser.add_argument('text', help='Path to txt file')
|
|
|
|
parser.add_argument('stand_off_data', help='Path to JSON file')
|
|
|
|
parser.add_argument('output', help='Path to vrt output file')
|
2021-04-30 09:44:35 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
with open(args.stand_off_data) as stand_of_data_file:
|
2021-08-06 16:50:22 +02:00
|
|
|
stand_off_data = StandOffData(json.load(stand_of_data_file))
|
2021-04-30 09:44:35 +02:00
|
|
|
|
|
|
|
with open(args.text, "rb") as text_file:
|
|
|
|
text_md5 = hashlib.md5()
|
|
|
|
for chunk in iter(lambda: text_file.read(128 * text_md5.block_size), b''): # noqa
|
|
|
|
text_md5.update(chunk)
|
2021-08-06 16:50:22 +02:00
|
|
|
if text_md5.hexdigest() != stand_off_data.meta['file']['md5']:
|
2021-04-30 09:44:35 +02:00
|
|
|
raise Exception('md5 not equal')
|
|
|
|
|
2021-08-10 14:43:55 +02:00
|
|
|
with open(args.text, encoding=stand_off_data.meta['file']['encoding']) as text_file:
|
|
|
|
text = text_file.read()
|
|
|
|
|
|
|
|
with open(args.output, 'w') as vrt_file:
|
|
|
|
vrt_file.write(stand_off_data.to_vrt(text))
|
|
|
|
|
2021-04-30 09:44:35 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|