Mercurial > touhou
comparison pytouhou/resource/loader.py @ 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 | dbb1a86c0235 |
comparison
equal
deleted
inserted
replaced
262:8fa660da5f0c | 263:ac677dd0ffe0 |
---|---|
13 | 13 |
14 | 14 |
15 from pytouhou.resource.anmwrapper import AnmWrapper | 15 from pytouhou.resource.anmwrapper import AnmWrapper |
16 | 16 |
17 | 17 |
18 class Directory(object): | |
19 def __init__(self, path): | |
20 self.path = path | |
21 | |
22 | |
23 def __enter__(self): | |
24 return self | |
25 | |
26 | |
27 def __exit__(self, type, value, traceback): | |
28 return False | |
29 | |
30 | |
31 def list_files(self): | |
32 file_list = [] | |
33 for path in os.listdir(self.path): | |
34 if os.path.isfile(os.path.join(self.path, path)): | |
35 file_list.append(path) | |
36 return file_list | |
37 | |
38 | |
39 def extract(self, name): | |
40 with open(os.path.join(self.path, str(name)), 'rb') as file: | |
41 contents = file.read() | |
42 return contents | |
43 | |
44 | |
45 | |
18 class ArchiveDescription(object): | 46 class ArchiveDescription(object): |
19 _formats = {'PBG3': PBG3} | 47 _formats = {'PBG3': PBG3} |
20 | 48 |
21 def __init__(self, path, format_class, file_list=None): | 49 def __init__(self, path, format_class, file_list=None): |
22 self.path = path | 50 self.path = path |
23 self.format_class = format_class | 51 self.format_class = format_class |
24 self.file_list = file_list or [] | 52 self.file_list = file_list or [] |
25 | 53 |
26 | 54 |
27 def open(self): | 55 def open(self): |
56 if self.format_class is Directory: | |
57 return self.format_class(self.path) | |
58 | |
28 file = open(self.path, 'rb') | 59 file = open(self.path, 'rb') |
29 instance = self.format_class.read(file) | 60 instance = self.format_class.read(file) |
30 return instance | 61 return instance |
31 | 62 |
32 | 63 |
33 @classmethod | 64 @classmethod |
34 def get_from_path(cls, path): | 65 def get_from_path(cls, path): |
66 if os.path.isdir(path): | |
67 instance = Directory(path) | |
68 file_list = instance.list_files() | |
69 return cls(path, Directory, file_list) | |
35 with open(path, 'rb') as file: | 70 with open(path, 'rb') as file: |
36 magic = file.read(4) | 71 magic = file.read(4) |
37 file.seek(0) | 72 file.seek(0) |
38 format_class = cls._formats[magic] | 73 format_class = cls._formats[magic] |
39 instance = format_class.read(file) | 74 instance = format_class.read(file) |