Skip to content

Commit

Permalink
Merge f532e7d into 2ea4c8b
Browse files Browse the repository at this point in the history
  • Loading branch information
PonteIneptique committed Nov 6, 2018
2 parents 2ea4c8b + f532e7d commit 816bb3e
Show file tree
Hide file tree
Showing 94 changed files with 5,486 additions and 945 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,3 +1,5 @@
.ipynb_checkpoints/
_test.py
*.pkl
# Compiled python modules.
*.pyc
Expand Down
5 changes: 5 additions & 0 deletions MyCapytain/common/base.py
@@ -1,6 +1,11 @@
from inspect import getmro


__all__ = [
"Exportable"
]


class Exportable(object):
""" Objects that supports Export
Expand Down
13 changes: 12 additions & 1 deletion MyCapytain/common/constants.py
Expand Up @@ -2,6 +2,17 @@
from rdflib.namespace import SKOS


__all__ = [
"XPATH_NAMESPACES",
"RDF_NAMESPACES",
"Mimetypes",
"GRAPH_BINDINGS",
"bind_graph",
"get_graph",
"set_graph",
"RDFLIB_MAPPING"
]

#: List of XPath Namespaces used in guidelines
XPATH_NAMESPACES = {
"tei": "http://www.tei-c.org/ns/1.0",
Expand Down Expand Up @@ -93,7 +104,7 @@ class PYTHON:
class MyCapytain:
""" MyCapytain Objects
:cvar ReadableText: MyCapytain.resources.prototypes.text.CitableText
:cvar ReadableText: MyCapytain.resources.prototypes.text.CtsText
"""
TextualElement = "Capitains/TextualElement"

Expand Down
26 changes: 24 additions & 2 deletions MyCapytain/common/metadata.py
Expand Up @@ -7,11 +7,16 @@
"""
from __future__ import unicode_literals
from MyCapytain.common.utils import make_xml_node
from MyCapytain.common.utils.xml import make_xml_node
from MyCapytain.common.constants import Mimetypes, get_graph
from MyCapytain.common.base import Exportable
from rdflib import BNode, Literal, Graph, URIRef, term
from typing import Union, Optional


__all__ = [
"Metadata"
]


class Metadata(Exportable):
Expand Down Expand Up @@ -47,6 +52,23 @@ def graph(self):
"""
return self.__graph__

def set(self, key: URIRef, value: Union[Literal, BNode, URIRef, str, int], lang: Optional[str]=None):
""" Set the VALUE for KEY predicate in the Metadata Graph
:param key: Predicate to be set (eg. DCT.creator)
:param value: Value to be stored (eg. "Cicero")
:param lang: [Optional] Language of the value (eg. "la")
"""
if not isinstance(value, Literal) and lang is not None:
value = Literal(value, lang=lang)
elif not isinstance(value, (BNode, URIRef)):
value, _type = term._castPythonToLiteral(value)
if _type is None:
value = Literal(value)
else:
value = Literal(value, datatype=_type)
self.graph.set((self.asNode(), key, value))

def add(self, key, value, lang=None):
""" Add a triple to the graph related to this node
Expand Down
2 changes: 1 addition & 1 deletion MyCapytain/common/reference/__init__.py
Expand Up @@ -8,4 +8,4 @@
"""
from ._base import NodeId, BaseCitationSet, BaseReference, BaseReferenceSet
from ._capitains_cts import Citation, CtsReference, CtsReferenceSet, URN
from ._dts_1 import DtsCitation, DtsCitationSet
from ._dts_1 import DtsCitation, DtsCitationSet, DtsReference, DtsReferenceSet
90 changes: 42 additions & 48 deletions MyCapytain/common/reference/_base.py
Expand Up @@ -2,6 +2,7 @@
from MyCapytain.common.constants import get_graph, Mimetypes, RDF_NAMESPACES
from MyCapytain.errors import CitationDepthError
from copy import copy
from typing import Tuple
from abc import abstractmethod


Expand Down Expand Up @@ -319,6 +320,7 @@ class BaseReference(tuple):
def __new__(cls, *refs):
if len(refs) == 1 and not isinstance(refs[0], tuple):
refs = refs[0], None

obj = tuple.__new__(cls, refs)

return obj
Expand Down Expand Up @@ -358,7 +360,7 @@ def __new__(cls, *refs, citation: BaseCitationSet=None, level: int=1):
obj._citation = None
obj._level = level

if citation:
if citation is not None:
obj._citation = citation
return obj

Expand All @@ -370,6 +372,14 @@ def citation(self) -> BaseCitationSet:
def level(self) -> int:
return self._level

def __repr__(self):
return "<{typ} ({repr}) level:{level}, citation:{citation}>".format(
typ=type(self).__name__,
repr=", ".join([str(s) for s in self]),
level=self.level,
citation=str(self.citation)
)


class NodeId(object):
""" Collection of directional references for a Tree
Expand All @@ -386,84 +396,68 @@ class NodeId(object):
:type depth: int
"""
def __init__(self, identifier=None, children=None, parent=None, siblings=(None, None), depth=None):
self.__children__ = children or []
self.__parent__ = parent
self.__prev__, self.__nextId__ = siblings
self.__identifier__ = identifier
self.__depth__ = depth
self._children = children or []
self._parent = parent
self._prev_id, self._next_id = siblings
self._identifier = identifier
self._depth = depth

@property
def depth(self):
def depth(self) -> int:
""" Depth of the node in the global hierarchy of the text tree
:rtype: int
"""
return self.__depth__
return self._depth

@property
def childIds(self):
""" Children Node
:rtype: [str]
def childIds(self) -> BaseReferenceSet:
""" Children Ids
"""
return self.__children__
return self._children

