-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_atmodat_pyessv_archive.py
196 lines (169 loc) · 6.15 KB
/
create_atmodat_pyessv_archive.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
"""
.. module:: write.py
:platform: Unix, Windows
:synopsis: Maps raw AtMoDat vocab files to normalized pyessv format.
.. moduleauthor:: Mark Conway-Greenslade <[email protected]>
"""
import argparse
import datetime as dt
import json
import os
import pyessv
# Define command line options.
_ARGS = argparse.ArgumentParser('Maps raw AtMoDat vocab files to normalized pyessv CV format.')
_ARGS.add_argument(
'--source',
help='Path from which raw AtMoDat vocab files will be read.',
dest='source',
type=str
)
# Ensure we use fixed creation date.
_CREATE_DATE = dt.datetime.today()
# CV authority = AtMoDat.
_AUTHORITY = pyessv.create_authority(
'AtMoDat',
'AtMoDat',
label='AtMoDat',
url='https://www.atmodat.de/',
create_date=_CREATE_DATE
)
# CV scope = AtMoDat.
_SCOPE_ATMODAT = pyessv.create_scope(
_AUTHORITY,
'AtMoDat',
'Controlled Vocabularies (CVs) for use with AtMoDat',
label='AtMoDat',
url='https://github.com/AtMoDat/AtMoDat_CVs',
create_date=_CREATE_DATE
)
# Map of scopes to collections.
_SCOPE_COLLECTIONS = {
_SCOPE_ATMODAT: {
'realm': {
'cim_document_type': None,
'cim_document_type_alternative_name': None,
'data_factory': lambda obj, name: {'description': obj[name]},
'is_virtual': False,
'label': None,
'ommitted': [],
'term_regex': None
},
'frequency': {
'cim_document_type': None,
'cim_document_type_alternative_name': None,
'data_factory': lambda obj, name: {'description': obj[name]},
'is_virtual': False,
'label': None,
'ommitted': [],
'term_regex': None
},
'featureType': {
'cim_document_type': None,
'cim_document_type_alternative_name': None,
'data_factory': None,
'is_virtual': False,
'label': None,
'ommitted': [],
'term_regex': None
},
'nominal_resolution': {
'cim_document_type': None,
'cim_document_type_alternative_name': None,
'data_factory': None,
'is_virtual': False,
'label': None,
'ommitted': [],
'term_regex': r'^[a-z0-9\-\.]*$'
},
'source_type': {
'cim_document_type': None,
'cim_document_type_alternative_name': None,
'data_factory': lambda obj, name: {'description': obj[name]},
'is_virtual': False,
'label': None,
'ommitted': [],
'term_regex': None
}
},
}
def _main(args):
"""Main entry point.
"""
if not os.path.isdir(args.source):
raise ValueError('AtMoDat vocab directory does not exist')
# Create collections.
for scope in _SCOPE_COLLECTIONS:
for collection in _SCOPE_COLLECTIONS[scope]:
cfg = _SCOPE_COLLECTIONS[scope][collection]
_create_collection(args.source, scope, collection, cfg)
# Add to archive & persist to file system.
pyessv.archive(_AUTHORITY)
def _create_collection(source, scope, collection_id, cfg):
"""Creates collection from a AtMoDat JSON file.
"""
# Create collection.
if collection_id.lower().replace('_', '-') in [collection.name for collection in scope.collections]:
collection = scope[collection_id]
collection.description = "AtMoDat CV collection: ".format(collection_id),
collection.label = cfg['label'] or collection_id.title().replace('_Id', '_ID').replace('_', ' '),
collection.create_date = _CREATE_DATE,
collection.term_regex = cfg['term_regex'] or pyessv.REGEX_CANONICAL_NAME,
collection.data = None if cfg['cim_document_type'] is None else {
'cim_document_type': cfg['cim_document_type'],
'cim_document_type_alternative_name': cfg['cim_document_type_alternative_name']
}
else:
collection = pyessv.create_collection(
scope,
collection_id,
"AtMoDat"
" CV collection: ".format(collection_id),
label=cfg['label'] or collection_id.title().replace('_Id', '_ID').replace('_', ' '),
create_date=_CREATE_DATE,
term_regex=cfg['term_regex'] or pyessv.REGEX_CANONICAL_NAME,
data=None if cfg['cim_document_type'] is None else {
'cim_document_type': cfg['cim_document_type'],
'cim_document_type_alternative_name': cfg['cim_document_type_alternative_name']
}
)
# Load JSON data & create terms (if collection is not a virtual one).
if not cfg['is_virtual']:
cv_data = _get_atmodat_cv(source, scope, collection_id)
data_factory = cfg['data_factory']
for term_name in [i for i in cv_data if i not in cfg['ommitted']]:
term_data = data_factory(cv_data, term_name) if data_factory else None
_create_term(collection, term_name, term_data)
def _create_term(collection, raw_name, data):
"""Creates & returns a new term.
"""
try:
description = data['description']
except (TypeError, KeyError):
description = None
else:
del data['description']
try:
label = data['label']
except (TypeError, KeyError):
label = raw_name
else:
del data['label']
term = pyessv.create_term(
collection,
raw_name,
description=description,
label=label,
create_date=_CREATE_DATE,
data=data
)
def _get_atmodat_cv(source, scope, collection_id):
"""Returns raw AtMoDat CV data.
"""
prefix = 'AtMoDat_' if scope.canonical_name == 'atmodat' else ''
fname = '{}{}.json'.format(prefix, collection_id)
fpath = os.path.join(source, fname)
with open(fpath, 'r') as fstream:
return json.loads(fstream.read())[collection_id]
# Entry point.
if __name__ == '__main__':
_main(_ARGS.parse_args())