Added get_app_dir 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 82973d0 · Browse files · 2 files·+57 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+57 added−1 removed
- other:+57 added−1 removed
Provenance
- Commit
3a0aacb- Tree
c8bb286- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
82973d0
2 files changed+57 added−1 removed
modified:click/__init__.py+2 added-1 removed
27 unmodified lines28 FLOAT, BOOL, UUID29 30 # Utilities31 from .utils import echo, get_binary_stream, get_text_stream, \32- format_filename32+ format_filename, get_app_dir33 34 # Terminal functions35 from .termui import prompt, confirm, get_terminal_size, echo_via_pager36 23 unmodified lines60 'FLOAT', 'BOOL', 'UUID',61 62 # Utilities63 'echo', 'get_binary_stream', 'get_text_stream', 'format_filename',64+ 'get_app_dir',65 66 # Terminal functions67 'prompt', 'confirm', 'get_terminal_size', 'echo_via_pager',68 10 unmodified linesmodified:click/utils.py+55 added
10 unmodified lines11 12 echo_native_types = string_types + (bytes, bytearray)13 14 15+def _posixify(name):16+ return '-'.join(name.split()).lower()17+18+19 def unpack_args(args, nargs_spec):20 """Given an iterable of arguments and an iterable of nargs specifications21 it returns a tuple with all the unpacked arguments at the first index22 and all remaining arguments as the second.256 unmodified lines279 """280 if shorten:281 filename = os.path.basename(filename)282 return filename_to_ui(filename)283+284+285+def get_app_dir(app_name, roaming=True, force_posix=False):286+ """Returns the config folder for the application. The default behavior287+ is to return whatever is most appropriate for the operating system.288+289+ To give you an idea, for an app called ``"Foo Bar"`` something like290+ the following folders could be returned:291+292+ Mac OS X:293+ ``~/Library/Application Support/Foo Bar``294+ Mac OS X (posix):295+ ``~/.foo-bar``296+ Unix:297+ ``~/.config/foo-bar``298+ Unix (posix):299+ ``~/.foo-bar``300+ Win XP (roaming):301+ ``C:\Documents and Settings\<user>\Local Settings\Application Data\Foo Bar``302+ Win XP (not roaming):303+ ``C:\Documents and Settings\<user>\Application Data\Foo Bar``304+ Win 7 (roaming):305+ ``C:\Users\<user>\AppData\Roaming\Foo Bar``306+ Win 7 (not roaming):307+ ``C:\Users\<user>\AppData\Local\Foo Bar``308+309+ .. versionadded:: 2.0310+311+ :param app_name: the application name. This should be properly capitalized312+ and can contain whitespace.313+ :param roaming: controls if the folder should be roaming or not on windows.314+ Has no affect otherwise.315+ :param force_posix: if this is set to `True` then on any posix system the316+ folder will be stored in the home folder with a leading317+ dot instead of the XDG config home or darwin's318+ application support folder.319+ """320+ if sys.platform.startswith('win'):321+ key = roaming and 'APPDATA' or 'LOCALAPPDATA'322+ folder = os.environ.get(key)323+ if folder is None:324+ folder = os.path.expanduser('~')325+ return os.path.join(folder, app_name)326+ if force_posix:327+ return os.path.join(os.path.expanduser('~/.' + _posixify(app_name)))328+ if sys.platform == 'darwin':329+ return os.path.join(os.path.expanduser(330+ '~/Library/Application Support'), app_name)331+ return os.path.join(332+ os.environ.get('XDG_CONFIG_HOME', os.path.expanduser('~/.config')),333+ _posixify(app_name))