Repository navigation
Expand file tree
/
Copy pathparse.py
More file actions
194 lines (142 loc) · 4.11 KB
/
Copy pathparse.py
File metadata and controls
194 lines (142 loc) · 4.11 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
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
import os
import re
from . import convert, purge
from .web import parse as web_parse
from .seconds import to_midnight as seconds_to_midnight
# _RE_ALIAS = re.compile(r"[\d.-_]+")
# def alias(text):
# chunks = _RE_ALIAS.split(purge.name(text))
# return "".join(word.capitalize() for word in chunks if word)
_BOOLMAP = {
"1": True,
"yes": True,
"true": True,
"on": True,
"0": False,
"no": False,
"false": False,
"off": False,
}
def boolean(text):
return _BOOLMAP.get(text.strip().lower())
def entries(text, allow_whitespaces=False):
chars = ";,|"
if not allow_whitespaces:
chars += r"\s"
pattr = rf"[{chars}]+"
return [entry for entry in re.split(pattr, text) if entry]
def name(text, safe_name=True):
try:
name = web_parse.name(text, safe_name=safe_name)
name = os.path.basename(name)
except Exception:
name = os.path.basename(text).strip()
if safe_name:
name = purge.name(name)
return name
_ONEWORDS = (
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen",
)
_TENWORDS = (
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety",
)
_RE_NUMBER = re.compile(r"[\s-]+")
def number(text):
# try:
# text = web.misc.translate(text).lower()
# except Exception:
# text = text.lower()
text = text.lower()
o_tuple = [(w, i) for i, w in enumerate(_ONEWORDS)]
t_tuple = [(w, i * 10) for i, w in enumerate(_TENWORDS, 2)]
numwords = dict(o_tuple + t_tuple)
tokens = _RE_NUMBER.split(text)
numbers = [_f for _f in (numwords.get(word) for word in tokens) if _f]
return sum(numbers) if numbers else None
_RE_PACKS = re.compile(r"[^a-z0-9]+(?:(cd|part).*?\d+)?", flags=re.I)
def packs(nameurls):
DEFAULT_URLNAME = "Unknown"
packs = {}
for urlname, url in nameurls:
urlname = name(urlname, safe_name=True)
urlname = os.path.splitext(urlname)[0].strip()
urlname = _RE_PACKS.sub("_", urlname).strip("_")
if not urlname:
urlname = DEFAULT_URLNAME
packs.setdefault(urlname, []).append(url)
return packs
_RE_SIZE = re.compile(r"(?P<S>-?[\d.,]+)\s*(?P<U>[a-zA-Z]*)")
_RE_SIZEFORMAT1 = re.compile(r"\d{1,3}(?:,\d{3})+(?:\.\d+)?$")
_RE_SIZEFORMAT2 = re.compile(r"\d+,\d{2}$")
_RE_SIZEFORMAT3 = re.compile(r'\d+(?:\.\d+)?$')
def bytesize(text, from_unit=None): # returns integer bytes
DEFAULT_UNIT = "byte"
m = _RE_SIZE.match(convert.to_str(text))
if m is None:
return None
raw_size = m.group("S")
if re.match(_RE_SIZEFORMAT1, raw_size):
raw_size = raw_size.replace(",", "")
elif re.match(_RE_SIZEFORMAT2, raw_size):
raw_size = raw_size.replace(",", ".")
elif not re.match(_RE_SIZEFORMAT3, raw_size):
return 0 #: Unknown format
if from_unit is None:
from_unit = m.group("U") or DEFAULT_UNIT
size = float(raw_size)
unit = from_unit[0].lower()
return int(convert.size(size, unit, "byte"))
_TIMEWORDS = ("this", "a", "an", "next")
_TIMEMAP = {"day": 60 ** 2 * 24, "hr": 60 ** 2, "hour": 60 ** 2, "min": 60, "sec": 1}
_RE_TIME = re.compile(r"(\d+|[a-zA-Z-]+)\s*(day|hr|hour|min|sec)|(\d+)")
def seconds(text):
def to_int(obj):
try:
return int(obj)
except ValueError:
return None
# try:
# text = web.misc.translate(text).lower()
# except Exception:
# text = text.lower()
text = text.lower()
w = "|".join(_TIMEWORDS)
pattr = rf"(?:(?:{w})\s+day)|today|daily"
m = re.search(pattr, text)
if m is not None:
return seconds_to_midnight()
seconds = sum(
(w in _TIMEWORDS or to_int(i or w) or number(w) or 1) * _TIMEMAP.get(u, 1)
for w, u, i in _RE_TIME.findall(text)
)
return seconds
def minutes(text):
return seconds(text) / 60
def hours(text):
return seconds(text) / 60 ** 2