[Reproducible-commits] [diffoscope] 01/01: Refactor configuration to use Python properties

Reiner Herrmann reiner at reiner-h.de
Wed Aug 19 15:46:54 UTC 2015


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

deki-guest pushed a commit to branch master
in repository diffoscope.

commit 0211ded8db39e966ace18fd40149aabe18506adf
Author: Reiner Herrmann <reiner at reiner-h.de>
Date:   Wed Aug 19 17:48:12 2015 +0200

    Refactor configuration to use Python properties
---
 diffoscope.py                 |  6 ++---
 diffoscope/config.py          | 63 ++++++++++++++++++++++++-------------------
 diffoscope/difference.py      |  6 ++---
 diffoscope/presenters/html.py |  2 +-
 4 files changed, 43 insertions(+), 34 deletions(-)

diff --git a/diffoscope.py b/diffoscope.py
index 3aea408..a1a8ccc 100755
--- a/diffoscope.py
+++ b/diffoscope.py
@@ -92,9 +92,9 @@ class ListToolsAction(argparse.Action):
 def main():
     parser = create_parser()
     parsed_args = parser.parse_args(sys.argv[1:])
-    Config.setMaxDiffBlockLines(parsed_args.max_diff_block_lines)
-    Config.setMaxDiffInputLines(parsed_args.max_diff_input_lines)
-    Config.setMaxReportSize(parsed_args.max_report_size)
+    Config.config().max_diff_block_lines = parsed_args.max_diff_block_lines
+    Config.config().max_diff_input_lines = parsed_args.max_diff_input_lines
+    Config.config().max_report_size = parsed_args.max_report_size
     if parsed_args.debug:
         logger.setLevel(logging.DEBUG)
     set_locale()
diff --git a/diffoscope/config.py b/diffoscope/config.py
index 42e5226..71e7d1f 100644
--- a/diffoscope/config.py
+++ b/diffoscope/config.py
@@ -19,34 +19,43 @@
 
 
 class Config(object):
-    MAX_DIFF_BLOCK_LINES = 50
-    MAX_DIFF_INPUT_LINES = 100000 # GNU diff cannot process arbitrary large files :(
-    MAX_REPORT_SIZE = 2000 * 2 ** 10 # 2000 kB
+    _config = None
 
-    @classmethod
-    def setMaxDiffBlockLines(cls, lines):
-        if lines:
-            cls.MAX_DIFF_BLOCK_LINES = lines
-
-    @classmethod
-    def setMaxDiffInputLines(cls, lines):
-        if lines:
-            cls.MAX_DIFF_INPUT_LINES = lines
-
-    @classmethod
-    def setMaxReportSize(cls, bytes):
-        if bytes:
-            cls.MAX_REPORT_SIZE = bytes
-
-    @classmethod
-    def maxReportSize(cls):
-        return cls.MAX_REPORT_SIZE
-
-    @classmethod
-    def maxDiffBlockLines(cls):
-        return cls.MAX_DIFF_BLOCK_LINES
+    def __init__(self):
+        self._max_diff_block_lines = 50
+        self._max_diff_input_lines = 100000 # GNU diff cannot process arbitrary large files :(
+        self._max_report_size = 2000 * 2 ** 10 # 2000 kB
 
     @classmethod
-    def maxDiffInputLines(cls):
-        return cls.MAX_DIFF_INPUT_LINES
+    def config(cls):
+        if not cls._config:
+            cls._config = Config()
+        return cls._config
+
+    @property
+    def max_diff_block_lines(self):
+        return self._max_diff_block_lines
+
+    @property
+    def max_diff_input_lines(self):
+        return self._max_diff_input_lines
+
+    @property
+    def max_report_size(self):
+        return self._max_report_size
+
+    @max_diff_block_lines.setter
+    def max_diff_block_lines(self, value):
+        if value:
+            self._max_diff_block_lines = value
+
+    @max_diff_input_lines.setter
+    def max_diff_input_lines(self, value):
+        if value:
+            self._max_diff_input_lines = value
+
+    @max_report_size.setter
+    def max_report_size(self, value):
+        if value:
+            self._max_report_size = value
 
diff --git a/diffoscope/difference.py b/diffoscope/difference.py
index aa96e10..7d43c52 100644
--- a/diffoscope/difference.py
+++ b/diffoscope/difference.py
@@ -104,7 +104,7 @@ class DiffParser(object):
         self._diff.write(line)
         if line[0] in ('-', '+') and line[0] == self._direction:
             self._block_len += 1
-            max_lines = Config.maxDiffBlockLines()
+            max_lines = Config.config().max_diff_block_lines
             if max_lines > 0 and self._block_len >= max_lines:
                 return self.skip_block
         else:
@@ -114,7 +114,7 @@ class DiffParser(object):
 
     def skip_block(self, line):
         if self._remaining_hunk_lines == 0 or line[0] != self._direction:
-            self._diff.write('%s[ %d lines removed ]\n' % (self._direction, self._block_len - Config.maxDiffBlockLines()))
+            self._diff.write('%s[ %d lines removed ]\n' % (self._direction, self._block_len - Config.config().max_diff_block_lines))
             return self.read_hunk(line)
         self._block_len += 1
         self._remaining_hunk_lines -= 1
@@ -229,7 +229,7 @@ def make_feeder_from_file(in_file, filter=lambda buf: buf.encode('utf-8')):
         for buf in in_file.readlines():
             line_count += 1
             out_file.write(filter(buf))
-            max_lines = Config.maxDiffInputLines()
+            max_lines = Config.config().max_diff_input_lines
             if max_lines > 0 and line_count >= max_lines:
                 out_file.write('[ Too much input for diff ]%s\n' % (' ' * out_file.fileno()))
                 end_nl = True
diff --git a/diffoscope/presenters/html.py b/diffoscope/presenters/html.py
index ded15a1..b80b3da 100644
--- a/diffoscope/presenters/html.py
+++ b/diffoscope/presenters/html.py
@@ -515,7 +515,7 @@ def output_header(css_url, print_func):
 def output_html(difference, css_url=None, print_func=None):
     if print_func is None:
         print_func = print
-    print_func = create_limited_print_func(print_func, Config.maxReportSize())
+    print_func = create_limited_print_func(print_func, Config.config().max_report_size)
     try:
         output_header(css_url, print_func)
         output_difference(difference, print_func, [])

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



More information about the Reproducible-commits mailing list