Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
cubicweb
cubicweb
Commits
eb0cd6060062
Commit
8e9a7e78
authored
Feb 14, 2020
by
Simon Chabot
Browse files
[rdf] add functions and tools to generate rdf graph
parent
0cd5b9057202
Changes
3
Hide whitespace changes
Inline
Side-by-side
cubicweb/entities/adapters.py
View file @
eb0cd606
...
...
@@ -23,7 +23,7 @@ from cubicweb import _
from
itertools
import
chain
from
logilab.mtconverter
import
TransformError
from
logilab.common.decorators
import
cached
from
logilab.common.decorators
import
cached
,
cachedproperty
from
cubicweb.entity
import
EntityAdapter
from
cubicweb
import
(
Unauthorized
,
ValidationError
,
ViolatedConstraint
,
...
...
@@ -32,6 +32,25 @@ from cubicweb.schema import constraint_name_for
from
cubicweb.predicates
import
is_instance
,
relation_possible
,
match_exception
class
EntityRDFAdapter
(
EntityAdapter
):
"""EntityRDFAdapter is to be specialized for each entity that wants to
be converted to RDF using the mechanism from cubicweb.rdf
"""
__abstract__
=
True
def
__init__
(
self
,
_cw
,
**
kwargs
):
super
().
__init__
(
_cw
,
**
kwargs
)
self
.
entity
.
complete
()
@
cachedproperty
def
uri
(
self
):
return
self
.
entity
.
cwuri
def
triples
(
self
):
"""return sequence of 3-tuple of rdflib identifiers"""
raise
NotImplementedError
()
class
IDublinCoreAdapter
(
EntityAdapter
):
__regid__
=
'IDublinCore'
__select__
=
is_instance
(
'Any'
)
...
...
cubicweb/rdf.py
0 → 100644
View file @
eb0cd606
# -*- coding: utf-8 -*-
# copyright 2019 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr -- mailto:contact@logilab.fr
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from
rdflib
import
Namespace
,
ConjunctiveGraph
,
plugin
import
rdflib_jsonld
# noqa
plugin
.
register
(
"jsonld"
,
plugin
.
Serializer
,
"rdflib_jsonld.serializer"
,
"JsonLDSerializer"
)
RDF_MIMETYPE_TO_FORMAT
=
{
'application/rdf+xml'
:
'xml'
,
'text/turtle'
:
'turtle'
,
'text/n3'
:
'n3'
,
'application/n-quads'
:
'nquads'
,
'application/n-triples'
:
'nt'
,
'application/trig'
:
'trig'
,
'application/ld+json'
:
'json-ld'
,
}
namespaces
=
{
"rdf"
:
"http://www.w3.org/1999/02/22-rdf-syntax-ns#"
,
"schema"
:
"http://schema.org/"
,
}
NS_VARS
=
{
ns
:
Namespace
(
uri
)
for
ns
,
uri
in
namespaces
.
items
()}
# dict: name of CWEType -> list of regid of adapters derived from EntityRDFAdapter
ETYPES_ADAPTERS
=
{
}
def
conjunctive_graph
():
"""factory to build a ``ConjunctiveGraph`` and bind all namespaces
"""
graph
=
ConjunctiveGraph
()
for
vocab
,
rdfns
in
NS_VARS
.
items
():
graph
.
bind
(
vocab
,
rdfns
)
return
graph
def
iter_rdf_adapters
(
entity
):
for
adapter_id
in
ETYPES_ADAPTERS
.
get
(
entity
.
__regid__
,
()):
adapter
=
entity
.
cw_adapt_to
(
adapter_id
)
if
adapter
:
yield
adapter
def
add_entity_to_graph
(
graph
,
entity
):
for
adapter
in
iter_rdf_adapters
(
entity
):
for
triple
in
adapter
.
triples
():
graph
.
add
(
triple
)
setup.py
View file @
eb0cd606
...
...
@@ -73,6 +73,8 @@ setup(
'pytz'
,
'Markdown'
,
'filelock'
,
'rdflib'
,
'rdflib-jsonld'
,
],
entry_points
=
{
'console_scripts'
:
[
...
...
@@ -102,10 +104,6 @@ setup(
'pyramid_multiauth'
,
'repoze.lru'
,
],
'rdf'
:
[
'rdflib'
,
'rdflib-jsonld'
,
],
'sparql'
:
[
'fyzz >= 0.1.0'
,
],
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment