mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-06 04:43:10 +03:00
Flake8 fixes
This commit is contained in:
parent
1208fe89cd
commit
1b98b76bcf
|
@ -256,7 +256,10 @@ __version__ = '0.42b'
|
||||||
|
|
||||||
import io
|
import io
|
||||||
import sys
|
import sys
|
||||||
import struct, array, os.path, datetime
|
import struct
|
||||||
|
import array
|
||||||
|
import os.path
|
||||||
|
import datetime
|
||||||
|
|
||||||
#=== COMPATIBILITY WORKAROUNDS ================================================
|
#=== COMPATIBILITY WORKAROUNDS ================================================
|
||||||
|
|
||||||
|
@ -326,12 +329,19 @@ else:
|
||||||
# [PL] DEBUG display mode: False by default, use set_debug_mode() or "-d" on
|
# [PL] DEBUG display mode: False by default, use set_debug_mode() or "-d" on
|
||||||
# command line to change it.
|
# command line to change it.
|
||||||
DEBUG_MODE = False
|
DEBUG_MODE = False
|
||||||
|
|
||||||
|
|
||||||
def debug_print(msg):
|
def debug_print(msg):
|
||||||
print(msg)
|
print(msg)
|
||||||
|
|
||||||
|
|
||||||
def debug_pass(msg):
|
def debug_pass(msg):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
debug = debug_pass
|
debug = debug_pass
|
||||||
|
|
||||||
|
|
||||||
def set_debug_mode(debug_mode):
|
def set_debug_mode(debug_mode):
|
||||||
"""
|
"""
|
||||||
Set debug mode on or off, to control display of debugging messages.
|
Set debug mode on or off, to control display of debugging messages.
|
||||||
|
@ -502,7 +512,6 @@ def _clsid(clsid):
|
||||||
tuple(map(i8, clsid[8:16]))))
|
tuple(map(i8, clsid[8:16]))))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def filetime2datetime(filetime):
|
def filetime2datetime(filetime):
|
||||||
"""
|
"""
|
||||||
convert FILETIME (64 bits int) to Python datetime.datetime
|
convert FILETIME (64 bits int) to Python datetime.datetime
|
||||||
|
@ -514,7 +523,6 @@ def filetime2datetime(filetime):
|
||||||
return _FILETIME_null_date + datetime.timedelta(microseconds=filetime//10)
|
return _FILETIME_null_date + datetime.timedelta(microseconds=filetime//10)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#=== CLASSES ==================================================================
|
#=== CLASSES ==================================================================
|
||||||
|
|
||||||
class OleMetadata(object):
|
class OleMetadata(object):
|
||||||
|
@ -622,7 +630,6 @@ class OleMetadata(object):
|
||||||
self.language = None
|
self.language = None
|
||||||
self.doc_version = None
|
self.doc_version = None
|
||||||
|
|
||||||
|
|
||||||
def parse_properties(self, olefile):
|
def parse_properties(self, olefile):
|
||||||
"""
|
"""
|
||||||
Parse standard properties of an OLE file, from the streams
|
Parse standard properties of an OLE file, from the streams
|
||||||
|
@ -833,7 +840,6 @@ class _OleDirectoryEntry(object):
|
||||||
DIRENTRY_SIZE = 128
|
DIRENTRY_SIZE = 128
|
||||||
assert struct.calcsize(STRUCT_DIRENTRY) == DIRENTRY_SIZE
|
assert struct.calcsize(STRUCT_DIRENTRY) == DIRENTRY_SIZE
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, entry, sid, olefile):
|
def __init__(self, entry, sid, olefile):
|
||||||
"""
|
"""
|
||||||
Constructor for an _OleDirectoryEntry object.
|
Constructor for an _OleDirectoryEntry object.
|
||||||
|
@ -929,8 +935,6 @@ class _OleDirectoryEntry(object):
|
||||||
minifat = False
|
minifat = False
|
||||||
olefile._check_duplicate_stream(self.isectStart, minifat)
|
olefile._check_duplicate_stream(self.isectStart, minifat)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def build_storage_tree(self):
|
def build_storage_tree(self):
|
||||||
"""
|
"""
|
||||||
Read and build the red-black tree attached to this _OleDirectoryEntry
|
Read and build the red-black tree attached to this _OleDirectoryEntry
|
||||||
|
@ -954,7 +958,6 @@ class _OleDirectoryEntry(object):
|
||||||
# (see rich comparison methods in this class)
|
# (see rich comparison methods in this class)
|
||||||
self.kids.sort()
|
self.kids.sort()
|
||||||
|
|
||||||
|
|
||||||
def append_kids(self, child_sid):
|
def append_kids(self, child_sid):
|
||||||
"""
|
"""
|
||||||
Walk through red-black tree of children of this directory entry to add
|
Walk through red-black tree of children of this directory entry to add
|
||||||
|
@ -998,7 +1001,6 @@ class _OleDirectoryEntry(object):
|
||||||
# Afterwards build kid's own tree if it's also a storage:
|
# Afterwards build kid's own tree if it's also a storage:
|
||||||
child.build_storage_tree()
|
child.build_storage_tree()
|
||||||
|
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
"Compare entries by name"
|
"Compare entries by name"
|
||||||
return self.name == other.name
|
return self.name == other.name
|
||||||
|
@ -1018,7 +1020,6 @@ class _OleDirectoryEntry(object):
|
||||||
#TODO: replace by the same function as MS implementation ?
|
#TODO: replace by the same function as MS implementation ?
|
||||||
# (order by name length first, then case-insensitive order)
|
# (order by name length first, then case-insensitive order)
|
||||||
|
|
||||||
|
|
||||||
def dump(self, tab = 0):
|
def dump(self, tab = 0):
|
||||||
"Dump this entry, and all its subentries (for debug purposes only)"
|
"Dump this entry, and all its subentries (for debug purposes only)"
|
||||||
TYPES = ["(invalid)", "(storage)", "(stream)", "(lockbytes)",
|
TYPES = ["(invalid)", "(storage)", "(stream)", "(lockbytes)",
|
||||||
|
@ -1033,7 +1034,6 @@ class _OleDirectoryEntry(object):
|
||||||
for kid in self.kids:
|
for kid in self.kids:
|
||||||
kid.dump(tab + 2)
|
kid.dump(tab + 2)
|
||||||
|
|
||||||
|
|
||||||
def getmtime(self):
|
def getmtime(self):
|
||||||
"""
|
"""
|
||||||
Return modification time of a directory entry.
|
Return modification time of a directory entry.
|
||||||
|
@ -1047,7 +1047,6 @@ class _OleDirectoryEntry(object):
|
||||||
return None
|
return None
|
||||||
return filetime2datetime(self.modifyTime)
|
return filetime2datetime(self.modifyTime)
|
||||||
|
|
||||||
|
|
||||||
def getctime(self):
|
def getctime(self):
|
||||||
"""
|
"""
|
||||||
Return creation time of a directory entry.
|
Return creation time of a directory entry.
|
||||||
|
@ -1134,7 +1133,6 @@ class OleFileIO(object):
|
||||||
if filename:
|
if filename:
|
||||||
self.open(filename, write_mode=write_mode)
|
self.open(filename, write_mode=write_mode)
|
||||||
|
|
||||||
|
|
||||||
def raise_defect(self, defect_level, message, exception_type=IOError):
|
def raise_defect(self, defect_level, message, exception_type=IOError):
|
||||||
"""
|
"""
|
||||||
This method should be called for any defect found during file parsing.
|
This method should be called for any defect found during file parsing.
|
||||||
|
@ -1158,7 +1156,6 @@ class OleFileIO(object):
|
||||||
# just record the issue, no exception raised:
|
# just record the issue, no exception raised:
|
||||||
self.parsing_issues.append((exception_type, message))
|
self.parsing_issues.append((exception_type, message))
|
||||||
|
|
||||||
|
|
||||||
def _decode_utf16_str(self, utf16_str, errors='replace'):
|
def _decode_utf16_str(self, utf16_str, errors='replace'):
|
||||||
"""
|
"""
|
||||||
Decode a string encoded in UTF-16 LE format, as found in the OLE
|
Decode a string encoded in UTF-16 LE format, as found in the OLE
|
||||||
|
@ -1177,7 +1174,6 @@ class OleFileIO(object):
|
||||||
# path_encoding=None, return the Unicode string as-is:
|
# path_encoding=None, return the Unicode string as-is:
|
||||||
return unicode_str
|
return unicode_str
|
||||||
|
|
||||||
|
|
||||||
def open(self, filename, write_mode=False):
|
def open(self, filename, write_mode=False):
|
||||||
"""
|
"""
|
||||||
Open an OLE2 file in read-only or read/write mode.
|
Open an OLE2 file in read-only or read/write mode.
|
||||||
|
@ -1393,14 +1389,12 @@ class OleFileIO(object):
|
||||||
self.ministream = None
|
self.ministream = None
|
||||||
self.minifatsect = self.MiniFatStart #i32(header, 60)
|
self.minifatsect = self.MiniFatStart #i32(header, 60)
|
||||||
|
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
"""
|
"""
|
||||||
close the OLE file, to release the file object
|
close the OLE file, to release the file object
|
||||||
"""
|
"""
|
||||||
self.fp.close()
|
self.fp.close()
|
||||||
|
|
||||||
|
|
||||||
def _check_duplicate_stream(self, first_sect, minifat=False):
|
def _check_duplicate_stream(self, first_sect, minifat=False):
|
||||||
"""
|
"""
|
||||||
Checks if a stream has not been already referenced elsewhere.
|
Checks if a stream has not been already referenced elsewhere.
|
||||||
|
@ -1426,7 +1420,6 @@ class OleFileIO(object):
|
||||||
else:
|
else:
|
||||||
used_streams.append(first_sect)
|
used_streams.append(first_sect)
|
||||||
|
|
||||||
|
|
||||||
def dumpfat(self, fat, firstindex=0):
|
def dumpfat(self, fat, firstindex=0):
|
||||||
"Displays a part of FAT in human-readable form for debugging purpose"
|
"Displays a part of FAT in human-readable form for debugging purpose"
|
||||||
# [PL] added only for debug
|
# [PL] added only for debug
|
||||||
|
@ -1464,7 +1457,6 @@ class OleFileIO(object):
|
||||||
print(name, end=" ")
|
print(name, end=" ")
|
||||||
print()
|
print()
|
||||||
|
|
||||||
|
|
||||||
def dumpsect(self, sector, firstindex=0):
|
def dumpsect(self, sector, firstindex=0):
|
||||||
"Displays a sector in a human-readable form, for debugging purpose."
|
"Displays a sector in a human-readable form, for debugging purpose."
|
||||||
if not DEBUG_MODE:
|
if not DEBUG_MODE:
|
||||||
|
@ -1501,7 +1493,6 @@ class OleFileIO(object):
|
||||||
a.byteswap()
|
a.byteswap()
|
||||||
return a
|
return a
|
||||||
|
|
||||||
|
|
||||||
def loadfat_sect(self, sect):
|
def loadfat_sect(self, sect):
|
||||||
"""
|
"""
|
||||||
Adds the indexes of the given sector to the FAT
|
Adds the indexes of the given sector to the FAT
|
||||||
|
@ -1533,7 +1524,6 @@ class OleFileIO(object):
|
||||||
self.fat = self.fat + nextfat
|
self.fat = self.fat + nextfat
|
||||||
return isect
|
return isect
|
||||||
|
|
||||||
|
|
||||||
def loadfat(self, header):
|
def loadfat(self, header):
|
||||||
"""
|
"""
|
||||||
Load the FAT table.
|
Load the FAT table.
|
||||||
|
@ -1608,7 +1598,6 @@ class OleFileIO(object):
|
||||||
debug('\nFAT:')
|
debug('\nFAT:')
|
||||||
self.dumpfat(self.fat)
|
self.dumpfat(self.fat)
|
||||||
|
|
||||||
|
|
||||||
def loadminifat(self):
|
def loadminifat(self):
|
||||||
"""
|
"""
|
||||||
Load the MiniFAT table.
|
Load the MiniFAT table.
|
||||||
|
@ -1674,7 +1663,6 @@ class OleFileIO(object):
|
||||||
self.raise_defect(DEFECT_FATAL, 'incomplete OLE sector')
|
self.raise_defect(DEFECT_FATAL, 'incomplete OLE sector')
|
||||||
return sector
|
return sector
|
||||||
|
|
||||||
|
|
||||||
def write_sect(self, sect, data, padding=b'\x00'):
|
def write_sect(self, sect, data, padding=b'\x00'):
|
||||||
"""
|
"""
|
||||||
Write given sector to file on disk.
|
Write given sector to file on disk.
|
||||||
|
@ -1701,7 +1689,6 @@ class OleFileIO(object):
|
||||||
raise ValueError("Data is larger than sector size")
|
raise ValueError("Data is larger than sector size")
|
||||||
self.fp.write(data)
|
self.fp.write(data)
|
||||||
|
|
||||||
|
|
||||||
def loaddirectory(self, sect):
|
def loaddirectory(self, sect):
|
||||||
"""
|
"""
|
||||||
Load the directory.
|
Load the directory.
|
||||||
|
@ -1737,7 +1724,6 @@ class OleFileIO(object):
|
||||||
# read and build all storage trees, starting from the root:
|
# read and build all storage trees, starting from the root:
|
||||||
self.root.build_storage_tree()
|
self.root.build_storage_tree()
|
||||||
|
|
||||||
|
|
||||||
def _load_direntry(self, sid):
|
def _load_direntry(self, sid):
|
||||||
"""
|
"""
|
||||||
Load a directory entry from the directory.
|
Load a directory entry from the directory.
|
||||||
|
@ -1763,14 +1749,12 @@ class OleFileIO(object):
|
||||||
self.direntries[sid] = _OleDirectoryEntry(entry, sid, self)
|
self.direntries[sid] = _OleDirectoryEntry(entry, sid, self)
|
||||||
return self.direntries[sid]
|
return self.direntries[sid]
|
||||||
|
|
||||||
|
|
||||||
def dumpdirectory(self):
|
def dumpdirectory(self):
|
||||||
"""
|
"""
|
||||||
Dump directory (for debugging only)
|
Dump directory (for debugging only)
|
||||||
"""
|
"""
|
||||||
self.root.dump()
|
self.root.dump()
|
||||||
|
|
||||||
|
|
||||||
def _open(self, start, size = 0x7FFFFFFF, force_FAT=False):
|
def _open(self, start, size = 0x7FFFFFFF, force_FAT=False):
|
||||||
"""
|
"""
|
||||||
Open a stream, either in FAT or MiniFAT according to its size.
|
Open a stream, either in FAT or MiniFAT according to its size.
|
||||||
|
@ -1806,7 +1790,6 @@ class OleFileIO(object):
|
||||||
sectorsize=self.sectorsize, fat=self.fat,
|
sectorsize=self.sectorsize, fat=self.fat,
|
||||||
filesize=self._filesize)
|
filesize=self._filesize)
|
||||||
|
|
||||||
|
|
||||||
def _list(self, files, prefix, node, streams=True, storages=False):
|
def _list(self, files, prefix, node, streams=True, storages=False):
|
||||||
"""
|
"""
|
||||||
listdir helper
|
listdir helper
|
||||||
|
@ -1835,7 +1818,6 @@ class OleFileIO(object):
|
||||||
else:
|
else:
|
||||||
self.raise_defect(DEFECT_INCORRECT, 'The directory tree contains an entry which is not a stream nor a storage.')
|
self.raise_defect(DEFECT_INCORRECT, 'The directory tree contains an entry which is not a stream nor a storage.')
|
||||||
|
|
||||||
|
|
||||||
def listdir(self, streams=True, storages=False):
|
def listdir(self, streams=True, storages=False):
|
||||||
"""
|
"""
|
||||||
Return a list of streams and/or storages stored in this file
|
Return a list of streams and/or storages stored in this file
|
||||||
|
@ -1849,7 +1831,6 @@ class OleFileIO(object):
|
||||||
self._list(files, [], self.root, streams, storages)
|
self._list(files, [], self.root, streams, storages)
|
||||||
return files
|
return files
|
||||||
|
|
||||||
|
|
||||||
def _find(self, filename):
|
def _find(self, filename):
|
||||||
"""
|
"""
|
||||||
Returns directory entry of given filename. (openstream helper)
|
Returns directory entry of given filename. (openstream helper)
|
||||||
|
@ -1881,7 +1862,6 @@ class OleFileIO(object):
|
||||||
node = kid
|
node = kid
|
||||||
return node.sid
|
return node.sid
|
||||||
|
|
||||||
|
|
||||||
def openstream(self, filename):
|
def openstream(self, filename):
|
||||||
"""
|
"""
|
||||||
Open a stream as a read-only file object (BytesIO).
|
Open a stream as a read-only file object (BytesIO).
|
||||||
|
@ -1903,7 +1883,6 @@ class OleFileIO(object):
|
||||||
raise IOError("this file is not a stream")
|
raise IOError("this file is not a stream")
|
||||||
return self._open(entry.isectStart, entry.size)
|
return self._open(entry.isectStart, entry.size)
|
||||||
|
|
||||||
|
|
||||||
def write_stream(self, stream_name, data):
|
def write_stream(self, stream_name, data):
|
||||||
"""
|
"""
|
||||||
Write a stream to disk. For now, it is only possible to replace an
|
Write a stream to disk. For now, it is only possible to replace an
|
||||||
|
@ -1953,7 +1932,7 @@ class OleFileIO(object):
|
||||||
% (size, self.sectorsize, len(data_sector), size % self.sectorsize))
|
% (size, self.sectorsize, len(data_sector), size % self.sectorsize))
|
||||||
assert(len(data_sector) % self.sectorsize == size % self.sectorsize)
|
assert(len(data_sector) % self.sectorsize == size % self.sectorsize)
|
||||||
self.write_sect(sect, data_sector)
|
self.write_sect(sect, data_sector)
|
||||||
## self.fp.write(data_sector)
|
# self.fp.write(data_sector)
|
||||||
# jump to next sector in the FAT:
|
# jump to next sector in the FAT:
|
||||||
try:
|
try:
|
||||||
sect = self.fat[sect]
|
sect = self.fat[sect]
|
||||||
|
@ -1964,7 +1943,6 @@ class OleFileIO(object):
|
||||||
if sect != ENDOFCHAIN:
|
if sect != ENDOFCHAIN:
|
||||||
raise IOError('incorrect last sector index in OLE stream')
|
raise IOError('incorrect last sector index in OLE stream')
|
||||||
|
|
||||||
|
|
||||||
def get_type(self, filename):
|
def get_type(self, filename):
|
||||||
"""
|
"""
|
||||||
Test if given filename exists as a stream or a storage in the OLE
|
Test if given filename exists as a stream or a storage in the OLE
|
||||||
|
@ -1984,7 +1962,6 @@ class OleFileIO(object):
|
||||||
except:
|
except:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def getmtime(self, filename):
|
def getmtime(self, filename):
|
||||||
"""
|
"""
|
||||||
Return modification time of a stream/storage.
|
Return modification time of a stream/storage.
|
||||||
|
@ -2000,7 +1977,6 @@ class OleFileIO(object):
|
||||||
entry = self.direntries[sid]
|
entry = self.direntries[sid]
|
||||||
return entry.getmtime()
|
return entry.getmtime()
|
||||||
|
|
||||||
|
|
||||||
def getctime(self, filename):
|
def getctime(self, filename):
|
||||||
"""
|
"""
|
||||||
Return creation time of a stream/storage.
|
Return creation time of a stream/storage.
|
||||||
|
@ -2016,7 +1992,6 @@ class OleFileIO(object):
|
||||||
entry = self.direntries[sid]
|
entry = self.direntries[sid]
|
||||||
return entry.getctime()
|
return entry.getctime()
|
||||||
|
|
||||||
|
|
||||||
def exists(self, filename):
|
def exists(self, filename):
|
||||||
"""
|
"""
|
||||||
Test if given filename exists as a stream or a storage in the OLE
|
Test if given filename exists as a stream or a storage in the OLE
|
||||||
|
@ -2032,7 +2007,6 @@ class OleFileIO(object):
|
||||||
except:
|
except:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def get_size(self, filename):
|
def get_size(self, filename):
|
||||||
"""
|
"""
|
||||||
Return size of a stream in the OLE container, in bytes.
|
Return size of a stream in the OLE container, in bytes.
|
||||||
|
@ -2049,7 +2023,6 @@ class OleFileIO(object):
|
||||||
raise TypeError('object is not an OLE stream')
|
raise TypeError('object is not an OLE stream')
|
||||||
return entry.size
|
return entry.size
|
||||||
|
|
||||||
|
|
||||||
def get_rootentry_name(self):
|
def get_rootentry_name(self):
|
||||||
"""
|
"""
|
||||||
Return root entry name. Should usually be 'Root Entry' or 'R' in most
|
Return root entry name. Should usually be 'Root Entry' or 'R' in most
|
||||||
|
@ -2057,7 +2030,6 @@ class OleFileIO(object):
|
||||||
"""
|
"""
|
||||||
return self.root.name
|
return self.root.name
|
||||||
|
|
||||||
|
|
||||||
def getproperties(self, filename, convert_time=False, no_conversion=None):
|
def getproperties(self, filename, convert_time=False, no_conversion=None):
|
||||||
"""
|
"""
|
||||||
Return properties described in substream.
|
Return properties described in substream.
|
||||||
|
@ -2295,11 +2267,11 @@ For more information, see http://www.decalage.info/olefile
|
||||||
print('NOT a stream : type=%d' % st_type)
|
print('NOT a stream : type=%d' % st_type)
|
||||||
print()
|
print()
|
||||||
|
|
||||||
## for streamname in ole.listdir():
|
# for streamname in ole.listdir():
|
||||||
## # print name using repr() to convert binary chars to \xNN:
|
# # print name using repr() to convert binary chars to \xNN:
|
||||||
## print('-', repr('/'.join(streamname)),'-', end=' ')
|
# print('-', repr('/'.join(streamname)),'-', end=' ')
|
||||||
## print(ole.getmtime(streamname))
|
# print(ole.getmtime(streamname))
|
||||||
## print()
|
# print()
|
||||||
|
|
||||||
print('Modification/Creation times of all directory entries:')
|
print('Modification/Creation times of all directory entries:')
|
||||||
for entry in ole.direntries:
|
for entry in ole.direntries:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user