Source code

Download source archive ↓

src/rdflib/tools/defined_namespace_creator.py

1"""
2This rdflib Python script creates a DefinedNamespace Python file from a given RDF file
3
4It is a very simple script: it finds all things defined in the RDF file within a given
5namespace:
6
7    <thing> a ?x
8
9    where ?x is anything and <thing> starts with the given namespace
10
11Nicholas J. Car, Dec, 2021
12"""
13
14from __future__ import annotations
15
16import argparse
17import datetime
18import keyword
19from pathlib import Path
20from typing import TYPE_CHECKING, Iterable, List, Tuple
21
22from rdflib.graph import Graph
23from rdflib.namespace import DCTERMS, OWL, RDFS, SKOS
24from rdflib.util import guess_format
25
26if TYPE_CHECKING:
27    from rdflib.query import ResultRow
28
29
30def validate_namespace(namespace: str) -> None:
31    if not namespace.endswith(("/", "#")):
32        raise ValueError("The supplied namespace must end with '/' or '#'")
33
34
35def validate_object_id(object_id: str) -> None:
36    for c in object_id:
37        if not c.isupper():
38            raise ValueError("The supplied object_id must be an all-capitals string")
39
40
41# This function is not used: it was originally written to get classes and to be used
42# alongside a method to get properties, but then it was decided that a single function
43# to get everything in the namespace, get_target_namespace_elements(), was both simper
44# and better covered all namespace elements, so that function is used instead.
45#
46# def get_classes(g, target_namespace):
47#     namespaces = {"dcterms": DCTERMS, "owl": OWL, "rdfs": RDFS, "skos": SKOS}
48#     q = """
49#         SELECT DISTINCT ?x ?def
50#         WHERE {
51#             # anything that is an instance of owl:Class or rdfs:Class
52#             # or any subclass of them
53#             VALUES ?c { owl:Class rdfs:Class }
54#             ?x rdfs:subClassOf*/a ?c .
55#
56#             # get any definitions, if they have one
57#             OPTIONAL {
58#                 ?x rdfs:comment|dcterms:description|skos:definition ?def
59#             }
60#
61#             # only get results for the targetted namespace (supplied by user)
62#             FILTER STRSTARTS(STR(?x), "xxx")
63#         }
64#         """.replace("xxx", target_namespace)
65#     classes = []
66#     for r in g.query(q, initNs=namespaces):
67#         classes.append((str(r[0]), str(r[1])))
68#
69#     classes.sort(key=lambda tup: tup[1])
70#
71#     return classes
72
73
74def get_target_namespace_elements(
75    g: Graph, target_namespace: str
76) -> Tuple[List[Tuple[str, str]], List[str], List[str]]:
77    namespaces = {"dcterms": DCTERMS, "owl": OWL, "rdfs": RDFS, "skos": SKOS}
78    q = """
79        SELECT ?s (GROUP_CONCAT(DISTINCT STR(?def)) AS ?defs)
80        WHERE {
81            # all things in the RDF data (anything RDF.type...)
82            ?s a ?o .
83
84            # get any definitions, if they have one
85            OPTIONAL {
86                ?s dcterms:description|rdfs:comment|skos:definition ?def
87            }
88
89            # only get results for the target namespace (supplied by user)
90            FILTER STRSTARTS(STR(?s), "xxx")
91            FILTER (STR(?s) != "xxx")
92        }
93        GROUP BY ?s
94        """.replace(
95        "xxx", target_namespace
96    )
97    elements: List[Tuple[str, str]] = []
98    for r in g.query(q, initNs=namespaces):
99        if TYPE_CHECKING:
100            assert isinstance(r, ResultRow)
101        elements.append((str(r[0]), str(r[1])))
102
103    elements.sort(key=lambda tup: tup[0])
104
105    elements_strs: List[str] = []
106    non_python_elements_strs: List[str] = []
107    for e in elements:
108        name = e[0].replace(target_namespace, "")
109        desc = e[1].replace("\n", " ")
110        if name.isidentifier() and not keyword.iskeyword(name):
111            elements_strs.append(f"    {name}: URIRef  # {desc}\n")
112        else:
113            non_python_elements_strs.append(f"""        "{name}",  # {desc}\n""")
114
115    return elements, elements_strs, non_python_elements_strs
116
117
118def make_dn_file(
119    output_file_name: Path,
120    target_namespace: str,
121    elements_strs: Iterable[str],
122    non_python_elements_strs: List[str],
123    object_id: str,
124    fail: bool,
125) -> None:
126    header = f'''from rdflib.namespace import DefinedNamespace, Namespace
127from rdflib.term import URIRef
128
129
130class {object_id}(DefinedNamespace):
131    """
132    DESCRIPTION_EDIT_ME_!
133
134    Generated from: SOURCE_RDF_FILE_EDIT_ME_!
135    Date: {datetime.datetime.utcnow()}
136    """
137'''
138    with open(output_file_name, "w") as f:
139        f.write(header)
140        f.write("\n")
141        f.write(f'    _NS = Namespace("{target_namespace}")')
142        f.write("\n\n")
143        if fail:
144            f.write("    _fail = True")
145            f.write("\n\n")
146        f.writelines(elements_strs)
147
148        if len(non_python_elements_strs) > 0:
149            f.write("\n")
150            f.write("    # Valid non-python identifiers")
151            f.write("\n")
152            f.write("    _extras = [")
153            f.write("\n")
154            f.writelines(non_python_elements_strs)
155            f.write("    ]")
156            f.write("\n")
157
158
159if __name__ == "__main__":
160    parser = argparse.ArgumentParser()
161
162    parser.add_argument(
163        "ontology_file",
164        type=str,
165        help="Path to the RDF ontology to extract a DefinedNamespace from.",
166    )
167
168    parser.add_argument(
169        "target_namespace",
170        type=str,
171        help="The namespace within the ontology that you want to create a "
172        "DefinedNamespace for.",
173    )
174
175    parser.add_argument(
176        "object_id",
177        type=str,
178        help="The RDFlib object ID of the DefinedNamespace, e.g. GEO for GeoSPARQL.",
179    )
180
181    parser.add_argument(
182        "-f",
183        "--fail",
184        dest="fail",
185        action="store_true",
186        help="Whether (true) or not (false) to mimic ClosedNamespace and fail on "
187        "non-element use",
188    )
189    parser.add_argument("--no-fail", dest="fail", action="store_false")
190    parser.set_defaults(feature=False)
191
192    args = parser.parse_args()
193
194    fmt = guess_format(args.ontology_file)
195    if fmt is None:
196        print("The format of the file you've supplied is unknown.")
197        exit(1)
198    g = Graph().parse(args.ontology_file, format=fmt)
199
200    validate_namespace(args.target_namespace)
201
202    validate_object_id(args.object_id)
203
204    print(
205        f"Creating DefinedNamespace file {args.object_id} "
206        f"for {args.target_namespace}..."
207    )
208    print(f"Ontology with {len(g)} triples loaded...")
209
210    print("Getting all namespace elements...")
211    elements = get_target_namespace_elements(g, args.target_namespace)
212
213    output_file_name = Path().cwd() / f"_{args.object_id}.py"
214    print(f"Creating DefinedNamespace Python file {output_file_name}")
215    make_dn_file(
216        output_file_name,
217        args.target_namespace,
218        elements[1],
219        elements[2],
220        args.object_id,
221        args.fail,
222    )