Skip to content

Commit

Permalink
Merge pull request #101 from lambdamusic/entities-title-option
Browse files Browse the repository at this point in the history
V 1.9.9
  • Loading branch information
lambdamusic authored Sep 27, 2021
2 parents 2ff11c9 + 4f4b0cd commit 7d8a114
Show file tree
Hide file tree
Showing 62 changed files with 3,270 additions and 2,164 deletions.
4 changes: 2 additions & 2 deletions ontospy/VERSION.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
# note: 1.8 major update with ontodocs split
##################

__version__ = "1.9.8.3" # Pypi latest: https://pypi.org/project/ontospy/
__version__ = "1.9.9" # Pypi latest: https://pypi.org/project/ontospy/

__copyright__ = "CopyRight (C) 2015-2018 by Michele Pasin"
__copyright__ = "CopyRight (C) 2015-2021 by Michele Pasin"
__license__ = "GNU"
__author__ = "Michele Pasin"
__author_email__ = "michele dot pasin at gmail dot com"
Expand Down
2 changes: 1 addition & 1 deletion ontospy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

from .VERSION import __version__, VERSION
from .core.ontospy import Ontospy
from .core.entities import RDF_Entity, Ontology, OntoClass, OntoProperty, OntoSKOSConcept, OntoShape
from .core.entities import RdfEntity, Ontology, OntoClass, OntoProperty, OntoSKOSConcept, OntoShape
18 changes: 18 additions & 0 deletions ontospy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ def scan(ctx, sources=None, endpoint=False, raw=False, extra=False):
help=
'THEME: select bootstrap style (only for the html-multi-page visualization). Default: simplex (random=use a random theme).'
)
@click.option(
'--preflabel',
help='PREF-LABEL: default value to use for entity titles (qname|label - default=qname).')
@click.option(
'--preflang',
help=
'PREF-LANGUAGE: default language for multilingual strings (default=en).'
)
@click.option(
'--nobrowser',
is_flag=True,
Expand All @@ -218,6 +226,8 @@ def gendocs(ctx,
type="",
title="",
theme="",
preflabel="",
preflang="",
nobrowser=False,
showthemes=False,
showtypes=False):
Expand Down Expand Up @@ -256,6 +266,12 @@ def gendocs(ctx,
if theme and theme == "random":
theme = random_theme()

if preflabel and preflabel not in ["label", "qname"]:
click.secho(
"WARNING: the valid preflabel options are either 'qname' or 'label' (= rdfs:label) only. Using defaults.",
fg="red")
preflabel = "qname"

if outputpath:
if not (os.path.exists(outputpath)) or not (os.path.isdir(outputpath)):
click.secho(
Expand Down Expand Up @@ -283,6 +299,8 @@ def gendocs(ctx,
title=title,
viztype=type,
theme=theme,
preflabel=preflabel,
preflang=preflang,
verbose=verbose)

if url and (not nobrowser): # open browser
Expand Down
6 changes: 4 additions & 2 deletions ontospy/core/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,8 @@ def action_visualize(args,
title="",
viztype="",
theme="",
preflabel="",
preflang="",
verbose=False):
"""
export model into another format eg html, d3 etc...
Expand Down Expand Up @@ -663,9 +665,9 @@ def action_visualize(args,
# 2017-01-23: bypass pickled stuff as it has wrong counts etc..
# get ontospy graph
printDebug("Loading graph...", dim=True)
g = Ontospy(ontouri, verbose=verbose)
g = Ontospy(ontouri, verbose=verbose, pref_title=preflabel, pref_lang=preflang)

# put in home folder by default: <ontouri>/<viztype>/files..
# put viz in home folder by default: <ontouri>/<viztype>/files..
if not path:
from os.path import expanduser
home = expanduser("~")
Expand Down
88 changes: 62 additions & 26 deletions ontospy/core/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .utils import *


class RDF_Entity(object):
class RdfEntity(object):
"""
Pythonic representation of an RDF resource - normally not instantiated but used for
inheritance purposes
Expand All @@ -30,14 +30,17 @@ class RDF_Entity(object):
_ids = count(0)

def __repr__(self):
return "<Ontospy: RDF_Entity object for uri *%s*>" % (self.uri)
return "<Ontospy: RdfEntity object for uri *%s*>" % (self.uri)

def __init__(self,
uri,
rdftype=None,
namespaces=None,
ext_model=False,
is_Bnode=False):
is_Bnode=False,
pref_title="qname",
pref_lang="en",
):
"""
Init ontology object. Load the graph in memory, then setup all necessary attributes.
Expand All @@ -53,6 +56,9 @@ def __init__(self,
self.locale = inferURILocalSymbol(self.uri)[0]
self.ext_model = ext_model
self.is_Bnode = is_Bnode
self._pref_title = pref_title
self._pref_lang = pref_lang

self.slug = None
self.rdftype = rdftype
self.triples = None
Expand Down Expand Up @@ -109,7 +115,7 @@ def _buildGraph(self):
for terzetto in self.triples:
self.rdflib_graph.add(terzetto)

# methods added to RDF_Entity even though they apply only to some subs
# methods added to RdfEntity even though they apply only to some subs

def ancestors(self, cl=None, noduplicates=True):
""" returns all ancestors in the taxonomy """
Expand Down Expand Up @@ -169,7 +175,7 @@ def getValuesForProperty(self, aPropURIRef):
aPropURIRef = rdflib.URIRef(aPropURIRef)
return list(self.rdflib_graph.objects(None, aPropURIRef))

def bestLabel(self, prefLanguage="en", qname_allowed=True, quotes=False):
def bestLabel(self, prefLanguage="", qname_allowed=True, quotes=False):
"""
facility for extrating the best available label for an entity
Expand All @@ -179,6 +185,9 @@ def bestLabel(self, prefLanguage="en", qname_allowed=True, quotes=False):
test = self.getValuesForProperty(rdflib.RDFS.label)
out = ""

if not prefLanguage:
prefLanguage = self._pref_lang

if test:
out = firstStringInList(test, prefLanguage)
else:
Expand All @@ -194,7 +203,7 @@ def bestLabel(self, prefLanguage="en", qname_allowed=True, quotes=False):
else:
return out

def bestDescription(self, prefLanguage="en", quotes=False):
def bestDescription(self, prefLanguage="", quotes=False):
"""
facility for extracting a human readable description for an entity
"""
Expand All @@ -204,6 +213,9 @@ def bestDescription(self, prefLanguage="en", quotes=False):
rdflib.namespace.DC.description, rdflib.namespace.SKOS.definition
]

if not prefLanguage:
prefLanguage = self._pref_lang

for pred in test_preds:
test = self.getValuesForProperty(pred)
# printDebug(str(test), "red")
Expand All @@ -214,8 +226,27 @@ def bestDescription(self, prefLanguage="en", quotes=False):
return joinStringsInList(test, prefLanguage)
return ""

@property
def title(self):
"""Entity title - used for display purposes only.
Can be set by user once ontospy is created.
Values allowed: 'qname' or 'label'
Defaults to 'qname'.
"""

if self._pref_title == "qname":
out = self.qname
elif self._pref_title == "label":
out = self.bestLabel()
else:
return self.qname

class Ontology(RDF_Entity):
return out



class Ontology(RdfEntity):
"""
Pythonic representation of an OWL ontology
"""
Expand All @@ -227,14 +258,17 @@ def __init__(self,
uri,
rdftype=None,
namespaces=None,
prefPrefix="",
ext_model=False):
pref_prefix="",
ext_model=False,
pref_title="qname",
pref_lang="en",
):
"""
Init ontology object. Load the graph in memory, then setup all necessary attributes.
"""
super(Ontology, self).__init__(uri, rdftype, namespaces, ext_model)
super().__init__(uri, rdftype, namespaces, ext_model, pref_title=pref_title, pref_lang=pref_lang)
# self.uri = uri # rdflib.Uriref
self.prefix = prefPrefix
self.prefix = pref_prefix
self.slug = "ontology-" + slugify(self.qname)
self.all_classes = []
self.all_properties = []
Expand Down Expand Up @@ -265,7 +299,7 @@ def stats(self):
printDebug("Properties..: %d" % len(self.all_properties))


