[diffoscope] 01/01: comparators: more tidying up and making names consistent

Ximin Luo infinity0 at debian.org
Tue Jul 25 18:35:04 CEST 2017


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

infinity0 pushed a commit to branch master
in repository diffoscope.

commit 1e401a7396c1d0da6c4f3ca0fe94e7941f78f7e4
Author: Ximin Luo <infinity0 at debian.org>
Date:   Tue Jul 25 18:34:45 2017 +0200

    comparators: more tidying up and making names consistent
---
 diffoscope/comparators/apk.py             |  6 +++---
 diffoscope/comparators/ar.py              |  2 +-
 diffoscope/comparators/bzip2.py           |  2 +-
 diffoscope/comparators/cpio.py            |  2 +-
 diffoscope/comparators/deb.py             |  2 +-
 diffoscope/comparators/debian.py          | 24 ++++++++++++------------
 diffoscope/comparators/debian_fallback.py |  8 +++-----
 diffoscope/comparators/dex.py             |  2 +-
 diffoscope/comparators/docx.py            |  2 +-
 diffoscope/comparators/dtb.py             |  2 +-
 diffoscope/comparators/elf.py             |  6 +++---
 diffoscope/comparators/fontconfig.py      |  5 ++---
 diffoscope/comparators/fonts.py           |  2 +-
 diffoscope/comparators/fsimage.py         |  2 +-
 diffoscope/comparators/gettext.py         |  2 +-
 diffoscope/comparators/gif.py             |  2 +-
 diffoscope/comparators/git.py             |  2 +-
 diffoscope/comparators/gzip.py            |  2 +-
 diffoscope/comparators/icc.py             |  2 +-
 diffoscope/comparators/image.py           |  4 ++--
 diffoscope/comparators/ipk.py             |  2 +-
 diffoscope/comparators/iso9660.py         |  4 ++--
 diffoscope/comparators/java.py            |  2 +-
 diffoscope/comparators/javascript.py      |  4 +---
 diffoscope/comparators/json.py            |  8 ++++----
 diffoscope/comparators/llvm.py            |  6 +++---
 diffoscope/comparators/macho.py           |  2 +-
 diffoscope/comparators/mono.py            |  2 +-
 diffoscope/comparators/odt.py             |  2 +-
 diffoscope/comparators/ogg.py             |  2 +-
 diffoscope/comparators/openssh.py         |  2 +-
 diffoscope/comparators/pcap.py            |  2 +-
 diffoscope/comparators/pdf.py             |  2 +-
 diffoscope/comparators/pgp.py             |  2 +-
 diffoscope/comparators/png.py             |  2 +-
 diffoscope/comparators/ppu.py             |  8 ++++----
 diffoscope/comparators/ps.py              |  2 +-
 diffoscope/comparators/rdata.py           |  5 +----
 diffoscope/comparators/rpm_fallback.py    |  2 +-
 diffoscope/comparators/rust.py            |  5 ++---
 diffoscope/comparators/sqlite.py          |  2 +-
 diffoscope/comparators/squashfs.py        |  2 +-
 diffoscope/comparators/tar.py             |  2 +-
 diffoscope/comparators/text.py            |  2 +-
 diffoscope/comparators/utils/file.py      | 27 ++++++++++++---------------
 diffoscope/comparators/xz.py              |  2 +-
 diffoscope/comparators/zip.py             |  2 +-
 47 files changed, 87 insertions(+), 99 deletions(-)

diff --git a/diffoscope/comparators/apk.py b/diffoscope/comparators/apk.py
index cb2badf..911cffa 100644
--- a/diffoscope/comparators/apk.py
+++ b/diffoscope/comparators/apk.py
@@ -146,9 +146,9 @@ class ApkContainer(Archive):
         return differences
 
 class ApkFile(File):
-    RE_FILE_TYPE_FALLBACK_HEADER = b"PK\x03\x04"
-    RE_FILE_TYPE = re.compile(r'^(Java|Zip) archive data.*\b')
-    RE_FILE_EXTENSION = re.compile(r'\.apk$')
+    FILE_TYPE_HEADER_PREFIX = b"PK\x03\x04"
+    FILE_TYPE_RE = re.compile(r'^(Java|Zip) archive data.*\b')
+    FILE_EXTENSION_SUFFIX = '.apk'
     CONTAINER_CLASS = ApkContainer
 
     def compare_details(self, other, source=None):
diff --git a/diffoscope/comparators/ar.py b/diffoscope/comparators/ar.py
index 0de0bff..4fbbfa5 100644
--- a/diffoscope/comparators/ar.py
+++ b/diffoscope/comparators/ar.py
@@ -56,7 +56,7 @@ class ArSymbolTableDumper(Command):
 
 class ArFile(File):
     CONTAINER_CLASS = ArContainer
