Skip to content

Commit

Permalink
LDPR instantiation improvements:
Browse files Browse the repository at this point in the history
* Reduce variables passed and inherited
* Change all `repr_opts` and `retr_options` variable names to `repr_options`
* Load inbound triples in LDP-NR
  • Loading branch information
scossu committed Feb 22, 2020
1 parent 5dea35d commit 6b71d46
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 128 deletions.
2 changes: 1 addition & 1 deletion lakesuperior/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def setup(self, config_dir=None, config=None):
manually before passing it to the setup.
"""
if hasattr(self, 'app_globals'):
logger.warn('The environment is already set up.')
logger.warning('The environment is already set up.')
return

if not config:
Expand Down
6 changes: 3 additions & 3 deletions lakesuperior/api/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def get(uid, repr_options={}):
- incl_children: include children URIs. Default: True.
- embed_children: Embed full graph of all child resources. Default: False
"""
rsrc = LdpFactory.from_stored(uid, repr_opts=repr_options)
rsrc = LdpFactory.from_stored(uid, repr_options=repr_options)
# Load graph before leaving the transaction.
rsrc.imr

Expand Down Expand Up @@ -327,8 +327,8 @@ def delete(uid, soft=True, inbound=True):
inbound = True if refint else inbound

if soft:
repr_opts = {'incl_inbound' : True} if inbound else {}
rsrc = LdpFactory.from_stored(uid, repr_opts)
repr_options = {'incl_inbound' : True} if inbound else {}
rsrc = LdpFactory.from_stored(uid, repr_options)
return rsrc.bury(inbound)
else:
Ldpr.forget(uid, inbound)
Expand Down
30 changes: 16 additions & 14 deletions lakesuperior/endpoints/ldp.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,10 @@ def _create_args_from_req(uid):
#logger.debug('stream: {}'.format(request.stream))
#pdb.set_trace()

kwargs = {}
kwargs['handling'], kwargs['disposition'] = _set_post_put_params()
handling, disposition = _set_post_put_params()
kwargs = {'handling': handling}
if disposition:
kwargs['disposition'] = disposition

link_hdr = request.headers.get('Link')
if link_hdr:
Expand Down Expand Up @@ -653,7 +655,7 @@ def _set_post_put_params():
return handling, disposition


def parse_repr_options(retr_opts, out_headers):
def parse_repr_options(repr_options, out_headers):
"""
Set options to retrieve IMR.
Expand All @@ -672,12 +674,12 @@ def parse_repr_options(retr_opts, out_headers):
All options above are ``False`` by default except for ``incl_srv_mgd``
which is only ``False`` if the ``return`` representation is ``minimal``.
:param dict retr_opts:: Options parsed from `Prefer` header.
:param dict repr_options:: Options parsed from `Prefer` header.
:param dict out_headers:: Response headers.
"""
logger.debug('Parsing retrieval options: {}'.format(retr_opts))
logger.debug('Parsing retrieval options: {}'.format(repr_options))

if retr_opts.get('value') == 'minimal':
if repr_options.get('value') == 'minimal':
imr_options = {
'embed_children' : False,
'incl_children' : False,
Expand All @@ -695,9 +697,9 @@ def parse_repr_options(retr_opts, out_headers):
}

# Override defaults.
if 'parameters' in retr_opts:
if 'parameters' in repr_options:
try:
pref_imr_options = _valid_preferences(retr_opts)
pref_imr_options = _valid_preferences(repr_options)
include = list()
omit = list()
for k, v in pref_imr_options.items():
Expand Down Expand Up @@ -745,20 +747,20 @@ def _preference_decision(include, omit, header):
return None


def _valid_preferences(retr_opts):
def _valid_preferences(repr_options):
"""
Parse the Preference header to determine which we are applying.
Re-used for response Preference-Applied header.
:param retr_opts: The incoming Preference header.
:param repr_options: The incoming Preference header.
:return: list of options being applied.
"""
imr_options = dict()
include = retr_opts['parameters']['include'].split(' ') \
if 'include' in retr_opts['parameters'] else []
omit = retr_opts['parameters']['omit'].split(' ') \
if 'omit' in retr_opts['parameters'] else []
include = repr_options['parameters']['include'].split(' ') \
if 'include' in repr_options['parameters'] else []
omit = repr_options['parameters']['omit'].split(' ') \
if 'omit' in repr_options['parameters'] else []

