-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_standard_units.py
104 lines (77 loc) · 2.57 KB
/
generate_standard_units.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""":Mod: generate_standard_units
:Synopsis:
:Author:
Duane Costa
:Created:
10/19/18
"""
from docopt import docopt
import logging
import sys
import xml.etree.ElementTree as ET
logging.basicConfig(format='%(asctime)s %(levelname)s (%(name)s): %(message)s',
datefmt='%Y-%m-%d %H:%M:%S%z',
# filename='offline_data' + '.log',
level=logging.INFO)
logger = logging.getLogger('offline_data')
def generate_standard_units(fin, fout=sys.stdout):
"""
Generates an alphabetically sorted list of standard units from the
EML 2.2 file eml-unitDictionary.xml. Units with a deprecatedInFavorOf
attribute are excluded from the list.
Also generates an alphabetically sorted list of deprecatedInFavorOf units,
though these are probably of less interest.
"""
standard_units = []
deprecated_units = []
try:
tree = ET.parse(fin)
root = tree.getroot()
for child in root:
if child.tag[-4:] == 'unit':
unit_name = child.get('name')
deprecatedInFavorOf = child.get('deprecatedInFavorOf')
if not deprecatedInFavorOf:
standard_units.append(unit_name)
else:
deprecated_units.append(unit_name)
print("STANDARD UNITS", file=fout)
standard_units.sort(key=lambda s: s.lower())
print(standard_units, file=fout)
print('', file=fout)
print('DEPRECATED UNITS', file=fout)
deprecated_units.sort(key=lambda s: s.lower())
print(deprecated_units, file=fout)
except Exception as e:
logger.error(e)
def main():
"""
Reports on PASTA data packages with offline data entities.
Usage:
generate_standard_units.py [-i | --input <input>]
generate_standard_units.py [-o | --output <output>]
generate_standard_units.py [-h | --help]
Options:
-i --input XML input file (usually eml-unitDictionary.xml)
-o --output Output results to file;
-h --help This page
"""
args = docopt(str(main.__doc__))
input_file = args['<input>']
output = args['<output>']
if input_file is None:
input_file = 'eml-unitDictionary.xml'
fin = open(input_file, 'rt')
if output is None:
fout = sys.stdout
else:
fout = open(output, 'w')
generate_standard_units(fin=fin, fout=fout)
if fin:
fin.close()
if output:
fout.close()
if __name__ == "__main__":
main()