[diffoscope] 01/02: Move 'container with one member' logic into separate class.
Maria Glukhova
siamezzze-guest at moszumanska.debian.org
Thu Feb 16 22:45:01 CET 2017
This is an automated email from the git hooks/post-receive script.
siamezzze-guest pushed a commit to branch siamezzze/containers
in repository diffoscope.
commit 926abd82bca355d55333ffad88f2dea3cba23b15
Author: Maria Glukhova <siamezzze at gmail.com>
Date: Thu Feb 16 23:14:28 2017 +0200
Move 'container with one member' logic into separate class.
Create OneMenberContainer class, that incapsulates the logic of
container having only one member. It has the ability (get_nested_container)
to check if this only member is also a container, and in such case,
return this inner container.
Change Bzip2Container, GzipContainer and XzContainer to use this
class, avoiding DRY violations.
---
diffoscope/comparators/bzip2.py | 12 +++---------
diffoscope/comparators/gzip.py | 12 +++---------
diffoscope/comparators/utils/container.py | 12 ++++++++++++
diffoscope/comparators/xz.py | 12 +++---------
4 files changed, 21 insertions(+), 27 deletions(-)
diff --git a/diffoscope/comparators/bzip2.py b/diffoscope/comparators/bzip2.py
index e8fa96e..98f4b47 100644
--- a/diffoscope/comparators/bzip2.py
+++ b/diffoscope/comparators/bzip2.py
@@ -27,13 +27,13 @@ from diffoscope.tools import tool_required
from .utils.file import File
from .utils.archive import Archive
+from .utils.container import OneMemberContainer
from .utils.filenames import get_compressed_content_name
-from .utils.specialize import specialize
logger = logging.getLogger(__name__)
-class Bzip2Container(Archive):
+class Bzip2Container(Archive, OneMemberContainer):
def open_archive(self):
return self
@@ -41,17 +41,11 @@ class Bzip2Container(Archive):
pass
def get_members(self):
- return collections.OrderedDict({'bzip2-content': self.get_member(self.get_member_names()[0])})
+ return collections.OrderedDict({'bzip2-content': self.get_the_only_member()})
def get_member_names(self):
return [get_compressed_content_name(self.source.path, '.bz2')]
- def get_nested_container(self):
- # If the only member of container is also container, return it.
- only_member = self.get_member(self.get_member_names()[0])
- specialize(only_member)
- return only_member.as_container
-
@tool_required('bzip2')
def extract(self, member_name, dest_dir):
dest_path = os.path.join(dest_dir, member_name)
diff --git a/diffoscope/comparators/gzip.py b/diffoscope/comparators/gzip.py
index 2541496..b1f0233 100644
--- a/diffoscope/comparators/gzip.py
+++ b/diffoscope/comparators/gzip.py
@@ -29,13 +29,13 @@ from diffoscope.difference import Difference
from .utils.file import File
from .utils.archive import Archive
+from .utils.container import OneMemberContainer
from .utils.filenames import get_compressed_content_name
-from .utils.specialize import specialize
logger = logging.getLogger(__name__)
-class GzipContainer(Archive):
+class GzipContainer(Archive, OneMemberContainer):
def open_archive(self):
return self
@@ -43,17 +43,11 @@ class GzipContainer(Archive):
pass
def get_members(self):
- return collections.OrderedDict({'gzip-content': self.get_member(self.get_member_names()[0])})
+ return collections.OrderedDict({'gzip-content': self.get_the_only_member()})
def get_member_names(self):
return [get_compressed_content_name(self.source.path, '.gz')]
- def get_nested_container(self):
- # If the only member of container is also container, return it.
- only_member = self.get_member(self.get_member_names()[0])
- specialize(only_member)
- return only_member.as_container
-
@tool_required('gzip')
def extract(self, member_name, dest_dir):
dest_path = os.path.join(dest_dir, member_name)
diff --git a/diffoscope/comparators/utils/container.py b/diffoscope/comparators/utils/container.py
index b5cb97c..ffb1f5a 100644
--- a/diffoscope/comparators/utils/container.py
+++ b/diffoscope/comparators/utils/container.py
@@ -28,6 +28,7 @@ from diffoscope.progress import Progress
from ..missing_file import MissingFile
from .fuzzy import perform_fuzzy_matching
+from .specialize import specialize
NO_COMMENT = None
@@ -168,3 +169,14 @@ class MissingContainer(Container):
def get_member(self, member_name):
return MissingFile('/dev/null')
+
+
+class OneMemberContainer(Container, metaclass=abc.ABCMeta):
+ def get_the_only_member(self):
+ return self.get_member(self.get_member_names()[0])
+
+ def get_nested_container(self):
+ # If the only member of container is also container, return it.
+ only_member = self.get_the_only_member()
+ specialize(only_member)
+ return only_member.as_container
diff --git a/diffoscope/comparators/xz.py b/diffoscope/comparators/xz.py
index 77edaec..bbd1ad2 100644
--- a/diffoscope/comparators/xz.py
+++ b/diffoscope/comparators/xz.py
@@ -27,13 +27,13 @@ from diffoscope.tools import tool_required
from .utils.file import File
from .utils.archive import Archive
+from .utils.container import OneMemberContainer
from .utils.filenames import get_compressed_content_name
-from .utils.specialize import specialize
logger = logging.getLogger(__name__)
-class XzContainer(Archive):
+class XzContainer(Archive, OneMemberContainer):
def open_archive(self):
return self
@@ -41,17 +41,11 @@ class XzContainer(Archive):
pass
def get_members(self):
- return collections.OrderedDict({'xz-content': self.get_member(self.get_member_names()[0])})
+ return collections.OrderedDict({'xz-content': self.get_the_only_member()})
def get_member_names(self):
return [get_compressed_content_name(self.source.path, '.xz')]
- def get_nested_container(self):
- # If the only member of container is also container, return it.
- only_member = self.get_member(self.get_member_names()[0])
- specialize(only_member)
- return only_member.as_container
-
@tool_required('xz')
def extract(self, member_name, dest_dir):
dest_path = os.path.join(dest_dir, member_name)
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git
More information about the diffoscope
mailing list