forked from oasis-open/openc2-jadn-software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-artifacts.py
47 lines (39 loc) · 1.88 KB
/
make-artifacts.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
"""
Translate each schema file in Source directory to multiple formats in Out directory
"""
import fire
import jadn
import os
import shutil
SCHEMA_DIR = 'Schemas'
OUTPUT_DIR = 'Out'
def translate(filename: str, sdir: str, odir: str) -> None:
if not os.path.isfile(p := os.path.join(sdir, filename)):
return
with open(p, encoding='utf8') as fp:
schema = jadn.load_any(fp)
print(f'{filename}:\n' + '\n'.join([f'{k:>15}: {v}' for k, v in jadn.analyze(jadn.check(schema)).items()]))
fn, ext = os.path.splitext(filename)
jadn.dump(schema, os.path.join(odir, fn + '.jadn'))
jadn.dump(jadn.transform.unfold_extensions(jadn.transform.strip_comments(schema)),
os.path.join(odir, fn + '-core.jadn'))
jadn.convert.diagram_dump(schema, os.path.join(odir, fn + '_ia.dot'),
style={'format': 'graphviz', 'detail': 'information', 'attributes': True, 'links': True})
jadn.convert.diagram_dump(schema, os.path.join(odir, fn + '_i.puml'),
style={'format': 'plantuml', 'detail': 'information', 'attributes': False, 'links': False})
jadn.convert.jidl_dump(schema, os.path.join(odir, fn + '.jidl'), style={'desc': 50})
jadn.convert.html_dump(schema, os.path.join(odir, fn + '.html'))
jadn.convert.markdown_dump(schema, os.path.join(odir, fn + '.md'))
jadn.translate.json_schema_dump(schema, os.path.join(odir, fn + '.json'))
def main(schema_dir: str = SCHEMA_DIR, output_dir: str = OUTPUT_DIR) -> None:
print(f'Installed JADN version: {jadn.__version__}\n')
css_dir = os.path.join(output_dir, 'css')
os.makedirs(css_dir, exist_ok=True)
shutil.copy(os.path.join(jadn.data_dir(), 'dtheme.css'), css_dir)
for f in os.listdir(schema_dir):
try:
translate(f, schema_dir, output_dir)
except (ValueError, IndexError) as e:
print(f'### {f}: {e}')
if __name__ == '__main__':
fire.Fire(main)