-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathutils.py
More file actions
113 lines (85 loc) · 2.91 KB
/
utils.py
File metadata and controls
113 lines (85 loc) · 2.91 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# -*- coding: utf-8 -*-
"""
readability.utils
~~~~~~~~~~~~~~~~~
This module provides various utils to the rest of the package.
"""
import logging
from datetime import datetime
from dateutil.parser import parse as parse_datetime
logger = logging.getLogger(__name__)
# map of filter names to a data type. This is used to map names to a
# casting function when needed.
filter_type_map = {
'added_since': 'datetime',
'added_until': 'datetime',
'archive': 'int',
'archived_since': 'datetime',
'archived_until': 'datetime',
'exclude_accessibility': 'string',
'favorite': 'int',
'favorited_since': 'datetime',
'favorited_until': 'datetime',
'domain': 'string',
'only_delete': 'int',
'opened_since': 'datetime',
'opened_until': 'datetime',
'order': 'string',
'page': 'int',
'per_page': 'int',
'tags': 'string',
'updated_since': 'datetime',
'updated_until': 'datetime',
}
def cast_datetime_filter(value):
"""Cast a datetime filter value.
:param value: string representation of a value that needs to be casted to
a `datetime` object.
"""
if isinstance(value, str):
dtime = parse_datetime(value)
elif isinstance(value, datetime):
dtime = value
else:
raise ValueError('Received value of type {0}'.format(type(value)))
return dtime.isoformat()
def cast_integer_filter(value):
"""Cast an integer filter value.
Theses are usually booleans in Python but they need to be sent as
1s and 0s to the API.
:param value: boolean value that needs to be casted to an int
"""
return int(value)
def filter_args_to_dict(filter_dict, accepted_filter_keys=[]):
"""Cast and validate filter args.
:param filter_dict: Filter kwargs
:param accepted_filter_keys: List of keys that are acceptable to use.
"""
out_dict = {}
for k, v in filter_dict.items():
# make sure that the filter k is acceptable
# and that there is a value associated with the key
if k not in accepted_filter_keys or v is None:
logger.debug(
'Filter was not in accepted_filter_keys or value is None.')
# skip it
continue
filter_type = filter_type_map.get(k, None)
if filter_type is None:
logger.debug('Filter key not foud in map.')
# hmm, this was an acceptable filter type but not in the map...
# Going to skip it.
continue
# map of casting funcitons to filter types
filter_cast_map = {
'int': cast_integer_filter,
'datetime': cast_datetime_filter
}
cast_function = filter_cast_map.get(filter_type, None)
# if we get a cast function, call it with v. If not, just use v.
if cast_function:
out_value = cast_function(v)
else:
out_value = v
out_dict[k] = out_value
return out_dict