Skip to content

Commit c803fa6

Browse files
authored
Merge pull request #193 from ionrock/ionrock/use-black-style
Use black formatting
2 parents 642f1c0 + ae286a8 commit c803fa6

28 files changed

+563
-611
lines changed

Makefile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
VENV = .venv
1+
VENV=.venv
2+
VENV_CMD=python3 -m venv
23
ACTIVATE = $(VENV)/bin/activate
34
CHEESE=https://pypi.python.org/pypi
45
BUMPTYPE=patch
56

67

7-
virtualenv:
8-
virtualenv $(VENV)
9-
108
$(VENV)/bin/pip:
11-
virtualenv $(VENV)
9+
$(VENV_CMD) $(VENV)
1210

1311
bootstrap: $(VENV)/bin/pip
1412
$(VENV)/bin/pip install -r dev_requirements.txt
1513

14+
format:
15+
$(VENV)/bin/black .
16+
1617
doc: $(VENV)/bin/sphinx-build
1718
. $(ACTIVATE);
1819
cd docs && make html

cachecontrol/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
33
Make it easy to import from cachecontrol without long namespaces.
44
"""
5-
__author__ = 'Eric Larson'
6-
__email__ = '[email protected]'
7-
__version__ = '0.12.4'
5+
__author__ = "Eric Larson"
6+
__email__ = "[email protected]"
7+
__version__ = "0.12.4"
88

99
from .wrapper import CacheControl
1010
from .adapter import CacheControlAdapter

cachecontrol/_cmd.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,19 @@ def setup_logging():
1717

1818
def get_session():
1919
adapter = CacheControlAdapter(
20-
DictCache(),
21-
cache_etags=True,
22-
serializer=None,
23-
heuristic=None,
20+
DictCache(), cache_etags=True, serializer=None, heuristic=None
2421
)
2522
sess = requests.Session()
26-
sess.mount('http://', adapter)
27-
sess.mount('https://', adapter)
23+
sess.mount("http://", adapter)
24+
sess.mount("https://", adapter)
2825

2926
sess.cache_controller = adapter.controller
3027
return sess
3128

3229

3330
def get_args():
3431
parser = ArgumentParser()
35-
parser.add_argument('url', help='The URL to try and cache')
32+
parser.add_argument("url", help="The URL to try and cache")
3633
return parser.parse_args()
3734

3835

@@ -51,10 +48,10 @@ def main(args=None):
5148

5249
# Now try to get it
5350
if sess.cache_controller.cached_request(resp.request):
54-
print('Cached!')
51+
print("Cached!")
5552
else:
56-
print('Not cached :(')
53+
print("Not cached :(")
5754

5855

59-
if __name__ == '__main__':
56+
if __name__ == "__main__":
6057
main()

cachecontrol/adapter.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,27 @@
1010

1111

1212
class CacheControlAdapter(HTTPAdapter):
13-
invalidating_methods = {'PUT', 'DELETE'}
14-
15-
def __init__(self, cache=None,
16-
cache_etags=True,
17-
controller_class=None,
18-
serializer=None,
19-
heuristic=None,
20-
cacheable_methods=None,
21-
*args, **kw):
13+
invalidating_methods = {"PUT", "DELETE"}
14+
15+
def __init__(
16+
self,
17+
cache=None,
18+
cache_etags=True,
19+
controller_class=None,
20+
serializer=None,
21+
heuristic=None,
22+
cacheable_methods=None,
23+
*args,
24+
**kw
25+
):
2226
super(CacheControlAdapter, self).__init__(*args, **kw)
2327
self.cache = cache or DictCache()
2428
self.heuristic = heuristic
25-
self.cacheable_methods = cacheable_methods or ('GET',)
29+
self.cacheable_methods = cacheable_methods or ("GET",)
2630

2731
controller_factory = controller_class or CacheController
2832
self.controller = controller_factory(
29-
self.cache,
30-
cache_etags=cache_etags,
31-
serializer=serializer,
33+
self.cache, cache_etags=cache_etags, serializer=serializer
3234
)
3335

3436
def send(self, request, cacheable_methods=None, **kw):
@@ -43,20 +45,18 @@ def send(self, request, cacheable_methods=None, **kw):
4345
except zlib.error:
4446
cached_response = None
4547
if cached_response:
46-
return self.build_response(request, cached_response,
47-
from_cache=True)
48+
return self.build_response(request, cached_response, from_cache=True)
4849

4950
# check for etags and add headers if appropriate
50-
request.headers.update(
51-
self.controller.conditional_headers(request)
52-
)
51+
request.headers.update(self.controller.conditional_headers(request))
5352

5453
resp = super(CacheControlAdapter, self).send(request, **kw)
5554

5655
return resp
5756

58-
def build_response(self, request, response, from_cache=False,
59-
cacheable_methods=None):
57+
def build_response(
58+
self, request, response, from_cache=False, cacheable_methods=None
59+
):
6060
"""
6161
Build a response by making a request or using the cache.
6262
@@ -101,10 +101,8 @@ def build_response(self, request, response, from_cache=False,
101101
response._fp = CallbackFileWrapper(
102102
response._fp,
103103
functools.partial(
104-
self.controller.cache_response,
105-
request,
106-
response,
107-
)
104+
self.controller.cache_response, request, response
105+
),
108106
)
109107
if response.chunked:
110108
super_update_chunk_length = response._update_chunk_length
@@ -113,11 +111,12 @@ def _update_chunk_length(self):
113111
super_update_chunk_length()
114112
if self.chunk_left == 0:
115113
self._fp._close()
116-
response._update_chunk_length = types.MethodType(_update_chunk_length, response)
117114