logger.debug('Include: {}'.format(include))
logger.debug('Omit: {}'.format(omit))
Expand Down
4 changes: 2 additions & 2 deletions lakesuperior/model/ldp/ldp_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def new_container(uid):


@staticmethod
def from_stored(uid, ver_label=None, repr_opts={}, strict=True, **kwargs):
def from_stored(uid, ver_label=None, repr_options={}, strict=True, **kwargs):
"""
Create an instance for retrieval purposes.
Expand All @@ -65,7 +65,7 @@ def from_stored(uid, ver_label=None, repr_opts={}, strict=True, **kwargs):
else:
raise exc.ResourceNotExistsError(uid)

rsrc = cls(uid, repr_opts, **kwargs)
rsrc = cls(uid, repr_options, **kwargs)
# Sneak in the already extracted metadata to save a query.
rsrc._metadata = rsrc_meta

Expand Down
30 changes: 11 additions & 19 deletions lakesuperior/model/ldp/ldp_nr.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,20 @@ class LdpNr(Ldpr):
Definition: https://www.w3.org/TR/ldp/#ldpnr
"""

base_types = {
nsc['fcrepo'].Binary,
nsc['fcrepo'].Resource,
nsc['ldp'].Resource,
nsc['ldp'].NonRDFSource,
}

def __init__(self, uuid, stream=None, mimetype=None,
disposition=None, prov_cksum_algo=None, prov_cksum=None,
**kwargs):
def __init__(self, *args, stream=None, mimetype=None, disposition=None,
prov_cksum_algo=None, prov_cksum=None, **kwargs):
"""
Extends Ldpr.__init__ by adding LDP-NR specific parameters.
Extends :meth:`lakesuperior.model.Ldpr.__init__` by adding LDP-NR
specific parameters.
"""
super().__init__(uuid, **kwargs)
super().__init__(*args, **kwargs)

self._imr_options = {}
if stream:
self.workflow = self.WRKF_INBOUND
self.stream = stream
else:
self.workflow = self.WRKF_OUTBOUND
self.base_types = super().base_types | {
nsc['fcrepo'].Binary,
nsc['ldp'].NonRDFSource,
}

self.stream = stream
if mimetype:
self.mimetype = mimetype
else:
Expand All @@ -52,9 +44,9 @@ def __init__(self, uuid, stream=None, mimetype=None,
if self.is_stored
else 'application/octet-stream')

self.disposition = disposition
self.prov_cksum_algo = prov_cksum_algo
self.prov_cksum = prov_cksum
self.disposition = disposition


@property
Expand Down
43 changes: 15 additions & 28 deletions lakesuperior/model/ldp/ldp_rs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,24 @@ class LdpRs(Ldpr):
https://www.w3.org/TR/ldp/#ldprs
"""
def __init__(self, uuid, repr_opts={}, handling='lenient', **kwargs):
"""
Extends :meth:`Ldpr.__init__`by adding LDP-RS specific parameters.

:param str handling: One of ``strict``, ``lenient`` (the default) or
``none``. ``strict`` raises an error if a server-managed term is in the
graph. ``lenient`` removes all sever-managed triples encountered.
``none`` skips all server-managed checks. It is used for internal
modifications.
def __init__(self, *args, **kwargs):
"""
Extends :meth:`lakesuperior.model.Ldpr.__init__` by adding LDP-RS
specific parameters.
"""
super().__init__(uuid, **kwargs)
super().__init__(*args, **kwargs)

self.base_types = super().base_types | {
nsc['fcrepo'].Container,
nsc['ldp'].Container,
nsc['ldp'].RDFSource,
}

# provided_imr can be empty. If None, it is an outbound resource.
if self.provided_imr is not None:
self.workflow = self.WRKF_INBOUND
else:
self.workflow = self.WRKF_OUTBOUND
self._imr_options = repr_opts

self.handling = handling



