Skip to content

Commit

Permalink
Now exportable objects take on Exportable parent with several methods
Browse files Browse the repository at this point in the history
  • Loading branch information
PonteIneptique committed Dec 15, 2016
1 parent da959b8 commit 3ac8d89
Show file tree
Hide file tree
Showing 25 changed files with 1,561 additions and 1,297 deletions.
185 changes: 185 additions & 0 deletions MyCapytain/common/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
from collections import namedtuple
from inspect import getmro

#: List of XPath Namespaces used in guidelines
NS = {
"tei": "http://www.tei-c.org/ns/1.0",
"ahab": "http://localhost.local",
"ti": "http://chs.harvard.edu/xmlns/cts",
"xml": "http://www.w3.org/XML/1998/namespace"
}

#: List of RDF Prefixes with their equivalents
RDF_PREFIX = {
"foaf": "http://xmlns.com/foaf/0.1/",
"dc": "http://purl.org/dc/elements/1.1/",
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"owl": "http://www.w3.org/2002/07/owl#",
"geonames": "http://www.geonames.org/ontology#",
"geo": "http://www.w3.org/2003/01/geo/wgs84_pos#",
"skos": "http://www.w3.org/2004/02/skos/core#",
"dbp": "http://dbpedia.org/property/",
"swrc": "http://swrc.ontoware.org/ontology#",
"sioc": "http://rdfs.org/sioc/ns#",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"dbo": "http://dbpedia.org/ontology/",
"dc11": "http://purl.org/dc/elements/1.1/",
"doap": "http://usefulinc.com/ns/doap#",
"dts": "http://w3id.org/dts-ontology/",
"dbpprop": "http://dbpedia.org/property/",
"content": "http://purl.org/rss/1.0/modules/content/",
"wot": "http://xmlns.com/wot/0.1/",
"rss": "http://purl.org/rss/1.0/",
"gen": "http://purl.org/gen/0.1#",
"dbpedia": "http://dbpedia.org/resource/",

"tei": "http://www.tei-c.org/ns/1.0/",
"ti": "http://chs.harvard.edu/xmlns/cts/"
}

#: List of RDF URI with their equivalent Prefix
RDF_MAPPING = {
'http://chs.harvard.edu/xmlns/cts/': 'ti',
'http://dbpedia.org/ontology/': 'dbo',
'http://dbpedia.org/property/': 'dbp',
'http://dbpedia.org/resource/': 'dbpedia',
'http://purl.org/dc/elements/1.1/': 'dc11',
'http://purl.org/gen/0.1#': 'gen',
'http://purl.org/rss/1.0/': 'rss',
'http://purl.org/rss/1.0/modules/content/': 'content',
'http://rdfs.org/sioc/ns#': 'sioc',
'http://swrc.ontoware.org/ontology#': 'swrc',
'http://usefulinc.com/ns/doap#': 'doap',
'http://www.geonames.org/ontology#': 'geonames',
'http://www.tei-c.org/ns/1.0/': 'tei',
'http://www.w3.org/1999/02/22-rdf-syntax-ns#': 'rdf',
'http://www.w3.org/2000/01/rdf-schema#': 'rdfs',
'http://www.w3.org/2001/XMLSchema#': 'xsd',
'http://www.w3.org/2002/07/owl#': 'owl',
'http://www.w3.org/2003/01/geo/wgs84_pos#': 'geo',
'http://www.w3.org/2004/02/skos/core#': 'skos',
'http://xmlns.com/foaf/0.1/': 'foaf',
'http://xmlns.com/wot/0.1/': 'wot'
}

#: Namespace tuple that can be used to express namespace information
Namespace = namedtuple("Namespace", ["uri", "prefix"])


class NAMESPACES:
""" Namespaces Constants used to provide Namespace capacities across the library
:cvar CTS: CTS Namespace
:type CTS: Namespace
:cvar TEI: TEI Namespace
:type TEI: Namespace
:cvar DC: DC Elements
:type DC: Namespace
"""
CTS = Namespace("http://chs.harvard.edu/xmlns/cts/", "ti")
TEI = Namespace("http://www.tei-c.org/ns/1.0/", "tei")
DC = Namespace("http://purl.org/dc/elements/1.1/", "dc")


class Mimetypes:
""" Mimetypes constants that are used to provide export functionality to base MyCapytain object.
:cvar JSON: JSON Resource mimetype
:cvar XML: XML Resource mimetype
:cvar PYTHON: Python Native Object
:cvar PLAINTEXT: Plain string format
"""

class JSON:
""" Json Mimetype
:cvar Std: Standard JSON Export
:cvar CTS: CTS Json Export
"""
Std = "application/text"
CTS = "application/ld+json:CTS"

class DTS:
""" JSON DTS Expression
:cvar Std: Standard DTS Json-LD Expression
:cvar NoParents: DTS Json-LD Expression without parents expression
"""
Std = "application/ld+json:DTS"
NoParents = "application/ld+json:DTS/NoParents"

