This repository has been archived by the owner on Aug 27, 2023. It is now read-only.
/
util.py
240 lines (199 loc) · 7.03 KB
/
util.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
""" Utilities """
import logging
import os
import re
import time
import unicodedata
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from distlib.locators import Locator
from distlib.util import split_filename
from distlib.wheel import Wheel
LOG = logging.getLogger(__name__)
ALL_EXTENSIONS = Locator.source_extensions + Locator.binary_extensions
SENTINEL = object()
def parse_filename(filename: str, name: Optional[str] = None) -> Tuple[str, str]:
"""Parse a name and version out of a filename"""
version = None
for ext in ALL_EXTENSIONS:
if filename.endswith(ext):
if ext == ".whl":
wheel = Wheel(filename)
return wheel.name, wheel.version
trimmed = filename[: -len(ext)]
parsed = split_filename(trimmed, name)
if parsed is None:
break
else:
parsed_name, version = parsed[:2]
break
if version is None:
raise ValueError("Cannot parse package file '%s'" % filename)
if name is None:
name = parsed_name
return normalize_name(name), version
def get_packagetype(name: str) -> str:
"""Get package type out of a filename"""
if name.endswith(".tar.gz"):
return "sdist"
elif name.endswith(".egg"):
return "bdist_egg"
elif name.endswith(".whl"):
return "bdist_wheel"
else:
return ""
def normalize_name(name: str) -> str:
"""Normalize a python package name"""
# Lifted directly from PEP503:
# https://www.python.org/dev/peps/pep-0503/#id4
return re.sub(r"[-_.]+", "-", name).lower()
def normalize_metadata_value(value: Union[str, bytes]) -> str:
"""Strip non-ASCII characters from metadata values"""
if isinstance(value, bytes):
value = value.decode("utf-8")
if isinstance(value, str):
value = "".join(c for c in unicodedata.normalize("NFKD", value) if ord(c) < 128)
return value
def normalize_metadata(metadata: Dict[str, Union[str, bytes]]) -> Dict[str, str]:
"""
Strip non-ASCII characters from metadata values
and replace "_" in metadata keys to "-"
"""
return {
key.replace("_", "-"): normalize_metadata_value(value)
for key, value in metadata.items()
}
def create_matcher(queries: List[str], query_type: str) -> Callable[[str], bool]:
"""
Create a matcher for a list of queries
Parameters
----------
queries : list
List of queries
query_type: str
Type of query to run: ["or"|"and"]
Returns
-------
Matcher function
"""
queries = [query.lower() for query in queries]
if query_type == "or":
return lambda x: any((q in x.lower() for q in queries))
else:
return lambda x: all((q in x.lower() for q in queries))
def get_environ_setting(settings: dict, key: str, default=None):
env_key = "PPC_" + key.upper().replace(".", "_")
return os.environ.get(env_key, settings.get(key, default))
def get_settings(settings: dict, prefix: str, **kwargs) -> dict:
"""
Convenience method for fetching settings
Returns a dict; any settings that were missing from the config file will
not be present in the returned dict (as opposed to being present with a
None value)
Parameters
----------
settings : dict
The settings dict
prefix : str
String to prefix all keys with when fetching value from settings
**kwargs : dict
Mapping of setting name to conversion function (e.g. str or asbool)
"""
computed = {}
for name, fxn in kwargs.items():
val = settings.get(prefix + name)
if val is not None:
computed[name] = fxn(val)
return computed
class TimedCache(dict):
"""
Dict that will store entries for a given time, then evict them
Parameters
----------
cache_time : int or None
The amount of time to cache entries for, in seconds. 0 will not cache.
None will cache forever.
factory : callable, optional
If provided, when the TimedCache is accessed and has no value, it will
attempt to populate itself by calling this function with the key it was
accessed with. This function should return a value to cache, or None if
no value is found.
"""
def __init__(
self, cache_time: Optional[int], factory: Optional[Callable[[Any], Any]] = None
):
super(TimedCache, self).__init__()
if cache_time is not None and cache_time < 0:
raise ValueError("cache_time cannot be negative")
self._cache_time = cache_time
self._factory = factory
self._times = {} # type: Dict[str, float]
def _has_expired(self, key):
"""Check if a key is both present and expired"""
if key not in self._times or self._cache_time is None:
return False
updated = self._times[key]
return updated is not None and time.time() - updated > self._cache_time
def _evict(self, key):
"""Remove a key if it has expired"""
if self._has_expired(key):
del self[key]
def __contains__(self, key):
self._evict(key)
return super(TimedCache, self).__contains__(key)
def __delitem__(self, key):
del self._times[key]
super(TimedCache, self).__delitem__(key)
def __setitem__(self, key, value):
if self._cache_time == 0:
return
self._times[key] = time.time()
super(TimedCache, self).__setitem__(key, value)
def __getitem__(self, key):
self._evict(key)
try:
value = super(TimedCache, self).__getitem__(key)
except KeyError:
if self._factory:
value = self._factory(key)
if value is None:
raise
self[key] = value
else:
raise
return value
def get(self, key, default=None):
self._evict(key)
value = super(TimedCache, self).get(key, SENTINEL)
if value is SENTINEL:
if self._factory is not None:
value = self._factory(key)
if value is not None:
self[key] = value
return value
else:
return default
else:
return default
else:
return value
def set_expire(self, key, value, expiration):
"""
Set a value in the cache with a specific expiration
Parameters
----------
key : str
value : value
expiration : int or None
Sets the value to expire this many seconds from now. If None, will
never expire.
"""
if expiration is not None:
if expiration <= 0:
try:
del self[key]
except KeyError:
pass
return
expiration = time.time() + expiration - self._cache_time
self._times[key] = expiration
super(TimedCache, self).__setitem__(key, value)