class Ldpc(LdpRs):
"""LDPC (LDP Container)."""
def __init__(self, uuid, *args, **kwargs):
super().__init__(uuid, *args, **kwargs)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.base_types |= {
nsc['fcrepo'].Container,
nsc['ldp'].Container,
Expand All @@ -56,8 +43,8 @@ def __init__(self, uuid, *args, **kwargs):

class LdpBc(Ldpc):
"""LDP-BC (LDP Basic Container)."""
def __init__(self, uuid, *args, **kwargs):
super().__init__(uuid, *args, **kwargs)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.base_types |= {
nsc['ldp'].BasicContainer,
}
Expand All @@ -66,8 +53,8 @@ def __init__(self, uuid, *args, **kwargs):

class LdpDc(Ldpc):
"""LDP-DC (LDP Direct Container)."""
def __init__(self, uuid, *args, **kwargs):
super().__init__(uuid, *args, **kwargs)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.base_types |= {
nsc['ldp'].DirectContainer,
}
Expand All @@ -76,8 +63,8 @@ def __init__(self, uuid, *args, **kwargs):

class LdpIc(Ldpc):
"""LDP-IC (LDP Indirect Container)."""
def __init__(self, uuid, *args, **kwargs):
super().__init__(uuid, *args, **kwargs)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.base_types |= {
nsc['ldp'].IndirectContainer,
}
Expand Down
33 changes: 18 additions & 15 deletions lakesuperior/model/ldp/ldpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ class Ldpr(metaclass=ABCMeta):
base_types = {
nsc['fcrepo'].Resource,
nsc['ldp'].Resource,
nsc['ldp'].RDFSource,
}
"""RDF Types that populate a new resource."""

Expand Down Expand Up @@ -155,21 +154,30 @@ class Ldpr(metaclass=ABCMeta):
These are used by setters and can be cleared with :py:meth:`_clear_cache`.
"""

# This gets overridden by LDP-NR.
mimetype = None

## MAGIC METHODS ##

def __init__(self, uid, repr_opts={}, provided_imr=None, **kwargs):
def __init__(
self, uid, repr_options={}, provided_imr=None, handling='lenient'):
"""
Instantiate an in-memory LDP resource.
:param str uid: uid of the resource. If None (must be explicitly
set) it refers to the root node. It can also be the full URI or
URN, in which case it will be converted.
:param dict repr_opts: Options used to retrieve the IMR. See
:param dict repr_options: Options used to retrieve the IMR. See
:py:meth:`~lakesuperior.endpoints.ldp.parse_repr_options` for
format details.
:param str provided_imr: RDF data provided by the client in
operations such as `PUT` or `POST`, serialized as a string. This
sets the :py:data:`~Ldpr.provided_imr` property.
:param str handling: One of ``strict``, ``lenient`` (the default) or
``none``. ``strict`` raises an error if a server-managed term is in
the graph. ``lenient`` removes all sever-managed triples
encountered. ``none`` skips all server-managed checks. It is used
for internal modifications.
"""
self.uid = (
rdfly.uri_to_uid(uid) if isinstance(uid, URIRef) else uid)
Expand All @@ -179,10 +187,8 @@ def __init__(self, uid, repr_opts={}, provided_imr=None, **kwargs):

self.provided_imr = provided_imr

# This gets overridden by LDP-NR.
self.mimetype = None

# Disable all internal checks e.g. for raw I/O.
self._imr_options = repr_options
self.handling = handling


#@property
Expand Down Expand Up @@ -224,12 +230,9 @@ def imr(self):
is not stored (yet).
"""
if not hasattr(self, '_imr'):
if hasattr(self, '_imr_options'):
logger.debug(
'Getting RDF triples for resource {}'.format(self.uid))
imr_options = self._imr_options
else:
imr_options = {}
logger.debug(
'Getting RDF triples for resource {}'.format(self.uid))
imr_options = getattr(self, '_imr_options', {})
options = dict(imr_options, strict=True)
self._imr = rdfly.get_imr(self.uid, **options)

Expand Down Expand Up @@ -501,7 +504,7 @@ def bury(self, inbound, tstone_pointer=None):
try:
desc_rsrc = LdpFactory.from_stored(
env.app_globals.rdfly.uri_to_uid(desc_uri),
repr_opts={'incl_children' : False})
repr_options={'incl_children' : False})
except (TombstoneError, ResourceNotExistsError):
continue
desc_rsrc.bury(inbound, tstone_pointer=self.uri)
Expand Down Expand Up @@ -924,7 +927,7 @@ def _containment_rel(self, create, ignore_type=True):
parent_uid = ROOT_UID

parent_rsrc = LdpFactory.from_stored(
parent_uid, repr_opts={'incl_children': False}, handling='none')
parent_uid, repr_options={'incl_children': False}, handling='none')

# Only update parent if the resource is new.
if create:
Expand Down
Loading

0 comments on commit 6b71d46

Please sign in to comment.