forked from RDFLib/pyLODE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
193 lines (163 loc) · 6.27 KB
/
cli.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import argparse
import os
from os import path
import shutil
import requests
import rdflib
from makedocco import MakeDocco
from profiles import OWL_PROFILE, SKOS_PROFILE
# used to know what RDF file types rdflib can handle
RDF_FILE_EXTENSIONS = [".rdf", ".owl", ".ttl", ".n3", ".nt", ".json"]
# used to know what RDF Media Types rdflib can handle
RDF_SERIALIZER_MAP = {
"text/turtle": "turtle",
"text/n3": "n3",
"application/n-triples": "nt",
"application/ld+json": "json-ld",
"application/rdf+xml": "xml",
# Some common but incorrect mimetypes
"application/rdf": "xml",
"application/rdf xml": "xml",
"application/json": "json-ld",
"application/ld json": "json-ld",
"text/ttl": "turtle",
"text/turtle;charset=UTF-8": "turtle",
"text/ntriples": "nt",
"text/n-triples": "nt",
"text/plain": "nt", # text/plain is the old/deprecated mimetype for n-triples
}
# used to determine whether or not a given path is actually a real file
def is_valid_file(parser, arg):
try:
return open(arg, "r")
except:
parser.error("The file %s does not exist!" % arg)
# used to capture graph parsing and content errors
class RdfGraphError(Exception):
pass
def main(args=None):
# read the input ontology file into a graph
parser = argparse.ArgumentParser()
overarching_group = parser.add_mutually_exclusive_group()
inputs_group = overarching_group.add_mutually_exclusive_group()
inputs_group.add_argument(
"-i",
"--inputfile",
help="The RDF ontology file you wish to generate HTML for. "
"Must be in either Turtle, RDF/XML, JSON-LD or N-Triples formats indicated by the file type extensions:"
"{} respectively.".format(", ".join(RDF_FILE_EXTENSIONS)),
type=lambda x: is_valid_file(parser, x),
)
inputs_group.add_argument(
"-u",
"--url",
help="The RDF ontology you wish to generate HTML for, online. "
"Must be an absolute URL that can be resolved to RDF, preferably via Content Negotiation.",
)
overarching_group.add_argument(
"-lp",
"--listprofiles",
help="This flag, if used, must be the only flag supplied. It will cause the program to list all the ontology"
" documentation profiles that it supports, indicating both their URI and their short token for use with the"
" -p (--profile) flag when creating HTML documentation",
action="store_true",
)
parser.add_argument(
"-p",
"--profile",
help="A profile - a specified information model - for an ontology. You can indicate this via the profile's URI"
"or its token. The list of profiles - URIs and their corresponding tokens - supported by pyLODE, can be "
"found by running the program with the flag -lp or --listprofiles.",
default="owl",
)
parser.add_argument(
"-c",
"--css",
help="Whether (true) or not (false) to copy the default CSS file to the output directory.",
choices=["true", "false"],
default="false",
)
parser.add_argument(
"-xc",
"--excludecss",
help="Whether (true) or not (false) to exclude the standard pyLODE CSS content from an HTML file output.",
choices=["true", "false"],
default="false",
)
parser.add_argument(
"-o",
"--outputfile",
help="A name you wish to assign to the output file. Will be postfixed with .html. If not specified, "
"the name of the input file or last segment of RDF URI will be used, + .html.",
)
parser.add_argument(
"-f",
"--outputformat",
help="The output format of the documentation.",
choices=["html", "md"],
default="html",
)
args = parser.parse_args()
if args.listprofiles:
print(MakeDocco.list_profiles())
exit()
elif args.inputfile or args.url:
# args are present so getting RDF from input file or uri into an rdflid Graph
if args.inputfile:
h = MakeDocco(input_data_file=args.inputfile.name, outputformat=args.outputformat, profile=args.profile)
elif args.url:
h = MakeDocco(input_uri=args.url.name, outputformat=args.outputformat, profile=args.profile)
else:
# we have neither an input file or a URI supplied
parser.error(
"Either an input file or a url is required to access the ontology's RDF"
)
if args.profile:
if not MakeDocco.is_supported_profile(args.profile):
parser.error("The profile you requested, '{}', is not supported. "
"Please choose from those given by List Profiles (-lp)")
else:
parser.error("An ontology source (-i/--inputfile or -u/--url) is required")
# here we have a parsed graph from either a local file or a URI
# set up output directories and resources
main_dir = path.dirname(path.realpath(__file__))
style_dir = path.join(main_dir, "style")
if h.publication_dir == "":
h.publication_dir = "."
os.makedirs(h.publication_dir, exist_ok=True)
# include CSS
msg_css = ""
if args.css == "true":
shutil.copyfile(
path.join(style_dir, "pylode.css"), path.join(h.publication_dir, "style.css")
)
msg_css = " and CSS"
if args.excludecss == "true":
exclude_css = True
else:
exclude_css = False
output_filename = (
os.path.basename(args.outputfile) if args.outputfile else "doc.html"
)
output_format = "HTML"
if args.outputformat == "html":
if not output_filename.endswith(".html"):
output_filename += ".html"
elif args.outputformat == "md":
if not output_filename.endswith(".md"):
output_filename += ".md"
output_format = "Markdown"
# generate the HTML doc
with open(path.join(h.publication_dir, output_filename), "w") as f:
f.write(h.document(exclude_css=exclude_css))
# print message for user
print(
"Finished. {} profile documentation in {} format created in file in {}".format(
args.profile,
output_format,
path.join(h.publication_dir, output_filename)
)
)
if __name__ == "__main__":
import sys
main(sys.argv[1:])