forked from SzymonSmagowski/SHACLER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
83 lines (68 loc) · 2.6 KB
/
main.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import argparse
from pathlib import Path
from shacl_doc_generator.generator import MarkdownGenerator
from shacl_doc_generator.parser import ShaclParser
def main():
supported_file_extensions = [".ttl"]
argparser = argparse.ArgumentParser(
description="SHACLER - SHACL Documentation Generator"
)
argparser.add_argument(
"--input",
required=True,
help=f"Path to SHACL file OR directory containing one or more SHACL files. Supported extensions: {supported_file_extensions}",
)
argparser.add_argument(
"--output",
required=False,
default="./docs",
help="Path to output directory for the generated .md files.",
)
args = argparser.parse_args()
input_path = Path(args.input)
output_dir = Path(args.output)
output_dir.mkdir(parents=True, exist_ok=True)
shacl_parser = ShaclParser()
md_generator = MarkdownGenerator()
if input_path.is_file():
if input_path.suffix not in supported_file_extensions:
print(
f"File extension {input_path.suffix} not supported. Supported file extensions: {supported_file_extensions}"
)
exit(1)
shacl_files = [input_path]
elif input_path.is_dir():
shacl_files = []
for file_path in input_path.iterdir():
if file_path.suffix in supported_file_extensions:
shacl_files.append(file_path)
if not shacl_files:
print(
f"SHACL files not found. Supported file extensions {supported_file_extensions}"
)
exit(1)
for shacl_file in shacl_files:
print(f"Parsing file {shacl_file}")
try:
shapes_for_this_file = shacl_parser.parse_file(shacl_file)
except Exception as e:
print(f"Failed to parse file {shacl_file}: {e}")
continue
output_file = output_dir / f"{shacl_file.stem}.md"
print(f"Writing to {output_file}")
try:
md_generator.generate_docs(
shapes_for_this_file, output_filename=str(output_file)
)
with open(shacl_file, "r", encoding="utf-8") as fp:
turtle_content = fp.read()
with open(output_file, "a", encoding="utf-8") as out:
out.write("\n## Original SHACL File\n\n")
out.write("```turtle\n")
out.write(turtle_content)
out.write("\n```\n")
except Exception as e:
print(f"Error occured when generating docs {output_file}: {e}")
continue
if __name__ == "__main__":
main()