@property
def firstId(self):
""" First child Node
:rtype: str
def firstId(self) -> BaseReference:
""" First child Id
"""
if len(self.__children__) == 0:
if len(self._children) == 0:
return None
return self.__children__[0]
return self._children[0]

@property
def lastId(self):
""" Last child Node
:rtype: str
def lastId(self) -> BaseReference:
""" Last child id
"""
if len(self.__children__) == 0:
if len(self._children) == 0:
return None
return self.__children__[-1]
return self._children[-1]

@property
def parentId(self):
""" Parent Node
:rtype: str
def parentId(self) -> BaseReference:
""" Parent Id
"""
return self.__parent__
return self._parent

@property
def siblingsId(self):
""" Siblings Node
:rtype: (str, str)
def siblingsId(self) -> Tuple[BaseReference, BaseReference]:
""" Siblings Id
"""
return self.__prev__, self.__nextId__
return self.prevId, self.nextId

@property
def prevId(self):
""" Previous Node (Sibling)
:rtype: str
def prevId(self) -> BaseReference:
""" Previous Id (Sibling)
"""
return self.__prev__
return self._prev_id

@property
def nextId(self):
""" Next Node (Sibling)
:rtype: str
def nextId(self) -> BaseReference:
""" Next Id
"""
return self.__nextId__
return self._next_id

@property
def id(self):
"""Current object identifier
:rtype: str
"""
return self.__identifier__
return self._identifier
2 changes: 1 addition & 1 deletion MyCapytain/common/reference/_capitains_cts.py
Expand Up @@ -3,7 +3,7 @@
from lxml.etree import _Element

from MyCapytain.common.constants import Mimetypes, get_graph, RDF_NAMESPACES, XPATH_NAMESPACES
from MyCapytain.common.utils import make_xml_node
from MyCapytain.common.utils.xml import make_xml_node

from ._base import BaseCitation, BaseReference, BaseReferenceSet

Expand Down
66 changes: 65 additions & 1 deletion MyCapytain/common/reference/_dts_1.py
@@ -1,4 +1,5 @@
from ._base import BaseCitationSet, BaseCitation
from ._base import BaseCitationSet, BaseCitation, BaseReference, BaseReferenceSet
from ..metadata import Metadata
from MyCapytain.common.constants import RDF_NAMESPACES


Expand All @@ -7,10 +8,73 @@
_cite_structure_term = str(_dts.term("citeStructure"))


class DtsReference(BaseReference):
def __new__(cls, *refs, metadata: Metadata=None, type_: str=None):
o = BaseReference.__new__(cls, *refs)
if metadata:
o._metadata = metadata
else:
o._metadata = Metadata() # toDo : Figure how to deal with Refs ID in the Sparql Graph

if type_:
o.type = type_
return o

@property
def metadata(self) -> Metadata:
return self._metadata

@property
def type(self):
return self.metadata.get_single(_dts.term("citeType"))

@type.setter
def type(self, value):
self.metadata.set(_dts.term("citeType"), value)

def __eq__(self, other):
return super(DtsReference, self).__eq__(other) and \
isinstance(other, DtsReference) and \
self.type == other.type

def __repr__(self):
return "<DtsReference <{}> [{}]>".format(
"><".join([str(x) for x in self if x]),
self.type
)


class DtsReferenceSet(BaseReferenceSet):
def __contains__(self, item: str) -> bool:
return BaseReferenceSet.__contains__(self, item) or \
BaseReferenceSet.__contains__(self, DtsReference(item))

def __eq__(self, other):
return super(DtsReferenceSet, self).__eq__(other) and \
self.level == other.level and \
isinstance(other, DtsReferenceSet) and \
self.citation == other.citation


class DtsCitation(BaseCitation):
def __init__(self, name=None, children=None, root=None):
super(DtsCitation, self).__init__(name=name, children=children, root=root)

def __eq__(self, other):
return isinstance(other, BaseCitation) and \
self.name == other.name and \
self.children == other.children and \
(
(
not self.is_root() and
not other.is_root() and
self.root == other.root
)
or (
self.is_root() and other.is_root()
)
)

@classmethod
def ingest(cls, resource, root=None, **kwargs):
""" Ingest a dictionary of DTS Citation object (as parsed JSON-LD) and
Expand Down
16 changes: 16 additions & 0 deletions MyCapytain/common/utils/__init__.py
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
"""
.. module:: MyCapytain.common.utils
:synopsis: Common useful tools
.. moduleauthor:: Thibault Clérice <leponteineptique@gmail.com>
"""

from ._generic import (
OrderedDefaultDict, nested_ordered_dictionary, nested_get, nested_set, normalize
)
from ._graph import Subgraph, expand_namespace
from ._http import parse_pagination, parse_uri
from ._json_ld import dict_to_literal, literal_to_dict

0 comments on commit 816bb3e

Please sign in to comment.