Skip to content

Commit

Permalink
Upgraded MyCapytain. Added cache support for texts + lxml.objectify a…
Browse files Browse the repository at this point in the history
…s default
  • Loading branch information
PonteIneptique committed Sep 20, 2016
1 parent c5c9060 commit 77a9ac4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
41 changes: 37 additions & 4 deletions capitains_nautilus/inventory/local.py
Expand Up @@ -7,14 +7,15 @@
from MyCapytain.resources.inventory import TextInventory, TextGroup, Work, Citation
from MyCapytain.resources.texts.local import Text
from MyCapytain.common.reference import URN
from MyCapytain.common.utils import xmlparser
from lxml.objectify import makeparser, parse as objectify
from capitains_nautilus.errors import *
from glob import glob
import os.path
from capitains_nautilus.inventory.proto import InventoryResolver
from capitains_nautilus import _cache_key
from capitains_nautilus.cache import BaseCache
import logging
import pickle


class XMLFolderResolver(InventoryResolver):
Expand All @@ -32,7 +33,7 @@ class XMLFolderResolver(InventoryResolver):
:type logger: logging
:cvar TEXT_CLASS: Text Class [not instantiated] to be used to parse Texts. Can be changed to support Cache for example
:type TEXT_CLASS: Text
:type TEXT_CLASS: class
:ivar inventory_cache_key: Werkzeug Cache key to get or set cache for the TextInventory
:ivar texts_cache_key: Werkzeug Cache key to get or set cache for lists of metadata texts objects
:ivar texts_parsed: Werkzeug Cache key to get or set cache for lists of parsed texts objects
Expand All @@ -53,6 +54,7 @@ def __init__(self, resource, inventories=None, cache=None, name=None, logger=Non
cache = BaseCache()

self.__inventories__ = inventories
self.__parser__ = makeparser()
self.__cache = cache
self.name = name

Expand All @@ -76,6 +78,14 @@ def __init__(self, resource, inventories=None, cache=None, name=None, logger=Non
elif auto_parse:
self.parse(resource)

def xmlparse(self, file):
""" Parse a XML file
:param file: Opened File
:return: Tree
"""
return objectify(file, parser=self.__parser__)

def cache(self, inventory, texts):
""" Cache main objects of the resolver : TextInventory and Texts Metadata objects
Expand All @@ -88,10 +98,33 @@ def cache(self, inventory, texts):
self.__cache.set(self.inventory_cache_key, inventory)
self.__cache.set(self.texts_metadata_cache_key, texts)

def text_to_cache(self, text):
""" Cache a text
:param text: Text to be cached
"""
self.__cache.set(
_cache_key(self.texts_parsed_cache_key, str(text.urn)),
text
)

def cache_to_text(self, urn):
""" Get a text from Cache
:param text: Text to be cached
:return: Text object
:rtype: Text
"""
return self.__cache.get(
_cache_key(self.texts_parsed_cache_key, str(urn)),
)

def flush(self):
""" Flush current resolver objects and cache
"""
self.inventory = TextInventory()
for text in self.__texts__:
self.__cache.delete(_cache_key(self.texts_parsed_cache_key, str(text.urn)))
self.__texts__ = []
self.__cache.delete(self.inventory_cache_key)
self.__cache.delete(self.texts_metadata_cache_key)
Expand Down Expand Up @@ -132,7 +165,7 @@ def parse(self, resource, cache=True):
if os.path.isfile(__text__.path):
try:
with io.open(__text__.path) as f:
t = Text(resource=f)
t = Text(resource=self.xmlparse(f))
cites = list()
for cite in [c for c in t.citation][::-1]:
if len(cites) >= 1:
Expand Down Expand Up @@ -184,7 +217,7 @@ def getText(self, urn):

text = self.inventory[str(urn)]
with io.open(text.path) as __xml__:
resource = self.TEXT_CLASS(urn=urn, resource=xmlparser(__xml__))
resource = self.TEXT_CLASS(urn=urn, resource=self.xmlparse(__xml__))

return resource, text

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
@@ -1,4 +1,4 @@
MyCapytain>=1.0.1
MyCapytain>=1.0.2
tornado>=4.3
Flask>=0.10.1
Werkzeug>=0.11.3
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -10,7 +10,7 @@
license='MIT',
packages=find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"]),
install_requires=[
"MyCapytain>=1.0.1",
"MyCapytain>=1.0.2",
"tornado>=4.3",
"Flask>=0.10.1",
"Werkzeug>=0.11.3",
Expand Down

0 comments on commit 77a9ac4

Please sign in to comment.