Skip to content

Commit

Permalink
Put the shelvecache file in tmp to avoid permission problems
Browse files Browse the repository at this point in the history
  • Loading branch information
erwan committed Sep 29, 2014
1 parent f7fccd1 commit b4f2ef8
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion prismic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

__title__ = 'prismic'
__version__ = '0.1.1'
__version__ = '1.0.1'
__author__ = 'Prismic Team'
__license__ = 'Apache 2'

Expand Down
6 changes: 4 additions & 2 deletions prismic/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Api>` object.
Expand All @@ -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))
Expand Down
12 changes: 10 additions & 2 deletions prismic/cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

import os
import tempfile
import shelve
from datetime import datetime

Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='[email protected]',
Expand Down
5 changes: 3 additions & 2 deletions tests/test_prismic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down Expand Up @@ -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."""
Expand All @@ -54,6 +54,7 @@ def html_serializer(element, content):
return """<a class="some-link" href="%s">""" % element.get_url(PrismicTestCase.link_resolver) + content + "</a>"
return None


class ApiIntegrationTestCase(PrismicTestCase):
"""Doing real HTTP requests to test API data fetching"""

Expand Down

0 comments on commit b4f2ef8

Please sign in to comment.