2015-11-24 09:50:57 +01:00
|
|
|
import json
|
|
|
|
|
|
|
|
debug = False
|
|
|
|
filename = 'verisc-labels.json'
|
|
|
|
namespace = 'veris'
|
|
|
|
description = 'Vocabulary for Event Recording and Incident Sharing (VERIS)'
|
|
|
|
|
|
|
|
output = {}
|
|
|
|
output['namespace'] = namespace
|
|
|
|
output['description'] = description
|
2015-11-24 10:57:19 +01:00
|
|
|
output['version'] = 2
|
2015-11-24 09:50:57 +01:00
|
|
|
output['predicates'] = []
|
|
|
|
output['values'] = []
|
|
|
|
|
|
|
|
with open(filename) as fp:
|
|
|
|
t = json.load(fp)
|
|
|
|
|
2017-04-02 22:06:32 +02:00
|
|
|
|
2015-11-24 09:50:57 +01:00
|
|
|
def lookupPredicate(predicate=False):
|
|
|
|
if not predicate:
|
|
|
|
return False
|
|
|
|
for p in output['predicates']:
|
|
|
|
if p['value'] == predicate:
|
|
|
|
return True
|
|
|
|
|
2017-04-02 22:06:32 +02:00
|
|
|
|
2015-11-24 09:50:57 +01:00
|
|
|
def lookupValues(predicate=False):
|
|
|
|
if not predicate:
|
2018-05-16 20:49:53 +02:00
|
|
|
return {}
|
2015-11-24 09:50:57 +01:00
|
|
|
for p in output['values']:
|
|
|
|
if p['predicate'] == predicate:
|
2018-05-16 20:49:53 +02:00
|
|
|
return p
|
|
|
|
return {}
|
2017-04-02 22:06:32 +02:00
|
|
|
|
2015-11-24 09:50:57 +01:00
|
|
|
|
|
|
|
def machineTag(namespace=False, predicate=False, value=None, expanded=None):
|
|
|
|
|
|
|
|
if namespace is False or predicate is False:
|
|
|
|
return None
|
|
|
|
if value is None:
|
|
|
|
return ('{0}:{1}'.format(namespace, predicate))
|
|
|
|
else:
|
|
|
|
if not lookupPredicate(predicate=predicate):
|
|
|
|
x = {}
|
|
|
|
x['value'] = predicate
|
|
|
|
output['predicates'].append(x)
|
|
|
|
z = {}
|
|
|
|
z['value'] = value
|
|
|
|
z['expanded'] = expanded
|
2018-05-16 20:49:53 +02:00
|
|
|
predicate_entries = lookupValues(predicate)
|
|
|
|
if predicate_entries:
|
|
|
|
predicate_entries['entry'].append(z)
|
|
|
|
else:
|
|
|
|
y = {}
|
|
|
|
y['predicate'] = predicate
|
|
|
|
y['entry'] = []
|
|
|
|
y['entry'].append(z)
|
|
|
|
output['values'].append(y)
|
2015-11-24 09:50:57 +01:00
|
|
|
return ('{0}:{1}=\"{2}\"'.format(namespace, predicate, value))
|
|
|
|
|
|
|
|
|
|
|
|
prefix = []
|
|
|
|
top = []
|
|
|
|
|
2017-04-02 22:06:32 +02:00
|
|
|
|
2015-11-24 09:50:57 +01:00
|
|
|
def flatten(root, prefix_keys=True):
|
|
|
|
dicts = [([], root)]
|
|
|
|
ret = {}
|
|
|
|
seen = set()
|
|
|
|
for path, d in dicts:
|
|
|
|
if id(d) in seen:
|
|
|
|
continue
|
|
|
|
seen.add(id(d))
|
2018-05-16 20:49:53 +02:00
|
|
|
for k, v in sorted(d.items()):
|
2015-11-24 09:50:57 +01:00
|
|
|
new_path = path + [k]
|
|
|
|
prefix = ':'.join(new_path) if prefix_keys else k
|
|
|
|
if hasattr(v, 'items'):
|
|
|
|
dicts.append((new_path, v))
|
|
|
|
else:
|
|
|
|
p = ':'.join(prefix.rsplit(':')[:-1])
|
|
|
|
if debug:
|
2017-04-02 22:06:32 +02:00
|
|
|
print(namespace + ":" + p + "=" + v)
|
2015-11-24 09:50:57 +01:00
|
|
|
machineTag(namespace=namespace, predicate=p, value=prefix.split(':')[-1], expanded=v)
|
|
|
|
ret[prefix] = v
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2017-04-02 22:06:32 +02:00
|
|
|
flatten(root=t)
|
|
|
|
|
|
|
|
print(json.dumps(output))
|