[diffoscope] 05/05: PEP8-ify some files.

Chris Lamb chris at chris-lamb.co.uk
Thu Jun 8 08:20:50 CEST 2017


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

lamby pushed a commit to branch experimental
in repository diffoscope.

commit d9505159b92daadc2c0993be83eebd55e41bb42c
Author: Chris Lamb <lamby at debian.org>
Date:   Wed Jun 7 19:48:17 2017 +0100

    PEP8-ify some files.
---
 diffoscope/diff.py             | 20 ++++++++++++++------
 diffoscope/presenters/utils.py |  4 ++++
 diffoscope/profiling.py        | 15 +++++++++------
 diffoscope/readers/__init__.py |  3 ++-
 diffoscope/readers/json.py     | 30 ++++++++++++++++++------------
 diffoscope/tempfiles.py        |  2 ++
 diffoscope/tools.py            |  2 +-
 setup.py                       |  1 +
 8 files changed, 51 insertions(+), 26 deletions(-)

diff --git a/diffoscope/diff.py b/diffoscope/diff.py
index b9501d8..99b5230 100644
--- a/diffoscope/diff.py
+++ b/diffoscope/diff.py
@@ -22,7 +22,6 @@ import io
 import os
 import errno
 import fcntl
-import hashlib
 import logging
 import threading
 import subprocess
@@ -147,7 +146,10 @@ class DiffParser(object):
         if self._remaining_hunk_lines == 0 or line[0] != self._direction:
             removed = self._block_len - Config().max_diff_block_lines_saved
             if removed:
-                self._diff.write('%s[ %d lines removed ]\n' % (self._direction, removed))
+                self._diff.write('%s[ %d lines removed ]\n' % (
+                    self._direction,
+                    removed,
+                ))
             return self.read_hunk(line)
 
         self._block_len += 1
@@ -155,6 +157,7 @@ class DiffParser(object):
 
         return self.skip_block
 
+
 @tool_required('diff')
 def run_diff(fifo1, fifo2, end_nl_q1, end_nl_q2):
     cmd = ['diff', '-aU7', fifo1, fifo2]
@@ -186,6 +189,7 @@ def run_diff(fifo1, fifo2, end_nl_q1, end_nl_q2):
 
     return parser.diff
 
+
 class FIFOFeeder(threading.Thread):
     def __init__(self, feeder, fifo_path, end_nl_q=None, daemon=True, *args):
         os.mkfifo(fifo_path)
@@ -210,7 +214,7 @@ class FIFOFeeder(threading.Thread):
             # more need for the FIFO, so stop the thread.
             while True:
                 try:
-                    fifo_fd = os.open(self.fifo_path, os.O_WRONLY | os.O_NONBLOCK)
+                    fd = os.open(self.fifo_path, os.O_WRONLY | os.O_NONBLOCK)
                 except OSError as error:
                     if error.errno != errno.ENXIO:
                         raise
@@ -220,8 +224,9 @@ class FIFOFeeder(threading.Thread):
                     break
 
             # Now clear the fd's nonblocking flag to let writes block normally.
-            fcntl.fcntl(fifo_fd, fcntl.F_SETFL, 0)
-            with open(fifo_fd, 'wb') as fifo:
+            fcntl.fcntl(fd, fcntl.F_SETFL, 0)
+
+            with open(fd, 'wb') as fifo:
                 # The queue works around a unified diff limitation: if there's
                 # no newlines in both don't make it a difference
                 end_nl = self.feeder(fifo)
@@ -235,15 +240,17 @@ class FIFOFeeder(threading.Thread):
         if self._exception is not None:
             raise self._exception
 
+
 def diff(feeder1, feeder2):
     tmpdir = get_temporary_directory().name
 
     fifo1_path = os.path.join(tmpdir, 'fifo1')
     fifo2_path = os.path.join(tmpdir, 'fifo2')
     with FIFOFeeder(feeder1, fifo1_path) as fifo1, \
-         FIFOFeeder(feeder2, fifo2_path) as fifo2:
+            FIFOFeeder(feeder2, fifo2_path) as fifo2:
         return run_diff(fifo1_path, fifo2_path, fifo1.end_nl_q, fifo2.end_nl_q)
 
+
 def reverse_unified_diff(diff):
     res = []
     for line in diff.splitlines(keepends=True):
@@ -269,6 +276,7 @@ def reverse_unified_diff(diff):
             res.append(line)
     return ''.join(res)
 
+
 def color_unified_diff(diff):
     RESET = '\033[0m'
     RED, GREEN, CYAN = '\033[31m', '\033[32m', '\033[0;36m'
diff --git a/diffoscope/presenters/utils.py b/diffoscope/presenters/utils.py
index 09f1b74..e69c5d4 100644
--- a/diffoscope/presenters/utils.py
+++ b/diffoscope/presenters/utils.py
@@ -58,12 +58,15 @@ class Presenter(object):
         # str.splitlines, etc.
         return prefix + val.rstrip().replace('\n', '\n{}'.format(prefix))
 
+
 class PrintLimitReached(Exception):
     pass
 
+
 class DiffBlockLimitReached(Exception):
     pass
 
+
 @contextlib.contextmanager
 def make_printer(path):
     output = sys.stdout
@@ -81,6 +84,7 @@ def make_printer(path):
     if path != '-':
         output.close()
 
+
 def create_limited_print_func(print_func, max_page_size):
     count = [0]
 
