comparison pytouhou/resource/loader.py @ 536:6b76c9ba3975

Make archives return files by default, instead of bytes.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 05 Apr 2014 18:53:48 +0200
parents 69c73023f7a0
children e15672733c93
comparison
equal deleted inserted replaced
535:a50c0a1b628f 536:6b76c9ba3975
13 ## 13 ##
14 14
15 import os 15 import os
16 from glob import glob 16 from glob import glob
17 from itertools import chain 17 from itertools import chain
18 from io import BytesIO
19 18
20 from pytouhou.formats import WrongFormatError 19 from pytouhou.formats import WrongFormatError
21 from pytouhou.formats.pbg3 import PBG3 20 from pytouhou.formats.pbg3 import PBG3
22 from pytouhou.formats.std import Stage 21 from pytouhou.formats.std import Stage
23 from pytouhou.formats.ecl import ECL 22 from pytouhou.formats.ecl import ECL
53 if os.path.isfile(os.path.join(self.path, path)): 52 if os.path.isfile(os.path.join(self.path, path)):
54 file_list.append(path) 53 file_list.append(path)
55 return file_list 54 return file_list
56 55
57 56
58 def extract(self, name): 57 def get_file(self, name):
59 with open(os.path.join(self.path, str(name)), 'rb') as file: 58 return open(os.path.join(self.path, str(name)), 'rb')
60 contents = file.read()
61 return contents
62 59
63 60
64 61
65 class ArchiveDescription(object): 62 class ArchiveDescription(object):
66 _formats = {'PBG3': PBG3} 63 _formats = {'PBG3': PBG3}
122 archive_description = ArchiveDescription.get_from_path(path) 119 archive_description = ArchiveDescription.get_from_path(path)
123 for name in archive_description.file_list: 120 for name in archive_description.file_list:
124 self.known_files[name] = archive_description 121 self.known_files[name] = archive_description
125 122
126 123
127 def get_file_data(self, name):
128 with self.known_files[name].open() as archive:
129 content = archive.extract(name)
130 return content
131
132
133 def get_file(self, name): 124 def get_file(self, name):
134 with self.known_files[name].open() as archive: 125 with self.known_files[name].open() as archive:
135 content = archive.extract(name) 126 return archive.get_file(name)
136 return BytesIO(content)
137 127
138 128
139 def get_anm(self, name): 129 def get_anm(self, name):
140 if name in self.loaded_anms: 130 if name in self.loaded_anms:
141 logger.warn('ANM0 %s already loaded', name) 131 logger.warn('ANM0 %s already loaded', name)