Skip to content

Commit 3b3b776

Browse files
authored
Merge pull request #129 from jezdez/redis-improvements
Remove accidental lockfile ImportError message when using RedisCache
2 parents f315052 + 6c20a3b commit 3b3b776

File tree

5 files changed

+30
-29
lines changed

5 files changed

+30
-29
lines changed

cachecontrol/caches/__init__.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,2 @@
1-
from textwrap import dedent
2-
3-
try:
4-
from .file_cache import FileCache # noqa
5-
except ImportError:
6-
notice = dedent('''
7-
NOTE: In order to use the FileCache you must have
8-
lockfile installed. You can install it via pip:
9-
pip install lockfile
10-
''')
11-
print(notice)
12-
13-
14-
try:
15-
import redis # noqa
16-
from .redis_cache import RedisCache # noqa
17-
except ImportError:
18-
pass
1+
from .file_cache import FileCache # noqa
2+
from .redis_cache import RedisCache # noqa

cachecontrol/caches/file_cache.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import hashlib
22
import os
3-
4-
from lockfile import LockFile
5-
from lockfile.mkdirlockfile import MkdirLockFile
3+
from textwrap import dedent
64

75
from ..cache import BaseCache
86
from ..controller import CacheController
@@ -55,19 +53,29 @@ def __init__(self, directory, forever=False, filemode=0o0600,
5553
if use_dir_lock is not None and lock_class is not None:
5654
raise ValueError("Cannot use use_dir_lock and lock_class together")
5755

58-
if use_dir_lock:
59-
lock_class = MkdirLockFile
60-
61-
if lock_class is None:
62-
lock_class = LockFile
56+
try:
57+
from lockfile import LockFile
58+
from lockfile.mkdirlockfile import MkdirLockFile
59+
except ImportError:
60+
notice = dedent("""
61+
NOTE: In order to use the FileCache you must have
62+
lockfile installed. You can install it via pip:
63+
pip install lockfile
64+
""")
65+
raise ImportError(notice)
66+
else:
67+
if use_dir_lock:
68+
lock_class = MkdirLockFile
69+
70+
elif lock_class is None:
71+
lock_class = LockFile
6372

6473
self.directory = directory
6574
self.forever = forever
6675
self.filemode = filemode
6776
self.dirmode = dirmode
6877
self.lock_class = lock_class
6978

70-
7179
@staticmethod
7280
def encode(x):
7381
return hashlib.sha224(x.encode()).hexdigest()

docs/storage.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,14 @@ RedisCache
6969

7070
The `RedisCache` uses a Redis database to store values. The values are
7171
stored as strings in redis, which means the get, set and delete
72-
actions are used.
72+
actions are used. It requires the `redis`_ library to be installed.
73+
74+
.. note::
75+
76+
Note that you can install this dependency automatically with pip
77+
by requesting the *redis* extra: ::
78+
79+
pip install cachecontrol[redis]
7380

7481
The `RedisCache` also provides a clear method to delete all keys in a
7582
database. Obviously, this should be used with caution as it is naive
@@ -94,3 +101,4 @@ a better method for utilizing redis as a cache.
94101
.. _httplib2: http://code.google.com/p/httplib2/
95102
.. _lockfile: https://github.com/smontanaro/pylockfile
96103
.. _requests 2.1: http://docs.python-requests.org/en/latest/community/updates/#id2
104+
.. _redis: https://github.com/andymccurdy/redis-py

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
],
2323
extras_require={
2424
'filecache': ['lockfile>=0.9'],
25+
'redis': ['redis>=2.10.5'],
2526
},
2627
entry_points={
2728
'console_scripts': [

tests/test_regressions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
from cachecontrol import CacheControl
6-
from cachecontrol.caches.file_cache import FileCache
6+
from cachecontrol.caches import FileCache
77
from cachecontrol.filewrapper import CallbackFileWrapper
88
from requests import Session
99

0 commit comments

Comments
 (0)