diff --git a/prismic/__init__.py b/prismic/__init__.py index c41488a..f7789a3 100644 --- a/prismic/__init__.py +++ b/prismic/__init__.py @@ -3,7 +3,7 @@ """ __title__ = 'prismic' -__version__ = '0.1.1' +__version__ = '1.0.1' __author__ = 'Prismic Team' __license__ = 'Apache 2' diff --git a/prismic/api.py b/prismic/api.py index 0355b4e..c00e77a 100644 --- a/prismic/api.py +++ b/prismic/api.py @@ -33,7 +33,7 @@ log = logging.getLogger(__name__) -def get(url, access_token=None, cache=ShelveCache()): +def get(url, access_token=None, cache=None): """Fetches the prismic api JSON. Returns :class:`Api ` object. @@ -43,8 +43,10 @@ def get(url, access_token=None, cache=ShelveCache()): return Api(_get_json(url, access_token=access_token, cache=cache), access_token, cache) -def _get_json(url, params=dict(), access_token=None, cache=ShelveCache()): +def _get_json(url, params=dict(), access_token=None, cache=None): full_params = params.copy() + if cache is None: + cache = ShelveCache(re.sub(r'/\\', '', url.split('/')[2])) if access_token is not None: full_params["access_token"] = access_token full_url = url if len(full_params) == 0 else (url + "?" + urlparse.urlencode(full_params, doseq=1)) diff --git a/prismic/cache.py b/prismic/cache.py index 5121873..21fd9cb 100644 --- a/prismic/cache.py +++ b/prismic/cache.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import os +import tempfile import shelve from datetime import datetime @@ -24,13 +25,20 @@ def get(self, key): class ShelveCache(object): """ A cache implementation based on Shelve: https://docs.python.org/2/library/shelve.html. + + By default, it will be created with "filename" equal to the api domain name. If you want to + run 2 processes using the same """ - def __init__(self): + def __init__(self, filename): + self.filename = filename self.db = None def _init_db(self): if self.db is None: - self.db = shelve.open(os.path.join(os.getcwd(), "cache")) + cache_dir = os.path.join(tempfile.gettempdir(), "prismic-cache") + if not os.path.exists(cache_dir): + os.makedirs(cache_dir) + self.db = shelve.open(os.path.join(cache_dir, self.filename)) def set(self, key, val, time=0): self._init_db() diff --git a/setup.py b/setup.py index 1f9cc09..adf3a8f 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setup( name='prismic', - version='1.0.0', + version='1.0.1', description='Prismic.io development kit', author='The Prismic.io Team', author_email='contact@prismic.io', diff --git a/tests/test_prismic.py b/tests/test_prismic.py index ef6a9fb..8fcb340 100644 --- a/tests/test_prismic.py +++ b/tests/test_prismic.py @@ -5,7 +5,7 @@ from __future__ import (absolute_import, division, print_function, unicode_literals) -from prismic.cache import NoCache +from prismic.cache import ShelveCache from prismic.exceptions import InvalidTokenError, AuthorizationNeededError, \ UnexpectedError from .test_prismic_fixtures import fixture_api, fixture_search, fixture_groups, \ @@ -36,7 +36,7 @@ def setUp(self): self.fixture_spans_labels = json.loads(fixture_spans_labels) self.fixture_custom_html = json.loads(fixture_custom_html) - self.api = prismic.Api(self.fixture_api, self.token, NoCache()) + self.api = prismic.Api(self.fixture_api, self.token, ShelveCache(None)) def tearDown(self): """Teardown.""" @@ -54,6 +54,7 @@ def html_serializer(element, content): return """""" % element.get_url(PrismicTestCase.link_resolver) + content + "" return None + class ApiIntegrationTestCase(PrismicTestCase): """Doing real HTTP requests to test API data fetching"""