Skip to content

Commit

Permalink
Implements subject sorting in output with --sort
Browse files Browse the repository at this point in the history
  • Loading branch information
hoijui committed May 18, 2024
1 parent 9683238 commit e8c35d2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
14 changes: 11 additions & 3 deletions pylode/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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:
Expand Down
6 changes: 5 additions & 1 deletion pylode/profiles/ontpub.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
get_ns,
prop_obj_pair_html,
section_html,
sort_ontology,
make_pylode_logo,
)

Expand All @@ -71,6 +72,7 @@
get_ns,
prop_obj_pair_html,
section_html,
sort_ontology,
make_pylode_logo,
)

Expand Down Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion pylode/profiles/supermodel/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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] = {}
Expand Down
6 changes: 5 additions & 1 deletion pylode/profiles/vocpub.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
get_ns,
prop_obj_pair_html,
section_html,
sort_ontology,
make_pylode_logo,
)

Expand All @@ -70,6 +71,7 @@
get_ns,
prop_obj_pair_html,
section_html,
sort_ontology,
make_pylode_logo,
)

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions pylode/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e8c35d2

Please sign in to comment.