Skip to content
Myr

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

5 files+90 added−10 removed

modified:click/__init__.py+2 added-2 removed
31 unmodified lines
32 format_filename, get_app_dir3334# Terminal functions35from .termui import prompt, confirm, get_terminal_size, echo_via_pager, \36 progressbar, clear, style, unstyle, secho, edit36 progressbar, clear, style, unstyle, secho, edit, launch3738# Exceptions39from .exceptions import ClickException, UsageError, BadParameter, \40 FileError, Abort
24 unmodified lines
65 'get_app_dir',6667 # 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',7071 # Exceptions72 'ClickException', 'UsageError', 'BadParameter', 'FileError',73 'Abort',
10 unmodified lines
modified:click/_termui_impl.py+43 added
363 unmodified lines
364 f.close()365 return rv.decode('utf-8-sig').replace('\r\n', '\n')366 finally:367 os.unlink(name)368369370def open_url(url, wait=False, locate=False):371 import subprocess372373 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 url381382 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 1
modified:click/termui.py+26 added-8 removed
381 unmodified lines
382 `require_save` and `extension` are ignored.383384 If the editor cannot be opened a :exc:`UsageError` is raised.385386 Example usage::387388 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')393386 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 lines
405 extension=extension)406 if filename is None:407 return editor.edit(text)408 editor.edit_file(filename)409410411def 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.417418 Examples::419420 click.open('http://click.pocoo.org/')421 click.open('/my/downloaded/file', locate=True)422423 .. versionadded:: 2.0424425 :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 lines
53.. autofunction:: secho5455.. autofunction:: edit5657.. autofunction:: launch5859.. autofunction:: format_filename6061.. autofunction:: get_terminal_size62
101 unmodified lines
modified:docs/utils.rst+17 added
121 unmodified lines
122 import click123 click.edit(filename='/etc/passwd')124125126Launching Applications127----------------------128129.. versionadded:: 2.0130131Click supports launching applications through :func:`launch`. This can be132used to open the default application assocated with a URL or file type.133This for instance can be used to both launch web browsers as well as134picture viewers etc. In addition to that it can also launch the file135manager and automatically select the provided file.136137Example usage::138139 click.open('http://click.pocoo.org/')140 click.open('/my/downloaded/file.txt', locate=True)141142143Printing Filenames144------------------145146Because filenames might not be unicode, formatting them can be a bit
113 unmodified lines