[diffoscope] 01/05: Add a global object to track progress throughout diffoscope run.

Chris Lamb chris at chris-lamb.co.uk
Mon Sep 26 18:57:50 CEST 2016


This is an automated email from the git hooks/post-receive script.

lamby pushed a commit to branch master
in repository diffoscope.

commit 50744ffa44143ed837e49c2eb29231f5fc634b0d
Author: Chris Lamb <lamby at debian.org>
Date:   Mon Sep 26 18:33:44 2016 +0200

    Add a global object to track progress throughout diffoscope run.
    
    Signed-off-by: Chris Lamb <lamby at debian.org>
---
 diffoscope/main.py     |  3 ++
 diffoscope/progress.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 79 insertions(+)

diff --git a/diffoscope/main.py b/diffoscope/main.py
index ff8382f..723d09f 100644
--- a/diffoscope/main.py
+++ b/diffoscope/main.py
@@ -33,6 +33,7 @@ import diffoscope.comparators
 from diffoscope import logger, VERSION, set_locale, clean_all_temp_files
 from diffoscope.exc import RequiredToolNotFound
 from diffoscope.config import Config
+from diffoscope.progress import ProgressManager
 from diffoscope.presenters.html import output_html, output_html_directory, \
     JQUERY_SYSTEM_LOCATIONS
 from diffoscope.presenters.text import output_text
@@ -213,8 +214,10 @@ def run_diffoscope(parsed_args):
         logger.setLevel(logging.DEBUG)
     set_locale()
     logger.debug('Starting comparison')
+    ProgressManager().setup(parsed_args)
     difference = diffoscope.comparators.compare_root_paths(
         parsed_args.path1, parsed_args.path2)
+    ProgressManager().finish()
     if difference:
         # no output desired? print text
         if not any((parsed_args.text_output, parsed_args.html_output, parsed_args.html_output_directory)):
diff --git a/diffoscope/progress.py b/diffoscope/progress.py
new file mode 100644
index 0000000..bb123ce
--- /dev/null
+++ b/diffoscope/progress.py
@@ -0,0 +1,76 @@
+# -*- coding: utf-8 -*-
+#
+# diffoscope: in-depth comparison of files, archives, and directories
+#
+# Copyright © 2016 Chris Lamb <lamby at debian.org>
+#
+# diffoscope is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# diffoscope is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with diffoscope.  If not, see <http://www.gnu.org/licenses/>.
+
+class ProgressManager(object):
+    _singleton = {}
+
+    def __init__(self):
+        self.__dict__ = self._singleton
+
+        if not self._singleton:
+            self.total = 0
+            self.current = 0
+            self.observers = []
+
+    def setup(self, parsed_args):
+        pass
+
+    ##
+
+    def register(self, observer):
+        self.observers.append(observer)
+
+    def step(self, delta=1):
+        delta = min(self.total - self.current, delta) # clamp
+        if not delta:
+            return
+
+        self.current += delta
+        for x in self.observers:
+            x.notify(self.current, self.total)
+
+    def new_total(self, delta):
+        self.total += delta
+        for x in self.observers:
+            x.notify(self.current, self.total)
+
+    def finish(self):
+        for x in self.observers:
+            x.finish()
+
+class Progress(object):
+    def __init__(self, total):
+        self.current = 0
+        self.total = total
+
+        ProgressManager().new_total(total)
+
+    def __enter__(self):
+        return self
+
+    def __exit__(self, exc_type, exc_value, exc_traceback):
+        self.step(self.total - self.current)
+
+    def step(self, delta=1):
+        delta = min(self.total - self.current, delta) # clamp
+        if not delta:
+            return
+
+        self.current += delta
+        ProgressManager().step(delta)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git


More information about the diffoscope mailing list