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
Provenance
- Commit
31043c3- Tree
7884639- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
39b5df2
5 files changed+51 added−3 removed
modified:click/__init__.py+3 added-2 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_stream31+from .utils import echo, get_binary_stream, get_text_stream, \32+ format_filename33 34 # Terminal functions35 from .termui import prompt, confirm, get_terminal_size, echo_via_pager36 21 unmodified lines58 'ParamType', 'File', 'Path', 'Choice', 'IntRange', 'STRING', 'INT',59 'FLOAT', 'BOOL', 'UUID',60 61 # Utilities61- 'echo', 'get_binary_stream', 'get_text_stream',62+ 'echo', 'get_binary_stream', 'get_text_stream', 'format_filename',63 64 # Terminal functions65 'prompt', 'confirm', 'get_terminal_size', 'echo_via_pager',66 9 unmodified linesmodified:click/utils.py+19 added-1 removed
1+import os2 import sys3 from collections import deque4 5 from ._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_ui7 8 if not PY2:9 from ._compat import _find_binary_writer10 244 unmodified lines255 opener = text_streams.get(name)256 if opener is None:257 raise TypeError('Unknown standard stream %r' % name)258 return opener(encoding, errors)259+260+261+def 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.267+268+ :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 lines41 .. autofunction:: prompt42 43 .. autofunction:: confirm44 45+.. autofunction:: format_filename46+47 .. autofunction:: get_terminal_size48 49 .. autofunction:: get_binary_stream50 95 unmodified linesmodified:docs/utils.rst+18 added
29 unmodified lines30 31 click.echo(b'\xe2\x98\x83', nl=False)32 33 34+Printing Filenames35+------------------36+37+Because filenames might not be unicode formatting them can be a bit38+tricky. Generally this is easier on Python 2 than 3 as you can just write39+the bytes to stdout with the print function, but at least on Python 3 you40+will need to always operate in unicode.41+42+The way this works with click is through the :func:`format_filename`43+function. It does a best effort conversion of the filename to Unicode and44+will never fail. This makes it possible to use these filenames in the45+context of a full unicode string.46+47+Example::48+49+ click.echo('Path: %s' % click.format_filename(b'foo.txt'))50+51+52 Standard Streams53 ----------------54 55 For command line utilities it's very important to get access to input and19 unmodified linesmodified:tests/test_utils.py+9 added
7 unmodified lines8 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'12+13+14+def 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'