Skip to content

Commit

Permalink
Code cleanup: remove CompressionPrograms map.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastian Kleineidam committed May 17, 2012
1 parent 0f976ef commit b3b48ad
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 34 deletions.
49 changes: 22 additions & 27 deletions patoolib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

# Supported archive formats
ArchiveFormats = ('7z', 'ace', 'alzip', 'ar', 'arc', 'arj', 'bzip2',
'cab', 'compress', 'cpio', 'deb', 'dms', 'gzip', 'lrzip', 'lzh', 'lzip', 'lzma',
'lzop', 'rar', 'rpm', 'rzip', 'tar', 'xz', 'zip', 'zoo')
'cab', 'compress', 'cpio', 'deb', 'dms', 'gzip', 'lrzip', 'lzh',
'lzip', 'lzma', 'lzop', 'rar', 'rpm', 'rzip', 'tar', 'xz', 'zip', 'zoo')

# Supported compressions (used with tar for example)
# Note that all compressions must also be archive formats
Expand Down Expand Up @@ -65,17 +65,6 @@
'application/x-dms': 'dms',
}

# List of programs supporting the given compression

CompressionPrograms = {
'gzip': ('pigz', 'gzip'),
'bzip2': ('pbzip2', 'lbzip2', 'bzip2'),
'compress': ('compress',),
'lzma': ('lzma',),
'xz': ('xz',),
'lzip': ('lzip', 'clzip', 'plzip', 'pdlzip'),
}

# List of programs supporting the given archive format and command.
# If command is None, the program supports all commands (list, extract, ...)
# Programs starting with "py_" are Python modules.
Expand Down Expand Up @@ -268,17 +257,16 @@ def find_archive_program (format, command):
raise util.PatoolError("could not find an executable program to %s format %s; candidates are (%s)," % (command, format, ",".join(programs)))


def find_compression_program (program, compression):
"""Find suitable compression program and return it. Returns None if
no compression program could be found"""
if program in ('tar', 'star'):
for enc_program in CompressionPrograms[compression]:
found = util.find_program(enc_program)
if found:
return found
elif program == 'py_tarfile':
def program_supports_compression (program, compression):
"""Decide if the given program supports the compression natively.
@return: True iff the program supports the given compression format
natively, else False.
"""
if program == 'py_tarfile':
return compression in ('gzip', 'bzip2')
return None
if program in ('tar', 'star'):
return compression in ('gzip', 'bzip2', 'xz', 'lzma')
return False


def list_formats ():
Expand Down Expand Up @@ -339,10 +327,17 @@ def parse_config (archive, format, compression, command, **kwargs):
value = program
config[key] = value
program = os.path.basename(config['program'])
if compression and not find_compression_program(program, compression):
msg = "cannot %s archive `%s': compression `%s' not supported by %s" % \
(command, archive, compression, program)
raise util.PatoolError(msg)
if compression:
# check if compression is supported
if not program_supports_compression(program, compression):
if command == 'create':
comp_command = command
else:
comp_command = 'extract'
comp_prog = find_archive_program(compression, comp_command)
if not comp_prog:
msg = "cannot %s archive `%s': compression `%s' not supported"
raise util.PatoolError(msg % (command, archive, compression))
return config


Expand Down
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,4 @@ def has_codec (program, codec):
"""Test if program supports given codec."""
if program == '7z' and codec == 'rar':
return patoolib.util.p7zip_supports_rar()
return patoolib.find_compression_program(program, codec)
return patoolib.program_supports_compression(program, codec)
11 changes: 7 additions & 4 deletions tests/test_archives.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def test_tar_gz (self):
self.archive_commands('t.tar.gz')
self.archive_commands('t.tgz')

@needs_codec('tar', 'compress')
@needs_program('tar')
@needs_program('compress')
def test_tar_z (self):
self.program = 'tar'
self.archive_commands('t.tar.Z')
Expand All @@ -47,8 +48,8 @@ def test_tar_lzma (self):

# even though clzip would support extracting .lz files, the
# file(1) --uncompress command does not use it for achive detection
@needs_program('tar')
@needs_program('lzip')
@needs_codec('tar', 'lzip')
def test_tar_lzip (self):
self.program = 'tar'
self.archive_commands('t.tar.lz')
Expand All @@ -69,7 +70,8 @@ def test_star_gz (self):
self.archive_commands('t.tar.gz')
self.archive_commands('t.tgz')

@needs_codec('star', 'compress')
@needs_program('star')
@needs_program('compress')
def test_star_z (self):
self.program = 'star'
self.archive_commands('t.tar.Z')
Expand All @@ -86,7 +88,8 @@ def test_star_lzma (self):
self.program = 'star'
self.archive_commands('t.tar.lzma')

@needs_codec('star', 'lzip')
@needs_program('star')
@needs_program('lzip')
def test_star_lzip (self):
self.program = 'star'
self.archive_commands('t.tar.lz')
Expand Down
4 changes: 2 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def test_archive_programs (self):
self.assertTrue(command in patoolib.ArchiveCommands)

def test_compression_programs (self):
self.assertEqual(set(patoolib.ArchiveCompressions),
set(patoolib.CompressionPrograms.keys()))
self.assertTrue(set(patoolib.ArchiveCompressions).issubset(
set(patoolib.ArchiveFormats)))

def test_encoding_mimes (self):
self.assertEqual(set(patoolib.ArchiveCompressions),
Expand Down

0 comments on commit b3b48ad

Please sign in to comment.