Skip to content
Myr

Added open_file function.

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

authored by Armin Ronacher · committed by Armin Ronacher · parent 5482865 · Browse files · 3 files+79 added−3 removed
This commit

State

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

Diff stats

3 files+79 added−3 removed

  • other:+79 added−3 removed

3 files+79 added−3 removed

modified:click/__init__.py+3 added-3 removed
26 unmodified lines
27from .types import ParamType, File, Path, Choice, IntRange, STRING, INT, \28 FLOAT, BOOL, UUID2930# Utilities31from .utils import echo, get_binary_stream, get_text_stream, \31from .utils import echo, get_binary_stream, get_text_stream, open_file, \32 format_filename, get_app_dir3334# Terminal functions35from .termui import prompt, confirm, get_terminal_size, echo_via_pager, \
25 unmodified lines
61 'ParamType', 'File', 'Path', 'Choice', 'IntRange', 'STRING', 'INT',62 'FLOAT', 'BOOL', 'UUID',6364 # Utilities65 'echo', 'get_binary_stream', 'get_text_stream', 'format_filename',66 'get_app_dir',65 'echo', 'get_binary_stream', 'get_text_stream', 'open_file',66 'format_filename', 'get_app_dir',6768 # Terminal functions69 'prompt', 'confirm', 'get_terminal_size', 'echo_via_pager',70 'progressbar', 'clear', 'style', 'unstyle', 'secho', 'edit', 'launch',
15 unmodified lines
modified:click/utils.py+51 added
190 unmodified lines
191 def __exit__(self, exc_type, exc_value, tb):192 self.close_intelligently()193194195class KeepOpenFile(object):196197 def __init__(self, file):198 self._file = file199200 def __getattr__(self, name):201 return getattr(self._file, name)202203 def __enter__(self):204 return self205206 def __exit__(self, exc_type, exc_value, tb):207 pass208209 def __repr__(self):210 return repr(self._file)211212213def echo(message=None, file=None, nl=True, err=False):214 """Prints a message plus a newline to the given file or stdout. On215 first sight, this looks like the print function, but it has improved216 support for handling Unicode and binary data that does not fail no
106 unmodified lines
323 raise TypeError('Unknown standard stream %r' % name)324 return opener(encoding, errors)325326327def open_file(filename, mode='r', encoding=None, errors='strict',328 lazy=False, atomic=False):329 """This is similar to how the :class:`File` works but for manual330 usage. Files are opened non lazy by default. This can open regular331 files as well as stdin/stdout if ``'-'`` is passed.332333 If stdin/stdout is returned the stream is wrapped so that the context334 manager will not close the stream accidentally. This makes it possible335 to always use the function like this without having to worry to336 accidentally close a standard stream:337338 with open_file(filename) as f:339 ...340341 .. versionadded:: 3.0342343 :param filename: the name of the file to open (or ``'-'`` for stdin/stdout).344 :param mode: the mode in which to open the file.345 :param encoding: the encoding to use.346 :param errors: the error handling for this file.347 :param lazy: can be flipped to true to open the file lazily.348 :param atomic: in atomic mode writes go into a temporary file and it's349 moved on close.350 """351 if lazy:352 return LazyFile(filename, mode, encoding, errors, atomic=atomic)353 f, should_close = open_stream(filename, mode, encoding, errors,354 atomic=atomic)355 if not should_close:356 f = KeepOpenFile(f)357 return f358359360def format_filename(filename, shorten=False):361 """Formats a filename for user display. The main purpose of this362 function is to ensure that the filename can be displayed at all. This363 will decode the filename to unicode if necessary in a way that it will
62 unmodified lines
modified:docs/utils.rst+25 added
258 unmodified lines
259 stdin_text = click.get_text_stream('stdin')260 stdout_binary = click.get_binary_stream('stdout')261262263Intelligent File Opening264------------------------265266.. versionadded:: 3.0267268Starting with Click 3.0 the logic for opening files from the :class:`File`269type is exposed through the :func:`open_file` function. It can270intelligently open stdin/stdout as well as any other file.271272Example::273274 import click275276 stdout = click.open_file('-', 'w')277 test_file = click.open_file('test.txt', 'w')278279If stdin or stdout are returned, the return value is wrapped in a special280file where the context manager will prevent the closing of the file. This281makes the handling of standard streams transparent and you can always use282it like this::283284 with click.open_file(filename, 'w') as f:285 f.write('Hello World!\n')286287288Finding Application Folders289---------------------------290291.. versionadded:: 2.0
70 unmodified lines