-    RE_FILE_TYPE = re.compile(r'\bar archive\b')
+    FILE_TYPE_RE = re.compile(r'\bar archive\b')
 
     def compare_details(self, other, source=None):
         return [Difference.from_command(ArSymbolTableDumper, self.path, other.path),
diff --git a/diffoscope/comparators/bzip2.py b/diffoscope/comparators/bzip2.py
index c2b8ddf..61007d5 100644
--- a/diffoscope/comparators/bzip2.py
+++ b/diffoscope/comparators/bzip2.py
@@ -53,4 +53,4 @@ class Bzip2Container(Archive):
 
 class Bzip2File(File):
     CONTAINER_CLASS = Bzip2Container
-    RE_FILE_TYPE = re.compile(r'^bzip2 compressed data\b')
+    FILE_TYPE_RE = re.compile(r'^bzip2 compressed data\b')
diff --git a/diffoscope/comparators/cpio.py b/diffoscope/comparators/cpio.py
index 9fde04d..2e9f7dc 100644
--- a/diffoscope/comparators/cpio.py
+++ b/diffoscope/comparators/cpio.py
@@ -28,7 +28,7 @@ from .utils.libarchive import LibarchiveContainer, list_libarchive
 
 class CpioFile(File):
     CONTAINER_CLASS = LibarchiveContainer
-    RE_FILE_TYPE = re.compile(r'\bcpio archive\b')
+    FILE_TYPE_RE = re.compile(r'\bcpio archive\b')
 
     def compare_details(self, other, source=None):
         return [Difference.from_text_readers(
diff --git a/diffoscope/comparators/deb.py b/diffoscope/comparators/deb.py
index 66a6d05..85e19e2 100644
--- a/diffoscope/comparators/deb.py
+++ b/diffoscope/comparators/deb.py
@@ -80,7 +80,7 @@ class DebContainer(LibarchiveContainer):
 
 class DebFile(File):
     CONTAINER_CLASS = DebContainer
-    RE_FILE_TYPE = re.compile(r'^Debian binary package')
+    FILE_TYPE_RE = re.compile(r'^Debian binary package')
 
     @property
     def md5sums(self):
diff --git a/diffoscope/comparators/debian.py b/diffoscope/comparators/debian.py
index a5e7900..5783eea 100644
--- a/diffoscope/comparators/debian.py
+++ b/diffoscope/comparators/debian.py
@@ -160,11 +160,11 @@ class DebControlFile(File):
         return differences
 
 class DotChangesFile(DebControlFile):
-    RE_FILE_EXTENSION = re.compile(r'\.changes$')
+    FILE_EXTENSION_SUFFIX = '.changes'
 
-    @staticmethod
-    def recognizes(file):
-        if not DotChangesFile.RE_FILE_EXTENSION.search(file.name):
+    @classmethod
+    def recognizes(cls, file):
+        if not super().recognizes(file):
             return False
 
         changes = Changes(filename=file.path)
@@ -201,11 +201,11 @@ class DotChangesFile(DebControlFile):
 
 
 class DotDscFile(DebControlFile):
-    RE_FILE_EXTENSION = re.compile(r'\.dsc$')
+    FILE_EXTENSION_SUFFIX = '.dsc'
 
-    @staticmethod
-    def recognizes(file):
-        if not DotDscFile.RE_FILE_EXTENSION.search(file.name):
+    @classmethod
+    def recognizes(cls, file):
+        if not super().recognizes(file):
             return False
 
         with open(file.path, 'rb') as f:
@@ -252,11 +252,11 @@ class DotBuildinfoContainer(DebControlContainer):
 
 class DotBuildinfoFile(DebControlFile):
     CONTAINER_CLASS = DotBuildinfoContainer
-    RE_FILE_EXTENSION = re.compile(r'\.buildinfo$')
+    FILE_EXTENSION_SUFFIX = '.buildinfo'
 
-    @staticmethod
-    def recognizes(file):
-        if not DotBuildinfoFile.RE_FILE_EXTENSION.search(file.name):
+    @classmethod
+    def recognizes(cls, file):
+        if not super().recognizes(file):
             return False
 
         with open(file.path, 'rb') as f:
diff --git a/diffoscope/comparators/debian_fallback.py b/diffoscope/comparators/debian_fallback.py
index e075ee2..4530eb6 100644
--- a/diffoscope/comparators/debian_fallback.py
+++ b/diffoscope/comparators/debian_fallback.py
@@ -17,13 +17,11 @@
 # You should have received a copy of the GNU General Public License
 # along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.
 
-import re
-
 from .text import TextFile
 
 
 class DotChangesFile(TextFile):
-    RE_FILE_EXTENSION = re.compile(r'\.changes$')
+    FILE_EXTENSION_SUFFIX = '.changes'
 
     def compare(self, other, *args, **kwargs):
         difference = super().compare(other, *args, **kwargs)
@@ -33,7 +31,7 @@ class DotChangesFile(TextFile):
         return difference
 
 class DotDscFile(TextFile):
-    RE_FILE_EXTENSION = re.compile(r'\.dsc$')
+    FILE_EXTENSION_SUFFIX = '.dsc'
 
     def compare(self, other, *args, **kwargs):
         difference = super().compare(other, *args, **kwargs)
@@ -43,7 +41,7 @@ class DotDscFile(TextFile):
         return difference
 
 class DotBuildinfoFile(TextFile):
-    RE_FILE_EXTENSION = re.compile(r'\.buildinfo$')
+    FILE_EXTENSION_SUFFIX = '.buildinfo'
 
     def compare(self, other, *args, **kwargs):
         difference = super().compare(other, *args, **kwargs)
diff --git a/diffoscope/comparators/dex.py b/diffoscope/comparators/dex.py
index 3b71b3b..3389987 100644
--- a/diffoscope/comparators/dex.py
+++ b/diffoscope/comparators/dex.py
@@ -53,5 +53,5 @@ class DexContainer(Archive):
         return dest_path
 
 class DexFile(File):
-    RE_FILE_TYPE = re.compile(r'^Dalvik dex file .*\b')
+    FILE_TYPE_RE = re.compile(r'^Dalvik dex file .*\b')
     CONTAINER_CLASS = DexContainer
diff --git a/diffoscope/comparators/docx.py b/diffoscope/comparators/docx.py
index 6988af6..e0a2a9e 100644
--- a/diffoscope/comparators/docx.py
+++ b/diffoscope/comparators/docx.py
@@ -37,7 +37,7 @@ class Docx2txt(Command):
 
 
 class DocxFile(File):
-    RE_FILE_TYPE = re.compile(r'^Microsoft Word 2007+\b')
+    FILE_TYPE_RE = re.compile(r'^Microsoft Word 2007+\b')
 
     def compare_details(self, other, source=None):
         return [Difference.from_command(
diff --git a/diffoscope/comparators/dtb.py b/diffoscope/comparators/dtb.py
index 12dbf39..d59d4f8 100644
--- a/diffoscope/comparators/dtb.py
+++ b/diffoscope/comparators/dtb.py
@@ -33,7 +33,7 @@ class DeviceTreeContents(Command):
         return ['fdtdump', self.path]
 
 class DeviceTreeFile(File):
-    RE_FILE_TYPE = re.compile(r'^Device Tree Blob')
+    FILE_TYPE_RE = re.compile(r'^Device Tree Blob')
 
     def compare_details(self, other, source=None):
         return [Difference.from_command(DeviceTreeContents, self.path, other.path)]
diff --git a/diffoscope/comparators/elf.py b/diffoscope/comparators/elf.py
index f6240cf..d1a0862 100644
--- a/diffoscope/comparators/elf.py
+++ b/diffoscope/comparators/elf.py
@@ -530,15 +530,15 @@ class ElfContainer(Container):
 
 class ElfFile(File):
     CONTAINER_CLASS = ElfContainer
-    RE_FILE_TYPE = re.compile(r'^ELF ')
+    FILE_TYPE_RE = re.compile(r'^ELF ')
 
     def compare_details(self, other, source=None):
         return _compare_elf_data(self.path, other.path)
 
 class StaticLibFile(File):
     CONTAINER_CLASS = ElfContainer
-    RE_FILE_TYPE = re.compile(r'\bar archive\b')
-    RE_FILE_EXTENSION = re.compile(r'\.a$')
+    FILE_TYPE_RE = re.compile(r'\bar archive\b')
+    FILE_EXTENSION_SUFFIX = '.a'
 
     def compare_details(self, other, source=None):
         differences = [Difference.from_text_readers(
diff --git a/diffoscope/comparators/fontconfig.py b/diffoscope/comparators/fontconfig.py
index 887fb6c..0ba312e 100644
--- a/diffoscope/comparators/fontconfig.py
+++ b/diffoscope/comparators/fontconfig.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 re
 import struct
 
 from diffoscope.difference import Difference
@@ -27,8 +26,8 @@ from .utils.command import Command
 
 
 class FontconfigCacheFile(File):
-    RE_FILE_TYPE_FALLBACK_HEADER = struct.pack('<H', 0xFC04)
-    RE_FILE_EXTENSION = re.compile(r'\-le64\.cache-4$')
+    FILE_TYPE_HEADER_PREFIX = struct.pack('<H', 0xFC04)
+    FILE_EXTENSION_SUFFIX = '-le64.cache-4'
 
     def compare_details(self, other, source=None):
         return [Difference.from_text(
diff --git a/diffoscope/comparators/fonts.py b/diffoscope/comparators/fonts.py
index 1584d07..46648ca 100644
--- a/diffoscope/comparators/fonts.py
+++ b/diffoscope/comparators/fonts.py
@@ -36,7 +36,7 @@ class Showttf(Command):
 
 
 class TtfFile(File):
-    RE_FILE_TYPE = re.compile(r'^(TrueType|OpenType) font data$')
+    FILE_TYPE_RE = re.compile(r'^(TrueType|OpenType) font data$')
 
     def compare_details(self, other, source=None):
         return [Difference.from_command(Showttf, self.path, other.path)]
diff --git a/diffoscope/comparators/fsimage.py b/diffoscope/comparators/fsimage.py
index 9900be4..028d8c6 100644
--- a/diffoscope/comparators/fsimage.py
+++ b/diffoscope/comparators/fsimage.py
@@ -74,7 +74,7 @@ class FsImageContainer(Archive):
 
 class FsImageFile(File):
     CONTAINER_CLASS = FsImageContainer
-    RE_FILE_TYPE = re.compile(r'^(Linux.*filesystem data|BTRFS Filesystem).*')
+    FILE_TYPE_RE = re.compile(r'^(Linux.*filesystem data|BTRFS Filesystem).*')
 
     def compare_details(self, other, source=None):
         differences = []
diff --git a/diffoscope/comparators/gettext.py b/diffoscope/comparators/gettext.py
index 5801e91..feab414 100644
--- a/diffoscope/comparators/gettext.py
+++ b/diffoscope/comparators/gettext.py
@@ -61,7 +61,7 @@ class Msgunfmt(Command):
 
 
 class MoFile(File):
-    RE_FILE_TYPE = re.compile(r'^GNU message catalog\b')
+    FILE_TYPE_RE = re.compile(r'^GNU message catalog\b')
 
     def compare_details(self, other, source=None):
         return [Difference.from_command(Msgunfmt, self.path, other.path)]
diff --git a/diffoscope/comparators/gif.py b/diffoscope/comparators/gif.py
index 73dac5f..c0e7c43 100644
--- a/diffoscope/comparators/gif.py
+++ b/diffoscope/comparators/gif.py
@@ -67,7 +67,7 @@ def can_compose_gif_images(image1, image2):
 
 
 class GifFile(File):
-    RE_FILE_TYPE = re.compile(r'^GIF image data\b')
+    FILE_TYPE_RE = re.compile(r'^GIF image data\b')
 
     def compare_details(self, other, source=None):
         gifbuild_diff = Difference.from_command(
diff --git a/diffoscope/comparators/git.py b/diffoscope/comparators/git.py
index 6d1b40f..72c91ab 100644
--- a/diffoscope/comparators/git.py
+++ b/diffoscope/comparators/git.py
@@ -28,7 +28,7 @@ from .utils.file import File
 
 
 class GitIndexFile(File):
-    RE_FILE_TYPE = re.compile(r'^Git index')
+    FILE_TYPE_RE = re.compile(r'^Git index')
 
     def compare_details(self, other, source=None):
         return [Difference.from_text(
diff --git a/diffoscope/comparators/gzip.py b/diffoscope/comparators/gzip.py
index 92a9b3e..70cbc02 100644
--- a/diffoscope/comparators/gzip.py
+++ b/diffoscope/comparators/gzip.py
@@ -55,7 +55,7 @@ class GzipContainer(Archive):
 
 class GzipFile(File):
     CONTAINER_CLASS = GzipContainer
-    RE_FILE_TYPE = re.compile(r'^gzip compressed data\b')
+    FILE_TYPE_RE = re.compile(r'^gzip compressed data\b')
 
     def compare_details(self, other, source=None):
         return [Difference.from_text(self.magic_file_type, other.magic_file_type, self, other, source='metadata')]
diff --git a/diffoscope/comparators/icc.py b/diffoscope/comparators/icc.py
index 8f5d141..a1785a9 100644
--- a/diffoscope/comparators/icc.py
+++ b/diffoscope/comparators/icc.py
@@ -33,7 +33,7 @@ class Iccdump(Command):
 
 
 class IccFile(File):
-    RE_FILE_TYPE = re.compile(r'\bColorSync (ICC|color) [Pp]rofile')
+    FILE_TYPE_RE = re.compile(r'\bColorSync (ICC|color) [Pp]rofile')
 
     def compare_details(self, other, source=None):
         return [Difference.from_command(Iccdump, self.path, other.path)]
diff --git a/diffoscope/comparators/image.py b/diffoscope/comparators/image.py
index 439b9dd..a6170b6 100644
--- a/diffoscope/comparators/image.py
+++ b/diffoscope/comparators/image.py
@@ -144,7 +144,7 @@ def same_size(image1, image2):
 
 
 class JPEGImageFile(File):
-    RE_FILE_TYPE = re.compile(r'\bJPEG image data\b')
+    FILE_TYPE_RE = re.compile(r'\bJPEG image data\b')
 
     def compare_details(self, other, source=None):
         content_diff = Difference.from_command(
@@ -179,7 +179,7 @@ class JPEGImageFile(File):
 
 
 class ICOImageFile(File):
-    RE_FILE_TYPE = re.compile(r'\bMS Windows icon resource\b')
+    FILE_TYPE_RE = re.compile(r'\bMS Windows icon resource\b')
 
     def compare_details(self, other, source=None):
         differences = []
diff --git a/diffoscope/comparators/ipk.py b/diffoscope/comparators/ipk.py
index 3131dcc..12db3f2 100644
--- a/diffoscope/comparators/ipk.py
+++ b/diffoscope/comparators/ipk.py
@@ -24,4 +24,4 @@ from .gzip import GzipFile
 
 
 class IpkFile(GzipFile):
-    RE_FILE_EXTENSION = re.compile('\.ipk$')
+    FILE_EXTENSION_SUFFIX = '.ipk'
diff --git a/diffoscope/comparators/iso9660.py b/diffoscope/comparators/iso9660.py
index b354c74..2cefaee 100644
--- a/diffoscope/comparators/iso9660.py
+++ b/diffoscope/comparators/iso9660.py
@@ -69,12 +69,12 @@ class ISO9660Listing(Command):
 
 class Iso9660File(File):
     CONTAINER_CLASS = LibarchiveContainer
-    RE_FILE_TYPE = re.compile(r'\bISO 9660\b')
+    FILE_TYPE_RE = re.compile(r'\bISO 9660\b')
 
     @classmethod
     def recognizes(cls, file):
         if file.magic_file_type and \
-                cls.RE_FILE_TYPE.search(file.magic_file_type):
+                cls.FILE_TYPE_RE.search(file.magic_file_type):
             return True
 
         # Sometimes CDs put things like MBRs at the front which is an expected
diff --git a/diffoscope/comparators/java.py b/diffoscope/comparators/java.py
index 4b3e724..537d95f 100644
--- a/diffoscope/comparators/java.py
+++ b/diffoscope/comparators/java.py
@@ -44,7 +44,7 @@ class Javap(Command):
 
 
 class ClassFile(File):
-    RE_FILE_TYPE = re.compile(r'^compiled Java class data\b')
+    FILE_TYPE_RE = re.compile(r'^compiled Java class data\b')
 
     def compare_details(self, other, source=None):
         return [Difference.from_command(Javap, self.path, other.path)]
diff --git a/diffoscope/comparators/javascript.py b/diffoscope/comparators/javascript.py
index 1cdc865..1529378 100644
--- a/diffoscope/comparators/javascript.py
+++ b/diffoscope/comparators/javascript.py
@@ -17,8 +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 re
-
 from diffoscope.tools import tool_required
 from diffoscope.difference import Difference
 
@@ -32,7 +30,7 @@ class JavaScriptBeautify(Command):
         return ['js-beautify', self.path]
 
 class JavaScriptFile(File):
-    RE_FILE_EXTENSION = re.compile(r'\.js$')
+    FILE_EXTENSION_SUFFIX = '.js'
 
     def compare_details(self, other, source=None):
         return [Difference.from_command(JavaScriptBeautify, self.path, other.path)]
diff --git a/diffoscope/comparators/json.py b/diffoscope/comparators/json.py
index 943ec64..313127a 100644
--- a/diffoscope/comparators/json.py
+++ b/diffoscope/comparators/json.py
@@ -27,11 +27,11 @@ from .utils.file import File
 
 
 class JSONFile(File):
-    RE_FILE_EXTENSION = re.compile(r'\.json$')
+    FILE_EXTENSION_SUFFIX = '.json'
 
-    @staticmethod
-    def recognizes(file):
-        if JSONFile.RE_FILE_EXTENSION.search(file.name) is None:
+    @classmethod
+    def recognizes(cls, file):
+        if not super().recognizes(file):
             return False
 
         with open(file.path) as f:
diff --git a/diffoscope/comparators/llvm.py b/diffoscope/comparators/llvm.py
index 8c0fa69..dd27ca2 100644
--- a/diffoscope/comparators/llvm.py
+++ b/diffoscope/comparators/llvm.py
@@ -18,6 +18,8 @@
 # You should have received a copy of the GNU General Public License
 # along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.
 
+import re
+
 from diffoscope.tools import tool_required
 from diffoscope.difference import Difference
 
@@ -39,9 +41,7 @@ class LlvmBcDisassembler(Command):
         return ['find', self.path, '-execdir', 'llvm-dis', '-o', '-', '{}', ';']
 
 class LlvmBitCodeFile(File):
-    @staticmethod
-    def recognizes(file):
-        return file.magic_file_type and file.magic_file_type.startswith('LLVM IR bitcode')
+    FILE_TYPE_RE = re.compile(r'^LLVM IR bitcode')
 
     def compare_details(self, other, source=None):
         return [Difference.from_command(LlvmBcAnalyzer,   self.path, other.path),
diff --git a/diffoscope/comparators/macho.py b/diffoscope/comparators/macho.py
index 6acd492..338bac1 100644
--- a/diffoscope/comparators/macho.py
+++ b/diffoscope/comparators/macho.py
@@ -68,7 +68,7 @@ class OtoolDisassemble(Otool):
 
 
 class MachoFile(File):
-    RE_FILE_TYPE = re.compile(r'^Mach-O ')
+    FILE_TYPE_RE = re.compile(r'^Mach-O ')
     RE_EXTRACT_ARCHS = re.compile(r'^(?:Architectures in the fat file: .* are|Non-fat file: .* is architecture): (.*)$')
 
     @staticmethod
diff --git a/diffoscope/comparators/mono.py b/diffoscope/comparators/mono.py
index 08f2646..4d62541 100644
--- a/diffoscope/comparators/mono.py
+++ b/diffoscope/comparators/mono.py
@@ -34,7 +34,7 @@ class Pedump(Command):
 
 
 class MonoExeFile(File):
-    RE_FILE_TYPE = re.compile(r'\bPE[0-9]+\b.*\bMono\b')
+    FILE_TYPE_RE = re.compile(r'\bPE[0-9]+\b.*\bMono\b')
 
     def compare_details(self, other, source=None):
         return [Difference.from_command(Pedump, self.path, other.path)]
diff --git a/diffoscope/comparators/odt.py b/diffoscope/comparators/odt.py
index 78dead3..69d919d 100644
--- a/diffoscope/comparators/odt.py
+++ b/diffoscope/comparators/odt.py
@@ -37,7 +37,7 @@ class Odt2txt(Command):
 
 
 class OdtFile(File):
-    RE_FILE_TYPE = re.compile(r'^OpenDocument Text\b')
+    FILE_TYPE_RE = re.compile(r'^OpenDocument Text\b')
 
     def compare_details(self, other, source=None):
         return [Difference.from_command(
diff --git a/diffoscope/comparators/ogg.py b/diffoscope/comparators/ogg.py
index a37f6ed..dfa4b37 100644
--- a/diffoscope/comparators/ogg.py
+++ b/diffoscope/comparators/ogg.py
@@ -33,7 +33,7 @@ class OggDump(Command):
 
 
 class OggFile(File):
-    RE_FILE_TYPE = re.compile(r'^Ogg data')
+    FILE_TYPE_RE = re.compile(r'^Ogg data')
 
     def compare_details(self, other, source=None):
         return [Difference.from_command(
diff --git a/diffoscope/comparators/openssh.py b/diffoscope/comparators/openssh.py
index f17c44a..5e00632 100644
--- a/diffoscope/comparators/openssh.py
+++ b/diffoscope/comparators/openssh.py
@@ -32,7 +32,7 @@ class SSHKeyList(Command):
         return ['ssh-keygen', '-l', '-f', self.path]
 
 class PublicKeyFile(File):
-    RE_FILE_TYPE = re.compile(r'^OpenSSH \S+ public key')
+    FILE_TYPE_RE = re.compile(r'^OpenSSH \S+ public key')
 
     def compare_details(self, other, source=None):
         return [Difference.from_command(SSHKeyList, self.path, other.path)]
diff --git a/diffoscope/comparators/pcap.py b/diffoscope/comparators/pcap.py
index 5a0ac73..cf0361d 100644
--- a/diffoscope/comparators/pcap.py
+++ b/diffoscope/comparators/pcap.py
@@ -39,7 +39,7 @@ class Tcpdump(Command):
 
 
 class PcapFile(File):
-    RE_FILE_TYPE = re.compile(r'^tcpdump capture file\b')
+    FILE_TYPE_RE = re.compile(r'^tcpdump capture file\b')
 
     def compare_details(self, other, source=None):
         return [Difference.from_command(
diff --git a/diffoscope/comparators/pdf.py b/diffoscope/comparators/pdf.py
index 6221cf4..243e2ea 100644
--- a/diffoscope/comparators/pdf.py
+++ b/diffoscope/comparators/pdf.py
@@ -42,7 +42,7 @@ class Pdftk(Command):
 
 
 class PdfFile(File):
-    RE_FILE_TYPE = re.compile(r'^PDF document\b')
+    FILE_TYPE_RE = re.compile(r'^PDF document\b')
 
     def compare_details(self, other, source=None):
         return [Difference.from_command(Pdftotext, self.path, other.path),
diff --git a/diffoscope/comparators/pgp.py b/diffoscope/comparators/pgp.py
index 77b6911..3a15d6c 100644
--- a/diffoscope/comparators/pgp.py
+++ b/diffoscope/comparators/pgp.py
@@ -41,7 +41,7 @@ class Pgpdump(Command):
 
 
 class PgpFile(File):
-    RE_FILE_TYPE = re.compile(r'^PGP message\b')
+    FILE_TYPE_RE = re.compile(r'^PGP message\b')
 
     def compare_details(self, other, source=None):
         return [Difference.from_command(
diff --git a/diffoscope/comparators/png.py b/diffoscope/comparators/png.py
index ef88a05..2e193b9 100644
--- a/diffoscope/comparators/png.py
+++ b/diffoscope/comparators/png.py
@@ -45,7 +45,7 @@ class Sng(Command):
 
 
 class PngFile(File):
-    RE_FILE_TYPE = re.compile(r'^PNG image data\b')
+    FILE_TYPE_RE = re.compile(r'^PNG image data\b')
 
     def compare_details(self, other, source=None):
         sng_diff = Difference.from_command(Sng, self.path, other.path, source='sng')
diff --git a/diffoscope/comparators/ppu.py b/diffoscope/comparators/ppu.py
index 3370236..f8460e9 100644
--- a/diffoscope/comparators/ppu.py
+++ b/diffoscope/comparators/ppu.py
@@ -58,11 +58,11 @@ class Ppudump(Command):
 
 
 class PpuFile(File):
-    RE_FILE_EXTENSION = re.compile(r'\.ppu$')
+    FILE_EXTENSION_SUFFIX = '.ppu'
 
-    @staticmethod
-    def recognizes(file):
-        if not PpuFile.RE_FILE_EXTENSION.search(file.name):
+    @classmethod
+    def recognizes(cls, file):
+        if not super().recognizes(file):
             return False
         with open(file.path, 'rb') as f:
             magic = f.read(3)
diff --git a/diffoscope/comparators/ps.py b/diffoscope/comparators/ps.py
index 650da45..f6117e6 100644
--- a/diffoscope/comparators/ps.py
+++ b/diffoscope/comparators/ps.py
@@ -37,7 +37,7 @@ class Pstotext(Command):
 
 
 class PsFile(TextFile):
-    RE_FILE_TYPE = re.compile(r'^PostScript document\b')
+    FILE_TYPE_RE = re.compile(r'^PostScript document\b')
 
     def compare(self, other, *args, **kwargs):
         differences = super().compare(other, *args, **kwargs)
diff --git a/diffoscope/comparators/rdata.py b/diffoscope/comparators/rdata.py
index 6992108..56d479c 100644
--- a/diffoscope/comparators/rdata.py
+++ b/diffoscope/comparators/rdata.py
@@ -82,10 +82,7 @@ class RdbReader(Command):
         return ['Rscript', '-e', DUMP_RDB, self.path[:-4]]
 
 class RdbFile(File):
-    @staticmethod
-    def recognizes(file):
-        if file.name.endswith(".rdb"):
-            return True
+    FILE_EXTENSION_SUFFIX = '.rdb'
 
     def compare_details(self, other, source=None):
         self_path = ensure_archive_rdx(self)
diff --git a/diffoscope/comparators/rpm_fallback.py b/diffoscope/comparators/rpm_fallback.py
index 481d54d..b55b097 100644
--- a/diffoscope/comparators/rpm_fallback.py
+++ b/diffoscope/comparators/rpm_fallback.py
@@ -23,7 +23,7 @@ from .utils.file import File
 
 
 class AbstractRpmFile(File):
-    RE_FILE_TYPE = re.compile('^RPM\s')
+    FILE_TYPE_RE = re.compile('^RPM\s')
 
 class RpmFile(AbstractRpmFile):
     def compare(self, other, source=None):
diff --git a/diffoscope/comparators/rust.py b/diffoscope/comparators/rust.py
index 9e7a6c9..a053340 100644
--- a/diffoscope/comparators/rust.py
+++ b/diffoscope/comparators/rust.py
@@ -18,7 +18,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 re
 import zlib
 import os.path
 import logging
@@ -59,8 +58,8 @@ class RustObjectContainer(Archive):
 
 class RustObjectFile(File):
     CONTAINER_CLASS = RustObjectContainer
-    RE_FILE_TYPE_FALLBACK_HEADER = b'RUST_OBJECT\x01\x00\x00\x00'
-    RE_FILE_EXTENSION = re.compile(r'\.deflate$')
+    FILE_TYPE_HEADER_PREFIX = b'RUST_OBJECT\x01\x00\x00\x00'
+    FILE_EXTENSION_SUFFIX = '.deflate'
 
     def compare_details(self, other, source=None):
         return [Difference.from_text(self.magic_file_type, other.magic_file_type, self, other, source='metadata')]
diff --git a/diffoscope/comparators/sqlite.py b/diffoscope/comparators/sqlite.py
index f057605..e58d60a 100644
--- a/diffoscope/comparators/sqlite.py
+++ b/diffoscope/comparators/sqlite.py
@@ -33,7 +33,7 @@ class Sqlite3Dump(Command):
 
 
 class Sqlite3Database(File):
-    RE_FILE_TYPE = re.compile(r'^SQLite 3.x database')
+    FILE_TYPE_RE = re.compile(r'^SQLite 3.x database')
 
     def compare_details(self, other, source=None):
         return [Difference.from_command(Sqlite3Dump, self.path, other.path)]
diff --git a/diffoscope/comparators/squashfs.py b/diffoscope/comparators/squashfs.py
index 5e1c70a..fc38364 100644
--- a/diffoscope/comparators/squashfs.py
+++ b/diffoscope/comparators/squashfs.py
@@ -298,7 +298,7 @@ class SquashfsContainer(Archive):
 
 class SquashfsFile(File):
     CONTAINER_CLASS = SquashfsContainer
-    RE_FILE_TYPE = re.compile(r'^Squashfs filesystem\b')
+    FILE_TYPE_RE = re.compile(r'^Squashfs filesystem\b')
 
     def compare_details(self, other, source=None):
         return [
diff --git a/diffoscope/comparators/tar.py b/diffoscope/comparators/tar.py
index 6e0331b..7c2de1c 100644
--- a/diffoscope/comparators/tar.py
+++ b/diffoscope/comparators/tar.py
@@ -30,7 +30,7 @@ class TarContainer(LibarchiveContainer):
 
 class TarFile(File):
     CONTAINER_CLASS = TarContainer
-    RE_FILE_TYPE = re.compile(r'\btar archive\b')
+    FILE_TYPE_RE = re.compile(r'\btar archive\b')
 
     def compare_details(self, other, source=None):
         return [Difference.from_text_readers(list_libarchive(self.path),
diff --git a/diffoscope/comparators/text.py b/diffoscope/comparators/text.py
index 3f968f9..0c56f26 100644
--- a/diffoscope/comparators/text.py
+++ b/diffoscope/comparators/text.py
@@ -36,7 +36,7 @@ def order_only_difference(unified_diff):
 
 
 class TextFile(File):
-    RE_FILE_TYPE = re.compile(r'\btext\b')
+    FILE_TYPE_RE = re.compile(r'\btext\b')
 
     @property
     def encoding(self):
diff --git a/diffoscope/comparators/utils/file.py b/diffoscope/comparators/utils/file.py
index bada1a5..0a8c65c 100644
--- a/diffoscope/comparators/utils/file.py
+++ b/diffoscope/comparators/utils/file.py
@@ -106,10 +106,9 @@ class File(object, metaclass=abc.ABCMeta):
     def __del__(self):
         self.cleanup()
 
-    RE_FILE_TYPE = None
-    RE_FILE_EXTENSION = None
-    RE_FILE_TYPE_FALLBACK_HEADER = None
-    RE_CLASS = re.compile("").__class__
+    FILE_EXTENSION_SUFFIX = None
+    FILE_TYPE_RE = None
+    FILE_TYPE_HEADER_PREFIX = None
 
     @classmethod
     def recognizes(cls, file):
@@ -118,26 +117,24 @@ class File(object, metaclass=abc.ABCMeta):
         # for a class are filtered out, so that we don't get into a "vacuous
         # truth" situation like a naive all([]) invocation would give.
 
-        def run_tests(tests, fold):
-            return fold(t(x, y) for x, t, y in tests)
+        def run_tests(fold, tests):
+            return fold(t(y, x) for x, t, y in tests)
 
         file_type_tests = [test for test in (
-            (cls.RE_FILE_TYPE,
-             cls.RE_CLASS.search, file.magic_file_type),
-            (cls.RE_FILE_TYPE_FALLBACK_HEADER,
-             lambda m, h: h.startswith(m), file.file_header),
+            (cls.FILE_TYPE_RE,
+             lambda m, t: t.search(m), file.magic_file_type),
+            (cls.FILE_TYPE_HEADER_PREFIX,
+             bytes.startswith, file.file_header),
         ) if test[0]] # filter out undefined tests
 
         all_tests = [test for test in (
-            (cls.RE_FILE_EXTENSION,
-             cls.RE_CLASS.search, file.name),
+            (cls.FILE_EXTENSION_SUFFIX,
+             str.endswith, file.name),
             (file_type_tests,
              run_tests, any),
         ) if test[0]] # filter out undefined tests, inc. file_type_tests if it's empty
 
-        if all_tests:
-            return run_tests(all_tests, all)
-        return False
+        return run_tests(all, all_tests) if all_tests else False
 
     # This might be different from path and is used to do file extension matching
     @property
diff --git a/diffoscope/comparators/xz.py b/diffoscope/comparators/xz.py
index e2421e6..da75050 100644
--- a/diffoscope/comparators/xz.py
+++ b/diffoscope/comparators/xz.py
@@ -53,4 +53,4 @@ class XzContainer(Archive):
 
 class XzFile(File):
     CONTAINER_CLASS = XzContainer
-    RE_FILE_TYPE = re.compile(r'^XZ compressed data$')
+    FILE_TYPE_RE = re.compile(r'^XZ compressed data$')
diff --git a/diffoscope/comparators/zip.py b/diffoscope/comparators/zip.py
index c079a95..9cd9247 100644
--- a/diffoscope/comparators/zip.py
+++ b/diffoscope/comparators/zip.py
@@ -106,7 +106,7 @@ class ZipContainer(Archive):
 
 class ZipFile(File):
     CONTAINER_CLASS = ZipContainer
-    RE_FILE_TYPE = re.compile(r'^(Zip archive|Java archive|EPUB document|OpenDocument (Text|Spreadsheet|Presentation|Drawing|Formula|Template|Text Template))\b')
+    FILE_TYPE_RE = re.compile(r'^(Zip archive|Java archive|EPUB document|OpenDocument (Text|Spreadsheet|Presentation|Drawing|Formula|Template|Text Template))\b')
 
     def compare_details(self, other, source=None):
         zipinfo_difference = Difference.from_command(Zipinfo, self.path, other.path) or \

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


More information about the diffoscope mailing list