style fixes (per flake8)

This commit is contained in:
Jan Petykiewicz 2020-10-16 19:00:00 -07:00
parent 4a878aa7bf
commit 60f879a1ad
5 changed files with 82 additions and 44 deletions

29
.flake8 Normal file
View File

@ -0,0 +1,29 @@
[flake8]
ignore =
# E501 line too long
E501,
# W391 newlines at EOF
W391,
# E241 multiple spaces after comma
E241,
# E302 expected 2 newlines
E302,
# W503 line break before binary operator (to be deprecated)
W503,
# E265 block comment should start with '# '
E265,
# E123 closing bracket does not match indentation of opening bracket's line
E123,
# E124 closing bracket does not match visual indentation
E124,
# E221 multiple spaces before operator
E221,
# E201 whitespace after '['
E201,
# E741 ambiguous variable name 'I'
E741,
per-file-ignores =
# F401 import without use
*/__init__.py: F401,

View File

@ -27,10 +27,12 @@
import pathlib import pathlib
from .main import OasisLayout, Cell, XName from .main import OasisLayout, Cell, XName
from .basic import NString, AString, Validation, OffsetTable, OffsetEntry, \ from .basic import (
EOFError, SignedError, InvalidDataError, InvalidRecordError, \ NString, AString, Validation, OffsetTable, OffsetEntry,
UnfilledModalError, \ EOFError, SignedError, InvalidDataError, InvalidRecordError,
ReuseRepetition, GridRepetition, ArbitraryRepetition UnfilledModalError,
ReuseRepetition, GridRepetition, ArbitraryRepetition
)
__author__ = 'Jan Petykiewicz' __author__ = 'Jan Petykiewicz'

View File

@ -13,7 +13,7 @@ import warnings
try: try:
import numpy import numpy
_USE_NUMPY = True _USE_NUMPY = True
except: except ImportError:
_USE_NUMPY = False _USE_NUMPY = False
@ -587,7 +587,7 @@ class NString:
@bytes.setter @bytes.setter
def bytes(self, bstring: bytes): def bytes(self, bstring: bytes):
if len(bstring) == 0 or not all(0x21 <= c <= 0x7e for c in bstring): if len(bstring) == 0 or not all(0x21 <= c <= 0x7e for c in bstring):
raise InvalidDataError('Invalid n-string {!r}'.format(bstring)) raise InvalidDataError('Invalid n-string {!r}'.format(bstring))
self._string = bstring.decode('ascii') self._string = bstring.decode('ascii')
@ -1491,9 +1491,10 @@ class ArbitraryRepetition:
for x, y in zip(self.x_displacements, self.y_displacements)) for x, y in zip(self.x_displacements, self.y_displacements))
return size return size
def __eq__(self, other: Any) -> bool: def __eq__(self, other: Any) -> bool:
return isinstance(other, type(self)) and self.x_displacements == other.x_displacements and self.y_displacements == other.y_displacements return (isinstance(other, type(self))
and self.x_displacements == other.x_displacements
and self.y_displacements == other.y_displacements)
def __repr__(self) -> str: def __repr__(self) -> str:
return 'ArbitraryRepetition: x{} y{})'.format(self.x_displacements, self.y_displacements) return 'ArbitraryRepetition: x{} y{})'.format(self.x_displacements, self.y_displacements)
@ -2020,7 +2021,7 @@ class OffsetTable:
textstrings = OffsetEntry() textstrings = OffsetEntry()
if propnames is None: if propnames is None:
propnames = OffsetEntry() propnames = OffsetEntry()
if propstrings is None: if propstrings is None:
propstrings = OffsetEntry() propstrings = OffsetEntry()
if layernames is None: if layernames is None:
layernames = OffsetEntry() layernames = OffsetEntry()

View File

@ -9,9 +9,11 @@ import logging
from . import records from . import records
from .records import Modals, Record from .records import Modals, Record
from .basic import OffsetEntry, OffsetTable, NString, AString, real_t, Validation, \ from .basic import (
read_magic_bytes, write_magic_bytes, read_uint, EOFError, \ OffsetEntry, OffsetTable, NString, AString, real_t, Validation,
InvalidDataError, InvalidRecordError read_magic_bytes, write_magic_bytes, read_uint, EOFError,
InvalidRecordError,
)
__author__ = 'Jan Petykiewicz' __author__ = 'Jan Petykiewicz'

View File

