[diffoscope] 04/05: Also add auto recognition for RE_FILE_EXTENSION

Chris Lamb chris at chris-lamb.co.uk
Sun Jan 15 12:52:37 CET 2017


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

lamby pushed a commit to branch master
in repository diffoscope.

commit 472053697535244adeef78565246f3d8bd9b368a
Author: Chris Lamb <lamby at debian.org>
Date:   Sun Jan 15 22:47:50 2017 +1100

    Also add auto recognition for RE_FILE_EXTENSION
    
    Signed-off-by: Chris Lamb <lamby at debian.org>
---
 diffoscope/comparators/debian_fallback.py  | 12 ------------
 diffoscope/comparators/ipk.py              |  4 ----
 diffoscope/comparators/javascript.py       |  4 ----
 diffoscope/comparators/utils/specialize.py | 11 ++++++++++-
 4 files changed, 10 insertions(+), 21 deletions(-)

diff --git a/diffoscope/comparators/debian_fallback.py b/diffoscope/comparators/debian_fallback.py
index 71ea392..a52f133 100644
--- a/diffoscope/comparators/debian_fallback.py
+++ b/diffoscope/comparators/debian_fallback.py
@@ -25,10 +25,6 @@ from .text import TextFile
 class DotChangesFile(TextFile):
     RE_FILE_EXTENSION = re.compile(r'\.changes$')
 
-    @staticmethod
-    def recognizes(file):
-        return DotChangesFile.RE_FILE_EXTENSION.search(file.name)
-
     def compare(self, other, source=None):
         difference = super().compare(other)
         if not difference:
@@ -39,10 +35,6 @@ class DotChangesFile(TextFile):
 class DotDscFile(TextFile):
     RE_FILE_EXTENSION = re.compile(r'\.dsc$')
 
-    @staticmethod
-    def recognizes(file):
-        return DotDscFile.RE_FILE_EXTENSION.search(file.name)
-
     def compare(self, other, source=None):
         difference = super().compare(other)
         if not difference:
@@ -53,10 +45,6 @@ class DotDscFile(TextFile):
 class DotBuildinfoFile(TextFile):
     RE_FILE_EXTENSION = re.compile(r'\.buildinfo$')
 
-    @staticmethod
-    def recognizes(file):
-        return DotBuildinfoFile.RE_FILE_EXTENSION.search(file.name)
-
     def compare(self, other, source=None):
         difference = super().compare(other)
         if not difference:
diff --git a/diffoscope/comparators/ipk.py b/diffoscope/comparators/ipk.py
index 785ee5d..3131dcc 100644
--- a/diffoscope/comparators/ipk.py
+++ b/diffoscope/comparators/ipk.py
@@ -25,7 +25,3 @@ from .gzip import GzipFile
 
 class IpkFile(GzipFile):
     RE_FILE_EXTENSION = re.compile('\.ipk$')
-
-    @staticmethod
-    def recognizes(file):
-        return IpkFile.RE_FILE_EXTENSION.search(file.name)
diff --git a/diffoscope/comparators/javascript.py b/diffoscope/comparators/javascript.py
index 645dc9b..1cdc865 100644
--- a/diffoscope/comparators/javascript.py
+++ b/diffoscope/comparators/javascript.py
@@ -34,10 +34,6 @@ class JavaScriptBeautify(Command):
 class JavaScriptFile(File):
     RE_FILE_EXTENSION = re.compile(r'\.js$')
 
-    @staticmethod
-    def recognizes(file):
-        return JavaScriptFile.RE_FILE_EXTENSION.search(file.name)
-
     def compare_details(self, other, source=None):
         return [Difference.from_command(JavaScriptBeautify, self.path, other.path)]
 
diff --git a/diffoscope/comparators/utils/specialize.py b/diffoscope/comparators/utils/specialize.py
index dd22e0b..57230da 100644
--- a/diffoscope/comparators/utils/specialize.py
+++ b/diffoscope/comparators/utils/specialize.py
@@ -32,7 +32,16 @@ def specialize(file):
         if isinstance(file, cls):
             return file
         with profile('recognizes', file):
-            if cls.recognizes(file):
+            ret = False
+            if hasattr(cls, 'recognizes'):
+                ret = cls.recognizes(file)
+            else:
+                # No recognizes() method specified, try an auto recognition
+                if hasattr(cls, 'RE_FILE_TYPE'):
+                    ret = cls.RE_FILE_TYPE.search(file.magic_file_type)
+                elif hasattr(cls, 'RE_FILE_EXTENSION'):
+                    ret = cls.RE_FILE_EXTENSION.search(file.name)
+            if ret:
                 logger.debug("Using %s for %s", cls.__name__, file.name)
                 new_cls = type(cls.__name__, (cls, type(file)), {})
                 file.__class__ = new_cls

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


More information about the diffoscope mailing list