Mercurial > repos > shellac > guppy_basecaller
annotate env/lib/python3.7/site-packages/click/utils.py @ 2:6af9afd405e9 draft
"planemo upload commit 0a63dd5f4d38a1f6944587f52a8cd79874177fc1"
| author | shellac | 
|---|---|
| date | Thu, 14 May 2020 14:56:58 -0400 | 
| parents | 26e78fe6e8c4 | 
| children | 
| rev | line source | 
|---|---|
| 0 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 1 import os | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 2 import sys | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 3 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 4 from ._compat import _default_text_stderr | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 5 from ._compat import _default_text_stdout | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 6 from ._compat import auto_wrap_for_ansi | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 7 from ._compat import binary_streams | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 8 from ._compat import filename_to_ui | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 9 from ._compat import get_filesystem_encoding | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 10 from ._compat import get_streerror | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 11 from ._compat import is_bytes | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 12 from ._compat import open_stream | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 13 from ._compat import PY2 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 14 from ._compat import should_strip_ansi | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 15 from ._compat import string_types | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 16 from ._compat import strip_ansi | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 17 from ._compat import text_streams | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 18 from ._compat import text_type | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 19 from ._compat import WIN | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 20 from .globals import resolve_color_default | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 21 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 22 if not PY2: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 23 from ._compat import _find_binary_writer | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 24 elif WIN: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 25 from ._winconsole import _get_windows_argv | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 26 from ._winconsole import _hash_py_argv | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 27 from ._winconsole import _initial_argv_hash | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 28 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 29 echo_native_types = string_types + (bytes, bytearray) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 30 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 31 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 32 def _posixify(name): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 33 return "-".join(name.split()).lower() | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 34 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 35 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 36 def safecall(func): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 37 """Wraps a function so that it swallows exceptions.""" | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 38 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 39 def wrapper(*args, **kwargs): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 40 try: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 41 return func(*args, **kwargs) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 42 except Exception: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 43 pass | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 44 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 45 return wrapper | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 46 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 47 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 48 def make_str(value): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 49 """Converts a value into a valid string.""" | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 50 if isinstance(value, bytes): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 51 try: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 52 return value.decode(get_filesystem_encoding()) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 53 except UnicodeError: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 54 return value.decode("utf-8", "replace") | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 55 return text_type(value) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 56 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 57 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 58 def make_default_short_help(help, max_length=45): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 59 """Return a condensed version of help string.""" | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 60 words = help.split() | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 61 total_length = 0 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 62 result = [] | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 63 done = False | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 64 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 65 for word in words: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 66 if word[-1:] == ".": | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 67 done = True | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 68 new_length = 1 + len(word) if result else len(word) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 69 if total_length + new_length > max_length: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 70 result.append("...") | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 71 done = True | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 72 else: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 73 if result: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 74 result.append(" ") | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 75 result.append(word) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 76 if done: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 77 break | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 78 total_length += new_length | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 79 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 80 return "".join(result) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 81 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 82 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 83 class LazyFile(object): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 84 """A lazy file works like a regular file but it does not fully open | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 85 the file but it does perform some basic checks early to see if the | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 86 filename parameter does make sense. This is useful for safely opening | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 87 files for writing. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 88 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 89 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 90 def __init__( | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 91 self, filename, mode="r", encoding=None, errors="strict", atomic=False | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 92 ): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 93 self.name = filename | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 94 self.mode = mode | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 95 self.encoding = encoding | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 96 self.errors = errors | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 97 self.atomic = atomic | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 98 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 99 if filename == "-": | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 100 self._f, self.should_close = open_stream(filename, mode, encoding, errors) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 101 else: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 102 if "r" in mode: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 103 # Open and close the file in case we're opening it for | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 104 # reading so that we can catch at least some errors in | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 105 # some cases early. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 106 open(filename, mode).close() | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 107 self._f = None | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 108 self.should_close = True | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 109 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 110 def __getattr__(self, name): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 111 return getattr(self.open(), name) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 112 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 113 def __repr__(self): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 114 if self._f is not None: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 115 return repr(self._f) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 116 return "<unopened file '{}' {}>".format(self.name, self.mode) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 117 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 118 def open(self): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 119 """Opens the file if it's not yet open. This call might fail with | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 120 a :exc:`FileError`. Not handling this error will produce an error | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 121 that Click shows. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 122 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 123 if self._f is not None: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 124 return self._f | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 125 try: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 126 rv, self.should_close = open_stream( | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 127 self.name, self.mode, self.encoding, self.errors, atomic=self.atomic | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 128 ) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 129 except (IOError, OSError) as e: # noqa: E402 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 130 from .exceptions import FileError | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 131 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 132 raise FileError(self.name, hint=get_streerror(e)) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 133 self._f = rv | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 134 return rv | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 135 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 136 def close(self): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 137 """Closes the underlying file, no matter what.""" | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 138 if self._f is not None: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 139 self._f.close() | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 140 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 141 def close_intelligently(self): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 142 """This function only closes the file if it was opened by the lazy | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 143 file wrapper. For instance this will never close stdin. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 144 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 145 if self.should_close: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 146 self.close() | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 147 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 148 def __enter__(self): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 149 return self | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 150 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 151 def __exit__(self, exc_type, exc_value, tb): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 152 self.close_intelligently() | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 153 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 154 def __iter__(self): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 155 self.open() | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 156 return iter(self._f) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 157 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 158 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 159 class KeepOpenFile(object): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 160 def __init__(self, file): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 161 self._file = file | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 162 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 163 def __getattr__(self, name): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 164 return getattr(self._file, name) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 165 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 166 def __enter__(self): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 167 return self | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 168 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 169 def __exit__(self, exc_type, exc_value, tb): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 170 pass | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 171 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 172 def __repr__(self): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 173 return repr(self._file) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 174 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 175 def __iter__(self): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 176 return iter(self._file) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 177 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 178 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 179 def echo(message=None, file=None, nl=True, err=False, color=None): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 180 """Prints a message plus a newline to the given file or stdout. On | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 181 first sight, this looks like the print function, but it has improved | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 182 support for handling Unicode and binary data that does not fail no | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 183 matter how badly configured the system is. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 184 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 185 Primarily it means that you can print binary data as well as Unicode | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 186 data on both 2.x and 3.x to the given file in the most appropriate way | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 187 possible. This is a very carefree function in that it will try its | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 188 best to not fail. As of Click 6.0 this includes support for unicode | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 189 output on the Windows console. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 190 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 191 In addition to that, if `colorama`_ is installed, the echo function will | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 192 also support clever handling of ANSI codes. Essentially it will then | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 193 do the following: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 194 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 195 - add transparent handling of ANSI color codes on Windows. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 196 - hide ANSI codes automatically if the destination file is not a | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 197 terminal. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 198 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 199 .. _colorama: https://pypi.org/project/colorama/ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 200 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 201 .. versionchanged:: 6.0 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 202 As of Click 6.0 the echo function will properly support unicode | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 203 output on the windows console. Not that click does not modify | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 204 the interpreter in any way which means that `sys.stdout` or the | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 205 print statement or function will still not provide unicode support. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 206 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 207 .. versionchanged:: 2.0 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 208 Starting with version 2.0 of Click, the echo function will work | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 209 with colorama if it's installed. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 210 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 211 .. versionadded:: 3.0 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 212 The `err` parameter was added. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 213 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 214 .. versionchanged:: 4.0 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 215 Added the `color` flag. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 216 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 217 :param message: the message to print | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 218 :param file: the file to write to (defaults to ``stdout``) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 219 :param err: if set to true the file defaults to ``stderr`` instead of | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 220 ``stdout``. This is faster and easier than calling | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 221 :func:`get_text_stderr` yourself. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 222 :param nl: if set to `True` (the default) a newline is printed afterwards. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 223 :param color: controls if the terminal supports ANSI colors or not. The | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 224 default is autodetection. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 225 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 226 if file is None: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 227 if err: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 228 file = _default_text_stderr() | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 229 else: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 230 file = _default_text_stdout() | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 231 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 232 # Convert non bytes/text into the native string type. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 233 if message is not None and not isinstance(message, echo_native_types): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 234 message = text_type(message) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 235 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 236 if nl: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 237 message = message or u"" | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 238 if isinstance(message, text_type): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 239 message += u"\n" | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 240 else: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 241 message += b"\n" | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 242 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 243 # If there is a message, and we're in Python 3, and the value looks | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 244 # like bytes, we manually need to find the binary stream and write the | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 245 # message in there. This is done separately so that most stream | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 246 # types will work as you would expect. Eg: you can write to StringIO | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 247 # for other cases. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 248 if message and not PY2 and is_bytes(message): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 249 binary_file = _find_binary_writer(file) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 250 if binary_file is not None: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 251 file.flush() | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 252 binary_file.write(message) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 253 binary_file.flush() | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 254 return | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 255 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 256 # ANSI-style support. If there is no message or we are dealing with | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 257 # bytes nothing is happening. If we are connected to a file we want | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 258 # to strip colors. If we are on windows we either wrap the stream | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 259 # to strip the color or we use the colorama support to translate the | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 260 # ansi codes to API calls. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 261 if message and not is_bytes(message): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 262 color = resolve_color_default(color) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 263 if should_strip_ansi(file, color): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 264 message = strip_ansi(message) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 265 elif WIN: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 266 if auto_wrap_for_ansi is not None: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 267 file = auto_wrap_for_ansi(file) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 268 elif not color: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 269 message = strip_ansi(message) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 270 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 271 if message: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 272 file.write(message) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 273 file.flush() | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 274 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 275 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 276 def get_binary_stream(name): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 277 """Returns a system stream for byte processing. This essentially | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 278 returns the stream from the sys module with the given name but it | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 279 solves some compatibility issues between different Python versions. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 280 Primarily this function is necessary for getting binary streams on | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 281 Python 3. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 282 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 283 :param name: the name of the stream to open. Valid names are ``'stdin'``, | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 284 ``'stdout'`` and ``'stderr'`` | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 285 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 286 opener = binary_streams.get(name) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 287 if opener is None: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 288 raise TypeError("Unknown standard stream '{}'".format(name)) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 289 return opener() | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 290 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 291 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 292 def get_text_stream(name, encoding=None, errors="strict"): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 293 """Returns a system stream for text processing. This usually returns | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 294 a wrapped stream around a binary stream returned from | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 295 :func:`get_binary_stream` but it also can take shortcuts on Python 3 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 296 for already correctly configured streams. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 297 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 298 :param name: the name of the stream to open. Valid names are ``'stdin'``, | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 299 ``'stdout'`` and ``'stderr'`` | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 300 :param encoding: overrides the detected default encoding. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 301 :param errors: overrides the default error mode. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 302 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 303 opener = text_streams.get(name) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 304 if opener is None: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 305 raise TypeError("Unknown standard stream '{}'".format(name)) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 306 return opener(encoding, errors) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 307 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 308 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 309 def open_file( | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 310 filename, mode="r", encoding=None, errors="strict", lazy=False, atomic=False | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 311 ): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 312 """This is similar to how the :class:`File` works but for manual | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 313 usage. Files are opened non lazy by default. This can open regular | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 314 files as well as stdin/stdout if ``'-'`` is passed. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 315 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 316 If stdin/stdout is returned the stream is wrapped so that the context | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 317 manager will not close the stream accidentally. This makes it possible | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 318 to always use the function like this without having to worry to | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 319 accidentally close a standard stream:: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 320 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 321 with open_file(filename) as f: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 322 ... | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 323 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 324 .. versionadded:: 3.0 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 325 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 326 :param filename: the name of the file to open (or ``'-'`` for stdin/stdout). | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 327 :param mode: the mode in which to open the file. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 328 :param encoding: the encoding to use. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 329 :param errors: the error handling for this file. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 330 :param lazy: can be flipped to true to open the file lazily. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 331 :param atomic: in atomic mode writes go into a temporary file and it's | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 332 moved on close. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 333 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 334 if lazy: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 335 return LazyFile(filename, mode, encoding, errors, atomic=atomic) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 336 f, should_close = open_stream(filename, mode, encoding, errors, atomic=atomic) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 337 if not should_close: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 338 f = KeepOpenFile(f) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 339 return f | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 340 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 341 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 342 def get_os_args(): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 343 """This returns the argument part of sys.argv in the most appropriate | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 344 form for processing. What this means is that this return value is in | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 345 a format that works for Click to process but does not necessarily | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 346 correspond well to what's actually standard for the interpreter. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 347 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 348 On most environments the return value is ``sys.argv[:1]`` unchanged. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 349 However if you are on Windows and running Python 2 the return value | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 350 will actually be a list of unicode strings instead because the | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 351 default behavior on that platform otherwise will not be able to | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 352 carry all possible values that sys.argv can have. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 353 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 354 .. versionadded:: 6.0 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 355 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 356 # We can only extract the unicode argv if sys.argv has not been | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 357 # changed since the startup of the application. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 358 if PY2 and WIN and _initial_argv_hash == _hash_py_argv(): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 359 return _get_windows_argv() | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 360 return sys.argv[1:] | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 361 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 362 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 363 def format_filename(filename, shorten=False): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 364 """Formats a filename for user display. The main purpose of this | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 365 function is to ensure that the filename can be displayed at all. This | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 366 will decode the filename to unicode if necessary in a way that it will | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 367 not fail. Optionally, it can shorten the filename to not include the | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 368 full path to the filename. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 369 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 370 :param filename: formats a filename for UI display. This will also convert | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 371 the filename into unicode without failing. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 372 :param shorten: this optionally shortens the filename to strip of the | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 373 path that leads up to it. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 374 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 375 if shorten: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 376 filename = os.path.basename(filename) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 377 return filename_to_ui(filename) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 378 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 379 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 380 def get_app_dir(app_name, roaming=True, force_posix=False): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 381 r"""Returns the config folder for the application. The default behavior | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 382 is to return whatever is most appropriate for the operating system. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 383 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 384 To give you an idea, for an app called ``"Foo Bar"``, something like | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 385 the following folders could be returned: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 386 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 387 Mac OS X: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 388 ``~/Library/Application Support/Foo Bar`` | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 389 Mac OS X (POSIX): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 390 ``~/.foo-bar`` | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 391 Unix: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 392 ``~/.config/foo-bar`` | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 393 Unix (POSIX): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 394 ``~/.foo-bar`` | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 395 Win XP (roaming): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 396 ``C:\Documents and Settings\<user>\Local Settings\Application Data\Foo Bar`` | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 397 Win XP (not roaming): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 398 ``C:\Documents and Settings\<user>\Application Data\Foo Bar`` | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 399 Win 7 (roaming): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 400 ``C:\Users\<user>\AppData\Roaming\Foo Bar`` | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 401 Win 7 (not roaming): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 402 ``C:\Users\<user>\AppData\Local\Foo Bar`` | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 403 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 404 .. versionadded:: 2.0 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 405 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 406 :param app_name: the application name. This should be properly capitalized | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 407 and can contain whitespace. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 408 :param roaming: controls if the folder should be roaming or not on Windows. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 409 Has no affect otherwise. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 410 :param force_posix: if this is set to `True` then on any POSIX system the | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 411 folder will be stored in the home folder with a leading | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 412 dot instead of the XDG config home or darwin's | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 413 application support folder. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 414 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 415 if WIN: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 416 key = "APPDATA" if roaming else "LOCALAPPDATA" | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 417 folder = os.environ.get(key) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 418 if folder is None: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 419 folder = os.path.expanduser("~") | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 420 return os.path.join(folder, app_name) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 421 if force_posix: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 422 return os.path.join(os.path.expanduser("~/.{}".format(_posixify(app_name)))) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 423 if sys.platform == "darwin": | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 424 return os.path.join( | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 425 os.path.expanduser("~/Library/Application Support"), app_name | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 426 ) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 427 return os.path.join( | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 428 os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config")), | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 429 _posixify(app_name), | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 430 ) | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 431 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 432 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 433 class PacifyFlushWrapper(object): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 434 """This wrapper is used to catch and suppress BrokenPipeErrors resulting | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 435 from ``.flush()`` being called on broken pipe during the shutdown/final-GC | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 436 of the Python interpreter. Notably ``.flush()`` is always called on | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 437 ``sys.stdout`` and ``sys.stderr``. So as to have minimal impact on any | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 438 other cleanup code, and the case where the underlying file is not a broken | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 439 pipe, all calls and attributes are proxied. | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 440 """ | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 441 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 442 def __init__(self, wrapped): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 443 self.wrapped = wrapped | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 444 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 445 def flush(self): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 446 try: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 447 self.wrapped.flush() | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 448 except IOError as e: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 449 import errno | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 450 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 451 if e.errno != errno.EPIPE: | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 452 raise | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 453 | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 454 def __getattr__(self, attr): | 
| 
26e78fe6e8c4
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
 shellac parents: diff
changeset | 455 return getattr(self.wrapped, attr) | 
