Skip to content
Myr

Added filename format 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 39b5df2 · Browse files · 5 files+51 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

5 files+51 added−3 removed

  • tests:+9 added−0 removed
  • other:+42 added−3 removed

5 files+51 added−3 removed

modified:click/__init__.py+3 added-2 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_stream31from .utils import echo, get_binary_stream, get_text_stream, \32 format_filename3334# Terminal functions35from .termui import prompt, confirm, get_terminal_size, echo_via_pager36
21 unmodified lines
58 'ParamType', 'File', 'Path', 'Choice', 'IntRange', 'STRING', 'INT',59 'FLOAT', 'BOOL', 'UUID',6061 # Utilities61 'echo', 'get_binary_stream', 'get_text_stream',62 'echo', 'get_binary_stream', 'get_text_stream', 'format_filename',6364 # Terminal functions65 'prompt', 'confirm', 'get_terminal_size', 'echo_via_pager',66
9 unmodified lines
modified:click/utils.py+19 added-1 removed
1import os2import sys3from collections import deque45from ._compat import text_type, open_stream, get_streerror, string_types, \5 PY2, get_best_encoding, binary_streams, text_streams6 PY2, get_best_encoding, binary_streams, text_streams, filename_to_ui78if not PY2:9 from ._compat import _find_binary_writer10
244 unmodified lines
255 opener = text_streams.get(name)256 if opener is None:257 raise TypeError('Unknown standard stream %r' % name)258 return opener(encoding, errors)259260261def format_filename(filename, shorten=False):262 """Formats a filename for user display. The main purpose of this263 function is to ensure that the filename can be displayed at all. This264 will decode the filename to unicode if necessary in a way that it will265 not fail. Optionally it can shorten the filename to not include the266 full path to the filename.267268 :param filename: formats a filename for UI display. This will also convert269 the filename into unicode without failing.270 :param shorten: this optionally shortens the filename to strip of the271 path that leads up to it.272 """273 if shorten:274 filename = os.path.basename(filename)275 return filename_to_ui(filename)
modified:docs/api.rst+2 added
40 unmodified lines
41.. autofunction:: prompt4243.. autofunction:: confirm4445.. autofunction:: format_filename4647.. autofunction:: get_terminal_size4849.. autofunction:: get_binary_stream50
95 unmodified lines
modified:docs/utils.rst+18 added
29 unmodified lines
3031 click.echo(b'\xe2\x98\x83', nl=False)323334Printing Filenames35------------------3637Because filenames might not be unicode formatting them can be a bit38tricky. Generally this is easier on Python 2 than 3 as you can just write39the bytes to stdout with the print function, but at least on Python 3 you40will need to always operate in unicode.4142The way this works with click is through the :func:`format_filename`43function. It does a best effort conversion of the filename to Unicode and44will never fail. This makes it possible to use these filenames in the45context of a full unicode string.4647Example::4849 click.echo('Path: %s' % click.format_filename(b'foo.txt'))505152Standard Streams53----------------5455For command line utilities it's very important to get access to input and
19 unmodified lines
modified:tests/test_utils.py+9 added
7 unmodified lines
8 click.echo(42, nl=False)9 click.echo(b'a', nl=False)10 bytes = out.getvalue()11 assert bytes == b'\xe2\x98\x83\nDD\n42a'121314def test_filename_formatting():15 assert click.format_filename(b'foo.txt') == 'foo.txt'16 assert click.format_filename(b'/x/foo.txt') == '/x/foo.txt'17 assert click.format_filename(u'/x/foo.txt') == '/x/foo.txt'18 assert click.format_filename(u'/x/foo.txt', shorten=True) == 'foo.txt'19 assert click.format_filename('/x/foo\xff.txt', shorten=True) \20 == u'foo\ufffd.txt'