Mercurial > touhou
changeset 263:ac677dd0ffe0
Add support for reading from a directory instead of a PBG3 (for debugging purposes).
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Mon, 23 Jan 2012 01:40:07 +0100 |
parents | 8fa660da5f0c |
children | 3ac8b135592c |
files | pytouhou/resource/loader.py |
diffstat | 1 files changed, 35 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/pytouhou/resource/loader.py +++ b/pytouhou/resource/loader.py @@ -15,6 +15,34 @@ from pytouhou.formats.exe import SHT as from pytouhou.resource.anmwrapper import AnmWrapper +class Directory(object): + def __init__(self, path): + self.path = path + + + def __enter__(self): + return self + + + def __exit__(self, type, value, traceback): + return False + + + def list_files(self): + file_list = [] + for path in os.listdir(self.path): + if os.path.isfile(os.path.join(self.path, path)): + file_list.append(path) + return file_list + + + def extract(self, name): + with open(os.path.join(self.path, str(name)), 'rb') as file: + contents = file.read() + return contents + + + class ArchiveDescription(object): _formats = {'PBG3': PBG3} @@ -25,6 +53,9 @@ class ArchiveDescription(object): def open(self): + if self.format_class is Directory: + return self.format_class(self.path) + file = open(self.path, 'rb') instance = self.format_class.read(file) return instance @@ -32,6 +63,10 @@ class ArchiveDescription(object): @classmethod def get_from_path(cls, path): + if os.path.isdir(path): + instance = Directory(path) + file_list = instance.list_files() + return cls(path, Directory, file_list) with open(path, 'rb') as file: magic = file.read(4) file.seek(0)