diff --git a/diffoscope/profiling.py b/diffoscope/profiling.py
index e381b78..8fbc818 100644
--- a/diffoscope/profiling.py
+++ b/diffoscope/profiling.py
@@ -70,11 +70,14 @@ class ProfileManager(object):
         with make_printer(parsed_args.profile_output) as fn:
             self.output(fn)
 
-    def output(self, print):
+    def output(self, print_fn):
         title = "Profiling output for: {}".format(' '.join(sys.argv))
 
-        print(title)
-        print("=" * len(title))
+        print_fn(title)
+        print_fn("=" * len(title))
+
+        def key(x):
+            return x[1]['time']
 
         for namespace, keys in sorted(self.data.items(), key=lambda x: x[0]):
             subtitle = "{} (total time: {:.3f}s)".format(
@@ -82,10 +85,10 @@ class ProfileManager(object):
                 sum(x['time'] for x in keys.values()),
             )
 
-            print("\n{}\n{}\n".format(subtitle, "-" * len(subtitle)))
+            print_fn("\n{}\n{}\n".format(subtitle, "-" * len(subtitle)))
 
-            for value, totals in sorted(keys.items(), key=lambda x: x[1]['time'], reverse=True):
-                print("  {:10.3f}s {:5d} call{}    {}".format(
+            for value, totals in sorted(keys.items(), key=key, reverse=True):
+                print_fn("  {:10.3f}s {:5d} call{}    {}".format(
                     totals['time'],
                     totals['count'],
                     ' ' if totals['count'] == 1 else 's',
diff --git a/diffoscope/readers/__init__.py b/diffoscope/readers/__init__.py
index 391be39..9f5bdf9 100644
--- a/diffoscope/readers/__init__.py
+++ b/diffoscope/readers/__init__.py
@@ -24,5 +24,6 @@ def load_diff_from_path(path):
     with open(path, 'rb') as fp:
         return load_diff(fp, path)
 
+
 def load_diff(fp, path):
-    return JSONReaderV1().load(fp, "stdin")
+    return JSONReaderV1().load(fp, 'stdin')
diff --git a/diffoscope/readers/json.py b/diffoscope/readers/json.py
index 7a24303..e088bca 100644
--- a/diffoscope/readers/json.py
+++ b/diffoscope/readers/json.py
@@ -17,29 +17,35 @@
 # You should have received a copy of the GNU General Public License
 # along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.
 
-import codecs
 import json
-from collections import OrderedDict
+import codecs
 
 from ..difference import Difference
 from ..presenters.json import JSON_FORMAT_MAGIC
+
 from .utils import UnrecognizedFormatError
 
 
 class JSONReaderV1(object):
-
     def load(self, fp, fn):
-        raw = json.load(codecs.getreader("utf-8")(fp))
+        raw = json.load(codecs.getreader('utf-8')(fp))
         if JSON_FORMAT_MAGIC not in raw or raw[JSON_FORMAT_MAGIC] != 1:
-            raise UnrecognizedFormatError("magic not found in json: %s" % JSON_FORMAT_MAGIC)
+            raise UnrecognizedFormatError(
+                "Magic not found in JSON: {}".format(JSON_FORMAT_MAGIC)
+            )
         return self.load_rec(raw)
 
     def load_rec(self, raw):
-        source1 = raw["source1"]
-        source2 = raw["source2"]
-        unified_diff = raw["unified_diff"]
-        has_internal_linenos = raw.get("has_internal_linenos", False)
-        comments = raw.get("comments", [])
-        details = [self.load_rec(child) for child in raw.get("details", [])]
+        source1 = raw['source1']
+        source2 = raw['source2']
+        unified_diff = raw['unified_diff']
+        comments = raw.get('comments', [])
+        details = [self.load_rec(child) for child in raw.get('details', [])]
 
-        return Difference(unified_diff, source1, source2, comment=comments, details=details)
+        return Difference(
+            unified_diff,
+            source1,
+            source2,
+            comment=comments,
+            details=details,
+        )
diff --git a/diffoscope/tempfiles.py b/diffoscope/tempfiles.py
index b63bb16..833ac16 100644
--- a/diffoscope/tempfiles.py
+++ b/diffoscope/tempfiles.py
@@ -34,6 +34,7 @@ def get_named_temporary_file(*args, **kwargs):
 
     return f
 
+
 def get_temporary_directory(*args, **kwargs):
     kwargs['suffix'] = kwargs.pop('suffix', '_diffoscope')
 
@@ -42,6 +43,7 @@ def get_temporary_directory(*args, **kwargs):
 
     return d
 
+
 def clean_all_temp_files():
     logger.debug("Cleaning %d temp files", len(_FILES))
 
diff --git a/diffoscope/tools.py b/diffoscope/tools.py
index 2882b01..7ffb603 100644
--- a/diffoscope/tools.py
+++ b/diffoscope/tools.py
@@ -17,7 +17,6 @@
 # You should have received a copy of the GNU General Public License
 # along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.
 
-import os
 import collections
 import platform
 import functools
@@ -70,6 +69,7 @@ def tool_required(command):
         return tool_check
     return wrapper
 
+
 def get_current_os():
     system = platform.system()
     if system == "Linux":
diff --git a/setup.py b/setup.py
index 789ae60..6bee89e 100644
--- a/setup.py
+++ b/setup.py
@@ -23,6 +23,7 @@ class PyTest(TestCommand):
         errno = pytest.main(self.pytest_args)
         sys.exit(errno)
 
+
 setup(
     name='diffoscope',
     version=diffoscope.VERSION,

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


More information about the diffoscope mailing list