add simple progress indicator for multiple URLs (#19)

The output can be configured via the 'output.progress'
config value.

Possible values:
    - true:     Show the default progress indicator
                "[{current}/{total}] {url}" (default)
    - false:    Never show the progress indicator
    - <string>: Show the progress indicator using this
                as a custom format string(1).
                Possible replacement keys are:
                - current: current URL index
                - total  : total number of URLs
                - url    : current URL

(1) https://docs.python.org/3/library/string.html#formatstrings
This commit is contained in:
Mike Fährmann
2017-06-09 20:12:15 +02:00
parent 3ee77a0902
commit d5a70f2580
3 changed files with 22 additions and 4 deletions

View File

@@ -41,6 +41,15 @@ def initialize_logging():
root.addHandler(handler)
def progress(urls, pformat):
if pformat is True:
pformat = "[{current}/{total}] {url}"
pinfo = {"total": len(urls)}
for pinfo["current"], pinfo["url"] in enumerate(urls, 1):
print(pformat.format_map(pinfo), file=sys.stderr)
yield pinfo["url"]
def sanatize_input(file):
for line in file:
line = line.strip()
@@ -98,8 +107,7 @@ def main():
file = sys.stdin
else:
file = open(args.inputfile)
import itertools
urls = itertools.chain(urls, sanatize_input(file))
urls += sanatize_input(file)
except OSError as exc:
log.warning("input-file: %s", exc)
@@ -109,6 +117,10 @@ def main():
except OSError as exc:
log.warning("unsupported-URL file: %s", exc)
pformat = config.get(("output", "progress"), True)
if pformat and len(urls) > 1 and args.loglevel < logging.ERROR:
urls = progress(urls, pformat)
for url in urls:
try:
log.debug("Starting %s for '%s'", jobtype.__name__, url)