class XML:
""" XML Mimetype
:cvar Std: Standard XML Export
:cvar RDF: RDF XML Expression Export
:cvar CTS: CTS API XML Expression Export
"""
Std = "text/xml"
RDF = "application/rdf+xml"
CTS = "text/xml:CTS"

class PYTHON:
""" Python Native Objects
:cvar NestedDict: Nested Dictionary Object
:cvar ETREE: Python LXML Etree Object
"""
NestedDict = "python/NestedDict"
ETREE = "python/lxml"

class MyCapytain:
""" MyCapytain Objects
:cvar ReadableText: MyCapytain.resources.prototypes.text.CitableText
"""
ReadableText = "Capitains/ReadableText"

PLAINTEXT = "text/plain"


class Exportable(object):
""" Objects that supports Export
:cvar EXPORT_TO: List of Mimetypes the resource can export to
"""
EXPORT_TO = []
DEFAULT_EXPORT = None

@property
def export_capacities(self):
""" List Mimetypes that current object can export to
"""
return [export for cls in getmro(type(self)) if hasattr(cls, "EXPORT_TO") for export in cls.EXPORT_TO]

def __export__(self, output=None, **kwargs):
""" Export the collection item in the Mimetype required.
..note:: If current implementation does not have special mimetypes, reuses default_export method
:param output: Mimetype to export to (Uses MyCapytain.common.utils.Mimetypes)
:type output: str
:return: Object using a different representation
"""
return None

def export(self, output=None, **kwargs):
""" Export the collection item in the Mimetype required.
..note:: If current implementation does not have special mimetypes, reuses default_export method
:param output: Mimetype to export to (Uses MyCapytain.common.utils.Mimetypes)
:type output: str
:return: Object using a different representation
"""
if output is None:
output = self.DEFAULT_EXPORT
if output is not None and output in self.export_capacities:
for cls in getmro(type(self)):
if hasattr(cls, "EXPORT_TO") and output in cls.EXPORT_TO:
return cls.__export__(self, output, **kwargs)
raise NotImplementedError(
"Mimetype {} has not been implemented for this resource".format(output or "(No Mimetype)")
)
12 changes: 9 additions & 3 deletions MyCapytain/common/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from random import randint
from types import GeneratorType
from collections import defaultdict, OrderedDict
from MyCapytain.common.utils import Mimetypes, RDF_PREFIX, Namespace, RDF_MAPPING
from MyCapytain.common.constants import Namespace, RDF_PREFIX, RDF_MAPPING, Mimetypes, Exportable
from MyCapytain.errors import UnknownNamespace


Expand Down Expand Up @@ -224,7 +224,7 @@ def __setstate__(self, dic):
return self


class Metadata(object):
class Metadata(Exportable):
"""
A metadatum aggregation object provided to centralize metadata
Expand All @@ -238,7 +238,13 @@ class Metadata(object):
.. automethod:: __iter__
.. automethod:: __len__
.. automethod:: __add__
:cvar EXPORT_TO: List of exportable supported formats
:cvar DEFAULT_EXPORT: Default export (CTS XML Inventory)
"""
EXPORT_TO = [Mimetypes.JSON.Std, Mimetypes.XML.RDF, Mimetypes.JSON.DTS.Std]
DEFAULT_EXPORT = Mimetypes.JSON.Std

def __init__(self, keys=None):
""" Initiate the object
"""
Expand Down Expand Up @@ -407,7 +413,7 @@ def keys(self):
"""
return self.__keys__

def export(self, output=Mimetypes.JSON.Std):
def __export__(self, output=Mimetypes.JSON.Std, **kwargs):
""" Export a set of Metadata
:param output: Mimetype to export to
Expand Down
2 changes: 1 addition & 1 deletion MyCapytain/common/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from copy import copy
import re
from lxml.etree import _Element
from MyCapytain.common.utils import NS
from MyCapytain.common.constants import NS

