0

nbt: code style fixes

We're ignoring E221 and E741 since I disagree with them; I think extra
whitespace around operators to align repeated operations can aid
readability and help spot bugs.

This was mostly whitespace issues, e.g. trailing whitespace and
a single space on blank lines.
This commit is contained in:
Nicolas F
2019-03-02 15:44:58 +01:00
parent 336850ea04
commit 846b7ab8f8

View File

@@ -13,10 +13,12 @@
# You should have received a copy of the GNU General Public License along # You should have received a copy of the GNU General Public License along
# with the Overviewer. If not, see <http://www.gnu.org/licenses/>. # with the Overviewer. If not, see <http://www.gnu.org/licenses/>.
import gzip, zlib
import struct
import StringIO
import functools import functools
import gzip
import StringIO
import struct
import zlib
# decorator that turns the first argument from a string into an open file # decorator that turns the first argument from a string into an open file
# handle # handle
@@ -29,6 +31,7 @@ def _file_loader(func):
return func(fileobj, *args) return func(fileobj, *args)
return wrapper return wrapper
@_file_loader @_file_loader
def load(fileobj): def load(fileobj):
"""Reads in the given file as NBT format, parses it, and returns the """Reads in the given file as NBT format, parses it, and returns the
@@ -36,6 +39,7 @@ def load(fileobj):
""" """
return NBTFileReader(fileobj).read_all() return NBTFileReader(fileobj).read_all()
@_file_loader @_file_loader
def load_region(fileobj): def load_region(fileobj):
"""Reads in the given file as a MCR region, and returns an object """Reads in the given file as a MCR region, and returns an object
@@ -45,23 +49,29 @@ def load_region(fileobj):
class CorruptionError(Exception): class CorruptionError(Exception):
pass pass
class CorruptRegionError(CorruptionError): class CorruptRegionError(CorruptionError):
"""An exception raised when the MCRFileReader class encounters an """An exception raised when the MCRFileReader class encounters an
error during region file parsing. error during region file parsing.
""" """
pass pass
class CorruptChunkError(CorruptionError): class CorruptChunkError(CorruptionError):
pass pass
class CorruptNBTError(CorruptionError): class CorruptNBTError(CorruptionError):
"""An exception raised when the NBTFileReader class encounters """An exception raised when the NBTFileReader class encounters
something unexpected in an NBT file.""" something unexpected in an NBT file."""
pass pass
class NBTFileReader(object): class NBTFileReader(object):
"""Low level class that reads the Named Binary Tag format used by Minecraft """Low level class that reads the Named Binary Tag format used by Minecraft
""" """
# compile the unpacker's into a classes # compile the unpacker's into a classes
_byte = struct.Struct("b") _byte = struct.Struct("b")
_short = struct.Struct(">h") _short = struct.Struct(">h")
@@ -96,9 +106,9 @@ class NBTFileReader(object):
7: self._read_tag_byte_array, 7: self._read_tag_byte_array,
8: self._read_tag_string, 8: self._read_tag_string,
9: self._read_tag_list, 9: self._read_tag_list,
10:self._read_tag_compound, 10: self._read_tag_compound,
11:self._read_tag_int_array, 11: self._read_tag_int_array,
12:self._read_tag_long_array, 12: self._read_tag_long_array,
} }
# These private methods read the payload only of the following types # These private methods read the payload only of the following types
@@ -137,12 +147,12 @@ class NBTFileReader(object):
def _read_tag_int_array(self): def _read_tag_int_array(self):
length = self._uint.unpack(self._file.read(4))[0] length = self._uint.unpack(self._file.read(4))[0]
int_bytes = self._file.read(length*4) int_bytes = self._file.read(length * 4)
return struct.unpack(">%ii" % length, int_bytes) return struct.unpack(">%ii" % length, int_bytes)
def _read_tag_long_array(self): def _read_tag_long_array(self):
length = self._uint.unpack(self._file.read(4))[0] length = self._uint.unpack(self._file.read(4))[0]
long_bytes = self._file.read(length*8) long_bytes = self._file.read(length * 8)
return struct.unpack(">%iq" % length, long_bytes) return struct.unpack(">%iq" % length, long_bytes)
def _read_tag_string(self): def _read_tag_string(self):
@@ -189,15 +199,14 @@ class NBTFileReader(object):
tagtype = ord(self._file.read(1)) tagtype = ord(self._file.read(1))
if tagtype != 10: if tagtype != 10:
raise Exception("Expected a tag compound") raise Exception("Expected a tag compound")
# Read the tag name # Read the tag name
name = self._read_tag_string() name = self._read_tag_string()
payload = self._read_tag_compound() payload = self._read_tag_compound()
return (name, payload) return (name, payload)
except (struct.error, ValueError, TypeError), e: except (struct.error, ValueError, TypeError), e:
raise CorruptNBTError("could not parse nbt: %s" % (str(e),)) raise CorruptNBTError("could not parse nbt: %s" % (str(e),))
# For reference, the MCR format is outlined at # For reference, the MCR format is outlined at
# <http://www.minecraftwiki.net/wiki/Beta_Level_Format> # <http://www.minecraftwiki.net/wiki/Beta_Level_Format>
class MCRFileReader(object): class MCRFileReader(object):
@@ -246,7 +255,7 @@ class MCRFileReader(object):
for x in xrange(32): for x in xrange(32):
for z in xrange(32): for z in xrange(32):
if self._locations[x + z * 32] >> 8 != 0: if self._locations[x + z * 32] >> 8 != 0:
yield (x,z) yield (x, z)
def get_chunk_timestamp(self, x, z): def get_chunk_timestamp(self, x, z):
"""Return the given chunk's modification time. If the given """Return the given chunk's modification time. If the given
@@ -273,8 +282,8 @@ class MCRFileReader(object):
x = x % 32 x = x % 32
z = z % 32 z = z % 32
location = self._locations[x + z * 32] location = self._locations[x + z * 32]
offset = (location >> 8) * 4096; offset = (location >> 8) * 4096
sectors = location & 0xff; sectors = location & 0xff
if offset == 0: if offset == 0:
return None return None
@@ -286,19 +295,21 @@ class MCRFileReader(object):
header = self._file.read(5) header = self._file.read(5)
if len(header) != 5: if len(header) != 5:
raise CorruptChunkError("chunk header is invalid") raise CorruptChunkError("chunk header is invalid")
data_length, compression = self._chunk_header_format.unpack(header) data_length, compression = self._chunk_header_format.unpack(header)
# figure out the compression # figure out the compression
is_gzip = True is_gzip = True
if compression == 1: if compression == 1:
# gzip -- not used by the official client, but trivial to support here so... # gzip -- not used by the official client, but trivial to
# support here so...
is_gzip = True is_gzip = True
elif compression == 2: elif compression == 2:
# deflate -- pure zlib stream # deflate -- pure zlib stream
is_gzip = False is_gzip = False
else: else:
# unsupported! # unsupported!
raise CorruptRegionError("unsupported chunk compression type: %i (should be 1 or 2)" % (compression,)) raise CorruptRegionError("unsupported chunk compression type: %i "
"(should be 1 or 2)" % (compression,))
# turn the rest of the data into a StringIO object # turn the rest of the data into a StringIO object
# (using data_length - 1, as we already read 1 byte for compression) # (using data_length - 1, as we already read 1 byte for compression)