class OntoClass(RDF_Entity):
class OntoClass(RdfEntity):
"""
Python representation of a generic class within an ontology.
Includes methods for representing and querying RDFS/OWL classes
Expand All @@ -280,13 +314,14 @@ class OntoClass(RDF_Entity):
]
"""

def __init__(self, uri, rdftype=None, namespaces=None, ext_model=False):
def __init__(self, uri, rdftype=None, namespaces=None,
ext_model=False, pref_title="qname", pref_lang="en"):
"""
...
"""
super(OntoClass, self).__init__(uri, rdftype, namespaces, ext_model)
super().__init__(uri, rdftype, namespaces, ext_model,
pref_title=pref_title, pref_lang=pref_lang)
self.slug = "class-" + slugify(self.qname)

self.domain_of = []
self.range_of = []
self.domain_of_inferred = []
Expand All @@ -308,7 +343,7 @@ def instances(self): # = all instances
if self.sparqlHelper:
qres = self.sparqlHelper.getClassInstances(self.uri)
for uri in [x[0] for x in qres]:
instance = RDF_Entity(uri, self.uri, self.namespaces)
instance = RdfEntity(uri, self.uri, self.namespaces)
instance.triples = self.sparqlHelper.entityTriples(
instance.uri)
instance._buildGraph() # force construction of mini graph
Expand Down Expand Up @@ -345,7 +380,7 @@ def describe(self):
# self.printGenericTree()


