- Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathutil.py
86 lines (70 loc) · 2.08 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
#
# This file is part of the micropython-esp32-ulp project,
# https://github.com/micropython/micropython-esp32-ulp
#
# SPDX-FileCopyrightText: 2018-2023, the micropython-esp32-ulp authors, see AUTHORS file.
# SPDX-License-Identifier: MIT
DEBUG=False
importgc
importos
NORMAL, WHITESPACE=0, 1
defgarbage_collect(msg, verbose=DEBUG):
free_before=gc.mem_free()
gc.collect()
free_after=gc.mem_free()
ifverbose:
print("%s: %d --gc--> %d bytes free"% (msg, free_before, free_after))
defsplit_tokens(line):
buf=""
tokens= []
state=NORMAL
forcinline:
ifcin"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_":
ifstate!=NORMAL:
iflen(buf) >0:
tokens.append(buf)
buf=""
state=NORMAL
buf+=c
elifcin" \t":
ifstate!=WHITESPACE:
iflen(buf) >0:
tokens.append(buf)
buf=""
state=WHITESPACE
buf+=c
else:
iflen(buf) >0:
tokens.append(buf)
buf=""
tokens.append(c)
iflen(buf) >0:
tokens.append(buf)
returntokens
defvalidate_expression(param):
fortokeninsplit_tokens(param):
state=0
forcintoken:
ifcnotin' \t+-*/%()<>&|~xX0123456789abcdefABCDEF':
returnFalse
# the following allows hex digits a-f after 0x but not otherwise
ifstate==0:
ifcin'abcdefABCDEF':
returnFalse
ifc=='0':
state=1
continue
ifstate==1:
state=2ifcin'xX'else0
continue
ifstate==2:
ifcnotin'0123456789abcdefABCDEF':
state=0
returnTrue
deffile_exists(filename):
try:
os.stat(filename)
returnTrue
exceptOSError:
pass
returnFalse