-
Notifications
You must be signed in to change notification settings - Fork 0
/
awking.py
189 lines (152 loc) · 5.14 KB
/
awking.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
"""Make it easier to use Python as an AWK replacement."""
import re
from collections import deque
from collections.abc import Callable
from functools import partial
def _ensure_predicate(value):
if isinstance(value, Callable):
return value
if isinstance(value, str):
return re.compile(value).search
if isinstance(value, re.Pattern):
return value.search
raise TypeError(type(value))
class _EndOfGroup(Exception):
pass
class _Group:
def __init__(self, grouper):
self.grouper = grouper
self.cache = deque()
def __iter__(self):
while True:
try:
yield self.cache.popleft()
except IndexError:
try:
# pylint: disable=protected-access
# noinspection PyProtectedMember
yield self.grouper._next_item()
except _EndOfGroup:
return
def append(self, item): # pylint: disable=missing-docstring
self.cache.append(item)
class RangeGrouper:
"""Groups items from an iterable using start/end predicates.
Each group is an iterator itself. Only as much input as needed is
consumed from the iterable.
"""
def __init__(self, begin, end, iterable):
self.begin = _ensure_predicate(begin)
self.end = _ensure_predicate(end)
self.iterable = iter(iterable)
self.current = None
def __iter__(self):
return self
def __next__(self):
while True:
item = next(self.iterable) # May raise StopIteration
# pylint: disable=no-else-return
if not self.current:
if self.begin(item):
group = _Group(self)
self.current = group
self._push_to_current(item)
return group
else:
continue
else:
self._push_to_current(item)
def _push_to_current(self, item):
self.current.append(item)
if self.end(item):
self.current = None
def _next_item(self):
if not self.current:
raise _EndOfGroup()
while True:
try:
item = next(self.iterable)
except StopIteration as ex:
raise _EndOfGroup from ex
if self.end(item):
self.current = None
return item
class LazyRecord:
"""A list of logical fields found in text.
Fields are extracted from `text` by applying `split`. A special
index `...` (Ellipsis) can be used to retrieve the entire text.
>>> r = LazyRecord('a bb ccc', lambda text: text.split())
>>> r[0] # AWK: $1
'a'
>>> r[-1] # AWK: $NF
'ccc'
>>> r[...] # AWK: $0
'a bb ccc'
>>> len(r) # AWK: NF
3
The actual splitting is only done once actually needed (hence the
class name).
"""
def __init__(self, text, split):
self.text = text
self.fields = None
self.split = split
def __getitem__(self, index):
if index is Ellipsis:
return self.text
self._ensure_split()
return self.fields[index]
def _ensure_split(self):
if self.fields is None:
self.fields = self.split(self.text)
def __len__(self):
self._ensure_split()
return len(self.fields)
def __str__(self):
return self.text
def __repr__(self):
split_name = getattr(self.split, '__name__',
self.split.__class__.__qualname__)
return f'{self.__class__.__qualname__}({repr(self.text)}, {split_name})'
def _split_columns(columns, text):
return [text[begin:end] for begin, end in columns]
def _make_columns(widths):
offset = 0
offsets = []
for width in widths:
offsets.append(offset)
if width is Ellipsis:
offset = None
break
offset += width
ends = offsets[1:] + [offset]
return list(zip(offsets, ends))
def records(iterable, *, separator=None, widths=None, pattern=None):
"""Generate LazyRecords from iterable of strings.
Without extra arguments each string is split on whitespace.
Args:
iterable: strings
separator: str or re.Pattern on which input will be split (AWK: FS)
widths: a list of column widths; may end with ... (Ellipsis)
which means "remaining characters" (AWK: FIELDWIDTHS)
pattern: str (a regular expression) or re.Pattern that describes
the contents of each field (AWK: FPAT)
Returns:
Iterable of LazyRecords
"""
if widths:
split = partial(_split_columns, _make_columns(widths))
elif isinstance(separator, str):
def split(text):
return text.split(separator)
elif isinstance(separator, re.Pattern):
split = separator.split
elif isinstance(pattern, str):
split = re.compile(pattern).findall
elif isinstance(pattern, re.Pattern):
split = pattern.findall
else:
def split(text):
return text.split()
for text in iterable:
yield LazyRecord(text, split)