forked from roomthily/ISO-19115-Distribution-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunique_identifier_cli.py
93 lines (76 loc) · 2.28 KB
/
unique_identifier_cli.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
#!/anaconda/bin/python
# -*- coding: utf-8 -*-
from optparse import OptionParser
import json
import sys
import os
from semproc.unique_identifiers import IdentifierExtractor
from semproc.nlp_utils import load_token_list
import traceback
import warnings
warnings.filterwarnings('ignore')
'''
a little cli for extracting unique
identifiers. same regex findall hangups
'''
def main():
op = OptionParser()
op.add_option('--url', '-u')
op.add_option('--file', '-f')
op.add_option('--debug', '-d', default='False')
options, arguments = op.parse_args()
if not options.url:
op.error('No url')
if not options.file:
op.error('No xml file')
debug = bool(options.debug)
with open(options.file, 'r') as f:
xml_as_string = f.read()
# TODO: don't hardcode this
# set up the exclude lists
equality = []
for f in ['namespaces.txt']:
equality += load_token_list(f)
contains = []
for f in ['cat_interop_urns.txt', 'mimetypes.txt', 'namespaces.txt', 'excludes_by_contains.txt']:
contains += load_token_list(f)
tag_paths = [
'@codeList',
'@schemaLocation',
'identifier/@type',
'Details/Licence',
'binding/@transport',
'@template',
'@uri-type',
'rights/@resource',
'digform/digtinfo/formspec',
'license/@href',
'definition/@href',
'dateTime/formatString',
'RDF/Description/@about',
'Binary/Thumbnail/Data' # to exclude base64 encoded data
]
extractor = IdentifierExtractor(
options.url,
xml_as_string,
equality,
contains,
tag_paths
)
identifiers = []
try:
for ident in extractor.process_text():
identifiers.append(ident)
except Exception as ex:
if debug:
traceback.print_exc()
sys.stderr.write('Failed extraction: {0}'.format(ex))
else:
sys.stderr.write('Failed extraction')
sys.exit(1)
# repack the things to send as strings back up the cli chain
# i think as a stringified json dict, streaming json style -
# one dict per line, one dict per identifier
print '\n'.join([json.dumps(ident.to_json()) for ident in identifiers])
if __name__ == '__main__':
main()