forked from balanced/balanced-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
93 lines (70 loc) · 2.22 KB
/
util.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
import sys
class _Writer(object):
def __init__(self):
self._indent = []
self._fragment = False
def __enter__(self):
self.indent()
return self
def __exit__(self, type, value, traceback):
self.outdent()
def indent(self, l=4):
self._indent.append(' ' * l)
def outdent(self):
self._indent.pop()
@property
def _indentation(self):
if self._fragment:
i = ''
else:
i = ''.join(self._indent)
return i
def line(self, *args):
raise NotImplementedError()
def fragment(self, *args):
raise NotImplementedError()
class _IOWriter(_Writer):
def __init__(self, io=sys.stdout):
super(_IOWriter, self).__init__()
self.io = io
def line(self, *args):
self.io.write(self._indentation + ' '.join(map(str, args)))
self.io.write('\n')
self._fragment = False
def fragment(self, *args):
self.io.write(self._indentation + ' '.join(map(str, args)))
self._fragment = True
class LineWriter(_Writer):
def __init__(self, lines):
super(LineWriter, self).__init__()
self.lines = lines
self.buffer = ''
def line(self, *args):
line = ''
if self.buffer:
line += self.buffer
self.buffer = ''
line += self._indentation + ' '.join(map(str, args))
self.lines.append(line if line.strip() else '')
self._fragment = False
def fragment(self, *args):
fragment = self._indentation + ' '.join(map(str, args))
self.buffer += fragment
self._fragment = True
class ViewWriter(_Writer):
def __init__(self, view):
super(ViewWriter, self).__init__()
self.view = view
self.buffer = ''
def line(self, *args):
line = ''
if self.buffer:
line += self.buffer
self.buffer = ''
line += self._indentation + ' '.join(map(str, args))
self.view.append(line if line.strip() else '', '<autopilo>')
self._fragment = False
def fragment(self, *args):
fragment = self._indentation + ' '.join(map(str, args))
self.buffer += fragment
self._fragment = True