diff --git a/pylode/cli.py b/pylode/cli.py index a23d7e05..d9c1e70e 100644 --- a/pylode/cli.py +++ b/pylode/cli.py @@ -34,6 +34,13 @@ default="true", ) +parser.add_argument( + "-s", + "--sort", + help="Enables sorting of the subjects in the ontology in the output", + action='store_true', +) + parser.add_argument( "-p", "--profile", @@ -50,12 +57,13 @@ def main(): args = parser.parse_args() try: + sort_subjects = args.sort if args.profile == "ontpub": - html = OntPub(args.input) + html = OntPub(args.input, sort_subjects) elif args.profile == "vocpub": - html = VocPub(args.input) + html = VocPub(args.input, sort_subjects) elif args.profile == "supermodel": - html = Supermodel(args.input) + html = Supermodel(args.input, sort_subjects) else: raise ValueError(f"Unexpected profile type '{args.profile}'") except PylodeError as e: diff --git a/pylode/profiles/ontpub.py b/pylode/profiles/ontpub.py index 3109a010..de0dcbec 100644 --- a/pylode/profiles/ontpub.py +++ b/pylode/profiles/ontpub.py @@ -56,6 +56,7 @@ get_ns, prop_obj_pair_html, section_html, + sort_ontology, make_pylode_logo, ) @@ -71,6 +72,7 @@ get_ns, prop_obj_pair_html, section_html, + sort_ontology, make_pylode_logo, ) @@ -102,8 +104,10 @@ class OntPub: od.make_html(destination="some-resulting-html-file.html") """ - def __init__(self, ontology: Union[Graph, Path, str]): + def __init__(self, ontology: Union[Graph, Path, str], sort_subjects: bool = False): self.ont = load_ontology(ontology) + if sort_subjects: + self.ont = sort_ontology(self.ont) self._ontdoc_inference(self.ont) self.back_onts = load_background_onts() self.back_onts_titles = load_background_onts_titles(self.back_onts) diff --git a/pylode/profiles/supermodel/html.py b/pylode/profiles/supermodel/html.py index ce669649..e1d08ffd 100644 --- a/pylode/profiles/supermodel/html.py +++ b/pylode/profiles/supermodel/html.py @@ -38,6 +38,7 @@ from pylode.utils import ( PylodeError, load_ontology, + sort_ontology, ) from pylode.profiles.supermodel.query import Query from pylode.profiles.supermodel.model import ( @@ -93,9 +94,11 @@ def get_headings(doc: dominate.document) -> list: class Supermodel: def __init__( - self, ontology: Graph | Path | str, QueryClass: Type[Query] = Query + self, ontology: Graph | Path | str, sort_subjects: bool = False, QueryClass: Type[Query] = Query ) -> None: self.ont = load_ontology(ontology) + if sort_subjects: + self.ont = sort_ontology(self.ont) self.query = QueryClass(self.ont) self.toc: dict[str, str] = {} diff --git a/pylode/profiles/vocpub.py b/pylode/profiles/vocpub.py index 00fbcae3..956c9cf0 100644 --- a/pylode/profiles/vocpub.py +++ b/pylode/profiles/vocpub.py @@ -55,6 +55,7 @@ get_ns, prop_obj_pair_html, section_html, + sort_ontology, make_pylode_logo, ) @@ -70,6 +71,7 @@ get_ns, prop_obj_pair_html, section_html, + sort_ontology, make_pylode_logo, ) @@ -105,8 +107,10 @@ class VocPub: od.make_html(destination="some-resulting-html-file.html") """ - def __init__(self, ontology: Union[Graph, Path, str]): + def __init__(self, ontology: Union[Graph, Path, str], sort_subjects: bool = False): self.ont = load_ontology(ontology) + if sort_subjects: + self.ont = sort_ontology(self.ont) self._ontdoc_inference(self.ont) self.back_onts = load_background_onts() self.back_onts_titles = load_background_onts_titles(self.back_onts) diff --git a/pylode/utils.py b/pylode/utils.py index 0ef908d5..d6bdaf15 100644 --- a/pylode/utils.py +++ b/pylode/utils.py @@ -288,6 +288,14 @@ def load_ontology(ontology: Union[Graph, Path, str]) -> Graph: print(f"{type(e).__name__} Error {e}") exit() +def sort_ontology(ont_orig: Graph) -> Graph: + """Creates a copy of the supplied ontology, sorted by subjects""" + trpls = ont_orig.triples((None, None, None)) + trpls_srt = sorted(trpls) + ont_sorted = Graph() + for trpl in trpls_srt: + ont_sorted.add(trpl) + return ont_sorted def load_background_onts(): """Loads background ontology files into an RDFLib graph from either