Skip to content
Myr

Make click.utils.KeepOpenFile() and LazyFile() iterators.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Kevin Wurster · committed by Kevin Wurster · parent 099b549 · Browse files · 2 files+29 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+29 added−1 removed

  • tests:+22 added−1 removed
  • other:+7 added−0 removed

2 files+29 added−1 removed

modified:click/utils.py+7 added
189 unmodified lines
190191 def __exit__(self, exc_type, exc_value, tb):192 self.close_intelligently()193194 def __iter__(self):195 self.open()196 return iter(self._f)197198199class KeepOpenFile(object):200201 def __init__(self, file):202 self._file = file
9 unmodified lines
212213 def __repr__(self):214 return repr(self._file)215216 def __iter__(self):217 return iter(self._file)218219220def echo(message=None, file=None, nl=True, err=False, color=None):221 """Prints a message plus a newline to the given file or stdout. On222 first sight, this looks like the print function, but it has improved223 support for handling Unicode and binary data that does not fail no
214 unmodified lines
modified:tests/test_utils.py+22 added-1 removed
1import os2import sys3import click34import click5import click.utils6import click._termui_impl789def test_echo(runner):
217 unmodified lines
227228 result = runner.invoke(cli, ['-'], input='foobar')229 assert result.exception is None230 assert result.output == 'foobar\nmeep\n'231232233def test_iter_keepopenfile(tmpdir):234235 expected = list(map(str, range(10)))236 p = tmpdir.mkdir('testdir').join('testfile')237 p.write(os.linesep.join(expected))238 f = p.open()239 for e_line, a_line in zip(expected, click.utils.KeepOpenFile(f)):240 assert e_line == a_line.strip()241242243def test_iter_lazyfile(tmpdir):244245 expected = list(map(str, range(10)))246 p = tmpdir.mkdir('testdir').join('testfile')247 p.write(os.linesep.join(expected))248 f = p.open()249 for e_line, a_line in zip(expected, click.utils.LazyFile(f.name)):250 assert e_line == a_line.strip()