A class to read zipped files
| 14 | |
| 15 | |
| 16 | class ZipReader(object): |
| 17 | """A class to read zipped files""" |
| 18 | zip_bank = dict() |
| 19 | |
| 20 | def __init__(self): |
| 21 | super(ZipReader, self).__init__() |
| 22 | |
| 23 | @staticmethod |
| 24 | def get_zipfile(path): |
| 25 | zip_bank = ZipReader.zip_bank |
| 26 | if path not in zip_bank: |
| 27 | zfile = zipfile.ZipFile(path, 'r') |
| 28 | zip_bank[path] = zfile |
| 29 | return zip_bank[path] |
| 30 | |
| 31 | @staticmethod |
| 32 | def split_zip_style_path(path): |
| 33 | pos_at = path.index('@') |
| 34 | assert pos_at != -1, "character '@' is not found from the given path '%s'" % path |
| 35 | |
| 36 | zip_path = path[0: pos_at] |
| 37 | folder_path = path[pos_at + 1:] |
| 38 | folder_path = str.strip(folder_path, '/') |
| 39 | return zip_path, folder_path |
| 40 | |
| 41 | @staticmethod |
| 42 | def list_folder(path): |
| 43 | zip_path, folder_path = ZipReader.split_zip_style_path(path) |
| 44 | |
| 45 | zfile = ZipReader.get_zipfile(zip_path) |
| 46 | folder_list = [] |
| 47 | for file_foler_name in zfile.namelist(): |
| 48 | file_foler_name = str.strip(file_foler_name, '/') |
| 49 | if file_foler_name.startswith(folder_path) and \ |
| 50 | len(os.path.splitext(file_foler_name)[-1]) == 0 and \ |
| 51 | file_foler_name != folder_path: |
| 52 | if len(folder_path) == 0: |
| 53 | folder_list.append(file_foler_name) |
| 54 | else: |
| 55 | folder_list.append(file_foler_name[len(folder_path) + 1:]) |
| 56 | |
| 57 | return folder_list |
| 58 | |
| 59 | @staticmethod |
| 60 | def list_files(path, extension=None): |
| 61 | if extension is None: |
| 62 | extension = ['.*'] |
| 63 | zip_path, folder_path = ZipReader.split_zip_style_path(path) |
| 64 | |
| 65 | zfile = ZipReader.get_zipfile(zip_path) |
| 66 | file_lists = [] |
| 67 | for file_foler_name in zfile.namelist(): |
| 68 | file_foler_name = str.strip(file_foler_name, '/') |
| 69 | if file_foler_name.startswith(folder_path) and \ |
| 70 | str.lower(os.path.splitext(file_foler_name)[-1]) in extension: |
| 71 | if len(folder_path) == 0: |
| 72 | file_lists.append(file_foler_name) |
| 73 | else: |
nothing calls this directly
no outgoing calls
no test coverage detected