-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutils.py
More file actions
100 lines (79 loc) · 2.87 KB
/
Copy pathutils.py
File metadata and controls
100 lines (79 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
# --------------------------------------------------------------------------
# Utility functions.
# --------------------------------------------------------------------------
import re
# Splits a string on instances of a delimiter character. Ignores quoted
# delimiters.
def splitc(s, delimiter, strip=False, discard_empty=False, maxsplit=-1):
tokens, buf, expecting, escaped = [], [], None, False
for index, char in enumerate(s):
if expecting:
buf.append(char)
if char == expecting and not escaped:
expecting = None
else:
if char == delimiter:
tokens.append(''.join(buf))
buf = []
if len(tokens) == maxsplit:
buf.append(s[index+1:])
break
else:
buf.append(char)
if char in ('"', "'"):
expecting = char
escaped = not escaped if char == '\\' else False
tokens.append(''.join(buf))
if strip:
tokens = [t.strip() for t in tokens]
if discard_empty:
tokens = [t for t in tokens if t]
return tokens
# Splits a string on blocks of whitespace. Strips leading and trailing
# whitespace. Ignores quoted whitespace.
def splitws(s, maxsplit=-1):
tokens, buf, expecting, escaped, wsrun = [], [], None, False, False
for index, char in enumerate(s.strip()):
if expecting:
buf.append(char)
if char == expecting and not escaped:
expecting = None
else:
if char.isspace():
if wsrun:
continue
tokens.append(''.join(buf))
buf = []
wsrun = True
if len(tokens) == maxsplit:
buf.append(s[index+1:].lstrip())
break
else:
buf.append(char)
wsrun = False
if char in ('"', "'"):
expecting = char
escaped = not escaped if char == '\\' else False
tokens.append(''.join(buf))
return tokens
# Splits a string using a list of regular expression patterns. Igores quoted
# delimiter matches.
def splitre(s, delimiters, keepdels=False):
tokens, buf = [], []
end_last_match = 0
pattern = r'''"(?:[^\\"]|\\.)*"|'(?:[^\\']|\\.)*'|%s'''
pattern %= '|'.join(delimiters)
for match in re.finditer(pattern, s):
if match.group()[0] in ["'", '"']:
buf.append(s[end_last_match:match.end()])
end_last_match = match.end()
continue
buf.append(s[end_last_match:match.start()])
tokens.append(''.join(buf))
buf = []
end_last_match = match.end()
if keepdels:
tokens.append(match.group())
buf.append(s[end_last_match:])
tokens.append(''.join(buf))
return tokens