REFSDECL_SPLITTER = re.compile(r"/+[*()|\sa-zA-Z0-9:\[\]@=\\{$'\".\s]+")
REFSDECL_REPLACER = re.compile(r"\$[0-9]+")
Expand Down
132 changes: 8 additions & 124 deletions MyCapytain/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,23 @@
"""
from __future__ import unicode_literals
from six import text_type
from functools import reduce

from collections import OrderedDict, namedtuple
from lxml import etree
from io import IOBase, StringIO
import re
from collections import OrderedDict
from copy import copy
from functools import reduce
from io import IOBase, StringIO

from lxml import etree
from lxml.objectify import ObjectifiedElement, parse
from six import text_type

from MyCapytain.common.constants import NS

__strip = re.compile("([ ]{2,})+")
__parser__ = etree.XMLParser(collect_ids=False, resolve_entities=False)

""" Namespace """
Namespace = namedtuple("Namespace", ["uri", "prefix"])


class NAMESPACES:
CTS = Namespace("http://chs.harvard.edu/xmlns/cts/", "ti")
TEI = Namespace("http://www.tei-c.org/ns/1.0/", "tei")
DC = Namespace("http://purl.org/dc/elements/1.1/", "dc")


def xmliter(node):
Expand All @@ -54,66 +50,10 @@ def normalize(string):
return __strip.sub(" ", string)

#: Dictionary of namespace that can be useful
NS = {
"tei": "http://www.tei-c.org/ns/1.0",
"ahab": "http://localhost.local",
"ti": "http://chs.harvard.edu/xmlns/cts",
"xml": "http://www.w3.org/XML/1998/namespace"
}

#: Dictionary of RDF Prefixes
RDF_PREFIX = {
"foaf": "http://xmlns.com/foaf/0.1/",
"dc": "http://purl.org/dc/elements/1.1/",
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"owl": "http://www.w3.org/2002/07/owl#",
"geonames": "http://www.geonames.org/ontology#",
"geo": "http://www.w3.org/2003/01/geo/wgs84_pos#",
"skos": "http://www.w3.org/2004/02/skos/core#",
"dbp": "http://dbpedia.org/property/",
"swrc": "http://swrc.ontoware.org/ontology#",
"sioc": "http://rdfs.org/sioc/ns#",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"dbo": "http://dbpedia.org/ontology/",
"dc11": "http://purl.org/dc/elements/1.1/",
"doap": "http://usefulinc.com/ns/doap#",
"dts": "http://w3id.org/dts-ontology/",
"dbpprop": "http://dbpedia.org/property/",
"content": "http://purl.org/rss/1.0/modules/content/",
"wot": "http://xmlns.com/wot/0.1/",
"rss": "http://purl.org/rss/1.0/",
"gen": "http://purl.org/gen/0.1#",
"dbpedia": "http://dbpedia.org/resource/",

"tei": "http://www.tei-c.org/ns/1.0/",
"ti": "http://chs.harvard.edu/xmlns/cts/"
}

#: Mapping of known domains to RDF Classical Prefixes
RDF_MAPPING = {
'http://chs.harvard.edu/xmlns/cts/': 'ti',
'http://dbpedia.org/ontology/': 'dbo',
'http://dbpedia.org/property/': 'dbp',
'http://dbpedia.org/resource/': 'dbpedia',
'http://purl.org/dc/elements/1.1/': 'dc11',
'http://purl.org/gen/0.1#': 'gen',
'http://purl.org/rss/1.0/': 'rss',
'http://purl.org/rss/1.0/modules/content/': 'content',
'http://rdfs.org/sioc/ns#': 'sioc',
'http://swrc.ontoware.org/ontology#': 'swrc',
'http://usefulinc.com/ns/doap#': 'doap',
'http://www.geonames.org/ontology#': 'geonames',
'http://www.tei-c.org/ns/1.0/': 'tei',
'http://www.w3.org/1999/02/22-rdf-syntax-ns#': 'rdf',
'http://www.w3.org/2000/01/rdf-schema#': 'rdfs',
'http://www.w3.org/2001/XMLSchema#': 'xsd',
'http://www.w3.org/2002/07/owl#': 'owl',
'http://www.w3.org/2003/01/geo/wgs84_pos#': 'geo',
'http://www.w3.org/2004/02/skos/core#': 'skos',
'http://xmlns.com/foaf/0.1/': 'foaf',
'http://xmlns.com/wot/0.1/': 'wot'
}


def xmlparser(xml, objectify=True):
Expand Down Expand Up @@ -387,59 +327,3 @@ def nested_set(dictionary, keys, value):
nested_get(dictionary, keys[:-1])[keys[-1]] = value


class Mimetypes:
""" Mimetypes constants that are used to provide export functionality to base MyCapytain object.
:cvar JSON: JSON Resource mimetype
:cvar XML: XML Resource mimetype
:cvar PYTHON: Python Native Object
:cvar PLAINTEXT: Plain string format
"""

class JSON:
""" Json Mimetype
:cvar Std: Standard JSON Export
:cvar CTS: CTS Json Export
"""
Std = "application/text"
CTS = "application/ld+json:CTS"

class DTS:
""" JSON DTS Expression
:cvar Std: Standard DTS Json-LD Expression
:cvar NoParents: DTS Json-LD Expression without parents expression
"""
Std = "application/ld+json:DTS"
NoParents = "application/ld+json:DTS/NoParents"

class XML:
""" XML Mimetype
:cvar Std: Standard XML Export
:cvar RDF: RDF XML Expression Export
:cvar CTS: CTS API XML Expression Export
"""
Std = "text/xml"
RDF = "application/rdf+xml"
CTS = "text/xml:CTS"

class PYTHON:
""" Python Native Objects
:cvar NestedDict: Nested Dictionary Object
:cvar ETREE: Python LXML Etree Object
"""
NestedDict = "python/NestedDict"
ETREE = "python/lxml"

class MyCapytain:
""" MyCapytain Objects
:cvar ReadableText: MyCapytain.resources.prototypes.text.CitableText
"""
ReadableText = "Capitains/ReadableText"

PLAINTEXT = "text/plain"

0 comments on commit 3ac8d89

Please sign in to comment.