Repository navigation
Expand file tree
/
Copy pathsep.py
More file actions
57 lines (46 loc) · 1.52 KB
/
Copy pathsep.py
File metadata and controls
57 lines (46 loc) · 1.52 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
from __future__ import annotations
from refinery.lib.types import Param, buf
from refinery.units import Arg, Unit
class sep(Unit):
"""
Insert a separator between chunks; the default separator is a line break.
If any of the input `refinery.lib.frame.Chunk`s is currently out of scope, `refinery.sep`
makes them visible by default. This can be prevented by using the `-s` flag.
"""
def __init__(
self, separator: Param[buf, Arg(help='Separator; the default is a line break.')] = B'\n',
scoped: Param[bool, Arg.Switch('-s', help=(
'Maintain chunk scope; i.e. do not turn all input chunks visible.'))] = False
):
super().__init__(separator=separator, scoped=scoped)
self.separate = False
def filter(self, chunks):
it = iter(chunks)
try:
chunk = next(it)
except StopIteration:
return
self.separate = True
for upcoming in it:
if not self.args.scoped:
chunk.visible = True
yield chunk
chunk = upcoming
self.separate = False
yield chunk
def process(self, data):
yield data
if self.separate:
yield self.args.separator
class sbs(sep):
"""
Separate all chunks By a single Space character.
"""
def __init__(self):
super().__init__(B' ', False)
class sbc(sep):
"""
Separate all chunks By a single Comma character.
"""
def __init__(self):
super().__init__(B',', False)