From e28ab0863ce9a7c233585481e9f82b4e51253ed6 Mon Sep 17 00:00:00 2001 From: kenkeiras Date: Wed, 5 Jul 2017 01:21:38 +0200 Subject: [PATCH] Add visual progress bar. --- macli.py | 21 ++++++++++++++++++--- progress_meter.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 progress_meter.py diff --git a/macli.py b/macli.py index 95ebdb7..7ad906e 100644 --- a/macli.py +++ b/macli.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 +import progress_meter import re import hashlib import os @@ -65,7 +66,6 @@ def get_extension(path): def request(url): - print("> {}".format(url)) req = urllib.request.Request( url, data=None, @@ -80,7 +80,7 @@ def show_error(e): print("\x1b[41;37m{}\x1b[0m") -def archive(content, base_url, selector, directory, attribute): +def archive(content, base_url, selector, directory, attribute, progbar): os.makedirs(directory, exist_ok=True) for part in content.find_all(**selector): if attribute not in part.attrs: @@ -88,6 +88,7 @@ def archive(content, base_url, selector, directory, attribute): href = urllib.parse.urljoin(base_url, part[attribute], allow_fragments=False) + progbar.next_iter(href) name = (hashlib.sha1(href.encode()).hexdigest() + '.' @@ -123,6 +124,18 @@ def relink_links(content, base_url): link['href'] = full_href +def get_num_elements(content): + count = 0 + for (selector, _, attribute) in OBJECT_TYPE_DESCRIPTORS: + count += len([True + for element + in content.find_all(**selector) + if attribute in element.attrs]) + + return count + + + def archive_to_dir(directory, url): current_path = os.getcwd() os.chdir(directory) @@ -134,9 +147,11 @@ def archive_to_dir(directory, url): relink_links(archived_content, url) # Archive objects + progbar = progress_meter.ProgressBar(get_num_elements(archived_content)) + for (selector, directory, attribute) in OBJECT_TYPE_DESCRIPTORS: archive(archived_content, url, - selector, directory, attribute) + selector, directory, attribute, progbar) with open('index.html', 'wt') as f: f.write(str(archived_content)) diff --git a/progress_meter.py b/progress_meter.py new file mode 100644 index 0000000..18a6dd0 --- /dev/null +++ b/progress_meter.py @@ -0,0 +1,30 @@ +import sys + +class ProgressBar: + def __init__(self, num_elements): + self.num_elements = num_elements + self.current_element = 0 + + def show(self, msg=''): + total_blocks = 10 + blocks_done = ((self.current_element * total_blocks) + // self.num_elements) + blocks_to_go = total_blocks - blocks_done + + print('\r' # Go to the start of the line + '\x1b[K' # Clear line + '\x1b[0m' # Restart the "style" + '\x1b[7l' # Disable line wrapping + '|' # Put the first "|" + + blocks_done * '█' # Completed blocks + + blocks_to_go * ' ' # Uncompleted blocks + + '\x1b[7m|\x1b[0m' # End the bar + + ' ' + + msg # Add message + + '\x1b[7h' # Enable line wrapping + + '\r', # Go back to the start + end='') + + def next_iter(self, msg=''): + self.current_element += 1 + self.show(msg)