Documented new functions in utils
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 12f50b4 · Browse files · 1 file·+76 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+76 added−0 removed
- other:+76 added−0 removed
Provenance
- Commit
397438a- Tree
b8b2b34- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
12f50b4
1 file changed+76 added−0 removed
modified:docs/utils.rst+76 added
70 unmodified lines71 import click72 73 stdin_text = click.get_text_stream('stdin')74 stdout_binary = click.get_binary_stream('stdout')75+76+77+Finding Application Folders78+---------------------------79+80+.. versionadded:: 2.081+82+Very often you want to open a config file that belongs to your83+application. However different operationg systems store these config84+files by their policy in different places. Click provides a85+:func:`get_app_dir` function which returns the most appropriate location86+for per user config files for your application depending on the OS.87+88+Example usage::89+90+ import os91+ import click92+ import ConfigParser93+94+ APP_NAME = 'My Application'95+96+ def read_config():97+ cfg = os.path.join(click.get_app_dir(APP_NAME), 'config.ini')98+ parser = ConfigParser.RawConfigParser()99+ parser.read([cfg])100+ rv = {}101+ for section in parser.sections():102+ for key, value in parser.items(section):103+ rv['%s.%s' % (section, key)] = value104+ return rv105+106+107+Showing Progress Bars108+---------------------109+110+.. versionadded:: 2.0111+112+Sometimes you have command line scripts that need to process a lot of data113+but you want to quickly show the user some progress about how long that114+will take. Click supports simple progress bar rendering for that through115+the :func:`progressbar` function.116+117+The basic usage is very simple: the idea is that you have an iterable that118+you want to operate on. For each item in the iterable it might take some119+time to do processing. So say you have a loop like this::120+121+ for user in all_the_users_to_process:122+ modify_the_user(user)123+124+To hook this up with an automatically updating progress bar all you need125+to do is to change the code to this::126+127+ import click128+129+ with click.progressbar(all_the_users_to_process) as bar:130+ for user in bar:131+ modify_the_user(user)132+133+Click will then automatically print a progress bar to the terminal and134+calculate the remaining time for you. The calculation of remaining time135+requires that the iterable has a length. If it does not have a length,136+but you know the length, you can explicitly provide it::137+138+ with click.progressbar(all_the_users_to_process,139+ length=number_of_users) as bar:140+ for user in bar:141+ modify_the_user(user)142+143+Another useful feature is to associate a label with the progress bar which144+will be shown before next to the progress bar::145+146+ with click.progressbar(all_the_users_to_process,147+ label='Modifying user accounts',148+ length=number_of_users) as bar:149+ for user in bar:150+ modify_the_user(user)