-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstringlist.py
More file actions
465 lines (342 loc) · 11.7 KB
/
stringlist.py
File metadata and controls
465 lines (342 loc) · 11.7 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#!/usr/bin/env python
#
# stringlist.py
"""
A list of strings that represent lines in a multiline string.
.. versionchanged:: 1.0.0
:class:`~domdf_python_tools.typing.String` should now be imported from :mod:`domdf_python_tools.typing`.
"""
#
# Copyright © 2020-2021 Dominic Davis-Foster <dominic@davis-foster.co.uk>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# stdlib
from contextlib import contextmanager
from typing import Any, Iterable, Iterator, List, Tuple, TypeVar, Union, cast, overload
# this package
from domdf_python_tools.doctools import prettify_docstrings
from domdf_python_tools.typing import String
from domdf_python_tools.utils import convert_indents
__all__ = ["Indent", "StringList", "DelimitedList", "_SL"]
_S = TypeVar("_S")
_SL = TypeVar("_SL", bound="StringList")
@prettify_docstrings
class Indent:
"""
Represents an indent, having a symbol/type and a size.
:param size: The indent size.
:param type: The indent character.
"""
def __init__(self, size: int = 0, type: str = '\t'): # noqa: A002 # pylint: disable=redefined-builtin
self.size = int(size)
self.type = str(type)
def __iter__(self) -> Iterator[Union[str, Any]]:
"""
Returns the size and type of the :class:`~domdf_python_tools.stringlist.Indent`.
"""
yield self.size
yield self.type
@property
def size(self) -> int:
"""
The indent size.
"""
return self._size
@size.setter
def size(self, size: int) -> None:
self._size = int(size)
@property # noqa: A002,A003 # pylint: disable=redefined-builtin
def type(self) -> str: # noqa: A002,A003 # pylint: disable=redefined-builtin
"""
The indent character.
"""
return self._type
@type.setter # noqa: A002,A003 # pylint: disable=redefined-builtin
def type(self, type: str) -> None: # noqa: A002 # pylint: disable=redefined-builtin
if not str(type):
raise ValueError("'type' cannot an empty string.")
self._type = str(type)
def __str__(self) -> str:
"""
Returns the :class:`~domdf_python_tools.stringlist.Indent` as a string.
"""
return self.type * self.size
def __repr__(self) -> str:
"""
Returns the string representation of the :class:`~domdf_python_tools.stringlist.Indent`.
"""
return f"{type(self).__name__}(size={self.size}, type={self.type!r})"
def __eq__(self, other):
if isinstance(other, Indent):
return other.size == self.size and other.type == self.type
elif isinstance(other, str):
return str(self) == other
elif isinstance(other, tuple):
return tuple(self) == other
else:
return NotImplemented
class StringList(List[str]):
"""
A list of strings that represent lines in a multiline string.
:param iterable: Content to populate the StringList with.
:param convert_indents: Whether indents at the start of lines should be converted.
"""
#: The indent to insert at the beginning of new lines.
indent: Indent
convert_indents: bool
"""
Whether indents at the start of lines should be converted.
Only applies to lines added after this is enabled/disabled.
Can only be used when the indent is ``'\\t'`` or ``'␣'``.
"""
def __init__(
self,
iterable: Iterable[String] = (),
convert_indents: bool = False,
) -> None:
if isinstance(iterable, str):
iterable = iterable.split('\n')
self.indent = Indent()
self.convert_indents = convert_indents
super().__init__([self._make_line(str(x)) for x in iterable])
def _make_line(self, line: str) -> str:
if not str(self.indent_type).strip(" \t") and self.convert_indents:
if self.indent_type == '\t':
line = convert_indents(line, tab_width=1, from_=" ", to='\t')
else: # pragma: no cover
line = convert_indents(line, tab_width=1, from_='\t', to=self.indent_type)
return f"{self.indent}{line}".rstrip()
def append(self, line: String) -> None:
"""
Append a line to the end of the :class:`~domdf_python_tools.stringlist.StringList`.
:param line:
"""
for inner_line in str(line).split('\n'):
super().append(self._make_line(inner_line))
def extend(self, iterable: Iterable[String]) -> None:
"""
Extend the :class:`~domdf_python_tools.stringlist.StringList` with lines from ``iterable``.
:param iterable: An iterable of string-like objects to add to the end of the
:class:`~domdf_python_tools.stringlist.StringList`.
"""
for line in iterable:
self.append(line)
def copy(self: _SL) -> _SL:
"""
Returns a shallow copy of the :class:`~domdf_python_tools.stringlist.StringList`.
Equivalent to ``a[:]``.
:rtype: :class:`~domdf_python_tools.stringlist.StringList`
"""
return self.__class__(super().copy())
def count_blanklines(self) -> int:
"""
Returns a count of the blank lines in the :class:`~domdf_python_tools.stringlist.StringList`.
.. versionadded:: 0.7.1
"""
return self.count('')
def insert(self, index: int, line: String) -> None:
"""
Insert a line into the :class:`~domdf_python_tools.stringlist.StringList` at the given position.
:param index:
:param line:
"""
lines: List[str]
if index < 0 or index > len(self):
lines = str(line).split('\n')
else:
lines = cast(list, reversed(str(line).split('\n')))
for inner_line in lines:
super().insert(index, self._make_line(inner_line))
@overload
def __setitem__(self, index: int, line: String) -> None: ...
@overload
def __setitem__(self, index: slice, line: Iterable[String]) -> None: ...
def __setitem__(self, index: Union[int, slice], line: Union[String, Iterable[String]]):
"""
Replaces the given line with new content.
If the new content consists of multiple lines subsequent content in the
:class:`~domdf_python_tools.stringlist.StringList` will be shifted down.
:param index:
:param line:
"""
if isinstance(index, int):
if self and index < len(self):
self.pop(index)
if index < 0:
index = len(self) + index + 1
self.insert(index, line)
elif isinstance(index, slice):
for line, idx in zip( # pylint: disable=redefined-argument-from-local
reversed(line), # type: ignore
reversed(range(index.start or 0, index.stop + 1, index.step or 1)),
):
self[idx] = line
@overload
def __getitem__(self, index: int) -> str: ...
@overload
def __getitem__(self: _SL, index: slice) -> _SL: ...
def __getitem__(self: _SL, index: Union[int, slice]) -> Union[str, _SL]:
r"""
Returns the line with the given index.
:param index:
:rtype: :py:obj:`~typing.Union`\[:class:`str`, :class:`~domdf_python_tools.stringlist.StringList`\]
.. versionchanged:: 1.8.0
Now returns a :class:`~domdf_python_tools.stringlist.StringList` when ``index`` is a :class:`slice`.
"""
if isinstance(index, slice):
return self.__class__(super().__getitem__(index))
else:
return super().__getitem__(index)
def blankline(self, ensure_single: bool = False):
"""
Append a blank line to the end of the :class:`~domdf_python_tools.stringlist.StringList`.
:param ensure_single: Ensure only a single blank line exists after the previous line of text.
"""
if ensure_single:
while self and not self[-1]:
self.pop(-1)
self.append('')
def set_indent_size(self, size: int = 0):
"""
Sets the size of the indent to insert at the beginning of new lines.
:param size: The indent size to use for new lines.
"""
self.indent.size = int(size)
def set_indent_type(self, indent_type: str = '\t'):
"""
Sets the type of the indent to insert at the beginning of new lines.
:param indent_type: The type of indent to use for new lines.
"""
self.indent.type = str(indent_type)
def set_indent(self, indent: Union[String, Indent], size: int = 0):
"""
Sets the indent to insert at the beginning of new lines.
:param indent: The :class:`~.Indent` to use for new lines, or the indent type.
:param size: If ``indent`` is an indent type, the indent size to use for new lines.
"""
if isinstance(indent, Indent):
if size:
raise TypeError("'size' argument cannot be used when providing an 'Indent' object.")
self.indent = indent
else:
self.indent = Indent(int(size), str(indent))
@property
def indent_size(self) -> int:
"""
The current indent size.
"""
return int(self.indent.size)
@indent_size.setter
def indent_size(self, size: int) -> None:
"""
Sets the indent size.
"""
self.indent.size = int(size)
@property
def indent_type(self) -> str:
"""
The current indent type.
"""
return str(self.indent.type)
@indent_type.setter
def indent_type(self, type: str) -> None: # noqa: A002 # pylint: disable=redefined-builtin
"""
Sets the indent type.
"""
self.indent.type = str(type)
def __str__(self) -> str:
"""
Returns the :class:`~domdf_python_tools.stringlist.StringList` as a string.
"""
return '\n'.join(self)
def __bytes__(self) -> bytes:
"""
Returns the :class:`~domdf_python_tools.stringlist.StringList` as bytes.
.. versionadded:: 2.1.0
"""
return str(self).encode("UTF-8")
def __eq__(self, other) -> bool:
"""
Returns whether the other object is equal to this :class:`~domdf_python_tools.stringlist.StringList`.
"""
if isinstance(other, str):
return str(self) == other
else:
return super().__eq__(other)
@contextmanager
def with_indent(self, indent: Union[String, Indent], size: int = 0):
"""
Context manager to temporarily use a different indent.
.. code-block:: python
>>> sl = StringList()
>>> with sl.with_indent(" ", 1):
... sl.append("Hello World")
:param indent: The :class:`~.Indent` to use within the ``with`` block, or the indent type.
:param size: If ``indent`` is an indent type, the indent size to use within the ``with`` block.
"""
original_indent: Tuple[int, str] = tuple(self.indent) # type: ignore
try:
self.set_indent(indent, size)
yield
finally:
self.indent = Indent(*original_indent)
@contextmanager
def with_indent_size(self, size: int = 0):
"""
Context manager to temporarily use a different indent size.
.. code-block:: python
>>> sl = StringList()
>>> with sl.with_indent_size(1):
... sl.append("Hello World")
:param size: The indent size to use within the ``with`` block.
"""
original_indent_size = self.indent_size
try:
self.indent_size = size
yield
finally:
self.indent_size = original_indent_size
@contextmanager
def with_indent_type(self, indent_type: str = '\t'):
"""
Context manager to temporarily use a different indent type.
.. code-block:: python
>>> sl = StringList()
>>> with sl.with_indent_type(" "):
... sl.append("Hello World")
:param indent_type: The type of indent to use within the ``with`` block.
"""
original_indent_type = self.indent_type
try:
self.indent_type = indent_type
yield
finally:
self.indent_type = original_indent_type
class DelimitedList(List[_S]):
"""
Subclass of :class:`list` that supports custom delimiters in format strings.
**Example:**
.. code-block:: python
>>> l = DelimitedList([1, 2, 3, 4, 5])
>>> format(l, ", ")
'1, 2, 3, 4, 5'
>>> f"Numbers: {l:, }"
'Numbers: 1, 2, 3, 4, 5'
.. versionadded:: 1.1.0
"""
def __format__(self, format_spec: str) -> str:
return format_spec.join([str(x) for x in self]) # pylint: disable=not-an-iterable