1
0
mirror of https://github.com/mon/ifstools.git synced 2024-11-12 04:30:50 +01:00

Python 2 compatibility

This commit is contained in:
Will Toohey 2017-12-27 12:19:32 +10:00
parent 9371df11c3
commit 7d1e389b02
8 changed files with 43 additions and 18 deletions

3
.gitignore vendored
View File

@ -1 +1,4 @@
__pycache__/
*.ifs
*_ifs
*.pyc

View File

@ -6,7 +6,7 @@ import lxml.etree as etree
from . import get_folder_handlers, escapes
from .GenericFile import GenericFile
class GenericFolder():
class GenericFolder(object):
def __init__(self, ifs, name, time, files, folders):
self.ifs = ifs

View File

@ -2,6 +2,7 @@ from io import BytesIO
from struct import unpack, pack
from os.path import getmtime, isfile, join, dirname
from os import utime, mkdir
import errno
from PIL import Image
import lxml.etree as etree
@ -23,7 +24,7 @@ dxt5_end = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + \
class ImageFile(GenericFile):
def __init__(self, gen_file, image_elem, fmt, compress):
super().__init__(gen_file.ifs, gen_file.path,
super(ImageFile, self).__init__(gen_file.ifs, gen_file.path,
gen_file.name + '.png', gen_file.time,
gen_file.start, gen_file.size)
self._packed_name = gen_file._packed_name
@ -46,7 +47,7 @@ class ImageFile(GenericFile):
raise Exception('ImageFile must be instantiated from existing element')
def _load_from_ifs(self, convert_kbin = False):
data = super()._load_from_ifs()
data = super(ImageFile, self)._load_from_ifs()
if self.compress == 'avslz':
uncompressed_size = unpack('>I', data[:4])[0]
@ -137,8 +138,17 @@ class ImageFile(GenericFile):
return data
def _mkdir(self, dir):
try: # python 3
try:
mkdir(dir)
except FileExistsError:
pass
except NameError: # python 2
try:
mkdir(dir)
except OSError as e:
if e.errno == errno.EEXIST:
pass
else:
raise

View File

@ -7,7 +7,7 @@ from . import GenericFolder
class MD5Folder(GenericFolder):
def __init__(self, ifs, name, time, files, folders):
super().__init__(ifs, name, time, files, folders)
super(MD5Folder, self).__init__(ifs, name, time, files, folders)
for filename, file in self.files.items():
if filename.endswith('.xml'):
@ -20,13 +20,13 @@ class MD5Folder(GenericFolder):
@classmethod
def from_xml(cls, ifs, element, name = '', md5_tag = None, extension = None):
self = super().from_xml(ifs, element, name)
self = super(MD5Folder, cls).from_xml(ifs, element, name)
self._apply_md5(md5_tag, extension)
return self
@classmethod
def from_filesystem(cls, ifs, tree, name = '', md5_tag = None, extension = None):
self = super().from_filesystem(ifs, tree, name)
self = super(MD5Folder, cls).from_filesystem(ifs, tree, name)
self._apply_md5(md5_tag, extension)
return self

View File

@ -2,18 +2,18 @@ from . import MD5Folder, GenericFile, ImageFile
class TexFolder(MD5Folder):
def __init__(self, ifs, name, time, files, folders):
super().__init__(ifs, name, time, files, folders)
super(TexFolder, self).__init__(ifs, name, time, files, folders)
self.compress = self.info_kbin.xml_doc.attrib.get('compress')
@classmethod
def from_xml(cls, ifs, element, name = ''):
self = super().from_xml(ifs, element, name, 'image', '.png')
self = super(TexFolder, cls).from_xml(ifs, element, name, 'image', '.png')
self._create_images()
return self
@classmethod
def from_filesystem(cls, ifs, tree, name = ''):
self = super().from_filesystem(ifs, tree, name, 'image', '.png')
self = super(TexFolder, cls).from_filesystem(ifs, tree, name, 'image', '.png')
self._create_images()
return self

View File

@ -17,7 +17,8 @@ def decompress(input):
decompressed = bytearray()
while True:
flag = input.read(1)[0]
# wrap in bytes for py2
flag = bytes(input.read(1))[0]
for i in range(8):
if (flag >> i) & 1 == 1:
decompressed.append(input.read(1)[0])

View File

@ -1,5 +1,6 @@
from os.path import basename, dirname, splitext, join, isdir, isfile, getmtime
from os import mkdir, utime, walk
import errno
from io import BytesIO
import hashlib
import lxml.etree as etree
@ -181,10 +182,19 @@ class IFS:
utime(outdir, (timestamp, timestamp))
def _mkdir(self, dir):
try: # python 3
try:
mkdir(dir)
except FileExistsError:
pass
except NameError: # python 2
try:
mkdir(dir)
except OSError as e:
if e.errno == errno.EEXIST:
pass
else:
raise
def load_file(self, start, size):
start = self.header_end+start

View File

@ -1,4 +1,5 @@
lxml
tqdm
pillow
future
git+https://github.com/mon/kbinxml.git