Added click.launch
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 721e527 · Browse files · 5 files·+90 added−10 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+90 added−10 removed
- other:+90 added−10 removed
Provenance
- Commit
ba36392- Tree
19fefbd- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
721e527
5 files changed+90 added−10 removed
modified:click/__init__.py+2 added-2 removed
31 unmodified lines32 format_filename, get_app_dir33 34 # Terminal functions35 from .termui import prompt, confirm, get_terminal_size, echo_via_pager, \36- progressbar, clear, style, unstyle, secho, edit36+ progressbar, clear, style, unstyle, secho, edit, launch37 38 # Exceptions39 from .exceptions import ClickException, UsageError, BadParameter, \40 FileError, Abort24 unmodified lines65 'get_app_dir',66 67 # Terminal functions68 'prompt', 'confirm', 'get_terminal_size', 'echo_via_pager',69- 'progressbar', 'clear', 'style', 'unstyle', 'secho', 'edit',69+ 'progressbar', 'clear', 'style', 'unstyle', 'secho', 'edit', 'launch',70 71 # Exceptions72 'ClickException', 'UsageError', 'BadParameter', 'FileError',73 'Abort',10 unmodified linesmodified:click/_termui_impl.py+43 added
363 unmodified lines364 f.close()365 return rv.decode('utf-8-sig').replace('\r\n', '\n')366 finally:367 os.unlink(name)368+369+370+def open_url(url, wait=False, locate=False):371+ import subprocess372+373+ def _unquote_file(url):374+ try:375+ import urllib376+ except ImportError:377+ import urllib378+ if url.startswith('file://'):379+ url = urllib.unquote(url[7:])380+ return url381+382+ if sys.platform == 'darwin':383+ args = ['open']384+ if wait:385+ args.append('-W')386+ if locate:387+ args.append('-R')388+ args.append(_unquote_file(url))389+ return subprocess.Popen(args).wait()390+ elif sys.platform.startswith('win'):391+ if locate:392+ url = _unquote_file(url)393+ args = ['explorer', '/select,"%s"' % _unquote_file(url)]394+ else:395+ args = ['start']396+ if wait:397+ args.append('/WAIT')398+ args.append(url)399+ return subprocess.Popen(args).wait()400+ try:401+ if locate:402+ url = os.path.dirname(_unquote_file(url)) or '.'403+ else:404+ url = _unquote_file(url)405+ c = subprocess.Popen(['xdg-open', url])406+ if wait:407+ return c.wait()408+ return 0409+ except OSError:410+ return 1modified:click/termui.py+26 added-8 removed
381 unmodified lines382 `require_save` and `extension` are ignored.383 384 If the editor cannot be opened a :exc:`UsageError` is raised.385 386- Example usage::387-388- def get_commit_message():389- MARKER = '# Everything below is ignored\n'390- message = click.edit('\n\n' + MARKER)391- if message is not None:392- return message.split(MARKER, 1)[0].rstrip('\n')393-386 Note for Windows: to simplify cross platform usage the newlines are387 automatically converted from posix to windows and reverse. As such the388 message here will have ``\n`` as newline markers.389 15 unmodified lines405 extension=extension)406 if filename is None:407 return editor.edit(text)408 editor.edit_file(filename)409+410+411+def launch(url, wait=False, locate=False):412+ """This function launches the given URL (or filename) in the default413+ viewer application for this file type. If this is an executable it414+ might launch the executable in a new session. The return value is415+ the exit code of the launched application. Usually ``0`` indicates416+ success.417+418+ Examples::419+420+ click.open('http://click.pocoo.org/')421+ click.open('/my/downloaded/file', locate=True)422+423+ .. versionadded:: 2.0424+425+ :param url: url or filename of the thing to launch.426+ :param wait: waits for the program to stop.427+ :param locate: if this is set to `True` then instead of launching the428+ application associated with the URL it will attempt to429+ launch a file manager with the file located. This430+ might have weird effects if the url does not point to431+ the filesystem.432+ """433+ from _termui_impl import open_url434+ return open_url(url, wait=wait, locate=locate)modified:docs/api.rst+2 added
52 unmodified lines53 .. autofunction:: secho54 55 .. autofunction:: edit56 57+.. autofunction:: launch58+59 .. autofunction:: format_filename60 61 .. autofunction:: get_terminal_size62 101 unmodified linesmodified:docs/utils.rst+17 added
121 unmodified lines122 import click123 click.edit(filename='/etc/passwd')124 125 126+Launching Applications127+----------------------128+129+.. versionadded:: 2.0130+131+Click supports launching applications through :func:`launch`. This can be132+used to open the default application assocated with a URL or file type.133+This for instance can be used to both launch web browsers as well as134+picture viewers etc. In addition to that it can also launch the file135+manager and automatically select the provided file.136+137+Example usage::138+139+ click.open('http://click.pocoo.org/')140+ click.open('/my/downloaded/file.txt', locate=True)141+142+143 Printing Filenames144 ------------------145 146 Because filenames might not be unicode, formatting them can be a bit113 unmodified lines