118-
resp = super(CacheControlAdapter, self).build_response(
119-
request, response
120-
)
115+
response._update_chunk_length = types.MethodType(
116+
_update_chunk_length, response
117+
)
118+
119+
resp = super(CacheControlAdapter, self).build_response(request, response)
121120

122121
# See if we should invalidate the cache.
123122
if request.method in self.invalidating_methods and resp.ok:

cachecontrol/caches/file_cache.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,24 @@ def _secure_open_write(filename, fmode):
4646
fd = os.open(filename, flags, fmode)
4747
try:
4848
return os.fdopen(fd, "wb")
49+
4950
except:
5051
# An error occurred wrapping our FD in a file object
5152
os.close(fd)
5253
raise
5354

5455

5556
class FileCache(BaseCache):
56-
def __init__(self, directory, forever=False, filemode=0o0600,
57-
dirmode=0o0700, use_dir_lock=None, lock_class=None):
57+
58+
def __init__(
59+
self,
60+
directory,
61+
forever=False,
62+
filemode=0o0600,
63+
dirmode=0o0700,
64+
use_dir_lock=None,
65+
lock_class=None,
66+
):
5867

5968
if use_dir_lock is not None and lock_class is not None:
6069
raise ValueError("Cannot use use_dir_lock and lock_class together")
@@ -63,12 +72,15 @@ def __init__(self, directory, forever=False, filemode=0o0600,
6372
from lockfile import LockFile
6473
from lockfile.mkdirlockfile import MkdirLockFile
6574
except ImportError:
66-
notice = dedent("""
75+
notice = dedent(
76+
"""
6777
NOTE: In order to use the FileCache you must have
6878
lockfile installed. You can install it via pip:
6979
pip install lockfile
70-
""")
80+
"""
81+
)
7182
raise ImportError(notice)
83+
7284
else:
7385
if use_dir_lock:
7486
lock_class = MkdirLockFile
@@ -96,8 +108,9 @@ def _fn(self, name):
96108
def get(self, key):
97109
name = self._fn(key)
98110
try:
99-
with open(name, 'rb') as fh:
111+
with open(name, "rb") as fh:
100112
return fh.read()
113+
101114
except FileNotFoundError:
102115
return None
103116

0 commit comments

Comments
 (0)