|
7 | 7 | import os |
8 | 8 | from os.path import join as pjoin |
9 | 9 | import json |
10 | | -import pkgutil |
11 | | -import datetime |
12 | 10 |
|
13 | 11 | import yaml |
14 | 12 | from yaml import Loader |
15 | 13 | from jsonschema import validate |
16 | 14 |
|
| 15 | +from .util import write_viz, merge_yamls |
17 | 16 |
|
18 | 17 | _basedir = os.path.dirname(__file__) |
19 | 18 | schema = json.load(open(pjoin(_basedir, "schema.json"), "r")) |
20 | 19 |
|
21 | | - |
22 | | -def write_viz(vega_taxonomy, outname="viz.html"): |
23 | | - """ |
24 | | - Use the current taxonomy to vizualize the tree |
25 | | - with d3. |
26 | | -
|
27 | | - >> import tdtax |
28 | | - >> tdtax.write_viz(tdtax.vega_taxonomy) |
29 | | -
|
30 | | - """ |
31 | | - text = pkgutil.get_data(__name__, "viz_template.html").decode() |
32 | | - text = text.replace("%%JSON%%", json.dumps(vega_taxonomy)) |
33 | | - text = text.replace("%%VERSION%%", __version__) |
34 | | - text = text.replace("%%DATE%%", str(datetime.datetime.utcnow())) |
35 | | - f = open(outname, "w") |
36 | | - f.write(text) |
37 | | - f.close() |
38 | | - print(f"wrote {outname}") |
39 | | - |
40 | | - |
41 | | -def walk_and_replace(d, path="./", verbose=False): |
42 | | - """ |
43 | | - recursively replace references to YAMLs |
44 | | - """ |
45 | | - if not isinstance(d, dict): |
46 | | - return |
47 | | - |
48 | | - for key, value in d.items(): |
49 | | - if isinstance(value, dict): |
50 | | - walk_and_replace(value, path=path, verbose=verbose) |
51 | | - elif isinstance(value, list): |
52 | | - for i in range(len(value)): |
53 | | - if isinstance(value[i], dict): |
54 | | - if value[i].get("ref") is not None: |
55 | | - ref = path + value[i].get("ref") |
56 | | - if os.path.exists(ref): |
57 | | - replacement = yaml.load(open(ref), Loader=Loader) |
58 | | - value[i] = replacement |
59 | | - else: |
60 | | - if verbose: |
61 | | - print( |
62 | | - f"Did not find file {ref}." |
63 | | - "Adding placeholder." |
64 | | - ) |
65 | | - basename = os.path.basename(ref).split(".")[0] |
66 | | - value[i] = {"class": basename + "-placeholder"} |
67 | | - walk_and_replace(value[i], path=path, verbose=verbose) |
68 | | - |
69 | | - |
70 | | -def merge_yamls(fname): |
71 | | - taxonomy = yaml.load(open(fname), Loader=Loader) |
72 | | - path = os.path.dirname(fname) + "/" |
73 | | - walk_and_replace(taxonomy, path) |
74 | | - return taxonomy |
75 | | - |
76 | | - |
77 | | -# get the taxonomy and validate |
| 20 | +# get the taxonomy and validate - raise an error if this does |
| 21 | +# not validate against the schema |
78 | 22 | taxonomy = merge_yamls(pjoin(_basedir, "top.yaml")) |
79 | 23 | validate(instance=taxonomy, schema=schema) |
80 | 24 |
|
81 | 25 | # get a version of the taxonomy suitable for vega/d3 viz |
82 | 26 | taxstr = json.dumps(taxonomy) |
83 | 27 | taxstr = taxstr.replace('"class":', '"name":') \ |
84 | | - .replace('"subclasses":', '"children":') |
| 28 | + .replace('"subclasses":', '"children":') |
85 | 29 | vega_taxonomy = json.loads(taxstr) |
86 | 30 |
|
| 31 | +__version__ = '0.0.4' |
87 | 32 |
|
88 | | -__all__ = ["taxonomy", "schema", "merge_yamls", "vega_taxonomy", "write_viz"] |
89 | | - |
90 | | -__version__ = '0.0.2' |
| 33 | +__all__ = ["taxonomy", "schema", "vega_taxonomy", "write_viz", "__version__"] |
91 | 34 |
|
92 | | -del (taxstr, walk_and_replace, merge_yamls, |
93 | | - os, json, pkgutil, datetime, yaml, Loader, pjoin) |
| 35 | +del (taxstr, merge_yamls, os, json, yaml, Loader, pjoin) |
0 commit comments