Skip to content

Commit

Permalink
small fixes for v1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tomerfiliba committed Nov 9, 2013
1 parent 336dc51 commit ab9197d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
2 changes: 1 addition & 1 deletion build.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python
from plumbum import local, cli
from plumbum.utils import delete
from plumbum.path.utils import delete


class BuildProject(cli.Application):
Expand Down
40 changes: 14 additions & 26 deletions plumbum/cli/terminal.py
Expand Up @@ -90,6 +90,8 @@ def ask(question, default = None):
:param question: The question to ask
:param default: If ``None``, the user must answer. If ``True`` or ``False``, lack of response is
interpreted as the default option
:returns: the user's choice
"""
question = question.rstrip().rstrip("?").rstrip() + "?"
if default is None:
Expand Down Expand Up @@ -122,7 +124,7 @@ def choose(question, options, default = None):
:param default: If ``None``, the user must answer. Otherwise, lack of response is interpreted
as this answer
:returns: The
:returns: The user's choice
Example::
Expand Down Expand Up @@ -174,15 +176,15 @@ def choose(question, options, default = None):
return choices[choice]

def hexdump(data_or_stream, bytes_per_line = 16, aggregate = True):
"""Convert the given data (or stream) to hexdump-formatted lines, with possible aggregation of identical lines.
Returns a generator of formatted lines.
"""Convert the given bytes (or a stream with a buffering ``read()`` method) to hexdump-formatted lines,
with possible aggregation of identical lines. Returns a generator of formatted lines.
"""
if hasattr(data_or_stream, "read"):
def read_chunk():
while True:
buf = data_or_stream.read(bytes_per_line)
if not buf:
raise StopIteration()
break
yield buf
else:
def read_chunk():
Expand All @@ -203,30 +205,21 @@ def read_chunk():
skipped = False


def pager(rows, outfile = None, pagercmd = None):
"""Opens a pager (e.g., ``less``) to display the given text. Requires a terminal."""
def pager(rows, pagercmd = None):
"""Opens a pager (e.g., ``less``) to display the given text. Requires a terminal.
:param rows: a ``bytes`` or a list/iterator of "rows" (``bytes``)
:param pagercmd: the pager program to run. Defaults to ``less -RSin``
"""
if not pagercmd:
pagercmd = local["less"]

if not outfile:
outfile = None
elif isinstance(outfile, str):
outfile = open(outfile, "w")
elif not hasattr(outfile, "write"):
raise TypeError("outfile must support write()")
pagercmd = local["less"]["-RSin"]
if hasattr(rows, "splitlines"):
rows = rows.splitlines()

pg = pagercmd.popen(["-RSin"], stdout = None, stderr = None)
pg = pagercmd.popen(stdout = None, stderr = None)
try:
if outfile:
pg.stdin.write("Dumping output to %s...\n" % (outfile.name,))
pg.stdin.flush()

for row in rows:
line = "%s\n" % (row,)
if outfile:
outfile.write(line)
try:
pg.stdin.write(line)
pg.stdin.flush()
Expand All @@ -235,11 +228,6 @@ def pager(rows, outfile = None, pagercmd = None):
pg.stdin.close()
pg.wait()
finally:
if outfile:
try:
outfile.close()
except Exception:
pass
try:
rows.close()
except Exception:
Expand Down
2 changes: 1 addition & 1 deletion plumbum/version.py
@@ -1,3 +1,3 @@
version = (1, 4, 0)
version_string = "1.4.0"
release_date = "2013.11.01"
release_date = "2013.11.09"
13 changes: 12 additions & 1 deletion tests/test_cli.py
Expand Up @@ -3,7 +3,7 @@
import unittest
from plumbum import cli
from contextlib import contextmanager
from plumbum.cli.terminal import ask, choose
from plumbum.cli.terminal import ask, choose, hexdump
from plumbum.lib import six
# string/unicode issues
if six.PY3:
Expand Down Expand Up @@ -198,6 +198,17 @@ def test_choose(self):
with captured_stdout("foo\n\n") as stream:
self.assertEqual(choose("What is your favorite color?", ["blue", "yellow", "green"], default = "yellow"), "yellow")
self.assertEqual(stream.getvalue(), "What is your favorite color?\n(1) blue\n(2) yellow\n(3) green\nChoice [2]: Invalid choice, please try again\nChoice [2]: ")

def test_hexdump(self):
data = "hello world my name is queen marry" + "A" * 66 + "foo bar"
output = """\
000000 | 68 65 6c 6c 6f 20 77 6f 72 6c 64 20 6d 79 20 6e | hello world my n
000010 | 61 6d 65 20 69 73 20 71 75 65 65 6e 20 6d 61 72 | ame is queen mar
000020 | 72 79 41 41 41 41 41 41 41 41 41 41 41 41 41 41 | ryAAAAAAAAAAAAAA
000030 | 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 | AAAAAAAAAAAAAAAA
*
000060 | 41 41 41 41 66 6f 6f 20 62 61 72 | AAAAfoo bar"""
self.assertEqual("\n".join(hexdump(data)), output)


if __name__ == "__main__":
Expand Down

0 comments on commit ab9197d

Please sign in to comment.