-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcache.py
79 lines (61 loc) · 2.4 KB
/
cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""Patch for pytest cache to serialize datetime.datetime."""
import datetime
import functools
import json
import py
from dateutil.parser import parse
def json_iso_datetimes(obj):
"""JSON serializer for objects not serializable by default json module."""
if isinstance(obj, datetime.datetime):
return obj.isoformat()
raise TypeError("Unserializable type %s" % type(obj))
def json_iso_datetime_string_to_datetime(obj):
"""JSON object hook that converts object vals from ISO datetime strings to
python datetime.datetime`s if possible."""
for k, v in obj.items():
if not isinstance(v, str):
continue
try:
obj[k] = parse(v, ignoretz=True)
except (OverflowError, ValueError):
pass
return obj
def datetime_encode_set(self, key, value):
"""save value for the given key.
:param key: must be a ``/`` separated value. Usually the first
name is the name of your plugin or your application.
:param value: must be of any combination of basic
python types, including nested types
like e. g. lists of dictionaries.
"""
path = self._getvaluepath(key)
try:
path.parent.mkdir(exist_ok=True, parents=True)
except OSError:
self.warn("could not create cache path {path}", path=path)
return
try:
f = path.open("w")
except OSError:
self.warn("cache could not write path {path}", path=path)
else:
with f:
json.dump(value, f, indent=2, sort_keys=True, default=json_iso_datetimes)
self._ensure_supporting_files()
def datetime_encode_get(self, key, default):
"""return cached value for the given key. If no value was yet cached or
the value cannot be read, the specified default is returned.
:param key: must be a ``/`` separated value. Usually the first
name is the name of your plugin or your application.
:param default: must be provided in case of a cache-miss or
invalid cache values.
"""
path = self._getvaluepath(key)
try:
with path.open("r") as f:
return json.load(f, object_hook=json_iso_datetime_string_to_datetime)
except (ValueError, OSError):
return default
def patch_cache_set(config):
config.cache.set = functools.partial(datetime_encode_set, config.cache)
config.cache.get = functools.partial(datetime_encode_get, config.cache)