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
Provenance
- Commit
075a77b- Tree
7ceb0de- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
5482865
3 files changed+79 added−3 removed
modified:click/__init__.py+3 added-3 removed
26 unmodified lines27 from .types import ParamType, File, Path, Choice, IntRange, STRING, INT, \28 FLOAT, BOOL, UUID29 30 # Utilities31-from .utils import echo, get_binary_stream, get_text_stream, \31+from .utils import echo, get_binary_stream, get_text_stream, open_file, \32 format_filename, get_app_dir33 34 # Terminal functions35 from .termui import prompt, confirm, get_terminal_size, echo_via_pager, \25 unmodified lines61 'ParamType', 'File', 'Path', 'Choice', 'IntRange', 'STRING', 'INT',62 'FLOAT', 'BOOL', 'UUID',63 64 # 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',67 68 # Terminal functions69 'prompt', 'confirm', 'get_terminal_size', 'echo_via_pager',70 'progressbar', 'clear', 'style', 'unstyle', 'secho', 'edit', 'launch',15 unmodified linesmodified:click/utils.py+51 added
190 unmodified lines191 def __exit__(self, exc_type, exc_value, tb):192 self.close_intelligently()193 194 195+class KeepOpenFile(object):196+197+ def __init__(self, file):198+ self._file = file199+200+ def __getattr__(self, name):201+ return getattr(self._file, name)202+203+ def __enter__(self):204+ return self205+206+ def __exit__(self, exc_type, exc_value, tb):207+ pass208+209+ def __repr__(self):210+ return repr(self._file)211+212+213 def 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 no106 unmodified lines323 raise TypeError('Unknown standard stream %r' % name)324 return opener(encoding, errors)325 326 327+def 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.332+333+ 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:337+338+ with open_file(filename) as f:339+ ...340+341+ .. versionadded:: 3.0342+343+ :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 f358+359+360 def 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 will62 unmodified linesmodified:docs/utils.rst+25 added
258 unmodified lines259 stdin_text = click.get_text_stream('stdin')260 stdout_binary = click.get_binary_stream('stdout')261 262 263+Intelligent File Opening264+------------------------265+266+.. versionadded:: 3.0267+268+Starting with Click 3.0 the logic for opening files from the :class:`File`269+type is exposed through the :func:`open_file` function. It can270+intelligently open stdin/stdout as well as any other file.271+272+Example::273+274+ import click275+276+ stdout = click.open_file('-', 'w')277+ test_file = click.open_file('test.txt', 'w')278+279+If stdin or stdout are returned, the return value is wrapped in a special280+file where the context manager will prevent the closing of the file. This281+makes the handling of standard streams transparent and you can always use282+it like this::283+284+ with click.open_file(filename, 'w') as f:285+ f.write('Hello World!\n')286+287+288 Finding Application Folders289 ---------------------------290 291 .. versionadded:: 2.070 unmodified lines