From 5eb3cbb4082a945b222f474fe5a546c7485dca48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20A=C5=9F=C4=B1c=C4=B1?= Date: Tue, 13 Jul 2010 09:31:21 +0000 Subject: [PATCH] file: Implement is_compressed method and simplify choosemethod --- pisi/file.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/pisi/file.py b/pisi/file.py index 216e80a9..b3092f72 100644 --- a/pisi/file.py +++ b/pisi/file.py @@ -55,6 +55,10 @@ class File: (xz, bz2, gzip, auto) = range(4) # compress enums (detached, whatelse) = range(2) + __compressed_file_extensions = {".xz": xz, + ".bz2": bz2, + ".gz": gzip} + @staticmethod def make_uri(uri): "handle URI arg" @@ -67,17 +71,18 @@ class File: @staticmethod def choose_method(filename, compress): if compress == File.auto: - if filename.endswith('.xz'): - return File.xz - elif filename.endswith('.bz2'): - return File.bz2 - elif filename.endswith('.gz'): - return File.gzip - else: - return None + for ext, method in File.__compressed_file_extensions.items(): + if filename.endswith(ext): + return method + + return None else: return compress + @staticmethod + def is_compressed(filename): + return filename.endswith(tuple(File.__compressed_file_extensions)) + @staticmethod def decompress(localfile, compress): compress = File.choose_method(localfile, compress)