@ -19,12 +19,14 @@ import io
import logging import logging
import pprint import pprint
from warnings import warn from warnings import warn
from .basic import AString, NString, repetition_t, property_value_t, real_t, \ from .basic import (
ReuseRepetition, OffsetTable, Validation, read_point_list, read_property_value, \ AString, NString, repetition_t, property_value_t, real_t,
read_bstring, read_uint, read_sint, read_real, read_repetition, read_interval, \ ReuseRepetition, OffsetTable, Validation, read_point_list, read_property_value,
write_bstring, write_uint, write_sint, write_real, write_interval, write_point_list, \ read_bstring, read_uint, read_sint, read_real, read_repetition, read_interval,
write_property_value, read_bool_byte, write_bool_byte, read_byte, write_byte, \ write_bstring, write_uint, write_sint, write_real, write_interval, write_point_list,
InvalidDataError, UnfilledModalError, PathExtensionScheme, _USE_NUMPY write_property_value, read_bool_byte, write_bool_byte, read_byte, write_byte,
InvalidDataError, UnfilledModalError, PathExtensionScheme, _USE_NUMPY,
)
if _USE_NUMPY: if _USE_NUMPY:
import numpy import numpy
@ -37,7 +39,7 @@ logger = logging.getLogger(__name__)
Type definitions Type definitions
''' '''
geometry_t = Union['Text', 'Rectangle', 'Polygon', 'Path', 'Trapezoid', geometry_t = Union['Text', 'Rectangle', 'Polygon', 'Path', 'Trapezoid',
'CTrapezoid', 'Circle', 'XElement', 'XGeometry'] 'CTrapezoid', 'Circle', 'XElement', 'XGeometry']
pathextension_t = Tuple['PathExtensionScheme', Optional[int]] pathextension_t = Tuple['PathExtensionScheme', Optional[int]]
point_list_t = Sequence[Sequence[int]] point_list_t = Sequence[Sequence[int]]
@ -119,6 +121,7 @@ def verify_modal(var: Optional[T]) -> T:
raise UnfilledModalError raise UnfilledModalError
return var return var
''' '''
Records Records
@ -487,7 +490,7 @@ class End(Record):
offset_table: Optional[OffsetTable] = OffsetTable.read(stream) offset_table: Optional[OffsetTable] = OffsetTable.read(stream)
else: else:
offset_table = None offset_table = None
_padding_string = read_bstring(stream) _padding_string = read_bstring(stream) # noqa
validation = Validation.read(stream) validation = Validation.read(stream)
record = End(validation, offset_table) record = End(validation, offset_table)
logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record))
@ -591,8 +594,8 @@ class CBlock(Record):
if compression_type == 0: if compression_type == 0:
count = len(decompressed_bytes) count = len(decompressed_bytes)
compressor = zlib.compressobj(wbits=-zlib.MAX_WBITS, **compression_args) compressor = zlib.compressobj(wbits=-zlib.MAX_WBITS, **compression_args)
compressed_bytes = compressor.compress(decompressed_bytes) + \ compressed_bytes = (compressor.compress(decompressed_bytes)
compressor.flush() + compressor.flush())
else: else:
raise InvalidDataError('Unknown compression type: ' raise InvalidDataError('Unknown compression type: '
'{}'.format(compression_type)) '{}'.format(compression_type))
@ -617,8 +620,8 @@ class CBlock(Record):
decompression_args = {} decompression_args = {}
if self.compression_type == 0: if self.compression_type == 0:
decompressor = zlib.decompressobj(wbits=-zlib.MAX_WBITS, **decompression_args) decompressor = zlib.decompressobj(wbits=-zlib.MAX_WBITS, **decompression_args)
decompressed_bytes = decompressor.decompress(self.compressed_bytes) + \ decompressed_bytes = (decompressor.decompress(self.compressed_bytes)
decompressor.flush() + decompressor.flush())
if len(decompressed_bytes) != self.decompressed_byte_count: if len(decompressed_bytes) != self.decompressed_byte_count:
raise InvalidDataError('Decompressed data length does not match!') raise InvalidDataError('Decompressed data length does not match!')
else: else:
@ -973,7 +976,7 @@ class Property(Record):
if record_id == 29: if record_id == 29:
record = Property() record = Property()
else: else:
byte = read_byte(stream) #UUUUVCNS byte = read_byte(stream) # UUUUVCNS
u = 0x0f & (byte >> 4) u = 0x0f & (byte >> 4)
v = 0x01 & (byte >> 3) v = 0x01 & (byte >> 3)
c = 0x01 & (byte >> 2) c = 0x01 & (byte >> 2)
@ -1420,8 +1423,9 @@ class Placement(Record):
r = self.repetition is not None r = self.repetition is not None
f = self.flip f = self.flip
if (self.magnification == 1 and if (self.magnification == 1
self.angle is not None and abs(self.angle % 90.0) < 1e-14): and self.angle is not None
and abs(self.angle % 90.0) < 1e-14):
aa = int((self.angle / 90) % 4.0) aa = int((self.angle / 90) % 4.0)
bools = (c, n, x, y, r, aa & 0b10, aa & 0b01, f) bools = (c, n, x, y, r, aa & 0b10, aa & 0b01, f)
m = False m = False
@ -1561,19 +1565,19 @@ class Text(Record, GeometryMixin):
size += write_bool_byte(stream, (0, c, n, x, y, r, d, l)) size += write_bool_byte(stream, (0, c, n, x, y, r, d, l))
if c: if c:
if n: if n:
size += write_uint(stream, self.string) # type: ignore size += write_uint(stream, self.string) # type: ignore
else: else:
size += self.string.write(stream) # type: ignore size += self.string.write(stream) # type: ignore
if l: if l:
size += write_uint(stream, self.layer) # type: ignore size += write_uint(stream, self.layer) # type: ignore
if d: if d:
size += write_uint(stream, self.datatype) # type: ignore size += write_uint(stream, self.datatype) # type: ignore
if x: if x:
size += write_sint(stream, self.x) # type: ignore size += write_sint(stream, self.x) # type: ignore
if y: if y:
size += write_sint(stream, self.y) # type: ignore size += write_sint(stream, self.y) # type: ignore
if r: if r:
size += self.repetition.write(stream) # type: ignore size += self.repetition.write(stream) # type: ignore
return size return size
@ -1983,11 +1987,11 @@ class Path(Record, GeometryMixin):
size = write_uint(stream, 21) size = write_uint(stream, 21)
size += write_bool_byte(stream, (e, w, p, x, y, r, d, l)) size += write_bool_byte(stream, (e, w, p, x, y, r, d, l))
if l: if l:
size += write_uint(stream, self.layer) # type: ignore size += write_uint(stream, self.layer) # type: ignore
if d: if d:
size += write_uint(stream, self.datatype) # type: ignore size += write_uint(stream, self.datatype) # type: ignore
if w: if w:
size += write_uint(stream, self.half_width) # type: ignore size += write_uint(stream, self.half_width) # type: ignore
if e: if e:
scheme = 0 scheme = 0
if self.extension_start is not None: if self.extension_start is not None:
@ -1996,11 +2000,11 @@ class Path(Record, GeometryMixin):
scheme += self.extension_end[0].value scheme += self.extension_end[0].value
size += write_uint(stream, scheme) size += write_uint(stream, scheme)
if scheme & 0b1100 == 0b1100: if scheme & 0b1100 == 0b1100:
size += write_sint(stream, self.extension_start[1]) # type: ignore size += write_sint(stream, self.extension_start[1]) # type: ignore
if scheme & 0b0011 == 0b0011: if scheme & 0b0011 == 0b0011:
size += write_sint(stream, self.extension_end[1]) # type: ignore size += write_sint(stream, self.extension_end[1]) # type: ignore
if p: if p:
size += write_point_list(stream, self.point_list, # type: ignore size += write_point_list(stream, self.point_list, # type: ignore
implicit_closed=False, fast=fast) implicit_closed=False, fast=fast)
if x: if x:
size += write_sint(stream, self.x) # type: ignore size += write_sint(stream, self.x) # type: ignore
@ -2084,11 +2088,11 @@ class Trapezoid(Record, GeometryMixin):
if self.is_vertical: if self.is_vertical:
if height is not None and delta_b - delta_a > height: if height is not None and delta_b - delta_a > height:
raise InvalidDataError('Trapezoid: h < delta_b - delta_a' raise InvalidDataError('Trapezoid: h < delta_b - delta_a'
' ({} < {} - {})'.format(height, delta_b, delta_a)) + ' ({} < {} - {})'.format(height, delta_b, delta_a))
else: else:
if width is not None and delta_b - delta_a > width: if width is not None and delta_b - delta_a > width:
raise InvalidDataError('Trapezoid: w < delta_b - delta_a' raise InvalidDataError('Trapezoid: w < delta_b - delta_a'
' ({} < {} - {})'.format(width, delta_b, delta_a)) + ' ({} < {} - {})'.format(width, delta_b, delta_a))
def get_is_vertical(self) -> bool: def get_is_vertical(self) -> bool:
return verify_modal(self.is_vertical) return verify_modal(self.is_vertical)
@ -2191,7 +2195,7 @@ class Trapezoid(Record, GeometryMixin):
class CTrapezoid(Record, GeometryMixin): class CTrapezoid(Record, GeometryMixin):
""" r"""
CTrapezoid record (ID 26) CTrapezoid record (ID 26)
Compact trapezoid formats. Compact trapezoid formats.
@ -2391,7 +2395,7 @@ class CTrapezoid(Record, GeometryMixin):
if d: if d:
size += write_uint(stream, self.datatype) # type: ignore size += write_uint(stream, self.datatype) # type: ignore
if t: if t:
size += write_uint(stream, self.ctrapezoid_type) # type: ignore size += write_uint(stream, self.ctrapezoid_type) # type: ignore
if w: if w:
size += write_uint(stream, self.width) # type: ignore size += write_uint(stream, self.width) # type: ignore
if h: if h: