|
| 1 | +from pathlib import Path |
| 2 | +import json |
| 3 | + |
| 4 | +import ruamel.yaml |
| 5 | +from jinja2 import Environment, FileSystemLoader, select_autoescape |
| 6 | + |
| 7 | +yaml = ruamel.yaml.YAML() |
| 8 | +yaml.indent(mapping=2, sequence=4, offset=2) |
| 9 | + |
| 10 | +ROOT = Path(__file__).parents[1] |
| 11 | + |
| 12 | +TEMPLATES_DIR = ROOT / "templates" |
| 13 | + |
| 14 | +LIBRARY_DIR = ROOT / "library" |
| 15 | + |
| 16 | +SCHEMA_DIR = ROOT / "linkml-schema" |
| 17 | + |
| 18 | + |
| 19 | +def return_jinja_env() -> Environment: |
| 20 | + return Environment( |
| 21 | + loader=FileSystemLoader(TEMPLATES_DIR), |
| 22 | + autoescape=select_autoescape(), |
| 23 | + lstrip_blocks=True, |
| 24 | + trim_blocks=True, |
| 25 | + ) |
| 26 | + |
| 27 | + |
| 28 | +def library_table() -> str: |
| 29 | + |
| 30 | + LIBRARY_URL = "https://github.com/ReproNim/reproschema-library" |
| 31 | + |
| 32 | + activities = [] |
| 33 | + |
| 34 | + for activity_path in (LIBRARY_DIR / "activities").iterdir(): |
| 35 | + |
| 36 | + if not activity_path: |
| 37 | + continue |
| 38 | + |
| 39 | + for file in activity_path.glob("*"): |
| 40 | + |
| 41 | + if file.is_dir() or file is None or "valueConstraints" in file.stem: |
| 42 | + continue |
| 43 | + |
| 44 | + with open(file) as f: |
| 45 | + content = json.load(f) |
| 46 | + |
| 47 | + activities.append( |
| 48 | + { |
| 49 | + "name": content["@id"], |
| 50 | + "description": ( |
| 51 | + content["description"] if "description" in content else "" |
| 52 | + ), |
| 53 | + "uri": f"{LIBRARY_URL}/tree/master/activities/{activity_path.stem}/{file.stem}{file.suffix}", |
| 54 | + } |
| 55 | + ) |
| 56 | + |
| 57 | + env = return_jinja_env() |
| 58 | + template = env.get_template("library_table.jinja") |
| 59 | + |
| 60 | + return template.render(activities=activities) |
| 61 | + |
| 62 | + |
| 63 | +def main(): |
| 64 | + print(library_table()) |
| 65 | + |
| 66 | + |
| 67 | +if __name__ == "__main__": |
| 68 | + main() |
0 commit comments