forked from DasIch/brownie
/
__init__.py
584 lines (479 loc) · 18.4 KB
/
__init__.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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# coding: utf-8
"""
brownie.terminal
~~~~~~~~~~~~~~~~
Utilities for handling simple output on a terminal.
.. versionadded:: 0.6
:copyright: 2010-2011 by Daniel Neuhäuser
:license: BSD, see LICENSE.rst for details
"""
from __future__ import with_statement
import re
import os
import sys
import codecs
import struct
try:
# all available on unix platforms
import fcntl
import termios
except ImportError: # pragma: no cover
fcntl = None
termios = None
from itertools import izip, imap
from contextlib import contextmanager
from brownie.datastructures import namedtuple
from brownie.terminal.progress import ProgressBar
_ansi_sequence = '\033[%sm'
ATTRIBUTES = dict((key, _ansi_sequence % value) for key, value in [
('reset','00'),
('bold', '01'),
('faint', '02'),
('standout', '03'),
('underline', '04'),
('blink', '05')
])
TEXT_COLOURS = {'reset': _ansi_sequence % '39'}
BACKGROUND_COLOURS = {'reset': _ansi_sequence % '49'}
_colour_names = [
'black',
'red',
'green',
'yellow',
'blue',
'purple',
'teal',
'white'
]
for i, name in enumerate(_colour_names):
TEXT_COLOURS[name] = _ansi_sequence % str(i + 30)
BACKGROUND_COLOURS[name] = _ansi_sequence % (i + 40)
Dimensions = namedtuple('Dimensions', ['height', 'width'], doc="""
A namedtuple representing the dimensions of a terminal.
:param height:
The height of the terminal.
:param width:
The width of the terminal.
""")
class TerminalWriter(object):
"""
This is a helper for dealing with output to a terminal.
:param stream:
A stream which takes unicode for writing.
:param prefix:
A prefix used when an entire line is written.
:param indent:
String used for indentation.
:param autoescape:
Defines if everything written is escaped (unless explicitly turned
off), see :func:`escape` for more information.
:param ignore_options:
Defines if options should be ignored or not, per default options are
ignored if the `stream` is not a tty.
After each call resulting in visible characters to be written to the
`stream` the stream is flushed, certain methods allow to override this
behaviour.
"""
#: Specifies the default terminal width.
default_width = 80
@classmethod
def from_bytestream(cls, stream, encoding=None, errors='strict', **kwargs):
"""
Returns a :class:`TerminalWriter` for the given byte `stream`.
If an encoding cannot be determined from the stream it will fallback
to the given `encoding`, if it is `None` :meth:`sys.getdefaultencoding`
will be used.
Should an error occur during encoding you can specify what should
happen with the `errors` parameter:
``'strict'``
Raise an exception if an error occurs.
``'ignore'``
Ignore the characters for which the error occurs, these will be
removed from the string.
``'replace'``
Replaces the characters for which the error occurs with 'safe'
characters, usually '?'.
"""
encoding = getattr(stream, 'encoding', encoding)
if encoding is None:
encoding = sys.getdefaultencoding()
return cls(
codecs.lookup(encoding).streamwriter(stream, errors),
**kwargs
)
def __init__(self, stream, prefix=u'', indent=' ' * 4, autoescape=True,
ignore_options=None):
#: The stream to which the output is written.
self.stream = stream
#: The prefix used by :meth:`writeline`.
self.prefix = prefix
#: The string used for indentation.
self.indent_string = indent
#: ``True`` if escaping should be done automatically.
self.autoescape = autoescape
is_a_tty = getattr(stream, 'isatty', lambda: False)()
if ignore_options is None and is_a_tty:
self.ignore_options = False
elif ignore_options is None:
self.ignore_options = True
else:
self.ignore_options = ignore_options
if is_a_tty and termios and hasattr(stream, 'fileno'):
self.control_characters = [
c for c in termios.tcgetattr(stream.fileno())[-1]
if isinstance(c, basestring)
]
else:
# just to be on the safe side...
self.control_characters = map(chr, range(32) + [127])
self.ansi_re = re.compile('[%s]' % ''.join(self.control_characters))
self.indentation_level = 0
def escape(self, string):
"""
Escapes all control characters in the given `string`.
This is useful if you are dealing with 'untrusted' strings you want to
write to a file, stdout or stderr which may be viewed using tools such
as `cat` which execute ANSI escape sequences.
.. seealso::
http://www.ush.it/team/ush/hack_httpd_escape/adv.txt
"""
return self.ansi_re.sub(
lambda m: m.group().encode('unicode-escape'),
string
)
def get_dimensions(self):
"""
Returns a :class:`Dimensions` object.
May raise :exc:`NotImplementedError` depending on the stream or
platform.
"""
try:
fileno = self.stream.fileno()
except AttributeError:
pass
else:
return Dimensions(*struct.unpack('hhhh', fcntl.ioctl(
fileno, termios.TIOCGWINSZ, '\000' * 8)
)[:2])
raise NotImplementedError(
'not implemented for the given stream or platform'
)
def get_width(self, default=None):
"""
Returns the width of the terminal.
This falls back to the `COLUMNS` environment variable and if that fails
to :attr:`default_width` unless `default` is not None, in which case
`default` would be used.
Therefore the returned value might not not be at all correct.
"""
default = self.default_width if default is None else default
try:
_, width = self.get_dimensions()
except NotImplementedError:
width = int(os.environ.get('COLUMNS', default))
return width
def get_usable_width(self, default_width=None):
"""
Returns the width of the terminal remaining once the prefix and
indentation has been written.
:param default_width:
The width which is assumed per default for the terminal, see
:meth:`get_width` for more information.
.. warning::
Tabs are considered to have a length of 1. This problem may be
solved in the future until then it is recommended to avoid tabs.
"""
return self.get_width(default_width) - (
len(self.prefix) + len(self.indent_string * self.indentation_level)
)
def indent(self):
"""
Indent the following lines with the given :attr:`indent`.
"""
self.indentation_level += 1
def dedent(self):
"""
Dedent the following lines.
"""
self.indentation_level -= 1
@contextmanager
def options(self, text_colour=None, background_colour=None, bold=None,
faint=None, standout=None, underline=None, blink=None,
indentation=False, escape=None):
"""
A contextmanager which allows you to set certain options for the
following writes.
:param text_colour:
The desired text colour.
:param background_colour:
The desired background colour.
:param bold:
If present the text is displayed bold.
:param faint:
If present the text is displayed faint.
:param standout:
If present the text stands out.
:param underline:
If present the text is underlined.
:param blink:
If present the text blinks.
:param indentation:
Adds a level of indentation if ``True``.
:param escape:
Overrides the escaping behaviour for this block.
.. note::
The underlying terminal may support only certain options, especially
the attributes (`bold`, `faint`, `standout` and `blink`) are not
necessarily available.
The following colours are available, the exact colour varies between
terminals and their configuration.
.. ansi-block::
:string_escape:
Colors
======
\x1b[30mblack\x1b[0m \x1b[33myellow\x1b[0m \x1b[36mteal\x1b[0m
\x1b[31mred\x1b[0m \x1b[34mblue\x1b[0m \x1b[37mwhite\x1b[0m
\x1b[32mgreen\x1b[0m \x1b[35mpurple\x1b[0m
"""
attributes = [
name for name, using in [
('bold', bold), ('faint', faint), ('standout', standout),
('underline', underline), ('blink', blink)
]
if using
]
if not self.ignore_options:
if text_colour:
self.stream.write(TEXT_COLOURS[text_colour])
if background_colour:
self.stream.write(BACKGROUND_COLOURS[background_colour])
for attribute in attributes:
if attribute:
self.stream.write(ATTRIBUTES[attribute])
if indentation:
self.indent()
if escape is not None:
previous_setting = self.autoescape
self.autoescape = escape
try:
yield self
finally:
if not self.ignore_options:
if text_colour:
self.stream.write(TEXT_COLOURS['reset'])
if background_colour:
self.stream.write(BACKGROUND_COLOURS['reset'])
if any(attributes):
self.stream.write(ATTRIBUTES['reset'])
if indentation:
self.dedent()
if escape is not None:
self.autoescape = previous_setting
def begin_line(self):
"""
Writes the prefix and indentation to the stream.
"""
self.write(
self.prefix + (self.indent_string * self.indentation_level),
escape=False,
flush=False
)
@contextmanager
def line(self):
"""
A contextmanager which can be used instead of :meth:`writeline`.
This is useful if you want to write a line with multiple different
options.
"""
self.begin_line()
try:
yield
finally:
self.newline()
def newline(self):
"""
Writes a newline to the :attr:`stream`.
"""
self.write('\n', escape=False, flush=False)
def should_escape(self, escape):
"""
Returns :attr:`autoescape` if `escape` is None otherwise `escape`.
"""
return self.autoescape if escape is None else escape
def write(self, string, escape=None, flush=True, **options):
"""
Writes the `given` string to the :attr:`stream`.
:param escape:
Overrides :attr:`autoescape` if given.
:param options:
Options for this operation, see :meth:`options`.
:param flush:
If ``True`` flushes the stream.
"""
with self.options(**options):
self.stream.write(
self.escape(string) if self.should_escape(escape) else string
)
if flush:
self.stream.flush()
def writeline(self, line, escape=None, flush=True, **options):
"""
Writes the given `line` to the :attr:`stream` respecting indentation.
:param escape:
Overrides :attr:`autoescape` if given.
:param options:
Options for this operation, see :meth:`options`.
:param flush:
If ``True`` flushes the stream.
"""
with self.options(**options):
self.begin_line()
self.write(line, escape=self.should_escape(escape), flush=False)
self.newline()
if flush:
self.stream.flush()
def writelines(self, lines, escape=None, flush=True, **options):
"""
Writes each line in the given iterable to the :attr:`stream` respecting
indentation.
:param escape:
Overrides :attr:`autoescape` if given.
:param options:
Options for this operation, see :meth:`options`.
:param flush:
If ``True`` flushes the stream.
"""
with self.options(**options):
for line in lines:
self.writeline(line, escape=escape, flush=False)
if flush:
self.stream.flush()
def hr(self, character=u'-'):
"""
Writes a horizontal ruler with the width of the terminal to the
:attr:`stream`.
:param character:
Specifies the character used for the ruler.
"""
self.writeline(character * self.get_width())
def table(self, content, head=None, padding=1):
"""
Writes a table using a list of rows (`content`) and an optional `head`.
:param padding:
Specifies the padding used for each cell to the left and right.
::
>>> import sys
>>> from brownie.terminal import TerminalWriter
>>> writer = TerminalWriter.from_bytestream(sys.stdout)
>>> writer.table([
... [u'foo', u'bar'],
... [u'spam', u'eggs']
... ])
foo | bar
spam | eggs
<BLANKLINE>
>>> writer.table(
... [
... [u'foo', u'bar'],
... [u'spam', u'eggs']
... ],
... [u'hello', u'world']
... )
hello | world
------+------
foo | bar
spam | eggs
<BLANKLINE>
"""
if not content:
raise ValueError()
if head is not None and len(head) != len(content[0]):
raise ValueError()
if any(len(content[0]) != len(row) for row in content[1:]):
raise ValueError()
all_rows = [head] if head is not None else [] + content
cell_lengths = [
max(map(len, column)) for column in izip(*all_rows)
]
def make_line(row):
return u'|'.join(
u' ' * padding + cell.ljust(cell_lengths[i]) + u' ' * padding
for i, cell in enumerate(imap(self.escape, row))
).strip()
result = map(make_line, content)
if head:
line = make_line(head)
self.writeline(line, escape=False)
self.writeline(
re.sub(r'[^\|]', '-', line)
.replace('|', '+')
.ljust(max(map(len, result)), '-'),
escape=False
)
self.writelines(result, flush=False)
self.newline()
self.stream.flush()
def progress(self, description, maxsteps=None, widgets=None):
"""
Returns a :class:`~brownie.terminal.progress.ProgressBar` object
which can be used to write the current progress to the stream.
The progress bar is created from the `description` which is a string
with the following syntax:
Widgets -- the parts of the progress bar which are changed with each
update -- are represented in the form ``$[a-zA-Z]+``.
Some widgets required that you provide an initial value, this can be
done by adding ``:string`` where string is either ``[a-zA-Z]+`` or a
double-quoted string.
If you want to escape a ``$`` you simply precede it with another ``$``,
so ``$$foo` will not be treated as a widget and in the progress bar
``$foo`` will be shown.
Quotes (``"``) in strings can be escaped with a backslash (``\``).
The following widgets are available:
`hint`
Shows a string of text that can be given using the `hint` argument
at any update performed with :meth:`.ProgressBar.init`,
:meth:`.ProgressBar.next` or :meth:`.ProgressBar.finish`. If the
argument is not given an empty string is used instead.
`percentage`
Shows the progress in percent; this requires `maxsteps` to be set.
`bar`
Shows a simple bar which moves which each update not corresponding
with the progress being made. This is useful if you just want to
show that something is happening.
`sizedbar`
Shows a simple progress bar which is being filled corresponding
to the percentage of progress. This requires `maxsteps` to be
set.
`step`
Shows the current at maximum number of steps as ``step of steps``,
this method takes an initial value determining the unit of each
step e.g. if each step represents a byte and you choose `bytes`
as a unit a reasonable prefix will be chosen.
Supported units:
- `bytes` - Uses a binary prefix.
This requires `maxsteps` to be set.
`time`
Shows the elapsed time in hours, minutes and seconds.
`speed`
Shows the speed in bytes (or with a reasonable prefix) per seconds,
this assumes that each `step` represents a byte.
If you want to implement your own widget(s) take a look at
:class:`brownie.terminal.progress.Widget`, you can use them by passing
them in a dictionary (mapping the name to the widget class) via the
`widgets` argument. You might also want to take a look at the source
code of the built-in widgets.
There are two things you have to look out for:
:class:`~brownie.terminal.progress.ProgressBar` objects are not
reusable if you need another object, call this method again. If you
attempt to write to the :attr:`stream` while using a progress bar the
behaviour is undefined.
.. seealso:: :ref:`creating-widgets`
"""
return ProgressBar.from_string(
description, self, maxsteps=maxsteps, widgets=None
)
def __repr__(self):
return '%s(%r, prefix=%r, indent=%r, autoescape=%r)' % (
self.__class__.__name__, self.stream, self.prefix,
self.indent_string, self.autoescape
)