Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

argument parsing, json schema generator added #8

Merged
merged 10 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 0 additions & 156 deletions Makefile

This file was deleted.

104 changes: 104 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import argparse
import subprocess
import yaml

from linkml.generators.jsonschemagen import JsonSchemaGenerator
from linkml.generators.shaclgen import ShaclGenerator
from linkml.generators.shexgen import ShExGenerator
from linkml.generators.owlgen import OwlSchemaGenerator
from linkml.generators.markdowngen import MarkdownGenerator
from linkml.generators.jsonldgen import JSONLDGenerator
from linkml.generators.jsonldcontextgen import ContextGenerator
from linkml.generators.typescriptgen import TypescriptGenerator
from linkml.generators.protogen import ProtoGenerator
from linkml.generators.docgen import DocGenerator
from pathlib import Path

RESOURCES_PATH = Path('resources')
GENS_PATH = RESOURCES_PATH / 'gens'
JSON_SCHEMA_PATH = GENS_PATH / 'jsonschema'
YAML_SCHEMA_PATH = RESOURCES_PATH / 'thing_description_schema.yaml'
SOURCE_PATH = Path('src')
LINKML_PATH = Path('linkml')
LINKML_GENERATORS_CONFIG_YAML_PATH = SOURCE_PATH / LINKML_PATH / 'config.yaml'
DOCDIR = GENS_PATH / 'docs'


def serve_docs():
subprocess.run(['mkdocs', 'serve'], check=True)


def generate_docs(yaml_path, docdir):
docdir.mkdir(parents=True, exist_ok=True)
doc_generator = DocGenerator(yaml_path)
doc_generator.serialize(directory=str(docdir))


def config_file_parse(config_path):
with open(config_path, 'r') as config_file:
config_content = config_file.read()
yaml_content = yaml.safe_load(config_content)
generator_args = yaml_content.get('generator_args', {})
generators = list(generator_args.keys())
return generators


def main(yaml_path, config_path, generate_docs_flag, serve_docs_flag):
yaml_content = yaml_path.read_text()
generators = config_file_parse(config_path)

for generator in generators:
output_dir = GENS_PATH / generator
output_dir.mkdir(parents=True, exist_ok=True)

if generator == 'jsonschema':
json_schema_generator = JsonSchemaGenerator(yaml_content)
(JSON_SCHEMA_PATH / f'{yaml_path.stem}.json').write_text(json_schema_generator.serialize())
elif generator == 'owl':
owl_generator = OwlSchemaGenerator(yaml_content)
(output_dir / f'{yaml_path.stem}.owl.ttl').write_text(owl_generator.serialize())
elif generator == 'markdown':
markdown_generator = MarkdownGenerator(yaml_content)
(output_dir / f'{yaml_path.stem}.md').write_text(markdown_generator.serialize(directory=str(output_dir)))
elif generator == 'shacl':
shacl_generator = ShaclGenerator(yaml_content)
(output_dir / f'{yaml_path.stem}.shacl.ttl').write_text(shacl_generator.serialize())
elif generator == 'shex':
shex_generator = ShExGenerator(yaml_content)
(output_dir / f'{yaml_path.stem}.shex').write_text(shex_generator.serialize())
elif generator == 'jsonld':
jsonld_generator = JSONLDGenerator(yaml_content)
(output_dir / f'{yaml_path.stem}.jsonld').write_text(jsonld_generator.serialize())
elif generator == 'jsonldcontext':
context_generator = ContextGenerator(yaml_content)
(output_dir / f'{yaml_path.stem}.context.jsonld').write_text(context_generator.serialize())
elif generator == 'typescript':
typescript_generator = TypescriptGenerator(yaml_content)
(output_dir / f'{yaml_path.stem}.js').write_text(typescript_generator.serialize())
elif generator == 'protobuf':
protobuf_generator = ProtoGenerator(yaml_content)
(output_dir / f'{yaml_path.stem}.js').write_text(protobuf_generator.serialize())
else:
print(f"Unknown generator: {generator}")
continue

if generate_docs_flag:
generate_docs(yaml_path, DOCDIR)

if serve_docs_flag:
generate_docs(yaml_path, DOCDIR)
serve_docs()


if __name__ == '__main__':
parser = argparse.ArgumentParser(description='WoT TD toolchain for automating specification generation')
parser.add_argument('-y', '--yaml', default=YAML_SCHEMA_PATH,
help='Path to the LinkML schema formatted as a YAML file.')
parser.add_argument('-c', '--config-file', default=LINKML_GENERATORS_CONFIG_YAML_PATH,
help='Path to YAML configuration for specifying the required LinkML generators.')
parser.add_argument('-l', '--local-docs', action='store_true',
help='Boolean for local documentation generation.')
parser.add_argument('-s', '--serve-docs', action='store_true',
help='Boolean for serving the generated documentation.')
args = parser.parse_args()
main(Path(args.yaml), Path(args.config_file), args.local_docs, args.serve_docs)
4 changes: 2 additions & 2 deletions resources/gens/datamodel/thing_description_schema.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Auto generated from thing_description_schema.yaml by pythongen.py version: 0.0.1
# Generation date: 2024-05-21T15:16:14
# Generation date: 2024-05-22T14:37:39
# Schema: thing-description-schema
#
# id: td
# description: LinkML schema for modelling the Web of Things Thing Description information model. This schema is used to generate
# description: LinkML schema for modelling the W3C Web of Things Thing Description information model. This schema is used to generate
# JSON Schema, SHACL shapes, and RDF.
# license: MIT

Expand Down
Loading
Loading