Skip to content

Commit 0e002c5

Browse files
committed
fix: overrides default rdflib.Graph and adds preferredLabel method
1 parent a9aaa8f commit 0e002c5

File tree

4 files changed

+92
-37
lines changed

4 files changed

+92
-37
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ dependencies = [
2121
"gunicorn>=23.0.0",
2222
"psycopg2-binary>=2.9.10",
2323
"pygithub>=2.7.0",
24-
"rdflib==6.1",
24+
"rdflib>=7.1.4",
2525
]
2626

2727
[dependency-groups]

uv.lock

Lines changed: 4 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vocabs/skos_import.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from django.contrib.auth.models import User
55
from django.core.exceptions import ObjectDoesNotExist
66
from django.db import transaction
7-
from rdflib import RDF, SKOS, Graph, Namespace, URIRef
7+
from rdflib import RDF, SKOS, Namespace, URIRef
88

99
from .models import (
1010
SKOS_RELATION_TYPES,
@@ -21,6 +21,7 @@
2121
SkosConcept,
2222
SkosConceptScheme,
2323
)
24+
from .utils import MyGraph as Graph
2425

2526
logging.getLogger().setLevel(logging.INFO)
2627

vocabs/utils.py

Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,105 @@
11
import os
2+
23
from django.conf import settings
3-
from github import Github
4-
from github import InputGitTreeElement
4+
from github import Github, InputGitTreeElement
5+
from rdflib import Graph, namespace
6+
7+
8+
class MyGraph(Graph):
9+
def preferredLabel(
10+
self,
11+
subject,
12+
lang=None,
13+
default=None,
14+
labelProperties=(namespace.SKOS.prefLabel, namespace.RDFS.label),
15+
):
16+
"""
17+
Find the preferred label for subject.
18+
By default prefers skos:prefLabels over rdfs:labels. In case at least
19+
one prefLabel is found returns those, else returns labels. In case a
20+
language string (e.g., "en", "de" or even "" for no lang-tagged
21+
literals) is given, only such labels will be considered.
22+
Return a list of (labelProp, label) pairs, where labelProp is either
23+
skos:prefLabel or rdfs:label.
24+
>>> from rdflib import ConjunctiveGraph, URIRef, Literal, namespace
25+
>>> from pprint import pprint
26+
>>> g = ConjunctiveGraph()
27+
>>> u = URIRef("http://example.com/foo")
28+
>>> g.add([u, namespace.RDFS.label, Literal("foo")]) # doctest: +ELLIPSIS
29+
<Graph identifier=... (<class 'rdflib.graph.ConjunctiveGraph'>)>
30+
>>> g.add([u, namespace.RDFS.label, Literal("bar")]) # doctest: +ELLIPSIS
31+
<Graph identifier=... (<class 'rdflib.graph.ConjunctiveGraph'>)>
32+
>>> pprint(sorted(g.preferredLabel(u)))
33+
[(rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#label'),
34+
rdflib.term.Literal('bar')),
35+
(rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#label'),
36+
rdflib.term.Literal('foo'))]
37+
>>> g.add([u, namespace.SKOS.prefLabel, Literal("bla")]) # doctest: +ELLIPSIS
38+
<Graph identifier=... (<class 'rdflib.graph.ConjunctiveGraph'>)>
39+
>>> pprint(g.preferredLabel(u))
40+
[(rdflib.term.URIRef('http://www.w3.org/2004/02/skos/core#prefLabel'),
41+
rdflib.term.Literal('bla'))]
42+
>>> g.add([u, namespace.SKOS.prefLabel, Literal("blubb", lang="en")]) # doctest: +ELLIPSIS
43+
<Graph identifier=... (<class 'rdflib.graph.ConjunctiveGraph'>)>
44+
>>> sorted(g.preferredLabel(u)) #doctest: +NORMALIZE_WHITESPACE
45+
[(rdflib.term.URIRef('http://www.w3.org/2004/02/skos/core#prefLabel'),
46+
rdflib.term.Literal('bla')),
47+
(rdflib.term.URIRef('http://www.w3.org/2004/02/skos/core#prefLabel'),
48+
rdflib.term.Literal('blubb', lang='en'))]
49+
>>> g.preferredLabel(u, lang="") #doctest: +NORMALIZE_WHITESPACE
50+
[(rdflib.term.URIRef('http://www.w3.org/2004/02/skos/core#prefLabel'),
51+
rdflib.term.Literal('bla'))]
52+
>>> pprint(g.preferredLabel(u, lang="en"))
53+
[(rdflib.term.URIRef('http://www.w3.org/2004/02/skos/core#prefLabel'),
54+
rdflib.term.Literal('blubb', lang='en'))]
55+
"""
56+
if default is None:
57+
default = []
58+
59+
# setup the language filtering
60+
if lang is not None:
61+
if lang == "": # we only want not language-tagged literals
62+
63+
def langfilter(l_):
64+
return l_.language is None
65+
66+
else:
67+
68+
def langfilter(l_):
69+
return l_.language == lang
70+
71+
else: # we don't care about language tags
72+
73+
def langfilter(l_):
74+
return True
75+
76+
for labelProp in labelProperties:
77+
labels = list(filter(langfilter, self.objects(subject, labelProp)))
78+
if len(labels) == 0:
79+
continue
80+
else:
81+
return [(labelProp, l_) for l_ in labels]
82+
return default
583

684

785
def push_to_gh(
886
files,
987
ghpat=settings.GHPAT,
1088
repo_name=settings.GHREPO,
1189
branch="master",
12-
commit_message="some message"
90+
commit_message="some message",
1391
):
1492
g = Github(ghpat)
1593
repo = g.get_repo(repo_name)
16-
master_ref = repo.get_git_ref(f'heads/{branch}')
94+
master_ref = repo.get_git_ref(f"heads/{branch}")
1795
master_sha = master_ref.object.sha
1896
base_tree = repo.get_git_tree(master_sha)
1997
element_list = list()
2098
for entry in files:
2199
_, file_name = os.path.split(entry)
22100
with open(entry) as input_file:
23101
data = input_file.read()
24-
element = InputGitTreeElement(f"dumps/{file_name}", '100644', 'blob', data)
102+
element = InputGitTreeElement(f"dumps/{file_name}", "100644", "blob", data)
25103
element_list.append(element)
26104
tree = repo.create_git_tree(element_list, base_tree)
27105
parent = repo.get_git_commit(master_sha)
@@ -44,12 +122,8 @@ def push_to_gh(
44122

45123
def handle_uploaded_file(file):
46124
file_name = file.name
47-
full_file_name = os.path.join(
48-
settings.MEDIA_ROOT,
49-
'uploads',
50-
file_name
51-
)
52-
with open(full_file_name, 'wb+') as destination:
125+
full_file_name = os.path.join(settings.MEDIA_ROOT, "uploads", file_name)
126+
with open(full_file_name, "wb+") as destination:
53127
for chunk in file.chunks():
54128
destination.write(chunk)
55129
return full_file_name

0 commit comments

Comments
 (0)