class OntoProperty(RDF_Entity):
class OntoProperty(RdfEntity):
"""
Python representation of a generic RDF/OWL property.
Expand All @@ -357,11 +392,12 @@ class OntoProperty(RDF_Entity):
"""

def __init__(self, uri, rdftype=None, namespaces=None, ext_model=False):
def __init__(self, uri, rdftype=None, namespaces=None, ext_model=False,
pref_title="qname", pref_lang="en"):
"""
...
"""
super(OntoProperty, self).__init__(uri, rdftype, namespaces, ext_model)
super().__init__(uri, rdftype, namespaces, ext_model, pref_title=pref_title, pref_lang=pref_lang)

self.slug = "prop-" + slugify(self.qname)
self.rdftype = inferMainPropertyType(rdftype)
Expand Down Expand Up @@ -395,19 +431,19 @@ def describe(self):
# self.printGenericTree()


class OntoSKOSConcept(RDF_Entity):
class OntoSKOSConcept(RdfEntity):
"""
Python representation of a generic SKOS concept within an ontology.
@todo: complete methods..
"""

def __init__(self, uri, rdftype=None, namespaces=None, ext_model=False):
def __init__(self, uri, rdftype=None, namespaces=None, ext_model=False, pref_title="qname", pref_lang="en"):
"""
...
"""
super(OntoSKOSConcept, self).__init__(uri, rdftype, namespaces,
ext_model)
super().__init__(uri, rdftype, namespaces,
ext_model, pref_title=pref_title, pref_lang=pref_lang)
self.slug = "concept-" + slugify(self.qname)
self.instance_of = []
self.ontology = None
Expand Down Expand Up @@ -436,17 +472,17 @@ def describe(self):
self.printGenericTree()


class OntoShape(RDF_Entity):
class OntoShape(RdfEntity):
"""
Python representation of a SHACL shape.
"""

def __init__(self, uri, rdftype=None, namespaces=None, ext_model=False):
def __init__(self, uri, rdftype=None, namespaces=None, ext_model=False, pref_title="qname", pref_lang="en"):
"""
...
"""
super(OntoShape, self).__init__(uri, rdftype, namespaces, ext_model)
super().__init__(uri, rdftype, namespaces, ext_model, pref_title=pref_title, pref_lang=pref_lang)
self.slug = "shape-" + slugify(self.qname)
self.ontology = None
self.targetClasses = []
Expand Down
Loading

0 comments on commit 7d8a114

Please sign in to comment.