-
Notifications
You must be signed in to change notification settings - Fork 164
/
helpers.py
232 lines (193 loc) · 6.36 KB
/
helpers.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
"""
logbook.helpers
~~~~~~~~~~~~~~~
Various helper functions
:copyright: (c) 2010 by Armin Ronacher, Georg Brandl.
:license: BSD, see LICENSE for more details.
"""
import errno
import os
import random
import re
import sys
import time
from datetime import datetime, timedelta, timezone
# this regexp also matches incompatible dates like 20070101 because
# some libraries (like the python xmlrpclib modules) use this
_iso8601_re = re.compile(
# date
r"(\d{4})(?:-?(\d{2})(?:-?(\d{2}))?)?"
# time
r"(?:T(\d{2}):(\d{2})(?::(\d{2}(?:\.\d+)?))?(Z|[+-]\d{2}:\d{2})?)?$"
)
_missing = object()
can_rename_open_file = False
if os.name == "nt":
try:
import ctypes
_MOVEFILE_REPLACE_EXISTING = 0x1
_MOVEFILE_WRITE_THROUGH = 0x8
_MoveFileEx = ctypes.windll.kernel32.MoveFileExW
def _rename(src, dst):
if _rename_atomic(src, dst):
return True
retry = 0
rv = False
while not rv and retry < 100:
rv = _MoveFileEx(
src, dst, _MOVEFILE_REPLACE_EXISTING | _MOVEFILE_WRITE_THROUGH
)
if not rv:
time.sleep(0.001)
retry += 1
return rv
# new in Vista and Windows Server 2008
_CreateTransaction = ctypes.windll.ktmw32.CreateTransaction
_CommitTransaction = ctypes.windll.ktmw32.CommitTransaction
_MoveFileTransacted = ctypes.windll.kernel32.MoveFileTransactedW
_CloseHandle = ctypes.windll.kernel32.CloseHandle
can_rename_open_file = True
def _rename_atomic(src, dst):
ta = _CreateTransaction(None, 0, 0, 0, 0, 1000, "Logbook rename")
if ta == -1:
return False
try:
retry = 0
rv = False
while not rv and retry < 100:
if rv := _MoveFileTransacted(
src,
dst,
None,
None,
_MOVEFILE_REPLACE_EXISTING | _MOVEFILE_WRITE_THROUGH,
ta,
):
rv = _CommitTransaction(ta)
break
else:
time.sleep(0.001)
retry += 1
return rv
finally:
_CloseHandle(ta)
except Exception:
def _rename(src, dst):
return False
def _rename_atomic(src, dst):
return False
def rename(src, dst):
# Try atomic or pseudo-atomic rename
if _rename(src, dst):
return
# Fall back to "move away and replace"
try:
os.rename(src, dst)
except OSError:
e = sys.exc_info()[1]
if e.errno not in (errno.EEXIST, errno.EACCES):
raise
old = f"{dst}-{random.randint(0, 2**31 - 1):08x}"
os.rename(dst, old)
os.rename(src, dst)
try:
os.unlink(old)
except Exception:
pass
else:
rename = os.rename
can_rename_open_file = True
_JSON_SIMPLE_TYPES = (bool, float, int, str)
def to_safe_json(data):
"""Makes a data structure safe for JSON silently discarding invalid
objects from nested structures. This also converts dates.
"""
def _convert(obj):
if obj is None:
return None
elif isinstance(obj, _JSON_SIMPLE_TYPES):
return obj
elif isinstance(obj, datetime):
return format_iso8601(obj)
elif isinstance(obj, list):
return [_convert(x) for x in obj]
elif isinstance(obj, tuple):
return tuple(_convert(x) for x in obj)
elif isinstance(obj, dict):
rv = {}
for key, value in obj.items():
if not isinstance(key, str):
key = str(key)
if not isinstance(key, str):
key = key
rv[key] = _convert(value)
return rv
return _convert(data)
if sys.version_info >= (3, 12):
def datetime_utcnow():
"""datetime.utcnow() but doesn't emit a deprecation warning.
Will be fixed by https://github.com/getlogbook/logbook/issues/353
"""
return datetime.now(timezone.utc).replace(tzinfo=None)
else:
datetime_utcnow = datetime.utcnow
def format_iso8601(d=None):
"""Returns a date in iso8601 format."""
if d is None:
d = datetime_utcnow()
rv = d.strftime("%Y-%m-%dT%H:%M:%S")
if d.microsecond:
rv += "." + str(d.microsecond)
return rv + "Z"
def parse_iso8601(value):
"""Parse an iso8601 date into a datetime object. The timezone is
normalized to UTC.
"""
m = _iso8601_re.match(value)
if m is None:
raise ValueError("not a valid iso8601 date value")
groups = m.groups()
args = []
for group in groups[:-2]:
if group is not None:
group = int(group)
args.append(group)
seconds = groups[-2]
if seconds is not None:
if "." in seconds:
sec, usec = seconds.split(".")
args.append(int(sec))
args.append(int(usec.ljust(6, "0")))
else:
args.append(int(seconds))
rv = datetime(*args)
tz = groups[-1]
if tz and tz != "Z":
args = [int(x) for x in tz[1:].split(":")]
delta = timedelta(hours=args[0], minutes=args[1])
if tz[0] == "+":
rv -= delta
else:
rv += delta
return rv
def get_application_name():
if not sys.argv or not sys.argv[0]:
return "Python"
return os.path.basename(sys.argv[0]).title()
class cached_property:
"""A property that is lazily calculated and then cached."""
def __init__(self, func, name=None, doc=None):
self.__name__ = name or func.__name__
self.__module__ = func.__module__
self.__doc__ = doc or func.__doc__
self.func = func
def __get__(self, obj, type=None):
if obj is None:
return self
value = obj.__dict__.get(self.__name__, _missing)
if value is _missing:
value = self.func(obj)
obj.__dict__[self.__name__] = value
return value
def get_iterator_next_method(it):
return lambda: next(it)