-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
131 lines (98 loc) · 2.87 KB
/
utils.py
File metadata and controls
131 lines (98 loc) · 2.87 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import gc
import microcontroller
import micropython
import re
import storage
import sys
debug = False
def shut_down(battery_sensor):
log(f'Battery at {battery_sensor.level}%; shutting down')
storage.umount('/')
log(f'Storage has been unmounted')
while True:
if (battery_sensor.level or 0) >= 10:
log(f'Power has returned after shutdown; resetting')
microcontroller.reset()
def free():
return 0
if hasattr(gc, 'mem_free'):
def free():
gc.collect()
return gc.mem_free()
def version_num():
return int(sys.path[0][1:].split('.')[0].split('-')[0])
def version_dir():
return sys.path[0]
def versions_present():
import fs # ignored by sort_imports
versions = []
for name in fs.listdir():
if name.startswith('v') and fs.isdir(name):
if fs.isfile(name + '/@VALID'):
pack_name = name.split('.')[0]
enabled_flag = '*' if fs.isfile(name + '/@ENABLED') else ''
versions.append(enabled_flag + pack_name)
return versions
last_ms = None
last_mem = None
min_mem = free()
def log(message=None, dump=False):
global last_ms
global last_mem
global min_mem
import time
ms = time.monotonic_ns()//1000000
mem = free()
min_mem = min(min_mem, mem)
if message:
msg = f'[{format_ms(ms)}: {mem} free] {message}'
if last_ms:
print(msg, f'[{format_ms(ms - last_ms)} s elapsed, {last_mem - mem} used]')
last_ms = None
last_mem = None
else:
print(msg)
else:
last_ms = ms
last_mem = mem
if dump or debug:
gc.collect()
micropython.mem_info(1)
print()
def format_ms(ms):
digits = str(ms)
if len(digits) < 4:
digits = ('0000' + digits)[-4:]
return digits[:-3] + '.' + digits[-3:]
def to_bytes(arg):
if isinstance(arg, bytes):
return arg
return bytes(str(arg), 'ascii')
def to_str(arg):
if isinstance(arg, bytes):
return str(arg, 'ascii')
return str(arg)
def report_error(e, message):
try:
import traceback
print(f'{message} due to:')
traceback.print_exception(e, e, e.__traceback__)
except:
print(f'{message}: {e} {repr(e)}')
def split_url(url):
match = re.match(r'^http(s?):/+([^/]+)(.*)', url)
if match:
return bool(match.group(1)), match.group(2), match.group(3) or '/'
return None, None, None
class Cycle: # note, inheriting from list causes hard crashes in CircuitPython!
def __init__(self, items):
self.items = items
self.index = 0
def advance(self, delta=0):
n = len(self.items)
self.index = (self.index + delta + n) % n
return self.items[self.index]
class NullContext:
def __enter__(*args): pass
def __exit__(*args): pass
null_context = NullContext()