-
Notifications
You must be signed in to change notification settings - Fork 11
/
excel2onto
executable file
·111 lines (95 loc) · 3.17 KB
/
excel2onto
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
#!/usr/bin/env python3
"""Creates and ontology from an excelfile.
The excel file must be in the formate provided by
ontology_template.xlsx
"""
import argparse
import sys
import os
import warnings
from ontopy.excelparser import create_ontology_from_excel, ExcelError
from ontopy.utils import write_catalog
from ontopy import get_ontology
import owlready2 # pylint: disable=C0411
def english(string):
"""Returns `string` as an English location string."""
return owlready2.locstr(string, lang="en")
def main(argv: list = None):
"""Main run function.
Parameters:
argv: List of arguments, similar to `sys.argv[1:]`.
Mainly for testing purposes, since it allows one to invoke the tool
manually / through Python.
"""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"excelpath",
help="path to excel book",
)
parser.add_argument(
"--output",
"-o",
default="ontology.ttl",
help="Name of output ontology, ´ontology.ttl´ is default",
)
parser.add_argument(
"--force",
"-f",
action="store_true",
help="Whether to force generation of ontology on non-fatal error.",
)
parser.add_argument(
"--update",
"-u",
default=True,
help="Whether to update the the ontology with new concepts "
"or regenerate the full ontology."
"Currently only supports adding new concepts"
"Default is True.",
)
parser.add_argument(
"--input_ontology",
"-i",
default=None,
help="Path of previously generated ontology to update with"
" new concepts."
"Deafult is the same as 'output'.",
)
try:
args = parser.parse_args(args=argv)
except SystemExit as exc:
sys.exit(exc.code) # Exit without traceback on invalid arguments
input_ontology = None
if args.update:
try:
if args.input_ontology:
input_ontology = get_ontology(args.input_ontology).load()
else:
input_ontology = get_ontology(args.output).load()
except FileNotFoundError as err:
if args.force:
warnings.warn(
"Did not find the output ontology to be updated, "
"will fully generate a new one."
)
else:
raise FileNotFoundError(
"The output ontology to be updated "
"does not exist. Missing file is: ",
args.output,
) from err
try:
ontology, catalog, _ = create_ontology_from_excel(
os.path.abspath(args.excelpath),
force=args.force,
input_ontology=input_ontology,
)
except ExcelError as exc:
parser.exit(1, f"ERROR: {exc}\n")
# Save new ontology as turtle
ontology.save(os.path.join(args.output), format="turtle", overwrite=True)
dirname = os.path.dirname(args.output)
if (not args.update) or (not os.path.exists(dirname + "/catalog-v001.xml")):
write_catalog(catalog, directory=dirname)
if __name__ == "__main__":
main()