diff --git a/.flake8 b/.flake8 index fb07707..0042015 100644 --- a/.flake8 +++ b/.flake8 @@ -27,4 +27,3 @@ ignore = per-file-ignores = # F401 import without use */__init__.py: F401, - __init__.py: F401, diff --git a/README.md b/README.md index 350a0d0..fc5f845 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,6 @@ **fatamorgana** is a Python package for reading and writing OASIS format layout files. **Homepage:** https://mpxd.net/code/jan/fatamorgana -* [PyPI](https://pypi.org/project/fatamorgana) -* [Github mirror](https://github.com/anewusername/fatamorgana) **Capabilities:** * This package is a work-in-progress and is largely untested -- it works for @@ -22,7 +20,7 @@ ## Installation **Dependencies:** -* python >=3.11 +* python 3.5 or newer * (optional) numpy diff --git a/fatamorgana/__init__.py b/fatamorgana/__init__.py index 27c9c74..d3bce93 100644 --- a/fatamorgana/__init__.py +++ b/fatamorgana/__init__.py @@ -15,7 +15,7 @@ numpy to speed up reading/writing. Dependencies: - - Python 3.11 or later + - Python 3.8 or later - numpy (optional, faster but no additional functionality) To get started, try: @@ -24,28 +24,17 @@ help(fatamorgana.OasisLayout) ``` """ -from .main import ( - OasisLayout as OasisLayout, - Cell as Cell, - XName as XName, - ) +import pathlib + +from .main import OasisLayout, Cell, XName from .basic import ( - NString as NString, - AString as AString, - Validation as Validation, - OffsetTable as OffsetTable, - OffsetEntry as OffsetEntry, - EOFError as EOFError, - SignedError as SignedError, - InvalidDataError as InvalidDataError, - InvalidRecordError as InvalidRecordError, - UnfilledModalError as UnfilledModalError, - ReuseRepetition as ReuseRepetition, - GridRepetition as GridRepetition, - ArbitraryRepetition as ArbitraryRepetition, + NString, AString, Validation, OffsetTable, OffsetEntry, + EOFError, SignedError, InvalidDataError, InvalidRecordError, + UnfilledModalError, + ReuseRepetition, GridRepetition, ArbitraryRepetition ) __author__ = 'Jan Petykiewicz' -__version__ = '0.13' +__version__ = '0.12' version = __version__ diff --git a/fatamorgana/basic.py b/fatamorgana/basic.py index fee40dc..6194ab0 100644 --- a/fatamorgana/basic.py +++ b/fatamorgana/basic.py @@ -2,17 +2,16 @@ This module contains all datatypes and parsing/writing functions for all abstractions below the 'record' or 'block' level. """ -from typing import Any, IO, Union -from collections.abc import Sequence +from typing import List, Tuple, Type, Union, Optional, Any, Sequence from fractions import Fraction from enum import Enum import math import struct +import io import warnings try: - import numpy - from numpy.typing import NDArray + import numpy # type: ignore _USE_NUMPY = True except ImportError: _USE_NUMPY = False @@ -21,10 +20,9 @@ except ImportError: ''' Type definitions ''' -real_t = int | float | Fraction +real_t = Union[int, float, Fraction] repetition_t = Union['ReuseRepetition', 'GridRepetition', 'ArbitraryRepetition'] property_value_t = Union[int, bytes, 'AString', 'NString', 'PropStringReference', float, Fraction] -bytes_t = bytes class FatamorganaError(Exception): @@ -86,7 +84,7 @@ MAGIC_BYTES: bytes = b'%SEMI-OASIS\r\n' ''' Basic IO ''' -def _read(stream: IO[bytes], n: int) -> bytes: +def _read(stream: io.BufferedIOBase, n: int) -> bytes: """ Read n bytes from the stream. Raise an EOFError if there were not enough bytes in the stream. @@ -107,7 +105,7 @@ def _read(stream: IO[bytes], n: int) -> bytes: return b -def read_byte(stream: IO[bytes]) -> int: +def read_byte(stream: io.BufferedIOBase) -> int: """ Read a single byte and return it. @@ -120,7 +118,7 @@ def read_byte(stream: IO[bytes]) -> int: return _read(stream, 1)[0] -def write_byte(stream: IO[bytes], n: int) -> int: +def write_byte(stream: io.BufferedIOBase, n: int) -> int: """ Write a single byte to the stream. @@ -133,7 +131,7 @@ def write_byte(stream: IO[bytes], n: int) -> int: return stream.write(bytes((n,))) -def _py_read_bool_byte(stream: IO[bytes]) -> list[bool]: +def _py_read_bool_byte(stream: io.BufferedIOBase) -> List[bool]: """ Read a single byte from the stream, and interpret its bits as a list of 8 booleans. @@ -148,7 +146,7 @@ def _py_read_bool_byte(stream: IO[bytes]) -> list[bool]: bits = [bool((byte >> i) & 0x01) for i in reversed(range(8))] return bits -def _py_write_bool_byte(stream: IO[bytes], bits: tuple[bool | int, ...]) -> int: +def _py_write_bool_byte(stream: io.BufferedIOBase, bits: Tuple[Union[bool, int], ...]) -> int: """ Pack 8 booleans into a byte, and write it to the stream. @@ -163,7 +161,7 @@ def _py_write_bool_byte(stream: IO[bytes], bits: tuple[bool | int, ...]) -> int: InvalidDataError if didn't receive 8 bits. """ if len(bits) != 8: - raise InvalidDataError(f'write_bool_byte received {len(bits)} bits, requires 8') + raise InvalidDataError('write_bool_byte received {} bits, requires 8'.format(len(bits))) byte = 0 for i, bit in enumerate(reversed(bits)): byte |= bit << i @@ -171,7 +169,7 @@ def _py_write_bool_byte(stream: IO[bytes], bits: tuple[bool | int, ...]) -> int: if _USE_NUMPY: - def _np_read_bool_byte(stream: IO[bytes]) -> NDArray[numpy.uint8]: + def _np_read_bool_byte(stream: io.BufferedIOBase) -> List[bool]: """ Read a single byte from the stream, and interpret its bits as a list of 8 booleans. @@ -185,7 +183,7 @@ if _USE_NUMPY: byte_arr = _read(stream, 1) return numpy.unpackbits(numpy.frombuffer(byte_arr, dtype=numpy.uint8)) - def _np_write_bool_byte(stream: IO[bytes], bits: tuple[bool | int, ...]) -> int: + def _np_write_bool_byte(stream: io.BufferedIOBase, bits: Tuple[Union[bool, int], ...]) -> int: """ Pack 8 booleans into a byte, and write it to the stream. @@ -200,16 +198,16 @@ if _USE_NUMPY: InvalidDataError if didn't receive 8 bits. """ if len(bits) != 8: - raise InvalidDataError(f'write_bool_byte received {len(bits)} bits, requires 8') + raise InvalidDataError('write_bool_byte received {} bits, requires 8'.format(len(bits))) return stream.write(numpy.packbits(bits)[0]) - read_bool_byte = _np_read_bool_byte # type: ignore + read_bool_byte = _np_read_bool_byte write_bool_byte = _np_write_bool_byte else: - read_bool_byte = _py_read_bool_byte # type: ignore + read_bool_byte = _py_read_bool_byte write_bool_byte = _py_write_bool_byte -def read_uint(stream: IO[bytes]) -> int: +def read_uint(stream: io.BufferedIOBase) -> int: """ Read an unsigned integer from the stream. @@ -235,7 +233,7 @@ def read_uint(stream: IO[bytes]) -> int: return result -def write_uint(stream: IO[bytes], n: int) -> int: +def write_uint(stream: io.BufferedIOBase, n: int) -> int: """ Write an unsigned integer to the stream. See format details in `read_uint()`. @@ -251,7 +249,7 @@ def write_uint(stream: IO[bytes], n: int) -> int: SignedError: if `n` is negative. """ if n < 0: - raise SignedError(f'uint must be positive: {n}') + raise SignedError('uint must be positive: {}'.format(n)) current = n byte_list = [] @@ -298,7 +296,7 @@ def encode_sint(sint: int) -> int: return (abs(sint) << 1) | (sint < 0) -def read_sint(stream: IO[bytes]) -> int: +def read_sint(stream: io.BufferedIOBase) -> int: """ Read a signed integer from the stream. See `decode_sint()` for format details. @@ -312,7 +310,7 @@ def read_sint(stream: IO[bytes]) -> int: return decode_sint(read_uint(stream)) -def write_sint(stream: IO[bytes], n: int) -> int: +def write_sint(stream: io.BufferedIOBase, n: int) -> int: """ Write a signed integer to the stream. See `decode_sint()` for format details. @@ -327,7 +325,7 @@ def write_sint(stream: IO[bytes], n: int) -> int: return write_uint(stream, encode_sint(n)) -def read_bstring(stream: IO[bytes]) -> bytes: +def read_bstring(stream: io.BufferedIOBase) -> bytes: """ Read a binary string from the stream. The format is: @@ -344,7 +342,7 @@ def read_bstring(stream: IO[bytes]) -> bytes: return _read(stream, length) -def write_bstring(stream: IO[bytes], bstring: bytes) -> int: +def write_bstring(stream: io.BufferedIOBase, bstring: bytes): """ Write a binary string to the stream. See `read_bstring()` for format details. @@ -360,7 +358,7 @@ def write_bstring(stream: IO[bytes], bstring: bytes) -> int: return stream.write(bstring) -def read_ratio(stream: IO[bytes]) -> Fraction: +def read_ratio(stream: io.BufferedIOBase) -> Fraction: """ Read a ratio (unsigned) from the stream. The format is: @@ -378,7 +376,7 @@ def read_ratio(stream: IO[bytes]) -> Fraction: return Fraction(numer, denom) -def write_ratio(stream: IO[bytes], r: Fraction) -> int: +def write_ratio(stream: io.BufferedIOBase, r: Fraction) -> int: """ Write an unsigned ratio to the stream. See `read_ratio()` for format details. @@ -394,13 +392,13 @@ def write_ratio(stream: IO[bytes], r: Fraction) -> int: SignedError: if r is negative. """ if r < 0: - raise SignedError(f'Ratio must be unsigned: {r}') + raise SignedError('Ratio must be unsigned: {}'.format(r)) size = write_uint(stream, r.numerator) size += write_uint(stream, r.denominator) return size -def read_float32(stream: IO[bytes]) -> float: +def read_float32(stream: io.BufferedIOBase) -> float: """ Read a 32-bit float from the stream. @@ -414,7 +412,7 @@ def read_float32(stream: IO[bytes]) -> float: return struct.unpack(" int: +def write_float32(stream: io.BufferedIOBase, f: float) -> int: """ Write a 32-bit float to the stream. @@ -429,7 +427,7 @@ def write_float32(stream: IO[bytes], f: float) -> int: return stream.write(b) -def read_float64(stream: IO[bytes]) -> float: +def read_float64(stream: io.BufferedIOBase) -> float: """ Read a 64-bit float from the stream. @@ -443,7 +441,7 @@ def read_float64(stream: IO[bytes]) -> float: return struct.unpack(" int: +def write_float64(stream: io.BufferedIOBase, f: float) -> int: """ Write a 64-bit float to the stream. @@ -458,7 +456,7 @@ def write_float64(stream: IO[bytes], f: float) -> int: return stream.write(b) -def read_real(stream: IO[bytes], real_type: int | None = None) -> real_t: +def read_real(stream: io.BufferedIOBase, real_type: int = None) -> real_t: """ Read a real number from the stream. @@ -505,14 +503,13 @@ def read_real(stream: IO[bytes], real_type: int | None = None) -> real_t: return read_float32(stream) if real_type == 7: return read_float64(stream) - raise InvalidDataError(f'Invalid real type: {real_type}') + raise InvalidDataError('Invalid real type: {}'.format(real_type)) -def write_real( - stream: IO[bytes], - r: real_t, - force_float32: bool = False - ) -> int: +def write_real(stream: io.BufferedIOBase, + r: real_t, + force_float32: bool = False + ) -> int: """ Write a real number to the stream. See read_real() for format details. @@ -564,7 +561,7 @@ class NString: """ _string: str - def __init__(self, string_or_bytes: bytes | str) -> None: + def __init__(self, string_or_bytes: Union[bytes, str]): """ Args: string_or_bytes: Content of the `NString`. @@ -579,9 +576,9 @@ class NString: return self._string @string.setter - def string(self, string: str) -> None: + def string(self, string: str): if len(string) == 0 or not all(0x21 <= ord(c) <= 0x7e for c in string): - raise InvalidDataError(f'Invalid n-string {string}') + raise InvalidDataError('Invalid n-string {}'.format(string)) self._string = string @property @@ -589,13 +586,13 @@ class NString: return self._string.encode('ascii') @bytes.setter - def bytes(self, bstring: bytes) -> None: + def bytes(self, bstring: bytes): if len(bstring) == 0 or not all(0x21 <= c <= 0x7e for c in bstring): - raise InvalidDataError(f'Invalid n-string {bstring!r}') + raise InvalidDataError('Invalid n-string {!r}'.format(bstring)) self._string = bstring.decode('ascii') @staticmethod - def read(stream: IO[bytes_t]) -> 'NString': + def read(stream: io.BufferedIOBase) -> 'NString': """ Create an NString object by reading a bstring from the provided stream. @@ -610,7 +607,7 @@ class NString: """ return NString(read_bstring(stream)) - def write(self, stream: IO[bytes_t]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: """ Write this NString to a stream. @@ -632,7 +629,7 @@ class NString: return self._string -def read_nstring(stream: IO[bytes]) -> str: +def read_nstring(stream: io.BufferedIOBase) -> str: """ Read a name string from the provided stream. See `NString` for constraints on name strings. @@ -649,7 +646,7 @@ def read_nstring(stream: IO[bytes]) -> str: return NString.read(stream).string -def write_nstring(stream: IO[bytes], string: str) -> int: +def write_nstring(stream: io.BufferedIOBase, string: str) -> int: """ Write a name string to a stream. See `NString` for constraints on name strings. @@ -678,7 +675,7 @@ class AString: """ _string: str - def __init__(self, string_or_bytes: bytes | str) -> None: + def __init__(self, string_or_bytes: Union[bytes, str]): """ Args: string_or_bytes: Content of the AString. @@ -693,9 +690,9 @@ class AString: return self._string @string.setter - def string(self, string: str) -> None: + def string(self, string: str): if not all(0x20 <= ord(c) <= 0x7e for c in string): - raise InvalidDataError(f'Invalid a-string "{string}"') + raise InvalidDataError('Invalid a-string {}'.format(string)) self._string = string @property @@ -703,13 +700,13 @@ class AString: return self._string.encode('ascii') @bytes.setter - def bytes(self, bstring: bytes) -> None: + def bytes(self, bstring: bytes): if not all(0x20 <= c <= 0x7e for c in bstring): - raise InvalidDataError(f'Invalid a-string "{bstring!r}"') + raise InvalidDataError('Invalid a-string {!r}'.format(bstring)) self._string = bstring.decode('ascii') @staticmethod - def read(stream: IO[bytes_t]) -> 'AString': + def read(stream: io.BufferedIOBase) -> 'AString': """ Create an `AString` object by reading a bstring from the provided stream. @@ -724,7 +721,7 @@ class AString: """ return AString(read_bstring(stream)) - def write(self, stream: IO[bytes_t]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: """ Write this `AString` to a stream. @@ -746,7 +743,7 @@ class AString: return self._string -def read_astring(stream: IO[bytes]) -> str: +def read_astring(stream: io.BufferedIOBase) -> str: """ Read an ASCII string from the provided stream. See `AString` for constraints on ASCII strings. @@ -763,7 +760,7 @@ def read_astring(stream: IO[bytes]) -> str: return AString.read(stream).string -def write_astring(stream: IO[bytes], string: str) -> int: +def write_astring(stream: io.BufferedIOBase, string: str) -> int: """ Write an ASCII string to a stream. See AString for constraints on ASCII strings. @@ -784,14 +781,15 @@ def write_astring(stream: IO[bytes], string: str) -> int: class ManhattanDelta: """ Class representing an axis-aligned ("Manhattan") vector. + + Attributes: + vertical (bool): `True` if aligned along y-axis + value (int): signed length of the vector """ - vertical: bool - """`True` if aligned along y-axis""" + vertical = None # type: bool + value = None # type: int - value: int - """signed length of the vector""" - - def __init__(self, x: int, y: int) -> None: + def __init__(self, x: int, y: int): """ One of `x` or `y` _must_ be zero! @@ -803,14 +801,14 @@ class ManhattanDelta: y = int(y) if x != 0: if y != 0: - raise InvalidDataError(f'Non-Manhattan ManhattanDelta ({x}, {y})') + raise InvalidDataError('Non-Manhattan ManhattanDelta ({}, {})'.format(x, y)) self.vertical = False self.value = x else: self.vertical = True self.value = y - def as_list(self) -> list[int]: + def as_list(self) -> List[int]: """ Return a list representation of this vector. @@ -853,7 +851,7 @@ class ManhattanDelta: return d @staticmethod - def read(stream: IO[bytes]) -> 'ManhattanDelta': + def read(stream: io.BufferedIOBase) -> 'ManhattanDelta': """ Read a `ManhattanDelta` object from the provided stream. @@ -868,7 +866,7 @@ class ManhattanDelta: n = read_uint(stream) return ManhattanDelta.from_uint(n) - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: """ Write a `ManhattanDelta` object to the provided stream. @@ -886,34 +884,33 @@ class ManhattanDelta: return hasattr(other, 'as_list') and self.as_list() == other.as_list() def __repr__(self) -> str: - return str(self.as_list()) + return '{}'.format(self.as_list()) class OctangularDelta: """ Class representing an axis-aligned or 45-degree ("Octangular") vector. + + Attributes: + proj_mag (int): projection of the vector onto the x or y axis (non-zero) + octangle (int): bitfield: + bit 2: 1 if non-axis-aligned (non-Manhattan) + if Manhattan: + bit 1: 1 if direction is negative + bit 0: 1 if direction is y + if non-Manhattan: + bit 1: 1 if in lower half-plane + bit 0: 1 if x==-y + + Resulting directions: + 0: +x, 1: +y, 2: -x, 3: -y, + 4: +x+y, 5: -x+y, + 6: +x-y, 7: -x-y """ proj_mag: int - """projection of the vector onto the x or y axis (non-zero)""" - octangle: int - """ - bitfield: - bit 2: 1 if non-axis-aligned (non-Manhattan) - if Manhattan: - bit 1: 1 if direction is negative - bit 0: 1 if direction is y - if non-Manhattan: - bit 1: 1 if in lower half-plane - bit 0: 1 if x==-y - Resulting directions: - 0: +x, 1: +y, 2: -x, 3: -y, - 4: +x+y, 5: -x+y, - 6: +x-y, 7: -x-y - """ - - def __init__(self, x: int, y: int) -> None: + def __init__(self, x: int, y: int): """ Either `abs(x)==abs(y)`, `x==0`, or `y==0` _must_ be true! @@ -935,9 +932,9 @@ class OctangularDelta: self.proj_mag = abs(x) self.octangle = (1 << 2) | (yn << 1) | (xn != yn) else: - raise InvalidDataError(f'Non-octangular delta! ({x}, {y})') + raise InvalidDataError('Non-octangular delta! ({}, {})'.format(x, y)) - def as_list(self) -> list[int]: + def as_list(self) -> List[int]: """ Return a list representation of this vector. @@ -950,7 +947,7 @@ class OctangularDelta: sign = self.octangle & 0x02 > 0 xy[axis] = self.proj_mag * (1 - 2 * sign) return xy - else: # noqa: RET505 + else: yn = (self.octangle & 0x02) > 0 xyn = (self.octangle & 0x01) > 0 ys = 1 - 2 * yn @@ -990,7 +987,7 @@ class OctangularDelta: return d @staticmethod - def read(stream: IO[bytes]) -> 'OctangularDelta': + def read(stream: io.BufferedIOBase) -> 'OctangularDelta': """ Read an `OctangularDelta` object from the provided stream. @@ -1005,7 +1002,7 @@ class OctangularDelta: n = read_uint(stream) return OctangularDelta.from_uint(n) - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: """ Write an `OctangularDelta` object to the provided stream. @@ -1023,20 +1020,21 @@ class OctangularDelta: return hasattr(other, 'as_list') and self.as_list() == other.as_list() def __repr__(self) -> str: - return str(self.as_list()) + return '{}'.format(self.as_list()) class Delta: """ Class representing an arbitrary vector + + Attributes + x (int): x-displacement + y (int): y-displacement """ x: int - """x-displacement""" - y: int - """y-displacement""" - def __init__(self, x: int, y: int) -> None: + def __init__(self, x: int, y: int): """ Args: x: x-displacement @@ -1047,7 +1045,7 @@ class Delta: self.x = x self.y = y - def as_list(self) -> list[int]: + def as_list(self) -> List[int]: """ Return a list representation of this vector. @@ -1057,7 +1055,7 @@ class Delta: return [self.x, self.y] @staticmethod - def read(stream: IO[bytes]) -> 'Delta': + def read(stream: io.BufferedIOBase) -> 'Delta': """ Read a `Delta` object from the provided stream. @@ -1083,7 +1081,7 @@ class Delta: y = read_sint(stream) return Delta(x, y) - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: """ Write a `Delta` object to the provided stream. @@ -1097,18 +1095,19 @@ class Delta: """ if self.x == 0 or self.y == 0 or abs(self.x) == abs(self.y): return write_uint(stream, OctangularDelta(self.x, self.y).as_uint() << 1) - size = write_uint(stream, (encode_sint(self.x) << 1) | 0x01) - size += write_uint(stream, encode_sint(self.y)) - return size + else: + size = write_uint(stream, (encode_sint(self.x) << 1) | 0x01) + size += write_uint(stream, encode_sint(self.y)) + return size def __eq__(self, other: Any) -> bool: return hasattr(other, 'as_list') and self.as_list() == other.as_list() def __repr__(self) -> str: - return str(self.as_list()) + return '{}'.format(self.as_list()) -def read_repetition(stream: IO[bytes]) -> repetition_t: +def read_repetition(stream: io.BufferedIOBase) -> repetition_t: """ Read a repetition entry from the given stream. @@ -1124,14 +1123,15 @@ def read_repetition(stream: IO[bytes]) -> repetition_t: rtype = read_uint(stream) if rtype == 0: return ReuseRepetition.read(stream, rtype) - if rtype in (1, 2, 3, 8, 9): + elif rtype in (1, 2, 3, 8, 9): return GridRepetition.read(stream, rtype) - if rtype in (4, 5, 6, 7, 10, 11): + elif rtype in (4, 5, 6, 7, 10, 11): return ArbitraryRepetition.read(stream, rtype) - raise InvalidDataError(f'Unexpected repetition type: {rtype}') + else: + raise InvalidDataError('Unexpected repetition type: {}'.format(rtype)) -def write_repetition(stream: IO[bytes], repetition: repetition_t) -> int: +def write_repetition(stream: io.BufferedIOBase, repetition: repetition_t) -> int: """ Write a repetition entry to the given stream. @@ -1151,10 +1151,10 @@ class ReuseRepetition: the most recently written repetition should be reused. """ @staticmethod - def read(_stream: IO[bytes], _repetition_type: int) -> 'ReuseRepetition': + def read(_stream: io.BufferedIOBase, _repetition_type: int) -> 'ReuseRepetition': return ReuseRepetition() - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: return write_uint(stream, 0) def __eq__(self, other: Any) -> bool: @@ -1166,36 +1166,31 @@ class ReuseRepetition: class GridRepetition: """ - A repetition entry denoting a 1D or 2D array of regularly-spaced elements. The - spacings are stored as one or two lattice vectors, and the extent of the grid - is stored as the number of elements along each lattice vector. - """ + Class representing a repetition entry denoting a 1D or 2D array + of regularly-spaced elements. The spacings are stored as one or + two lattice vectors, and the extent of the grid is stored as the + number of elements along each lattice vector. - a_vector: list[int] - """`(xa, ya)` vector specifying a center-to-center - displacement between adjacent elements in the grid. + Attributes: + a_vector (Tuple[int, int]): `(xa, ya)` vector specifying a center-to-center + displacement between adjacent elements in the grid. + b_vector (Optional[Tuple[int, int]]): `(xb, yb)`, a second displacement, + present if a 2D grid is being specified. + a_count (int): number of elements (>=1) along the grid axis specified by + `a_vector`. + b_count (Optional[int]): Number of elements (>=1) along the grid axis + specified by `b_vector`, if `b_vector` is not `None`. """ - - b_vector: list[int] | None = None - """`(xb, yb)`, a second displacement, - present if a 2D grid is being specified. - """ - + a_vector: List[int] + b_vector: Optional[List[int]] = None a_count: int - """number of elements (>=1) along the grid axis specified by `a_vector`.""" + b_count: Optional[int] = None - b_count: int | None = None - """Number of elements (>=1) along the grid axis - specified by `b_vector`, if `b_vector` is not `None`. - """ - - def __init__( - self, - a_vector: Sequence[int], - a_count: int, - b_vector: Sequence[int] | None = None, - b_count: int | None = None, - ) -> None: + def __init__(self, + a_vector: List[int], + a_count: int, + b_vector: Optional[List[int]] = None, + b_count: Optional[int] = None): """ Args: a_vector: First lattice vector, of the form `[x, y]`. @@ -1221,18 +1216,18 @@ class GridRepetition: if b_count < 2: b_count = None b_vector = None - warnings.warn('Removed b_count and b_vector since b_count == 1', stacklevel=2) + warnings.warn('Removed b_count and b_vector since b_count == 1') if a_count < 2: - raise InvalidDataError(f'Repetition has too-small a_count: {a_count}') - - self.a_vector = list(a_vector) - self.b_vector = list(b_vector) if b_vector is not None else None + raise InvalidDataError('Repetition has too-small a_count: ' + '{}'.format(a_count)) + self.a_vector = a_vector + self.b_vector = b_vector self.a_count = a_count self.b_count = b_count @staticmethod - def read(stream: IO[bytes], repetition_type: int) -> 'GridRepetition': + def read(stream: io.BufferedIOBase, repetition_type: int) -> 'GridRepetition': """ Read a `GridRepetition` from a stream. @@ -1247,8 +1242,8 @@ class GridRepetition: Raises: InvalidDataError: if `repetition_type` is invalid. """ - nb: int | None - b_vector: list[int] | None + nb: Optional[int] + b_vector: Optional[List[int]] if repetition_type == 1: na = read_uint(stream) + 2 nb = read_uint(stream) + 2 @@ -1275,10 +1270,11 @@ class GridRepetition: a_vector = Delta.read(stream).as_list() b_vector = None else: - raise InvalidDataError(f'Invalid type for grid repetition {repetition_type}') + raise InvalidDataError('Invalid type for grid repetition ' + '{}'.format(repetition_type)) return GridRepetition(a_vector, na, b_vector, nb) - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: """ Write the `GridRepetition` to a stream. @@ -1296,7 +1292,7 @@ class GridRepetition: """ if self.b_vector is None or self.b_count is None: if self.b_vector is not None or self.b_count is not None: - raise InvalidDataError(f'Malformed repetition {self}') + raise InvalidDataError('Malformed repetition {}'.format(self)) if self.a_vector[1] == 0: size = write_uint(stream, 2) @@ -1310,7 +1306,7 @@ class GridRepetition: size = write_uint(stream, 9) size += write_uint(stream, self.a_count - 2) size += Delta(*self.a_vector).write(stream) - else: # noqa: PLR5501 + else: if self.a_vector[1] == 0 and self.b_vector[0] == 0: size = write_uint(stream, 1) size += write_uint(stream, self.a_count - 2) @@ -1342,41 +1338,41 @@ class GridRepetition: return True if self.b_vector is None or other.b_vector is None: return False - if any(self.b_vector[ii] != other.b_vector[ii] for ii in range(2)): # noqa: SIM103 + if any(self.b_vector[ii] != other.b_vector[ii] for ii in range(2)): return False return True def __repr__(self) -> str: - return f'GridRepetition: ({self.a_count} : {self.a_vector} | {self.b_count} : {self.b_vector})' + return 'GridRepetition: ({} : {} | {} : {})'.format(self.a_count, self.a_vector, + self.b_count, self.b_vector) class ArbitraryRepetition: """ Class representing a repetition entry denoting a 1D or 2D array of arbitrarily-spaced elements. + + Attributes: + x_displacements (List[int]): x-displacements between consecutive elements + y_displacements (List[int]): y-displacements between consecutive elements """ - x_displacements: list[int] - """x-displacements between consecutive elements""" + x_displacements: List[int] + y_displacements: List[int] - y_displacements: list[int] - """y-displacements between consecutive elements""" - - def __init__( - self, - x_displacements: Sequence[int], - y_displacements: Sequence[int], - ) -> None: + def __init__(self, + x_displacements: List[int], + y_displacements: List[int]): """ Args: x_displacements: x-displacements between consecutive elements y_displacements: y-displacements between consecutive elements """ - self.x_displacements = list(x_displacements) - self.y_displacements = list(y_displacements) + self.x_displacements = x_displacements + self.y_displacements = y_displacements @staticmethod - def read(stream: IO[bytes], repetition_type: int) -> 'ArbitraryRepetition': + def read(stream: io.BufferedIOBase, repetition_type: int) -> 'ArbitraryRepetition': """ Read an `ArbitraryRepetition` from a stream. @@ -1427,10 +1423,10 @@ class ArbitraryRepetition: x_displacements.append(x * mult) y_displacements.append(y * mult) else: - raise InvalidDataError(f'Invalid ArbitraryRepetition repetition_type: {repetition_type}') + raise InvalidDataError('Invalid ArbitraryRepetition repetition_type: {}'.format(repetition_type)) return ArbitraryRepetition(x_displacements, y_displacements) - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: """ Write the `ArbitraryRepetition` to a stream. @@ -1444,7 +1440,7 @@ class ArbitraryRepetition: Returns: Number of bytes written. """ - def get_gcd(vals: list[int]) -> int: + def get_gcd(vals: List[int]) -> int: """ Get the greatest common denominator of a list of ints. """ @@ -1486,13 +1482,13 @@ class ArbitraryRepetition: size = write_uint(stream, 10) size += write_uint(stream, len(self.x_displacements) - 1) size += sum(Delta(x, y).write(stream) - for x, y in zip(self.x_displacements, self.y_displacements, strict=True)) + for x, y in zip(self.x_displacements, self.y_displacements)) else: size = write_uint(stream, 11) size += write_uint(stream, len(self.x_displacements) - 1) size += write_uint(stream, gcd) size += sum(Delta(x // gcd, y // gcd).write(stream) - for x, y in zip(self.x_displacements, self.y_displacements, strict=True)) + for x, y in zip(self.x_displacements, self.y_displacements)) return size def __eq__(self, other: Any) -> bool: @@ -1501,13 +1497,12 @@ class ArbitraryRepetition: and self.y_displacements == other.y_displacements) def __repr__(self) -> str: - return f'ArbitraryRepetition: x{self.x_displacements} y{self.y_displacements})' + return 'ArbitraryRepetition: x{} y{})'.format(self.x_displacements, self.y_displacements) -def read_point_list( - stream: IO[bytes], - implicit_closed: bool, - ) -> Sequence[Sequence[int]]: +def read_point_list(stream: io.BufferedIOBase, + implicit_closed: bool, + ) -> List[List[int]]: """ Read a point list from a stream. @@ -1538,7 +1533,7 @@ def read_point_list( for i in range(list_len): n = read_sint(stream) if n == 0: - raise InvalidDataError('Zero-sized 1-delta') + raise Exception('Zero-sized 1-delta') point = [0, 0] point[(i + 1) % 2] = n points.append(point) @@ -1580,7 +1575,7 @@ def read_point_list( assert (dx == 0) or (dy == 0) close_points = [[-dx, -dy]] elif list_type == 3: - assert 0 in (dx, dy) or dx in (dy, -dy) + assert (dx == 0) or (dy == 0) or (dx == dy) or (dx == -dy) close_points = [[-dx, -dy]] else: close_points = [[-dx, -dy]] @@ -1588,17 +1583,16 @@ def read_point_list( if _USE_NUMPY: points = numpy.vstack((points, close_points)) else: - points += close_points + points.append(close_points) return points -def write_point_list( - stream: IO[bytes], - points: list[Sequence[int]], - fast: bool = False, - implicit_closed: bool = True - ) -> int: +def write_point_list(stream: io.BufferedIOBase, + points: List[Sequence[int]], + fast: bool = False, + implicit_closed: bool = True + ) -> int: """ Write a point list to a stream. @@ -1636,10 +1630,11 @@ def write_point_list( h_first = False v_first = False break - elif point[1] != previous[1] or point[0] == previous[0]: - h_first = False - v_first = False - break + else: + if point[1] != previous[1] or point[0] == previous[0]: + h_first = False + v_first = False + break previous = point # If one of h_first or v_first, write a bunch of 1-deltas @@ -1648,27 +1643,27 @@ def write_point_list( size += write_uint(stream, len(points)) size += sum(write_sint(stream, x + y) for x, y in points) return size - if v_first: + elif v_first: size = write_uint(stream, 1) size += write_uint(stream, len(points)) size += sum(write_sint(stream, x + y) for x, y in points) return size # Try writing a bunch of Manhattan or Octangular deltas - deltas: list[ManhattanDelta] | list[OctangularDelta] | list[Delta] + deltas: Union[List[ManhattanDelta], List[OctangularDelta], List[Delta]] list_type = None try: deltas = [ManhattanDelta(x, y) for x, y in points] if implicit_closed: ManhattanDelta(points[-1][0] - points[0][0], points[-1][1] - points[0][1]) list_type = 2 - except InvalidDataError: + except: try: deltas = [OctangularDelta(x, y) for x, y in points] if implicit_closed: OctangularDelta(points[-1][0] - points[0][0], points[-1][1] - points[0][1]) list_type = 3 - except InvalidDataError: + except: pass if list_type is not None: size = write_uint(stream, list_type) @@ -1694,19 +1689,19 @@ def write_point_list( deltas = [Delta(*points[0])] + [Delta(x, y) for x, y in diff] else: previous = [0, 0] - diffl = [] + diff = [] for point in points: d = [point[0] - previous[0], point[1] - previous[1]] previous = point - diffl.append(d) + diff.append(d) - if sum(sum(p) for p in points) < sum(sum(d) for d in diffl) * decision_factor: + if sum(sum(p) for p in points) < sum(sum(d) for d in diff) * decision_factor: list_type = 4 deltas = [Delta(x, y) for x, y in points] else: list_type = 5 - deltas = [Delta(x, y) for x, y in diffl] + deltas = [Delta(x, y) for x, y in diff] size = write_uint(stream, list_type) size += write_uint(stream, len(points)) @@ -1717,30 +1712,30 @@ def write_point_list( class PropStringReference: """ Reference to a property string. + + Attributes: + ref (int): ID of the target + ref_type (Type): Type of the target: `bytes`, `NString`, or `AString` """ ref: int - """ID of the target""" + reference_type: Type - reference_type: type - """Type of the target: `bytes`, `NString`, or `AString`""" - - def __init__(self, ref: int, ref_type: type) -> None: + def __init__(self, ref: int, ref_type: Type): """ - Args: - ref: ID number of the target. - ref_type: Type of the target. One of bytes, NString, AString. + :param ref: ID number of the target. + :param ref_type: Type of the target. One of bytes, NString, AString. """ self.ref = ref self.ref_type = ref_type def __eq__(self, other: Any) -> bool: - return isinstance(other, type(self)) and self.ref == other.ref and self.reference_type is other.reference_type + return isinstance(other, type(self)) and self.ref == other.ref and self.reference_type == other.reference_type def __repr__(self) -> str: - return f'[{self.ref_type} : {self.ref}]' + return '[{} : {}]'.format(self.ref_type, self.ref) -def read_property_value(stream: IO[bytes]) -> property_value_t: +def read_property_value(stream: io.BufferedIOBase) -> property_value_t: """ Read a property value from a stream. @@ -1767,42 +1762,42 @@ def read_property_value(stream: IO[bytes]) -> property_value_t: Raises: InvalidDataError: if an invalid type is read. """ - ref_type: type + ref_type: Type prop_type = read_uint(stream) if 0 <= prop_type <= 7: return read_real(stream, prop_type) - if prop_type == 8: + elif prop_type == 8: return read_uint(stream) - if prop_type == 9: + elif prop_type == 9: return read_sint(stream) - if prop_type == 10: + elif prop_type == 10: return AString.read(stream) - if prop_type == 11: + elif prop_type == 11: return read_bstring(stream) - if prop_type == 12: + elif prop_type == 12: return NString.read(stream) - if prop_type == 13: + elif prop_type == 13: ref_type = AString ref = read_uint(stream) return PropStringReference(ref, ref_type) - if prop_type == 14: + elif prop_type == 14: ref_type = bytes ref = read_uint(stream) return PropStringReference(ref, ref_type) - if prop_type == 15: + elif prop_type == 15: ref_type = NString ref = read_uint(stream) return PropStringReference(ref, ref_type) - raise InvalidDataError(f'Invalid property type: {prop_type}') + else: + raise InvalidDataError('Invalid property type: {}'.format(prop_type)) -def write_property_value( - stream: IO[bytes], - value: property_value_t, - force_real: bool = False, - force_signed_int: bool = False, - force_float32: bool = False, - ) -> int: +def write_property_value(stream: io.BufferedIOBase, + value: property_value_t, + force_real: bool = False, + force_signed_int: bool = False, + force_float32: bool = False + ) -> int: """ Write a property value to a stream. @@ -1829,7 +1824,7 @@ def write_property_value( else: size = write_uint(stream, 8) size += write_uint(stream, value) - elif isinstance(value, Fraction | float | int): + elif isinstance(value, (Fraction, float, int)): size = write_real(stream, value, force_float32) elif isinstance(value, AString): size = write_uint(stream, 10) @@ -1841,19 +1836,19 @@ def write_property_value( size = write_uint(stream, 12) size += value.write(stream) elif isinstance(value, PropStringReference): - if value.ref_type is AString: + if value.ref_type == AString: size = write_uint(stream, 13) - elif value.ref_type is bytes: + elif value.ref_type == bytes: size = write_uint(stream, 14) - if value.ref_type is AString: + if value.ref_type == AString: size = write_uint(stream, 15) size += write_uint(stream, value.ref) else: - raise InvalidDataError(f'Invalid property type: {type(value)} ({value})') + raise Exception('Invalid property type: {} ({})'.format(type(value), value)) return size -def read_interval(stream: IO[bytes]) -> tuple[int | None, int | None]: +def read_interval(stream: io.BufferedIOBase) -> Tuple[Optional[int], Optional[int]]: """ Read an interval from a stream. These are used for storing layer info. @@ -1880,23 +1875,23 @@ def read_interval(stream: IO[bytes]) -> tuple[int | None, int | None]: interval_type = read_uint(stream) if interval_type == 0: return None, None - if interval_type == 1: + elif interval_type == 1: return None, read_uint(stream) - if interval_type == 2: + elif interval_type == 2: return read_uint(stream), None - if interval_type == 3: + elif interval_type == 3: v = read_uint(stream) return v, v - if interval_type == 4: + elif interval_type == 4: return read_uint(stream), read_uint(stream) - raise InvalidDataError(f'Unrecognized interval type: {interval_type}') + else: + raise InvalidDataError('Unrecognized interval type: {}'.format(interval_type)) -def write_interval( - stream: IO[bytes], - min_bound: int | None = None, - max_bound: int | None = None, - ) -> int: +def write_interval(stream: io.BufferedIOBase, + min_bound: Optional[int] = None, + max_bound: Optional[int] = None + ) -> int: """ Write an interval to a stream. Used for layer data; see `read_interval()` for format details. @@ -1912,40 +1907,42 @@ def write_interval( if min_bound is None: if max_bound is None: return write_uint(stream, 0) - return write_uint(stream, 1) + write_uint(stream, max_bound) - if max_bound is None: - return write_uint(stream, 2) + write_uint(stream, min_bound) - if min_bound == max_bound: - return write_uint(stream, 3) + write_uint(stream, min_bound) - size = write_uint(stream, 4) - size += write_uint(stream, min_bound) - size += write_uint(stream, max_bound) - return size + else: + return write_uint(stream, 1) + write_uint(stream, max_bound) + else: + if max_bound is None: + return write_uint(stream, 2) + write_uint(stream, min_bound) + elif min_bound == max_bound: + return write_uint(stream, 3) + write_uint(stream, min_bound) + else: + size = write_uint(stream, 4) + size += write_uint(stream, min_bound) + size += write_uint(stream, max_bound) + return size class OffsetEntry: """ Entry for the file's offset table. - """ + Attributes: + strict (bool): If `False`, the records pointed to by this + offset entry may also appear elsewhere in the file. If `True`, all + records of the type pointed to by this offset entry must be present + in a contiuous block at the specified offset [pad records also allowed]. + Additionally: + - All references to strict-mode records must be + explicit (using reference_number). + - The offset may point to an encapsulating CBlock record, if the first + record in that CBlock is of the target record type. A strict modei + table cannot begin in the middle of a CBlock. + offset (int): offset from the start of the file; may be 0 + for records that are not present. + """ strict: bool = False - """ - If `False`, the records pointed to by this offset entry may also appear - elsewhere in the file. If `True`, all records of the type pointed to by - this offset entry must be present in a contiuous block at the specified - offset [pad records also allowed]. - Additionally: - - All references to strict-mode records must be explicit (using - `reference_number`). - - The offset may point to an encapsulating CBlock record, if the first - record in that CBlock is of the target record type. A strict mode - table cannot begin in the middle of a CBlock. - """ - offset: int = 0 - """offset from the start of the file; 0 for records that are not present.""" - def __init__(self, strict: bool = False, offset: int = 0) -> None: + def __init__(self, strict: bool = False, offset: int = 0): """ Args: strict: `True` if the records referenced are written in @@ -1958,7 +1955,7 @@ class OffsetEntry: self.offset = offset @staticmethod - def read(stream: IO[bytes]) -> 'OffsetEntry': + def read(stream: io.BufferedIOBase) -> 'OffsetEntry': """ Read an offset entry from a stream. @@ -1973,7 +1970,7 @@ class OffsetEntry: entry.offset = read_uint(stream) return entry - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: """ Write this offset entry to a stream. @@ -1986,7 +1983,7 @@ class OffsetEntry: return write_uint(stream, self.strict) + write_uint(stream, self.offset) def __repr__(self) -> str: - return f'Offset(s: {self.strict}, o: {self.offset})' + return 'Offset(s: {}, o: {})'.format(self.strict, self.offset) class OffsetTable: @@ -2002,6 +1999,14 @@ class OffsetTable: XName which are stored in the above order in the file's offset table. + + Attributes: + cellnames (OffsetEntry): Offset for CellNames + textstrings (OffsetEntry): Offset for TextStrings + propnames (OffsetEntry): Offset for PropNames + propstrings (OffsetEntry): Offset for PropStrings + layernames (OffsetEntry): Offset for LayerNames + xnames (OffsetEntry): Offset for XNames """ cellnames: OffsetEntry textstrings: OffsetEntry @@ -2010,15 +2015,13 @@ class OffsetTable: layernames: OffsetEntry xnames: OffsetEntry - def __init__( - self, - cellnames: OffsetEntry | None = None, - textstrings: OffsetEntry | None = None, - propnames: OffsetEntry | None = None, - propstrings: OffsetEntry | None = None, - layernames: OffsetEntry | None = None, - xnames: OffsetEntry | None = None, - ) -> None: + def __init__(self, + cellnames: Optional[OffsetEntry] = None, + textstrings: Optional[OffsetEntry] = None, + propnames: Optional[OffsetEntry] = None, + propstrings: Optional[OffsetEntry] = None, + layernames: Optional[OffsetEntry] = None, + xnames: Optional[OffsetEntry] = None): """ All parameters default to a non-strict entry with offset `0`. @@ -2051,7 +2054,7 @@ class OffsetTable: self.xnames = xnames @staticmethod - def read(stream: IO[bytes]) -> 'OffsetTable': + def read(stream: io.BufferedIOBase) -> 'OffsetTable': """ Read an offset table from a stream. See class docstring for format details. @@ -2071,7 +2074,7 @@ class OffsetTable: table.xnames = OffsetEntry.read(stream) return table - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: """ Write this offset table to a stream. See class docstring for format details. @@ -2095,7 +2098,7 @@ class OffsetTable: self.propstrings, self.layernames, self.xnames]) -def read_u32(stream: IO[bytes]) -> int: +def read_u32(stream: io.BufferedIOBase) -> int: """ Read a 32-bit unsigned integer (little endian) from a stream. @@ -2109,7 +2112,7 @@ def read_u32(stream: IO[bytes]) -> int: return struct.unpack(' int: +def write_u32(stream: io.BufferedIOBase, n: int) -> int: """ Write a 32-bit unsigned integer (little endian) to a stream. @@ -2124,7 +2127,7 @@ def write_u32(stream: IO[bytes], n: int) -> int: SignedError: if `n` is negative. """ if n < 0: - raise SignedError(f'Negative u32: {n}') + raise SignedError('Negative u32: {}'.format(n)) return stream.write(struct.pack(' None: + def __init__(self, checksum_type: int, checksum: int = None): """ Args: checksum_type: 0,1,2 (No checksum, crc32, checksum32) @@ -2166,7 +2165,7 @@ class Validation: self.checksum = checksum @staticmethod - def read(stream: IO[bytes]) -> 'Validation': + def read(stream: io.BufferedIOBase) -> 'Validation': """ Read a validation entry from a stream. See class docstring for format details. @@ -2183,13 +2182,15 @@ class Validation: checksum_type = read_uint(stream) if checksum_type == 0: checksum = None - elif checksum_type in (1, 2): + elif checksum_type == 1: + checksum = read_u32(stream) + elif checksum_type == 2: checksum = read_u32(stream) else: raise InvalidDataError('Invalid validation type!') return Validation(checksum_type, checksum) - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: """ Write this validation entry to a stream. See class docstring for format details. @@ -2205,19 +2206,22 @@ class Validation: """ if self.checksum_type == 0: return write_uint(stream, 0) - if self.checksum is None: - raise InvalidDataError(f'Checksum is empty but type is {self.checksum_type}') - if self.checksum_type == 1: + elif self.checksum is None: + raise InvalidDataError('Checksum is empty but type is ' + '{}'.format(self.checksum_type)) + elif self.checksum_type == 1: return write_uint(stream, 1) + write_u32(stream, self.checksum) - if self.checksum_type == 2: + elif self.checksum_type == 2: return write_uint(stream, 2) + write_u32(stream, self.checksum) - raise InvalidDataError(f'Unrecognized checksum type: {self.checksum_type}') + else: + raise InvalidDataError('Unrecognized checksum type: ' + '{}'.format(self.checksum_type)) def __repr__(self) -> str: - return f'Validation(type: {self.checksum_type} sum: {self.checksum})' + return 'Validation(type: {} sum: {})'.format(self.checksum_type, self.checksum) -def write_magic_bytes(stream: IO[bytes]) -> int: +def write_magic_bytes(stream: io.BufferedIOBase) -> int: """ Write the magic byte sequence to a stream. @@ -2230,7 +2234,7 @@ def write_magic_bytes(stream: IO[bytes]) -> int: return stream.write(MAGIC_BYTES) -def read_magic_bytes(stream: IO[bytes]) -> None: +def read_magic_bytes(stream: io.BufferedIOBase): """ Read the magic byte sequence from a stream. Raise an `InvalidDataError` if it was not found. @@ -2243,4 +2247,5 @@ def read_magic_bytes(stream: IO[bytes]) -> None: """ magic = _read(stream, len(MAGIC_BYTES)) if magic != MAGIC_BYTES: - raise InvalidDataError(f'Could not read magic bytes, found {magic!r}') + raise InvalidDataError('Could not read magic bytes, ' + 'found {!r}'.format(magic)) diff --git a/fatamorgana/main.py b/fatamorgana/main.py index d0f5c29..b73d4c2 100644 --- a/fatamorgana/main.py +++ b/fatamorgana/main.py @@ -3,7 +3,7 @@ This module contains data structures and functions for reading from and writing to whole OASIS layout files, and provides a few additional abstractions for the data contained inside them. """ -from typing import IO +from typing import List, Dict, Union, Optional, Type import io import logging @@ -27,20 +27,20 @@ class FileModals: """ File-scoped modal variables """ - cellname_implicit: bool | None = None - propname_implicit: bool | None = None - xname_implicit: bool | None = None - textstring_implicit: bool | None = None - propstring_implicit: bool | None = None + cellname_implicit: Optional[bool] = None + propname_implicit: Optional[bool] = None + xname_implicit: Optional[bool] = None + textstring_implicit: Optional[bool] = None + propstring_implicit: Optional[bool] = None - property_target: list[records.Property] + property_target: List[records.Property] within_cell: bool = False within_cblock: bool = False end_has_offset_table: bool = False started: bool = False - def __init__(self, property_target: list[records.Property]) -> None: + def __init__(self, property_target: List[records.Property]): self.property_target = property_target @@ -53,49 +53,43 @@ class OasisLayout: record objects. Cells are stored using `Cell` objects (different from `records.Cell` record objects). + + Attributes: + (File properties) + version (AString): Version string ('1.0') + unit (real_t): grid steps per micron + validation (Validation): checksum data + + (Names) + cellnames (Dict[int, CellName]): Cell names + propnames (Dict[int, NString]): Property names + xnames (Dict[int, XName]): Custom names + + (Strings) + textstrings (Dict[int, AString]): Text strings + propstrings (Dict[int, AString]): Property strings + + (Data) + layers (List[records.LayerName]): Layer definitions + properties (List[records.Property]): Property values + cells (List[Cell]): Layout cells """ - # File properties version: AString - """File format version string ('1.0')""" - unit: real_t - """grid steps per micron""" - validation: Validation - """checksum data""" - # Data - properties: list[records.Property] - """Property values""" + properties: List[records.Property] + cells: List['Cell'] - cells: list['Cell'] - """Layout cells""" + cellnames: Dict[int, 'CellName'] + propnames: Dict[int, NString] + xnames: Dict[int, 'XName'] - layers: list[records.LayerName] - """Layer definitions""" + textstrings: Dict[int, AString] + propstrings: Dict[int, AString] + layers: List[records.LayerName] - # Names - cellnames: dict[int, 'CellName'] - """Cell names""" - - propnames: dict[int, NString] - """Property names""" - - xnames: dict[int, 'XName'] - """Custom names""" - - # String storage - textstrings: dict[int, AString] - """Text strings""" - - propstrings: dict[int, AString] - """Property strings""" - - def __init__( - self, - unit: real_t, - validation: Validation | None = None, - ) -> None: + def __init__(self, unit: real_t, validation: Validation = None): """ Args: unit: Real number (i.e. int, float, or `Fraction`), grid steps per micron. @@ -118,7 +112,7 @@ class OasisLayout: self.layers = [] @staticmethod - def read(stream: IO[bytes]) -> 'OasisLayout': + def read(stream: io.BufferedIOBase) -> 'OasisLayout': """ Read an entire .oas file into an `OasisLayout` object. @@ -138,12 +132,11 @@ class OasisLayout: pass return layout - def read_record( - self, - stream: IO[bytes], - modals: Modals, - file_state: FileModals - ) -> bool: + def read_record(self, + stream: io.BufferedIOBase, + modals: Modals, + file_state: FileModals + ) -> bool: """ Read a single record of unspecified type from a stream, adding its contents into this `OasisLayout` object. @@ -163,12 +156,13 @@ class OasisLayout: """ try: record_id = read_uint(stream) - except EOFError: + except EOFError as e: if file_state.within_cblock: return True - raise + else: + raise e - logger.info(f'read_record of type {record_id} at position 0x{stream.tell():x}') + logger.info('read_record of type {} at position 0x{:x}'.format(record_id, stream.tell())) record: Record @@ -188,11 +182,12 @@ class OasisLayout: # Make sure order is valid (eg, no out-of-cell geometry) if not file_state.started and record_id != 1: - raise InvalidRecordError(f'Non-Start record {record_id} before Start') + raise InvalidRecordError('Non-Start record {} before Start'.format(record_id)) if record_id == 1: if file_state.started: raise InvalidRecordError('Duplicate Start record') - file_state.started = True + else: + file_state.started = True if record_id == 2 and file_state.within_cblock: raise InvalidRecordError('End within CBlock') @@ -202,11 +197,11 @@ class OasisLayout: file_state.within_cell = False elif record_id in range(15, 28) or record_id in (32, 33): if not file_state.within_cell: - raise InvalidRecordError('Geometry outside Cell') + raise Exception('Geometry outside Cell') elif record_id in (13, 14): file_state.within_cell = True else: - raise InvalidRecordError(f'Unknown record id: {record_id}') + raise InvalidRecordError('Unknown record id: {}'.format(record_id)) if record_id == 0: ''' Pad ''' @@ -340,10 +335,10 @@ class OasisLayout: self.cells[-1].geometry.append(record) file_state.property_target = record.properties else: - raise InvalidRecordError(f'Unknown record id: {record_id}') + raise InvalidRecordError('Unknown record id: {}'.format(record_id)) return False - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: """ Write this object in OASIS fromat to a stream. @@ -404,28 +399,32 @@ class OasisLayout: class Cell: """ Representation of an OASIS cell. + + Attributes: + name (Union[NString, int]): name or "CellName reference" number + + properties (List[records.Property]): Properties of this cell + placements (List[records.Placement]): Placement record objects + geometry: (List[records.geometry_t]): Geometry record objectes """ - name: NString | int - """name or "CellName reference" number""" + name: Union[NString, int] + properties: List[records.Property] + placements: List[records.Placement] + geometry: List[records.geometry_t] - properties: list[records.Property] - placements: list[records.Placement] - geometry: list[records.geometry_t] - - def __init__( - self, - name: NString | str | int, - *, - properties: list[records.Property] | None = None, - placements: list[records.Placement] | None = None, - geometry: list[records.geometry_t] | None = None, - ) -> None: - self.name = name if isinstance(name, NString | int) else NString(name) + def __init__(self, + name: Union[NString, str, int], + *, + properties: Optional[List[records.Property]] = None, + placements: Optional[List[records.Placement]] = None, + geometry: Optional[List[records.geometry_t]] = None, + ): + self.name = name if isinstance(name, (NString, int)) else NString(name) self.properties = [] if properties is None else properties self.placements = [] if placements is None else placements self.geometry = [] if geometry is None else geometry - def dedup_write(self, stream: IO[bytes], modals: Modals) -> int: + def dedup_write(self, stream: io.BufferedIOBase, modals: Modals) -> int: """ Write this cell to a stream, using the provided modal variables to deduplicate any repeated data. @@ -459,13 +458,11 @@ class CellName: with the reference data stripped out. """ nstring: NString - properties: list[records.Property] + properties: List[records.Property] - def __init__( - self, - nstring: NString | str, - properties: list[records.Property] | None = None, - ) -> None: + def __init__(self, + nstring: Union[NString, str], + properties: Optional[List[records.Property]] = None): """ Args: nstring: The contained string. @@ -502,7 +499,7 @@ class XName: attribute: int bstring: bytes - def __init__(self, attribute: int, bstring: bytes) -> None: + def __init__(self, attribute: int, bstring: bytes): """ Args: attribute: Attribute number. @@ -526,7 +523,7 @@ class XName: # Mapping from record id to record class. -_GEOMETRY: dict[int, type[records.geometry_t]] = { +_GEOMETRY: Dict[int, Type[records.geometry_t]] = { 19: records.Text, 20: records.Rectangle, 21: records.Polygon, diff --git a/fatamorgana/records.py b/fatamorgana/records.py index 88863a7..19d50f1 100644 --- a/fatamorgana/records.py +++ b/fatamorgana/records.py @@ -10,8 +10,7 @@ Higher-level code (e.g. monitoring for combinations of records with parse, or code for dealing with nested records in a CBlock) should live in main.py instead. """ -from typing import Any, TypeVar, IO, Union, Protocol -from collections.abc import Sequence +from typing import List, Dict, Tuple, Union, Optional, Sequence, Any, TypeVar from abc import ABCMeta, abstractmethod import copy import math @@ -30,7 +29,7 @@ from .basic import ( ) if _USE_NUMPY: - import numpy + import numpy # type: ignore logger = logging.getLogger(__name__) @@ -41,45 +40,46 @@ logger = logging.getLogger(__name__) ''' geometry_t = Union['Text', 'Rectangle', 'Polygon', 'Path', 'Trapezoid', 'CTrapezoid', 'Circle', 'XElement', 'XGeometry'] -pathextension_t = tuple['PathExtensionScheme', int | None] +pathextension_t = Tuple['PathExtensionScheme', Optional[int]] point_list_t = Sequence[Sequence[int]] class Modals: """ - Modal variables, used to store data about previously-written or -read records. + Modal variables, used to store data about previously-written or + -read records. """ - repetition: repetition_t | None = None + repetition: Optional[repetition_t] = None placement_x: int = 0 placement_y: int = 0 - placement_cell: NString | None = None - layer: int | None = None - datatype: int | None = None - text_layer: int | None = None - text_datatype: int | None = None + placement_cell: Optional[NString] = None + layer: Optional[int] = None + datatype: Optional[int] = None + text_layer: Optional[int] = None + text_datatype: Optional[int] = None text_x: int = 0 text_y: int = 0 - text_string: AString | int | None = None + text_string: Union[AString, int, None] = None geometry_x: int = 0 geometry_y: int = 0 xy_relative: bool = False - geometry_w: int | None = None - geometry_h: int | None = None - polygon_point_list: point_list_t | None = None - path_half_width: int | None = None - path_point_list: point_list_t | None = None - path_extension_start: pathextension_t | None = None - path_extension_end: pathextension_t | None = None - ctrapezoid_type: int | None = None - circle_radius: int | None = None - property_value_list: Sequence[property_value_t] | None = None - property_name: int | NString | None = None - property_is_standard: bool | None = None + geometry_w: Optional[int] = None + geometry_h: Optional[int] = None + polygon_point_list: Optional[point_list_t] = None + path_half_width: Optional[int] = None + path_point_list: Optional[point_list_t] = None + path_extension_start: Optional[pathextension_t] = None + path_extension_end: Optional[pathextension_t] = None + ctrapezoid_type: Optional[int] = None + circle_radius: Optional[int] = None + property_value_list: Optional[Sequence[property_value_t]] = None + property_name: Union[int, NString, None] = None + property_is_standard: Optional[bool] = None - def __init__(self) -> None: + def __init__(self): self.reset() - def reset(self) -> None: + def reset(self): """ Resets all modal variables to their default values. Default values are: @@ -116,24 +116,24 @@ class Modals: T = TypeVar('T') -def verify_modal(var: T | None) -> T: +def verify_modal(var: Optional[T]) -> T: if var is None: raise UnfilledModalError return var -# -# -# Records -# -# +''' + + Records + +''' class Record(metaclass=ABCMeta): """ Common interface for records. """ @abstractmethod - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): """ Copy all defined values from this record into the modal variables. Fill all undefined values in this record from the modal variables. @@ -144,7 +144,7 @@ class Record(metaclass=ABCMeta): pass @abstractmethod - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): """ Check all defined values in this record against those in the modal variables. If any values are equal, remove them from @@ -159,7 +159,7 @@ class Record(metaclass=ABCMeta): @staticmethod @abstractmethod - def read(stream: IO[bytes], record_id: int) -> 'Record': + def read(stream: io.BufferedIOBase, record_id: int) -> 'Record': """ Read a record of this type from a stream. This function does not merge with modal variables. @@ -179,7 +179,7 @@ class Record(metaclass=ABCMeta): pass @abstractmethod - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: """ Write this record to a stream as-is. This function does not merge or deduplicate with modal variables. @@ -195,7 +195,7 @@ class Record(metaclass=ABCMeta): """ pass - def dedup_write(self, stream: IO[bytes], modals: Modals) -> int: + def dedup_write(self, stream: io.BufferedIOBase, modals: Modals) -> int: """ Run `.deduplicate_with_modals()` and then `.write()` to the stream. @@ -224,26 +224,17 @@ class Record(metaclass=ABCMeta): return copy.deepcopy(self) def __repr__(self) -> str: - return f'{self.__class__}: ' + pprint.pformat(self.__dict__) - - -class HasRepetition(Protocol): - repetition: repetition_t | None - - -class HasXY(Protocol): - x: int | None - y: int | None + return '{}: {}'.format(self.__class__, pprint.pformat(self.__dict__)) class GeometryMixin(metaclass=ABCMeta): """ Mixin defining common functions for geometry records """ - x: int | None - y: int | None - layer: int | None - datatype: int | None + x: Optional[int] + y: Optional[int] + layer: Optional[int] + datatype: Optional[int] def get_x(self) -> int: return verify_modal(self.x) @@ -251,7 +242,7 @@ class GeometryMixin(metaclass=ABCMeta): def get_y(self) -> int: return verify_modal(self.y) - def get_xy(self) -> tuple[int, int]: + def get_xy(self) -> Tuple[int, int]: return (self.get_x(), self.get_y()) def get_layer(self) -> int: @@ -260,15 +251,14 @@ class GeometryMixin(metaclass=ABCMeta): def get_datatype(self) -> int: return verify_modal(self.datatype) - def get_layer_tuple(self) -> tuple[int, int]: + def get_layer_tuple(self) -> Tuple[int, int]: return (self.get_layer(), self.get_datatype()) -def read_refname( - stream: IO[bytes], - is_present: bool | int, - is_reference: bool | int, - ) -> int | NString | None: +def read_refname(stream: io.BufferedIOBase, + is_present: Union[bool, int], + is_reference: Union[bool, int] + ) -> Union[None, int, NString]: """ Helper function for reading a possibly-absent, possibly-referenced NString. @@ -283,16 +273,16 @@ def read_refname( """ if not is_present: return None - if is_reference: + elif is_reference: return read_uint(stream) - return NString.read(stream) + else: + return NString.read(stream) -def read_refstring( - stream: IO[bytes], - is_present: bool | int, - is_reference: bool | int, - ) -> int | AString | None: +def read_refstring(stream: io.BufferedIOBase, + is_present: Union[bool, int], + is_reference: Union[bool, int], + ) -> Union[None, int, AString]: """ Helper function for reading a possibly-absent, possibly-referenced `AString`. @@ -307,91 +297,95 @@ def read_refstring( """ if not is_present: return None - if is_reference: + elif is_reference: return read_uint(stream) - return AString.read(stream) + else: + return AString.read(stream) class Pad(Record): """ Pad record (ID 0) """ - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): pass - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): pass @staticmethod - def read(stream: IO[bytes], record_id: int) -> 'Pad': + def read(stream: io.BufferedIOBase, record_id: int) -> 'Pad': if record_id != 0: - raise InvalidDataError(f'Invalid record id for Pad {record_id}') + raise InvalidDataError('Invalid record id for Pad ' + '{}'.format(record_id)) record = Pad() - logger.debug(f'Record ending at 0x{stream.tell():x}:\n {record}') + logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: return write_uint(stream, 0) class XYMode(Record): """ XYMode record (ID 15, 16) + + Attributes: + relative (bool): default `False` """ - relative: bool + relative: bool = False @property def absolute(self) -> bool: return not self.relative @absolute.setter - def absolute(self, b: bool) -> None: + def absolute(self, b: bool): self.relative = not b - def __init__(self, relative: bool) -> None: + def __init__(self, relative: bool): """ Args: relative: `True` if the mode is 'relative', `False` if 'absolute'. """ self.relative = relative - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): modals.xy_relative = self.relative - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): pass @staticmethod - def read(stream: IO[bytes], record_id: int) -> 'XYMode': + def read(stream: io.BufferedIOBase, record_id: int) -> 'XYMode': if record_id not in (15, 16): raise InvalidDataError('Invalid record id for XYMode') record = XYMode(record_id == 16) - logger.debug(f'Record ending at 0x{stream.tell():x}:\n {record}') + logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: return write_uint(stream, 15 + self.relative) class Start(Record): """ Start Record (ID 1) + + Attributes: + version (AString): "1.0" + unit (real_t): positive real number, grid steps per micron + offset_table (Optional[OffsetTable]): If `None` then table must be + placed in the `End` record) """ version: AString - """File format version string""" - unit: real_t - """positive real number, grid steps per micron""" + offset_table: Optional[OffsetTable] = None - offset_table: OffsetTable | None - """If `None` then table must be placed in the `End` record""" - - def __init__( - self, - unit: real_t, - version: AString | str = "1.0", - offset_table: OffsetTable | None = None, - ) -> None: + def __init__(self, + unit: real_t, + version: Union[AString, str] = None, + offset_table: Optional[OffsetTable] = None): """ Args unit: Grid steps per micron (positive real number) @@ -400,45 +394,50 @@ class Start(Record): it in the `End` record instead. """ if unit <= 0: - raise InvalidDataError(f'Non-positive unit: {unit}') + raise InvalidDataError('Non-positive unit: {}'.format(unit)) if math.isnan(unit): raise InvalidDataError('NaN unit') if math.isinf(unit): raise InvalidDataError('Non-finite unit') self.unit = unit + if version is None: + version = AString('1.0') if isinstance(version, AString): self.version = version else: self.version = AString(version) if self.version.string != '1.0': - raise InvalidDataError(f'Invalid version string, only "1.0" is allowed: "{self.version.string}"') + raise InvalidDataError('Invalid version string, ' + 'only "1.0" is allowed: ' + + str(self.version.string)) self.offset_table = offset_table - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): modals.reset() - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): modals.reset() @staticmethod - def read(stream: IO[bytes], record_id: int) -> 'Start': + def read(stream: io.BufferedIOBase, record_id: int) -> 'Start': if record_id != 1: - raise InvalidDataError(f'Invalid record id for Start: {record_id}') + raise InvalidDataError('Invalid record id for Start: ' + '{}'.format(record_id)) version = AString.read(stream) unit = read_real(stream) has_offset_table = read_uint(stream) == 0 - offset_table: OffsetTable | None + offset_table: Optional[OffsetTable] if has_offset_table: offset_table = OffsetTable.read(stream) else: offset_table = None record = Start(unit, version, offset_table) - logger.debug(f'Record ending at 0x{stream.tell():x}:\n {record}') + logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: size = write_uint(stream, 1) size += self.version.write(stream) size += write_real(stream, self.unit) @@ -453,18 +452,18 @@ class End(Record): End record (ID 2) The end record is always padded to a total length of 256 bytes. + + Attributes: + offset_table (Optional[OffsetTable]): `None` if offset table was + written into the `Start` record instead + validation (Validation): object containing checksum """ - offset_table: OffsetTable | None - """`None` if offset table was written into the `Start` record instead""" - + offset_table: Optional[OffsetTable] = None validation: Validation - """object containing checksum""" - def __init__( - self, - validation: Validation, - offset_table: OffsetTable | None = None, - ) -> None: + def __init__(self, + validation: Validation, + offset_table: Optional[OffsetTable] = None): """ Args: validation: `Validation` object for this file. @@ -474,31 +473,30 @@ class End(Record): self.validation = validation self.offset_table = offset_table - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): pass - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): pass @staticmethod - def read( - stream: IO[bytes], - record_id: int, - has_offset_table: bool - ) -> 'End': + def read(stream: io.BufferedIOBase, + record_id: int, + has_offset_table: bool + ) -> 'End': if record_id != 2: - raise InvalidDataError(f'Invalid record id for End {record_id}') + raise InvalidDataError('Invalid record id for End {}'.format(record_id)) if has_offset_table: - offset_table: OffsetTable | None = OffsetTable.read(stream) + offset_table: Optional[OffsetTable] = OffsetTable.read(stream) else: offset_table = None _padding_string = read_bstring(stream) # noqa validation = Validation.read(stream) record = End(validation, offset_table) - logger.debug(f'Record ending at 0x{stream.tell():x}:\n {record}') + logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: size = write_uint(stream, 2) if self.offset_table is not None: size += self.offset_table.write(stream) @@ -518,22 +516,20 @@ class End(Record): class CBlock(Record): """ CBlock (Compressed Block) record (ID 34) + + Attributes: + compression_type (int): `0` for zlib + decompressed_byte_count (int): size after decompressing + compressed_bytes (bytes): compressed data """ compression_type: int - """ `0` for zlib""" - decompressed_byte_count: int - """size after decompressing""" - compressed_bytes: bytes - """compressed data""" - def __init__( - self, - compression_type: int, - decompressed_byte_count: int, - compressed_bytes: bytes, - ) -> None: + def __init__(self, + compression_type: int, + decompressed_byte_count: int, + compressed_bytes: bytes): """ Args: compression_type: `0` (zlib) @@ -541,30 +537,32 @@ class CBlock(Record): compressed_bytes: The compressed data. """ if compression_type != 0: - raise InvalidDataError(f'CBlock: Invalid compression scheme {compression_type}') + raise InvalidDataError('CBlock: Invalid compression scheme ' + '{}'.format(compression_type)) self.compression_type = compression_type self.decompressed_byte_count = decompressed_byte_count self.compressed_bytes = compressed_bytes - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): pass - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): pass @staticmethod - def read(stream: IO[bytes], record_id: int) -> 'CBlock': + def read(stream: io.BufferedIOBase, record_id: int) -> 'CBlock': if record_id != 34: - raise InvalidDataError(f'Invalid record id for CBlock: {record_id}') + raise InvalidDataError('Invalid record id for CBlock: ' + '{}'.format(record_id)) compression_type = read_uint(stream) decompressed_count = read_uint(stream) compressed_bytes = read_bstring(stream) record = CBlock(compression_type, decompressed_count, compressed_bytes) - logger.debug(f'CBlock ending at 0x{stream.tell():x} was read successfully') + logger.debug('CBlock ending at 0x{:x} was read successfully'.format(stream.tell())) return record - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: size = write_uint(stream, 34) size += write_uint(stream, self.compression_type) size += write_uint(stream, self.decompressed_byte_count) @@ -572,11 +570,10 @@ class CBlock(Record): return size @staticmethod - def from_decompressed( - decompressed_bytes: bytes, - compression_type: int = 0, - compression_args: dict[str, Any] | None = None, - ) -> 'CBlock': + def from_decompressed(decompressed_bytes: bytes, + compression_type: int = 0, + compression_args: Dict = None + ) -> 'CBlock': """ Create a CBlock record from uncompressed data. @@ -600,11 +597,12 @@ class CBlock(Record): compressed_bytes = (compressor.compress(decompressed_bytes) + compressor.flush()) else: - raise InvalidDataError(f'Unknown compression type: {compression_type}') + raise InvalidDataError('Unknown compression type: ' + '{}'.format(compression_type)) return CBlock(compression_type, count, compressed_bytes) - def decompress(self, decompression_args: dict[str, Any] | None = None) -> bytes: + def decompress(self, decompression_args: Dict = None) -> bytes: """ Decompress the contents of this CBlock. @@ -627,25 +625,25 @@ class CBlock(Record): if len(decompressed_bytes) != self.decompressed_byte_count: raise InvalidDataError('Decompressed data length does not match!') else: - raise InvalidDataError(f'Unknown compression type: {self.compression_type}') + raise InvalidDataError('Unknown compression type: ' + '{}'.format(self.compression_type)) return decompressed_bytes class CellName(Record): """ CellName record (ID 3, 4) + + Attributes: + nstring (NString): name + reference_number (Optional[int]): `None` results in implicit assignment """ nstring: NString - """name string""" + reference_number: Optional[int] = None - reference_number: int | None - """`None` results in implicit assignment""" - - def __init__( - self, - nstring: str | NString, - reference_number: int | None = None, - ) -> None: + def __init__(self, + nstring: Union[NString, str], + reference_number: int = None): """ Args: nstring: The contained string. @@ -658,26 +656,27 @@ class CellName(Record): self.nstring = NString(nstring) self.reference_number = reference_number - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): modals.reset() - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): modals.reset() @staticmethod - def read(stream: IO[bytes], record_id: int) -> 'CellName': + def read(stream: io.BufferedIOBase, record_id: int) -> 'CellName': if record_id not in (3, 4): - raise InvalidDataError(f'Invalid record id for CellName {record_id}') + raise InvalidDataError('Invalid record id for CellName ' + '{}'.format(record_id)) nstring = NString.read(stream) if record_id == 4: - reference_number: int | None = read_uint(stream) + reference_number: Optional[int] = read_uint(stream) else: reference_number = None record = CellName(nstring, reference_number) - logger.debug(f'Record ending at 0x{stream.tell():x}:\n {record}') + logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: record_id = 3 + (self.reference_number is not None) size = write_uint(stream, record_id) size += self.nstring.write(stream) @@ -688,18 +687,17 @@ class CellName(Record): class PropName(Record): """ PropName record (ID 7, 8) + + Attributes: + nstring (NString): name + reference_number (Optional[int]): `None` results in implicit assignment """ nstring: NString - """name string""" + reference_number: Optional[int] = None - reference_number: int | None = None - """`None` results in implicit assignment""" - - def __init__( - self, - nstring: str | NString, - reference_number: int | None = None, - ) -> None: + def __init__(self, + nstring: Union[NString, str], + reference_number: int = None): """ Args: nstring: The contained string. @@ -712,26 +710,27 @@ class PropName(Record): self.nstring = NString(nstring) self.reference_number = reference_number - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): modals.reset() - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): modals.reset() @staticmethod - def read(stream: IO[bytes], record_id: int) -> 'PropName': + def read(stream: io.BufferedIOBase, record_id: int) -> 'PropName': if record_id not in (7, 8): - raise InvalidDataError(f'Invalid record id for PropName {record_id}') + raise InvalidDataError('Invalid record id for PropName ' + '{}'.format(record_id)) nstring = NString.read(stream) if record_id == 8: - reference_number: int | None = read_uint(stream) + reference_number: Optional[int] = read_uint(stream) else: reference_number = None record = PropName(nstring, reference_number) - logger.debug(f'Record ending at 0x{stream.tell():x}:\n {record}') + logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: record_id = 7 + (self.reference_number is not None) size = write_uint(stream, record_id) size += self.nstring.write(stream) @@ -743,18 +742,17 @@ class PropName(Record): class TextString(Record): """ TextString record (ID 5, 6) + + Attributes: + astring (AString): string data + reference_number (Optional[int]): `None` results in implicit assignment """ astring: AString - """string contents""" + reference_number: Optional[int] = None - reference_number: int | None = None - """`None` results in implicit assignment""" - - def __init__( - self, - string: AString | str, - reference_number: int | None = None, - ) -> None: + def __init__(self, + string: Union[AString, str], + reference_number: int = None): """ Args: string: The contained string. @@ -767,26 +765,27 @@ class TextString(Record): self.astring = AString(string) self.reference_number = reference_number - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): modals.reset() - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): modals.reset() @staticmethod - def read(stream: IO[bytes], record_id: int) -> 'TextString': + def read(stream: io.BufferedIOBase, record_id: int) -> 'TextString': if record_id not in (5, 6): - raise InvalidDataError(f'Invalid record id for TextString: {record_id}') + raise InvalidDataError('Invalid record id for TextString: ' + '{}'.format(record_id)) astring = AString.read(stream) if record_id == 6: - reference_number: int | None = read_uint(stream) + reference_number: Optional[int] = read_uint(stream) else: reference_number = None record = TextString(astring, reference_number) - logger.debug(f'Record ending at 0x{stream.tell():x}:\n {record}') + logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: record_id = 5 + (self.reference_number is not None) size = write_uint(stream, record_id) size += self.astring.write(stream) @@ -798,18 +797,17 @@ class TextString(Record): class PropString(Record): """ PropString record (ID 9, 10) + + Attributes: + astring (AString): string data + reference_number (Optional[int]): `None` results in implicit assignment """ astring: AString - """string contents""" + reference_number: Optional[int] = None - reference_number: int | None - """`None` results in implicit assignment""" - - def __init__( - self, - string: AString | str, - reference_number: int | None = None, - ) -> None: + def __init__(self, + string: Union[AString, str], + reference_number: int = None): """ Args: string: The contained string. @@ -822,26 +820,27 @@ class PropString(Record): self.astring = AString(string) self.reference_number = reference_number - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): modals.reset() - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): modals.reset() @staticmethod - def read(stream: IO[bytes], record_id: int) -> 'PropString': + def read(stream: io.BufferedIOBase, record_id: int) -> 'PropString': if record_id not in (9, 10): - raise InvalidDataError(f'Invalid record id for PropString: {record_id}') + raise InvalidDataError('Invalid record id for PropString: ' + '{}'.format(record_id)) astring = AString.read(stream) if record_id == 10: - reference_number: int | None = read_uint(stream) + reference_number: Optional[int] = read_uint(stream) else: reference_number = None record = PropString(astring, reference_number) - logger.debug(f'Record ending at 0x{stream.tell():x}:\n {record}') + logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: record_id = 9 + (self.reference_number is not None) size = write_uint(stream, record_id) size += self.astring.write(stream) @@ -853,31 +852,30 @@ class PropString(Record): class LayerName(Record): """ LayerName record (ID 11, 12) + + Attributes: + nstring (NString): name + layer_interval (Tuple[Optional[int], Optional[int]]): bounds on the interval + type_interval (Tuple[Optional[int], Optional[int]]): bounds on the interval + is_textlayer (bool): Is this a text layer? """ nstring: NString - """name string""" - - layer_interval: tuple[int | None, int | None] - """bounds on the interval""" - - type_interval: tuple[int | None, int | None] - """bounds on the interval""" - + layer_interval: Tuple + type_interval: Tuple is_textlayer: bool - """Is this a text layer?""" - def __init__( - self, - nstring: str | NString, - layer_interval: tuple[int | None, int | None], - type_interval: tuple[int | None, int | None], - is_textlayer: bool, - ) -> None: + def __init__(self, + nstring: Union[NString, str], + layer_interval: Tuple, + type_interval: Tuple, + is_textlayer: bool): """ Args: nstring: The layer name. - layer_interval: Tuple giving bounds (or lack of thereof) on the layer number. - type_interval: Tuple giving bounds (or lack of thereof) on the type number. + layer_interval: Tuple (int or None, int or None) giving bounds + (or lack of thereof) on the layer number. + type_interval: Tuple (int or None, int or None) giving bounds + (or lack of thereof) on the type number. is_textlayer: `True` if the layer is a text layer. """ if isinstance(nstring, NString): @@ -888,25 +886,26 @@ class LayerName(Record): self.type_interval = type_interval self.is_textlayer = is_textlayer - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): modals.reset() - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): modals.reset() @staticmethod - def read(stream: IO[bytes], record_id: int) -> 'LayerName': + def read(stream: io.BufferedIOBase, record_id: int) -> 'LayerName': if record_id not in (11, 12): - raise InvalidDataError(f'Invalid record id for LayerName: {record_id}') + raise InvalidDataError('Invalid record id for LayerName: ' + '{}'.format(record_id)) is_textlayer = (record_id == 12) nstring = NString.read(stream) layer_interval = read_interval(stream) type_interval = read_interval(stream) record = LayerName(nstring, layer_interval, type_interval, is_textlayer) - logger.debug(f'Record ending at 0x{stream.tell():x}:\n {record}') + logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: record_id = 11 + self.is_textlayer size = write_uint(stream, record_id) size += self.nstring.write(stream) @@ -918,19 +917,21 @@ class LayerName(Record): class Property(Record): """ LayerName record (ID 28, 29) - """ - name: NString | int | None - """`int` is an explicit reference, `None` is a flag to use Modal""" - values: list[property_value_t] | None - is_standard: bool | None - """Whether this is a standard property.""" - def __init__( - self, - name: NString | str | int | None = None, - values: list[property_value_t] | None= None, - is_standard: bool | None = None, - ) -> None: + Attributes: + name (Union[NString, int, None]): `int` is an explicit reference, + `None` is a flag to use Modal) + values (Optional[List[property_value_t]]): List of property values. + is_standard (bool): Whether this is a standard property. + """ + name: Optional[Union[NString, int]] = None + values: Optional[List[property_value_t]] = None + is_standard: Optional[bool] = None + + def __init__(self, + name: Union[NString, str, int, None] = None, + values: Optional[List[property_value_t]] = None, + is_standard: Optional[bool] = None): """ Args: name: Property name, reference number, or `None` (i.e. use modal) @@ -940,116 +941,118 @@ class Property(Record): is_standard: `True` if this is a standard property. `None` to use modal. Default `None`. """ - if isinstance(name, NString | int) or name is None: + if isinstance(name, (NString, int)) or name is None: self.name = name else: self.name = NString(name) self.values = values self.is_standard = is_standard - def get_name(self) -> NString | int: + def get_name(self) -> Union[NString, int]: return verify_modal(self.name) # type: ignore - def get_values(self) -> list[property_value_t]: + def get_values(self) -> List[property_value_t]: return verify_modal(self.values) def get_is_standard(self) -> bool: return verify_modal(self.is_standard) - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): adjust_field(self, 'name', modals, 'property_name') adjust_field(self, 'values', modals, 'property_value_list') adjust_field(self, 'is_standard', modals, 'property_is_standard') - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): dedup_field(self, 'name', modals, 'property_name') dedup_field(self, 'values', modals, 'property_value_list') if self.values is None and self.name is None: dedup_field(self, 'is_standard', modals, 'property_is_standard') @staticmethod - def read(stream: IO[bytes], record_id: int) -> 'Property': + def read(stream: io.BufferedIOBase, record_id: int) -> 'Property': if record_id not in (28, 29): - raise InvalidDataError(f'Invalid record id for PropertyValue: {record_id}') + raise InvalidDataError('Invalid record id for PropertyValue: ' + '{}'.format(record_id)) if record_id == 29: record = Property() else: byte = read_byte(stream) # UUUUVCNS - uu = 0x0f & (byte >> 4) - vv = 0x01 & (byte >> 3) - cc = 0x01 & (byte >> 2) - nn = 0x01 & (byte >> 1) - ss = 0x01 & (byte >> 0) + u = 0x0f & (byte >> 4) + v = 0x01 & (byte >> 3) + c = 0x01 & (byte >> 2) + n = 0x01 & (byte >> 1) + s = 0x01 & (byte >> 0) - name = read_refname(stream, cc, nn) - if vv == 0: - if uu < 0x0f: - value_count = uu + name = read_refname(stream, c, n) + if v == 0: + if u < 0x0f: + value_count = u else: value_count = read_uint(stream) - values: list[property_value_t] | None = [read_property_value(stream) - for _ in range(value_count)] + values: Optional[List[property_value_t]] = [read_property_value(stream) + for _ in range(value_count)] else: values = None -# if uu != 0: +# if u != 0: # logger.warning('Malformed property record header; requested modal' # ' values but had nonzero count. Ignoring count.') - record = Property(name, values, bool(ss)) - logger.debug(f'Record ending at 0x{stream.tell():x}:\n {record}') + record = Property(name, values, bool(s)) + logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: if self.is_standard is None and self.values is None and self.name is None: return write_uint(stream, 29) - - if self.is_standard is None: - raise InvalidDataError('Property has value or name, but no is_standard flag!') - - if self.values is not None: - value_count = len(self.values) - vv = 0 - uu = 0x0f if value_count >= 0x0f else value_count else: - vv = 1 - uu = 0 - - cc = self.name is not None - nn = cc and isinstance(self.name, int) - ss = self.is_standard - - size = write_uint(stream, 28) - size += write_byte(stream, (uu << 4) | (vv << 3) | (cc << 2) | (nn << 1) | ss) - if cc: - if nn: - size += write_uint(stream, self.name) # type: ignore + if self.is_standard is None: + raise InvalidDataError('Property has value or name, ' + 'but no is_standard flag!') + if self.values is not None: + value_count = len(self.values) + v = 0 + if value_count >= 0x0f: + u = 0x0f + else: + u = value_count else: - size += self.name.write(stream) # type: ignore - if not vv: - if uu == 0x0f: - size += write_uint(stream, len(self.values)) # type: ignore - size += sum(write_property_value(stream, pp) for pp in self.values) # type: ignore + v = 1 + u = 0 + + c = self.name is not None + n = c and isinstance(self.name, int) + s = self.is_standard + + size = write_uint(stream, 28) + size += write_byte(stream, (u << 4) | (v << 3) | (c << 2) | (n << 1) | s) + if c: + if n: + size += write_uint(stream, self.name) # type: ignore + else: + size += self.name.write(stream) # type: ignore + if not v: + if u == 0x0f: + size += write_uint(stream, len(self.values)) # type: ignore + size += sum(write_property_value(stream, p) for p in self.values) # type: ignore return size class XName(Record): """ XName record (ID 30, 31) + + Attributes: + attribute (int): Attribute number + bstring (bytes): XName data + reference_number (Optional[int]): None means to use implicit numbering """ attribute: int - """Attribute number""" - bstring: bytes - """XName data""" + reference_number: Optional[int] = None - reference_number: int | None - """None means to use implicit numbering""" - - def __init__( - self, - attribute: int, - bstring: bytes, - reference_number: int | None = None, - ) -> None: + def __init__(self, + attribute: int, + bstring: bytes, + reference_number: int = None): """ Args: attribute: Attribute number. @@ -1061,27 +1064,28 @@ class XName(Record): self.bstring = bstring self.reference_number = reference_number - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): modals.reset() - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): modals.reset() @staticmethod - def read(stream: IO[bytes], record_id: int) -> 'XName': + def read(stream: io.BufferedIOBase, record_id: int) -> 'XName': if record_id not in (30, 31): - raise InvalidDataError(f'Invalid record id for XName: {record_id}') + raise InvalidDataError('Invalid record id for XName: ' + '{}'.format(record_id)) attribute = read_uint(stream) bstring = read_bstring(stream) if record_id == 31: - reference_number: int | None = read_uint(stream) + reference_number: Optional[int] = read_uint(stream) else: reference_number = None record = XName(attribute, bstring, reference_number) - logger.debug(f'Record ending at 0x{stream.tell():x}:\n {record}') + logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: record_id = 30 + (self.reference_number is not None) size = write_uint(stream, record_id) size += write_uint(stream, self.attribute) @@ -1094,21 +1098,19 @@ class XName(Record): class XElement(Record): """ XElement record (ID 32) + + Attributes: + attribute (int): Attribute number. + bstring (bytes): XElement data. """ attribute: int - """Attribute number""" - bstring: bytes - """XElement data""" + properties: List['Property'] - properties: list['Property'] - - def __init__( - self, - attribute: int, - bstring: bytes, - properties: list['Property'] | None = None, - ) -> None: + def __init__(self, + attribute: int, + bstring: bytes, + properties: Optional[List['Property']] = None): """ Args: attribute: Attribute number. @@ -1119,23 +1121,24 @@ class XElement(Record): self.bstring = bstring self.properties = [] if properties is None else properties - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): pass - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): pass @staticmethod - def read(stream: IO[bytes], record_id: int) -> 'XElement': + def read(stream: io.BufferedIOBase, record_id: int) -> 'XElement': if record_id != 32: - raise InvalidDataError(f'Invalid record id for XElement: {record_id}') + raise InvalidDataError('Invalid record id for XElement: ' + '{}'.format(record_id)) attribute = read_uint(stream) bstring = read_bstring(stream) record = XElement(attribute, bstring) - logger.debug(f'Record ending at 0x{stream.tell():x}:\n {record}') + logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: size = write_uint(stream, 32) size += write_uint(stream, self.attribute) size += write_bstring(stream, self.bstring) @@ -1145,31 +1148,35 @@ class XElement(Record): class XGeometry(Record, GeometryMixin): """ XGeometry record (ID 33) + + Attributes: + attribute (int): Attribute number. + bstring (bytes): XGeometry data. + layer (Optional[int]): None means reuse modal + datatype (Optional[int]): None means reuse modal + x (Optional[int]): None means reuse modal + y (Optional[int]): None means reuse modal + repetition (Optional[repetition_t]): Repetition, if any + properties (List[Property]): List of property records associate with this record. """ attribute: int - """Attribute number""" - bstring: bytes - """XGeometry data""" + layer: Optional[int] = None + datatype: Optional[int] = None + x: Optional[int] = None + y: Optional[int] = None + repetition: Optional[repetition_t] = None + properties: List['Property'] - layer: int | None = None - datatype: int | None = None - x: int | None = None - y: int | None = None - repetition: repetition_t | None = None - properties: list['Property'] - - def __init__( - self, - attribute: int, - bstring: bytes, - layer: int | None = None, - datatype: int | None = None, - x: int | None = None, - y: int | None = None, - repetition: repetition_t | None = None, - properties: list['Property'] | None = None, - ) -> None: + def __init__(self, + attribute: int, + bstring: bytes, + layer: Optional[int] = None, + datatype: Optional[int] = None, + x: Optional[int] = None, + y: Optional[int] = None, + repetition: Optional[repetition_t] = None, + properties: Optional[List['Property']] = None): """ Args: attribute: Attribute number for this XGeometry. @@ -1190,64 +1197,65 @@ class XGeometry(Record, GeometryMixin): self.repetition = repetition self.properties = [] if properties is None else properties - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): adjust_coordinates(self, modals, 'geometry_x', 'geometry_y') adjust_repetition(self, modals) adjust_field(self, 'layer', modals, 'layer') adjust_field(self, 'datatype', modals, 'datatype') - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): dedup_coordinates(self, modals, 'geometry_x', 'geometry_y') dedup_repetition(self, modals) dedup_field(self, 'layer', modals, 'layer') dedup_field(self, 'datatype', modals, 'datatype') @staticmethod - def read(stream: IO[bytes], record_id: int) -> 'XGeometry': + def read(stream: io.BufferedIOBase, record_id: int) -> 'XGeometry': if record_id != 33: - raise InvalidDataError(f'Invalid record id for XGeometry: {record_id}') + raise InvalidDataError('Invalid record id for XGeometry: ' + '{}'.format(record_id)) - z0, z1, z2, xx, yy, rr, dd, ll = read_bool_byte(stream) + z0, z1, z2, x, y, r, d, l = read_bool_byte(stream) if z0 or z1 or z2: raise InvalidDataError('Malformed XGeometry header') attribute = read_uint(stream) - optional: dict[str, Any] = {} - if ll: + optional: Dict[str, Any] = {} + if l: optional['layer'] = read_uint(stream) - if dd: + if d: optional['datatype'] = read_uint(stream) bstring = read_bstring(stream) - if xx: + if x: optional['x'] = read_sint(stream) - if yy: + if y: optional['y'] = read_sint(stream) - if rr: + if r: optional['repetition'] = read_repetition(stream) record = XGeometry(attribute, bstring, **optional) - logger.debug(f'Record ending at 0x{stream.tell():x}:\n {record}') + logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record - def write(self, stream: IO[bytes]) -> int: - xx = self.x is not None - yy = self.y is not None - rr = self.repetition is not None - dd = self.datatype is not None - ll = self.layer is not None + def write(self, stream: io.BufferedIOBase) -> int: + x = self.x is not None + y = self.y is not None + r = self.repetition is not None + d = self.datatype is not None + l = self.layer is not None size = write_uint(stream, 33) - size += write_bool_byte(stream, (0, 0, 0, xx, yy, rr, dd, ll)) + size += write_bool_byte(stream, (0, 0, 0, x, y, r, d, l)) size += write_uint(stream, self.attribute) - if ll: + if l: size += write_uint(stream, self.layer) # type: ignore - if dd: + if d: size += write_uint(stream, self.datatype) # type: ignore size += write_bstring(stream, self.bstring) - if xx: + if x: size += write_sint(stream, self.x) # type: ignore - if yy: + if y: size += write_sint(stream, self.y) # type: ignore - if rr: + if r: size += self.repetition.write(stream) # type: ignore return size @@ -1255,37 +1263,40 @@ class XGeometry(Record, GeometryMixin): class Cell(Record): """ Cell record (ID 13, 14) - """ - name: int | NString - """int specifies "CellName reference" number""" - def __init__(self, name: int | str | NString) -> None: + Attributes: + name (Union[int, NString]): int specifies "CellName reference" number + """ + name: Union[int, NString] + + def __init__(self, name: Union[int, str, NString]): """ Args: name: `NString`, or an int specifying a `CellName` reference number. """ - self.name = name if isinstance(name, int | NString) else NString(name) + self.name = name if isinstance(name, (int, NString)) else NString(name) - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): modals.reset() - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): modals.reset() @staticmethod - def read(stream: IO[bytes], record_id: int) -> 'Cell': - name: int | NString + def read(stream: io.BufferedIOBase, record_id: int) -> 'Cell': + name: Union[int, NString] if record_id == 13: name = read_uint(stream) elif record_id == 14: name = NString.read(stream) else: - raise InvalidDataError(f'Invalid record id for Cell: {record_id}') + raise InvalidDataError('Invalid record id for Cell: ' + '{}'.format(record_id)) record = Cell(name) - logger.debug(f'Record ending at 0x{stream.tell():x}:\n {record}') + logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record - def write(self, stream: IO[bytes]) -> int: + def write(self, stream: io.BufferedIOBase) -> int: size = 0 if isinstance(self.name, int): size += write_uint(stream, 13) @@ -1299,35 +1310,36 @@ class Cell(Record): class Placement(Record): """ Placement record (ID 17, 18) + + Attributes: + name (Union[NString, int, None]): name, "CellName reference" + number, or reuse modal + magnification (real_t): Magnification factor + angle (real_t): Rotation, degrees counterclockwise + x (Optional[int]): x-offset, None means reuse modal + y (Optional[int]): y-offset, None means reuse modal + repetition (repetition_t or None): Repetition, if any + flip (bool): Whether to perform reflection about the x-axis. + properties (List[Property]): List of property records associate with this record. """ - name: int | NString | None = None - """name, "CellName reference" number, or reuse modal""" - - magnification: real_t | None = None - """magnification factor""" - - angle: real_t | None = None - """Rotation, degrees counterclockwise""" - - x: int | None = None - y: int | None = None - repetition: repetition_t | None = None + name: Union[NString, int, None] = None + magnification: Optional[real_t] = None + angle: Optional[real_t] = None + x: Optional[int] = None + y: Optional[int] = None + repetition: Optional[repetition_t] = None flip: bool - """Whether to perform reflection about the x-axis""" + properties: List['Property'] - properties: list['Property'] - - def __init__( - self, - flip: bool, - name: NString | str | int | None = None, - magnification: real_t | None = None, - angle: real_t | None = None, - x: int | None = None, - y: int | None = None, - repetition: repetition_t | None = None, - properties: list['Property'] | None = None, - ) -> None: + def __init__(self, + flip: bool, + name: Union[NString, str, int, None] = None, + magnification: Optional[real_t] = None, + angle: Optional[real_t] = None, + x: Optional[int] = None, + y: Optional[int] = None, + repetition: Optional[repetition_t] = None, + properties: Optional[List['Property']] = None): """ Args: flip: Whether to perform reflection about the x-axis. @@ -1347,13 +1359,13 @@ class Placement(Record): self.flip = flip self.magnification = magnification self.angle = angle - if isinstance(name, int | NString) or name is None: + if isinstance(name, (int, NString)) or name is None: self.name = name else: self.name = NString(name) self.properties = [] if properties is None else properties - def get_name(self) -> NString | int: + def get_name(self) -> Union[NString, int]: return verify_modal(self.name) # type: ignore def get_x(self) -> int: @@ -1362,85 +1374,86 @@ class Placement(Record): def get_y(self) -> int: return verify_modal(self.y) - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): adjust_coordinates(self, modals, 'placement_x', 'placement_y') adjust_repetition(self, modals) adjust_field(self, 'name', modals, 'placement_cell') - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): dedup_coordinates(self, modals, 'placement_x', 'placement_y') dedup_repetition(self, modals) dedup_field(self, 'name', modals, 'placement_cell') @staticmethod - def read(stream: IO[bytes], record_id: int) -> 'Placement': + def read(stream: io.BufferedIOBase, record_id: int) -> 'Placement': if record_id not in (17, 18): - raise InvalidDataError(f'Invalid record id for Placement: {record_id}') + raise InvalidDataError('Invalid record id for Placement: ' + '{}'.format(record_id)) #CNXYRAAF (17) or CNXYRMAF (18) - cc, nn, xx, yy, rr, ma0, ma1, flip = read_bool_byte(stream) + c, n, x, y, r, ma0, ma1, flip = read_bool_byte(stream) - optional: dict[str, Any] = {} - name = read_refname(stream, cc, nn) + optional: Dict[str, Any] = {} + name = read_refname(stream, c, n) if record_id == 17: - aa = int((ma0 << 1) | ma1) + aa = (ma0 << 1) | ma1 optional['angle'] = aa * 90 elif record_id == 18: - mm = ma0 - aa1 = ma1 - if mm: + m = ma0 + a = ma1 + if m: optional['magnification'] = read_real(stream) - if aa1: + if a: optional['angle'] = read_real(stream) - if xx: + if x: optional['x'] = read_sint(stream) - if yy: + if y: optional['y'] = read_sint(stream) - if rr: + if r: optional['repetition'] = read_repetition(stream) record = Placement(flip, name, **optional) - logger.debug(f'Record ending at 0x{stream.tell():x}:\n {record}') + logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record - def write(self, stream: IO[bytes]) -> int: - cc = self.name is not None - nn = cc and isinstance(self.name, int) - xx = self.x is not None - yy = self.y is not None - rr = self.repetition is not None - ff = self.flip + def write(self, stream: io.BufferedIOBase) -> int: + c = self.name is not None + n = c and isinstance(self.name, int) + x = self.x is not None + y = self.y is not None + r = self.repetition is not None + f = self.flip if (self.magnification == 1 and self.angle is not None and abs(self.angle % 90.0) < 1e-14): aa = int((self.angle / 90) % 4.0) - bools = (cc, nn, xx, yy, rr, aa & 0b10, aa & 0b01, ff) - mm = False - aq = False + bools = (c, n, x, y, r, aa & 0b10, aa & 0b01, f) + m = False + a = False record_id = 17 else: - mm = self.magnification is not None - aq = self.angle is not None - bools = (cc, nn, xx, yy, rr, mm, aq, ff) + m = self.magnification is not None + a = self.angle is not None + bools = (c, n, x, y, r, m, a, f) record_id = 18 size = write_uint(stream, record_id) size += write_bool_byte(stream, bools) - if cc: - if nn: + if c: + if n: size += write_uint(stream, self.name) # type: ignore else: size += self.name.write(stream) # type: ignore - if mm: + if m: size += write_real(stream, self.magnification) # type: ignore - if aa: + if a: size += write_real(stream, self.angle) # type: ignore - if xx: + if x: size += write_sint(stream, self.x) # type: ignore - if yy: + if y: size += write_sint(stream, self.y) # type: ignore - if rr: + if r: size += self.repetition.write(stream) # type: ignore return size @@ -1448,25 +1461,32 @@ class Placement(Record): class Text(Record, GeometryMixin): """ Text record (ID 19) - """ - string: AString | int | None = None - layer: int | None = None - datatype: int | None = None - x: int | None = None - y: int | None = None - repetition: repetition_t | None = None - properties: list['Property'] - def __init__( - self, - string: AString | str | int | None = None, - layer: int | None = None, - datatype: int | None = None, - x: int | None = None, - y: int | None = None, - repetition: repetition_t | None = None, - properties: list['Property'] | None = None, - ) -> None: + Attributes: + string (Union[AString, int, None]): None means reuse modal + layer (Optiona[int]): None means reuse modal + datatype (Optional[int]): None means reuse modal + x (Optional[int]): x-offset, None means reuse modal + y (Optional[int]): y-offset, None means reuse modal + repetition (Optional[repetition_t]): Repetition, if any + properties (List[Property]): List of property records associate with this record. + """ + string: Optional[Union[AString, int]] = None + layer: Optional[int] = None + datatype: Optional[int] = None + x: Optional[int] = None + y: Optional[int] = None + repetition: Optional[repetition_t] = None + properties: List['Property'] + + def __init__(self, + string: Union[AString, str, int, None] = None, + layer: Optional[int] = None, + datatype: Optional[int] = None, + x: Optional[int] = None, + y: Optional[int] = None, + repetition: Optional[repetition_t] = None, + properties: Optional[List['Property']] = None): """ Args: string: Text content, or `TextString` reference number. @@ -1483,23 +1503,23 @@ class Text(Record, GeometryMixin): self.x = x self.y = y self.repetition = repetition - if isinstance(string, int | AString) or string is None: + if isinstance(string, (AString, int)) or string is None: self.string = string else: self.string = AString(string) self.properties = [] if properties is None else properties - def get_string(self) -> AString | int: + def get_string(self) -> Union[AString, int]: return verify_modal(self.string) # type: ignore - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): adjust_coordinates(self, modals, 'text_x', 'text_y') adjust_repetition(self, modals) adjust_field(self, 'string', modals, 'text_string') adjust_field(self, 'layer', modals, 'text_layer') adjust_field(self, 'datatype', modals, 'text_datatype') - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): dedup_coordinates(self, modals, 'text_x', 'text_y') dedup_repetition(self, modals) dedup_field(self, 'string', modals, 'text_string') @@ -1507,56 +1527,57 @@ class Text(Record, GeometryMixin): dedup_field(self, 'datatype', modals, 'text_datatype') @staticmethod - def read(stream: IO[bytes], record_id: int) -> 'Text': + def read(stream: io.BufferedIOBase, record_id: int) -> 'Text': if record_id != 19: - raise InvalidDataError(f'Invalid record id for Text: {record_id}') + raise InvalidDataError('Invalid record id for Text: ' + '{}'.format(record_id)) - z0, cc, nn, xx, yy, rr, dd, ll = read_bool_byte(stream) + z0, c, n, x, y, r, d, l = read_bool_byte(stream) if z0: raise InvalidDataError('Malformed Text header') - optional: dict[str, Any] = {} - string = read_refstring(stream, cc, nn) - if ll: + optional: Dict[str, Any] = {} + string = read_refstring(stream, c, n) + if l: optional['layer'] = read_uint(stream) - if dd: + if d: optional['datatype'] = read_uint(stream) - if xx: + if x: optional['x'] = read_sint(stream) - if yy: + if y: optional['y'] = read_sint(stream) - if rr: + if r: optional['repetition'] = read_repetition(stream) record = Text(string, **optional) - logger.debug(f'Record ending at 0x{stream.tell():x}:\n {record}') + logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record - def write(self, stream: IO[bytes]) -> int: - cc = self.string is not None - nn = cc and isinstance(self.string, int) - xx = self.x is not None - yy = self.y is not None - rr = self.repetition is not None - dd = self.datatype is not None - ll = self.layer is not None + def write(self, stream: io.BufferedIOBase) -> int: + c = self.string is not None + n = c and isinstance(self.string, int) + x = self.x is not None + y = self.y is not None + r = self.repetition is not None + d = self.datatype is not None + l = self.layer is not None size = write_uint(stream, 19) - size += write_bool_byte(stream, (0, cc, nn, xx, yy, rr, dd, ll)) - if cc: - if nn: + size += write_bool_byte(stream, (0, c, n, x, y, r, d, l)) + if c: + if n: size += write_uint(stream, self.string) # type: ignore else: size += self.string.write(stream) # type: ignore - if ll: + if l: size += write_uint(stream, self.layer) # type: ignore - if dd: + if d: size += write_uint(stream, self.datatype) # type: ignore - if xx: + if x: size += write_sint(stream, self.x) # type: ignore - if yy: + if y: size += write_sint(stream, self.y) # type: ignore - if rr: + if r: size += self.repetition.write(stream) # type: ignore return size @@ -1566,44 +1587,42 @@ class Rectangle(Record, GeometryMixin): Rectangle record (ID 20) (x, y) denotes the lower-left (min-x, min-y) corner of the rectangle. + + Attributes: + is_square (bool): `True` if this is a square. + If `True`, `height` must be `None`. + width (Optional[int]): X-width. `None` means reuse modal. + height (Optional[int]): Y-height. Must be `None` if `is_square` is `True`. + If `is_square` is `False`, `None` means reuse modal. + layer (Optional[int]): None means reuse modal + datatype (Optional[int]): None means reuse modal + x (Optional[int]): x-offset of the rectangle's lower-left (min-x) point. + None means reuse modal. + y (Optional[int]): y-offset of the rectangle's lower-left (min-y) point. + None means reuse modal + repetition (Optional[repetition_t]): Repetition, if any. + properties (List[Property]): List of property records associate with this record. """ - layer: int | None - datatype: int | None - width: int | None - """X-width. `None` means reuse modal""" + layer: Optional[int] = None + datatype: Optional[int] = None + width: Optional[int] = None + height: Optional[int] = None + x: Optional[int] = None + y: Optional[int] = None + repetition: Optional[repetition_t] = None + is_square: bool = False + properties: List['Property'] - height: int | None - """Y-height. Must be `None` if `is_square` is `True`. - If `is_square` is `False`, `None` means reuse modal - """ - - x: int | None - """x-offset of the rectangle's lower-left (min-x) point. - None means reuse modal. - """ - y: int | None - """y-offset of the rectangle's lower-left (min-y) point. - None means reuse modal - """ - - repetition: repetition_t | None - is_square: bool - """If `True`, `height` must be `None`""" - - properties: list['Property'] - - def __init__( - self, - is_square: bool = False, - layer: int | None = None, - datatype: int | None = None, - width: int | None = None, - height: int | None = None, - x: int | None = None, - y: int | None = None, - repetition: repetition_t | None = None, - properties: list['Property'] | None = None, - ) -> None: + def __init__(self, + is_square: bool = False, + layer: Optional[int] = None, + datatype: Optional[int] = None, + width: Optional[int] = None, + height: Optional[int] = None, + x: Optional[int] = None, + y: Optional[int] = None, + repetition: Optional[repetition_t] = None, + properties: Optional[List['Property']] = None): self.is_square = is_square self.layer = layer self.datatype = datatype @@ -1624,7 +1643,7 @@ class Rectangle(Record, GeometryMixin): return verify_modal(self.width) return verify_modal(self.height) - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): adjust_coordinates(self, modals, 'geometry_x', 'geometry_y') adjust_repetition(self, modals) adjust_field(self, 'layer', modals, 'layer') @@ -1635,7 +1654,7 @@ class Rectangle(Record, GeometryMixin): else: adjust_field(self, 'height', modals, 'geometry_h') - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): dedup_coordinates(self, modals, 'geometry_x', 'geometry_y') dedup_repetition(self, modals) dedup_field(self, 'layer', modals, 'layer') @@ -1647,55 +1666,56 @@ class Rectangle(Record, GeometryMixin): dedup_field(self, 'height', modals, 'geometry_h') @staticmethod - def read(stream: IO[bytes], record_id: int) -> 'Rectangle': + def read(stream: io.BufferedIOBase, record_id: int) -> 'Rectangle': if record_id != 20: - raise InvalidDataError(f'Invalid record id for Rectangle: {record_id}') + raise InvalidDataError('Invalid record id for Rectangle: ' + '{}'.format(record_id)) - is_square, ww, hh, xx, yy, rr, dd, ll = read_bool_byte(stream) - optional: dict[str, Any] = {} - if ll: + is_square, w, h, x, y, r, d, l = read_bool_byte(stream) + optional: Dict[str, Any] = {} + if l: optional['layer'] = read_uint(stream) - if dd: + if d: optional['datatype'] = read_uint(stream) - if ww: + if w: optional['width'] = read_uint(stream) - if hh: + if h: optional['height'] = read_uint(stream) - if xx: + if x: optional['x'] = read_sint(stream) - if yy: + if y: optional['y'] = read_sint(stream) - if rr: + if r: optional['repetition'] = read_repetition(stream) record = Rectangle(is_square, **optional) - logger.debug(f'Record ending at 0x{stream.tell():x}:\n {record}') + logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record - def write(self, stream: IO[bytes]) -> int: - ss = self.is_square - ww = self.width is not None - hh = self.height is not None - xx = self.x is not None - yy = self.y is not None - rr = self.repetition is not None - dd = self.datatype is not None - ll = self.layer is not None + def write(self, stream: io.BufferedIOBase) -> int: + s = self.is_square + w = self.width is not None + h = self.height is not None + x = self.x is not None + y = self.y is not None + r = self.repetition is not None + d = self.datatype is not None + l = self.layer is not None size = write_uint(stream, 20) - size += write_bool_byte(stream, (ss, ww, hh, xx, yy, rr, dd, ll)) - if ll: + size += write_bool_byte(stream, (s, w, h, x, y, r, d, l)) + if l: size += write_uint(stream, self.layer) # type: ignore - if dd: + if d: size += write_uint(stream, self.datatype) # type: ignore - if ww: + if w: size += write_uint(stream, self.width) # type: ignore - if hh: + if h: size += write_uint(stream, self.height) # type: ignore - if xx: + if x: size += write_sint(stream, self.x) # type: ignore - if yy: + if y: size += write_sint(stream, self.y) # type: ignore - if rr: + if r: size += self.repetition.write(stream) # type: ignore return size @@ -1703,39 +1723,41 @@ class Rectangle(Record, GeometryMixin): class Polygon(Record, GeometryMixin): """ Polygon record (ID 21) - """ - layer: int | None - datatype: int | None - x: int | None - """x-offset of the polygon's first point. - None means reuse modal - """ - y: int | None - """y-offset of the polygon's first point. - None means reuse modal - """ - repetition: repetition_t | None - point_list: point_list_t | None - """ - List of offsets from the initial vertex (x, y) to the remaining - vertices, `[[dx0, dy0], [dx1, dy1], ...]`. - The list is an implicitly closed path, vertices are [int, int]. - The initial vertex is located at (x, y) and is not represented in `point_list`. - `None` means reuse modal. - """ - properties: list['Property'] + Attributes: + point_list (Optional[point_list_t]): List of offsets from the + initial vertex (x, y) to the remaining vertices, + `[[dx0, dy0], [dx1, dy1], ...]`. + The list is an implicitly closed path, vertices are [int, int], + The initial vertex is located at (x, y) and is not represented + in `point_list`. + `None` means reuse modal. + layer (Optional[int]): Layer number. None means reuse modal + datatype (Optional[int]): Datatype number. None means reuse modal + x (Optional[int]): x-offset of the polygon's first point. + None means reuse modal + y (Optional[int]): y-offset of the polygon's first point. + None means reuse modal + repetition (Optional[repetition_t]): Repetition, if any. + Default no repetition. + properties (List[Property]): List of property records associate with this record. + """ + layer: Optional[int] = None + datatype: Optional[int] = None + x: Optional[int] = None + y: Optional[int] = None + repetition: Optional[repetition_t] = None + point_list: Optional[point_list_t] = None + properties: List['Property'] - def __init__( - self, - point_list: point_list_t | None = None, - layer: int | None = None, - datatype: int | None = None, - x: int | None = None, - y: int | None = None, - repetition: repetition_t | None = None, - properties: list['Property'] | None = None, - ) -> None: + def __init__(self, + point_list: Optional[point_list_t] = None, + layer: Optional[int] = None, + datatype: Optional[int] = None, + x: Optional[int] = None, + y: Optional[int] = None, + repetition: Optional[repetition_t] = None, + properties: Optional[List['Property']] = None): self.layer = layer self.datatype = datatype self.x = x @@ -1744,20 +1766,21 @@ class Polygon(Record, GeometryMixin): self.point_list = point_list self.properties = [] if properties is None else properties - if point_list is not None and len(point_list) < 3: - warn('Polygon with < 3 points', stacklevel=2) + if point_list is not None: + if len(point_list) < 3: + warn('Polygon with < 3 points') def get_point_list(self) -> point_list_t: return verify_modal(self.point_list) - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): adjust_coordinates(self, modals, 'geometry_x', 'geometry_y') adjust_repetition(self, modals) adjust_field(self, 'layer', modals, 'layer') adjust_field(self, 'datatype', modals, 'datatype') adjust_field(self, 'point_list', modals, 'polygon_point_list') - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): dedup_coordinates(self, modals, 'geometry_x', 'geometry_y') dedup_repetition(self, modals) dedup_field(self, 'layer', modals, 'layer') @@ -1765,53 +1788,54 @@ class Polygon(Record, GeometryMixin): dedup_field(self, 'point_list', modals, 'polygon_point_list') @staticmethod - def read(stream: IO[bytes], record_id: int) -> 'Polygon': + def read(stream: io.BufferedIOBase, record_id: int) -> 'Polygon': if record_id != 21: - raise InvalidDataError(f'Invalid record id for Polygon: {record_id}') + raise InvalidDataError('Invalid record id for Polygon: ' + '{}'.format(record_id)) - z0, z1, pp, xx, yy, rr, dd, ll = read_bool_byte(stream) + z0, z1, p, x, y, r, d, l = read_bool_byte(stream) if z0 or z1: raise InvalidDataError('Invalid polygon header') - optional: dict[str, Any] = {} - if ll: + optional: Dict[str, Any] = {} + if l: optional['layer'] = read_uint(stream) - if dd: + if d: optional['datatype'] = read_uint(stream) - if pp: + if p: optional['point_list'] = read_point_list(stream, implicit_closed=True) - if xx: + if x: optional['x'] = read_sint(stream) - if yy: + if y: optional['y'] = read_sint(stream) - if rr: + if r: optional['repetition'] = read_repetition(stream) record = Polygon(**optional) - logger.debug('Record ending at 0x{stream.tell():x}:\n {record}') + logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record - def write(self, stream: IO[bytes], fast: bool = False) -> int: - pp = self.point_list is not None - xx = self.x is not None - yy = self.y is not None - rr = self.repetition is not None - dd = self.datatype is not None - ll = self.layer is not None + def write(self, stream: io.BufferedIOBase, fast: bool = False) -> int: + p = self.point_list is not None + x = self.x is not None + y = self.y is not None + r = self.repetition is not None + d = self.datatype is not None + l = self.layer is not None size = write_uint(stream, 21) - size += write_bool_byte(stream, (0, 0, pp, xx, yy, rr, dd, ll)) - if ll: + size += write_bool_byte(stream, (0, 0, p, x, y, r, d, l)) + if l: size += write_uint(stream, self.layer) # type: ignore - if dd: + if d: size += write_uint(stream, self.datatype) # type: ignore - if pp: + if p: size += write_point_list(stream, self.point_list, # type: ignore implicit_closed=True, fast=fast) - if xx: + if x: size += write_sint(stream, self.x) # type: ignore - if yy: + if y: size += write_sint(stream, self.y) # type: ignore - if rr: + if r: size += self.repetition.write(stream) # type: ignore return size @@ -1819,51 +1843,50 @@ class Polygon(Record, GeometryMixin): class Path(Record, GeometryMixin): """ Polygon record (ID 22) - """ - layer: int | None = None - datatype: int | None = None - x: int | None = None - y: int | None = None - repetition: repetition_t | None = None - point_list: point_list_t | None = None - """ - List of offsets from the initial vertex (x, y) to the remaining vertices, - `[[dx0, dy0], [dx1, dy1], ...]`. - The initial vertex is located at (x, y) and is not represented in `point_list`. - Offsets are [int, int]; `None` means reuse modal. - """ - half_width: int | None = None - """None means reuse modal""" - - extension_start: pathextension_t | None = None - """ - `None` means reuse modal. - Tuple is of the form (`PathExtensionScheme`, int | None) - Second value is None unless using `PathExtensionScheme.Arbitrary` - Value determines extension past start point. + Attributes: + point_list (Optional[point_list_t]): List of offsets from the + initial vertex (x, y) to the remaining vertices, + `[[dx0, dy0], [dx1, dy1], ...]`. + The initial vertex is located at (x, y) and is not represented + in `point_list`. + Offsets are [int, int]; `None` means reuse modal. + half_width (Optional[int]): None means reuse modal + extension_start (Optional[Tuple]): None means reuse modal. + Tuple is of the form (`PathExtensionScheme`, Optional[int]) + Second value is None unless using `PathExtensionScheme.Arbitrary` + Value determines extension past start point. + extension_end (Optional[Tuple]): Same form as `extension_end`. + Value determines extension past end point. + layer (Optional[int]): None means reuse modal + datatype (Optional[int]): None means reuse modal + x (Optional[int]): x-offset, None means reuse modal + y (Optional[int]): y-offset, None means reuse modal + repetition (Optional[repetition_t]): Repetition, if any + properties (List[Property]): List of property records associate with this record. """ + layer: Optional[int] = None + datatype: Optional[int] = None + x: Optional[int] = None + y: Optional[int] = None + repetition: Optional[repetition_t] = None + point_list: Optional[point_list_t] = None + half_width: Optional[int] = None + extension_start: Optional[pathextension_t] = None + extension_end: Optional[pathextension_t] = None + properties: List['Property'] - extension_end: pathextension_t | None = None - """ - Same form as `extension_end`. Value determines extension past end point. - """ - - properties: list['Property'] - - def __init__( - self, - point_list: point_list_t | None = None, - half_width: int | None = None, - extension_start: pathextension_t | None = None, - extension_end: pathextension_t | None = None, - layer: int | None = None, - datatype: int | None = None, - x: int | None = None, - y: int | None = None, - repetition: repetition_t | None = None, - properties: list['Property'] | None = None, - ) -> None: + def __init__(self, + point_list: Optional[point_list_t] = None, + half_width: Optional[int] = None, + extension_start: Optional[pathextension_t] = None, + extension_end: Optional[pathextension_t] = None, + layer: Optional[int] = None, + datatype: Optional[int] = None, + x: Optional[int] = None, + y: Optional[int] = None, + repetition: Optional[repetition_t] = None, + properties: Optional[List['Property']] = None): self.layer = layer self.datatype = datatype self.x = x @@ -1887,7 +1910,7 @@ class Path(Record, GeometryMixin): def get_extension_end(self) -> pathextension_t: return verify_modal(self.extension_end) - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): adjust_coordinates(self, modals, 'geometry_x', 'geometry_y') adjust_repetition(self, modals) adjust_field(self, 'layer', modals, 'layer') @@ -1897,7 +1920,7 @@ class Path(Record, GeometryMixin): adjust_field(self, 'extension_start', modals, 'path_extension_start') adjust_field(self, 'extension_end', modals, 'path_extension_end') - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): dedup_coordinates(self, modals, 'geometry_x', 'geometry_y') dedup_repetition(self, modals) dedup_field(self, 'layer', modals, 'layer') @@ -1908,67 +1931,69 @@ class Path(Record, GeometryMixin): dedup_field(self, 'extension_end', modals, 'path_extension_end') @staticmethod - def read(stream: IO[bytes], record_id: int) -> 'Path': + def read(stream: io.BufferedIOBase, record_id: int) -> 'Path': if record_id != 22: - raise InvalidDataError(f'Invalid record id for Path: {record_id}') + raise InvalidDataError('Invalid record id for Path: ' + '{}'.format(record_id)) - ee, ww, pp, xx, yy, rr, dd, ll = read_bool_byte(stream) - optional: dict[str, Any] = {} - if ll: + e, w, p, x, y, r, d, l = read_bool_byte(stream) + optional: Dict[str, Any] = {} + if l: optional['layer'] = read_uint(stream) - if dd: + if d: optional['datatype'] = read_uint(stream) - if ww: + if w: optional['half_width'] = read_uint(stream) - if ee: + if e: scheme = read_uint(stream) scheme_end = scheme & 0b11 scheme_start = (scheme >> 2) & 0b11 - def get_pathext(ext_scheme: int) -> pathextension_t | None: + def get_pathext(ext_scheme: int) -> Optional[pathextension_t]: if ext_scheme == 0: return None - if ext_scheme == 1: + elif ext_scheme == 1: return PathExtensionScheme.Flush, None - if ext_scheme == 2: + elif ext_scheme == 2: return PathExtensionScheme.HalfWidth, None - if ext_scheme == 3: + elif ext_scheme == 3: return PathExtensionScheme.Arbitrary, read_sint(stream) - raise InvalidDataError(f'Invalid ext_scheme: {ext_scheme}') + else: + raise InvalidDataError('Invalid ext_scheme: {}'.format(ext_scheme)) optional['extension_start'] = get_pathext(scheme_start) optional['extension_end'] = get_pathext(scheme_end) - if pp: + if p: optional['point_list'] = read_point_list(stream, implicit_closed=False) - if xx: + if x: optional['x'] = read_sint(stream) - if yy: + if y: optional['y'] = read_sint(stream) - if rr: + if r: optional['repetition'] = read_repetition(stream) record = Path(**optional) - logger.debug(f'Record ending at 0x{stream.tell():x}:\n {record}') + logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record - def write(self, stream: IO[bytes], fast: bool = False) -> int: - ee = self.extension_start is not None or self.extension_end is not None - ww = self.half_width is not None - pp = self.point_list is not None - xx = self.x is not None - yy = self.y is not None - rr = self.repetition is not None - dd = self.datatype is not None - ll = self.layer is not None + def write(self, stream: io.BufferedIOBase, fast: bool = False) -> int: + e = self.extension_start is not None or self.extension_end is not None + w = self.half_width is not None + p = self.point_list is not None + x = self.x is not None + y = self.y is not None + r = self.repetition is not None + d = self.datatype is not None + l = self.layer is not None size = write_uint(stream, 21) - size += write_bool_byte(stream, (ee, ww, pp, xx, yy, rr, dd, ll)) - if ll: + size += write_bool_byte(stream, (e, w, p, x, y, r, d, l)) + if l: size += write_uint(stream, self.layer) # type: ignore - if dd: + if d: size += write_uint(stream, self.datatype) # type: ignore - if ww: + if w: size += write_uint(stream, self.half_width) # type: ignore - if ee: + if e: scheme = 0 if self.extension_start is not None: scheme += self.extension_start[0].value << 2 @@ -1979,14 +2004,14 @@ class Path(Record, GeometryMixin): size += write_sint(stream, self.extension_start[1]) # type: ignore if scheme & 0b0011 == 0b0011: size += write_sint(stream, self.extension_end[1]) # type: ignore - if pp: + if p: size += write_point_list(stream, self.point_list, # type: ignore implicit_closed=False, fast=fast) - if xx: + if x: size += write_sint(stream, self.x) # type: ignore - if yy: + if y: size += write_sint(stream, self.y) # type: ignore - if rr: + if r: size += self.repetition.write(stream) # type: ignore return size @@ -1997,62 +2022,54 @@ class Trapezoid(Record, GeometryMixin): Trapezoid with at least two sides parallel to the x- or y-axis. (x, y) denotes the lower-left (min-x, min-y) corner of the trapezoid's bounding box. + + Attributes: + delta_a (Optional[int]): If horizontal, signed x-distance from top left + vertex to bottom left vertex. If vertical, signed y-distance from + bottom left vertex to bottom right vertex. + None means reuse modal. + delta_b (Optional[int]): If horizontal, signed x-distance from bottom right + vertex to top right vertex. If vertical, signed y-distance from top + right vertex to top left vertex. + None means reuse modal. + is_vertical (bool): `True` if the left and right sides are aligned to + the y-axis. If the trapezoid is a rectangle, either `True` or `False` + can be used. + width (Optional[int]): Bounding box x-width, None means reuse modal. + height (Optional[int]): Bounding box y-height, None means reuse modal. + layer (Optional[int]): None means reuse modal + datatype (Optional[int]): None means reuse modal + x (Optional[int]): x-offset to lower-left corner of the trapezoid's bounding box. + None means reuse modal + y (Optional[int]): y-offset to lower-left corner of the trapezoid's bounding box. + None means reuse modal + repetition (Optional[repetition_t]): Repetition, if any + properties (List[Property]): List of property records associate with this record. """ - layer: int | None = None - datatype: int | None = None - width: int | None = None - """Bounding box x-width, None means reuse modal.""" - - height: int | None = None - """Bounding box y-height, None means reuse modal.""" - - x: int | None = None - """x-offset to lower-left corner of the trapezoid's bounding box. - None means reuse modal - """ - - y: int | None = None - """y-offset to lower-left corner of the trapezoid's bounding box. - None means reuse modal - """ - - repetition: repetition_t | None = None + layer: Optional[int] = None + datatype: Optional[int] = None + width: Optional[int] = None + height: Optional[int] = None + x: Optional[int] = None + y: Optional[int] = None + repetition: Optional[repetition_t] = None delta_a: int = 0 - """ - If horizontal, signed x-distance from top left vertex to bottom left vertex. - If vertical, signed y-distance from bottom left vertex to bottom right vertex. - None means reuse modal. - """ - delta_b: int = 0 - """ - If horizontal, signed x-distance from bottom right vertex to top right vertex. - If vertical, signed y-distance from top right vertex to top left vertex. - None means reuse modal. - """ - is_vertical: bool - """ - `True` if the left and right sides are aligned to the y-axis. - If the trapezoid is a rectangle, either `True` or `False` can be used. - """ + properties: List['Property'] - properties: list['Property'] - - def __init__( - self, - is_vertical: bool, - delta_a: int = 0, - delta_b: int = 0, - layer: int | None = None, - datatype: int | None = None, - width: int | None = None, - height: int | None = None, - x: int | None = None, - y: int | None = None, - repetition: repetition_t | None = None, - properties: list['Property'] | None = None, - ) -> None: + def __init__(self, + is_vertical: bool, + delta_a: int = 0, + delta_b: int = 0, + layer: int = None, + datatype: int = None, + width: int = None, + height: int = None, + x: int = None, + y: int = None, + repetition: repetition_t = None, + properties: Optional[List['Property']] = None): """ Raises: InvalidDataError: if dimensions are impossible. @@ -2071,9 +2088,12 @@ class Trapezoid(Record, GeometryMixin): if self.is_vertical: if height is not None and delta_b - delta_a > height: - raise InvalidDataError(f'Trapezoid: h < delta_b - delta_a ({height} < {delta_b} - {delta_a})') - elif width is not None and delta_b - delta_a > width: - raise InvalidDataError(f'Trapezoid: w < delta_b - delta_a ({width} < {delta_b} - {delta_a})') + raise InvalidDataError('Trapezoid: h < delta_b - delta_a' + + ' ({} < {} - {})'.format(height, delta_b, delta_a)) + else: + if width is not None and delta_b - delta_a > width: + raise InvalidDataError('Trapezoid: w < delta_b - delta_a' + + ' ({} < {} - {})'.format(width, delta_b, delta_a)) def get_is_vertical(self) -> bool: return verify_modal(self.is_vertical) @@ -2090,7 +2110,7 @@ class Trapezoid(Record, GeometryMixin): def get_height(self) -> int: return verify_modal(self.height) - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): adjust_coordinates(self, modals, 'geometry_x', 'geometry_y') adjust_repetition(self, modals) adjust_field(self, 'layer', modals, 'layer') @@ -2098,7 +2118,7 @@ class Trapezoid(Record, GeometryMixin): adjust_field(self, 'width', modals, 'geometry_w') adjust_field(self, 'height', modals, 'geometry_h') - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): dedup_coordinates(self, modals, 'geometry_x', 'geometry_y') dedup_repetition(self, modals) dedup_field(self, 'layer', modals, 'layer') @@ -2107,43 +2127,44 @@ class Trapezoid(Record, GeometryMixin): dedup_field(self, 'height', modals, 'geometry_h') @staticmethod - def read(stream: IO[bytes], record_id: int) -> 'Trapezoid': + def read(stream: io.BufferedIOBase, record_id: int) -> 'Trapezoid': if record_id not in (23, 24, 25): - raise InvalidDataError(f'Invalid record id for Trapezoid: {record_id}') + raise InvalidDataError('Invalid record id for Trapezoid: ' + '{}'.format(record_id)) - is_vertical, ww, hh, xx, yy, rr, dd, ll = read_bool_byte(stream) - optional: dict[str, Any] = {} - if ll: + is_vertical, w, h, x, y, r, d, l = read_bool_byte(stream) + optional: Dict[str, Any] = {} + if l: optional['layer'] = read_uint(stream) - if dd: + if d: optional['datatype'] = read_uint(stream) - if ww: + if w: optional['width'] = read_uint(stream) - if hh: + if h: optional['height'] = read_uint(stream) if record_id != 25: optional['delta_a'] = read_sint(stream) if record_id != 24: optional['delta_b'] = read_sint(stream) - if xx: + if x: optional['x'] = read_sint(stream) - if yy: + if y: optional['y'] = read_sint(stream) - if rr: + if r: optional['repetition'] = read_repetition(stream) record = Trapezoid(bool(is_vertical), **optional) - logger.debug(f'Record ending at 0x{stream.tell():x}:\n {record}') + logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record - def write(self, stream: IO[bytes]) -> int: - vv = self.is_vertical - ww = self.width is not None - hh = self.height is not None - xx = self.x is not None - yy = self.y is not None - rr = self.repetition is not None - dd = self.datatype is not None - ll = self.layer is not None + def write(self, stream: io.BufferedIOBase) -> int: + v = self.is_vertical + w = self.width is not None + h = self.height is not None + x = self.x is not None + y = self.y is not None + r = self.repetition is not None + d = self.datatype is not None + l = self.layer is not None if self.delta_b == 0: record_id = 24 @@ -2152,24 +2173,24 @@ class Trapezoid(Record, GeometryMixin): else: record_id = 23 size = write_uint(stream, record_id) - size += write_bool_byte(stream, (vv, ww, hh, xx, yy, rr, dd, ll)) - if ll: + size += write_bool_byte(stream, (v, w, h, x, y, r, d, l)) + if l: size += write_uint(stream, self.layer) # type: ignore - if dd: + if d: size += write_uint(stream, self.datatype) # type: ignore - if ww: + if w: size += write_uint(stream, self.width) # type: ignore - if hh: + if h: size += write_uint(stream, self.height) # type: ignore if record_id != 25: size += write_sint(stream, self.delta_a) # type: ignore if record_id != 24: size += write_sint(stream, self.delta_b) # type: ignore - if xx: + if x: size += write_sint(stream, self.x) # type: ignore - if yy: + if y: size += write_sint(stream, self.y) # type: ignore - if rr: + if r: size += self.repetition.write(stream) # type: ignore return size @@ -2212,46 +2233,43 @@ class CTrapezoid(Record, GeometryMixin): w = h w = 2h w = h set h = None set w = None set h = None - """ - ctrapezoid_type: int | None = None - """See class docstring for details. None means reuse modal.""" - layer: int | None = None - datatype: int | None = None - width: int | None = None - """width: Bounding box x-width - None means unnecessary, or reuse modal if necessary. + Attributes: + ctrapezoid_type (Optional[int]): See above for details. + None means reuse modal. + width (Optional[int]): Bounding box x-width. + None means unnecessary, or reuse modal if necessary. + height (Optional[int]): Bounding box y-height. + None means unnecessary, or reuse modal if necessary. + layer (Optional[int]): None means reuse modal + datatype (Optional[int]): None means reuse modal + x (Optional[int]): x-offset of lower-left (min-x) point of bounding box. + None means reuse modal + y (Optional[int]): y-offset of lower-left (min-y) point of bounding box. + None means reuse modal + repetition (Optional[repetition_t]): Repetition, if any + properties (List[Property]): List of property records associate with this record. """ + ctrapezoid_type: Optional[int] = None + layer: Optional[int] = None + datatype: Optional[int] = None + width: Optional[int] = None + height: Optional[int] = None + x: Optional[int] = None + y: Optional[int] = None + repetition: Optional[repetition_t] = None + properties: List['Property'] - height: int | None = None - """Bounding box y-height. - None means unnecessary, or reuse modal if necessary. - """ - - x: int | None = None - """x-offset of lower-left (min-x) point of bounding box. - None means reuse modal - """ - y: int | None = None - """y-offset of lower-left (min-y) point of bounding box. - None means reuse modal - """ - - repetition: repetition_t | None = None - properties: list['Property'] - - def __init__( - self, - ctrapezoid_type: int | None = None, - layer: int | None = None, - datatype: int | None = None, - width: int | None = None, - height: int | None = None, - x: int | None = None, - y: int | None = None, - repetition: repetition_t | None = None, - properties: list['Property'] | None = None, - ) -> None: + def __init__(self, + ctrapezoid_type: int = None, + layer: int = None, + datatype: int = None, + width: int = None, + height: int = None, + x: int = None, + y: int = None, + repetition: repetition_t = None, + properties: Optional[List['Property']] = None): """ Raises: InvalidDataError: if dimensions are invalid. @@ -2285,7 +2303,7 @@ class CTrapezoid(Record, GeometryMixin): return verify_modal(self.height) return verify_modal(self.width) - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): adjust_coordinates(self, modals, 'geometry_x', 'geometry_y') adjust_repetition(self, modals) adjust_field(self, 'layer', modals, 'layer') @@ -2294,19 +2312,21 @@ class CTrapezoid(Record, GeometryMixin): if self.ctrapezoid_type in (20, 21): if self.width is not None: - raise InvalidDataError(f'CTrapezoid has spurious width entry: {self.width}') + raise InvalidDataError('CTrapezoid has spurious width entry: ' + '{}'.format(self.width)) else: adjust_field(self, 'width', modals, 'geometry_w') if self.ctrapezoid_type in (16, 17, 18, 19, 22, 23, 25): if self.height is not None: - raise InvalidDataError(f'CTrapezoid has spurious height entry: {self.height}') + raise InvalidDataError('CTrapezoid has spurious height entry: ' + '{}'.format(self.height)) else: adjust_field(self, 'height', modals, 'geometry_h') self.check_valid() - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): dedup_coordinates(self, modals, 'geometry_x', 'geometry_y') dedup_repetition(self, modals) dedup_field(self, 'layer', modals, 'layer') @@ -2317,121 +2337,138 @@ class CTrapezoid(Record, GeometryMixin): if self.ctrapezoid_type in (20, 21): if self.width is not None: - raise InvalidDataError(f'CTrapezoid has spurious width entry: {self.width}') + raise InvalidDataError('CTrapezoid has spurious width entry: ' + '{}'.format(self.width)) else: dedup_field(self, 'width', modals, 'geometry_w') if self.ctrapezoid_type in (16, 17, 18, 19, 22, 23, 25): if self.height is not None: - raise InvalidDataError(f'CTrapezoid has spurious height entry: {self.height}') + raise InvalidDataError('CTrapezoid has spurious height entry: ' + '{}'.format(self.height)) else: dedup_field(self, 'height', modals, 'geometry_h') self.check_valid() @staticmethod - def read(stream: IO[bytes], record_id: int) -> 'CTrapezoid': + def read(stream: io.BufferedIOBase, record_id: int) -> 'CTrapezoid': if record_id != 26: - raise InvalidDataError(f'Invalid record id for CTrapezoid: {record_id}') + raise InvalidDataError('Invalid record id for CTrapezoid: ' + '{}'.format(record_id)) - tt, ww, hh, xx, yy, rr, dd, ll = read_bool_byte(stream) - optional: dict[str, Any] = {} - if ll: + t, w, h, x, y, r, d, l = read_bool_byte(stream) + optional: Dict[str, Any] = {} + if l: optional['layer'] = read_uint(stream) - if dd: + if d: optional['datatype'] = read_uint(stream) - if tt: + if t: optional['ctrapezoid_type'] = read_uint(stream) - if ww: + if w: optional['width'] = read_uint(stream) - if hh: + if h: optional['height'] = read_uint(stream) - if xx: + if x: optional['x'] = read_sint(stream) - if yy: + if y: optional['y'] = read_sint(stream) - if rr: + if r: optional['repetition'] = read_repetition(stream) record = CTrapezoid(**optional) - logger.debug(f'Record ending at 0x{stream.tell():x}:\n {record}') + logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record - def write(self, stream: IO[bytes]) -> int: - tt = self.ctrapezoid_type is not None - ww = self.width is not None - hh = self.height is not None - xx = self.x is not None - yy = self.y is not None - rr = self.repetition is not None - dd = self.datatype is not None - ll = self.layer is not None + def write(self, stream: io.BufferedIOBase) -> int: + t = self.ctrapezoid_type is not None + w = self.width is not None + h = self.height is not None + x = self.x is not None + y = self.y is not None + r = self.repetition is not None + d = self.datatype is not None + l = self.layer is not None size = write_uint(stream, 26) - size += write_bool_byte(stream, (tt, ww, hh, xx, yy, rr, dd, ll)) - if ll: + size += write_bool_byte(stream, (t, w, h, x, y, r, d, l)) + if l: size += write_uint(stream, self.layer) # type: ignore - if dd: + if d: size += write_uint(stream, self.datatype) # type: ignore - if tt: + if t: size += write_uint(stream, self.ctrapezoid_type) # type: ignore - if ww: + if w: size += write_uint(stream, self.width) # type: ignore - if hh: + if h: size += write_uint(stream, self.height) # type: ignore - if xx: + if x: size += write_sint(stream, self.x) # type: ignore - if yy: + if y: size += write_sint(stream, self.y) # type: ignore - if rr: + if r: size += self.repetition.write(stream) # type: ignore return size - def check_valid(self) -> None: + def check_valid(self): ctrapezoid_type = self.ctrapezoid_type width = self.width height = self.height if ctrapezoid_type in (20, 21) and width is not None: - raise InvalidDataError(f'CTrapezoid has spurious width entry: {width}') + raise InvalidDataError('CTrapezoid has spurious width entry: ' + '{}'.format(width)) if ctrapezoid_type in (16, 17, 18, 19, 22, 23, 25) and height is not None: - raise InvalidDataError(f'CTrapezoid has spurious height entry: {height}') + raise InvalidDataError('CTrapezoid has spurious height entry: ' + '{}'.format(height)) if width is not None and height is not None: - if ctrapezoid_type in range(0, 4) and width < height: # noqa: PIE808 - raise InvalidDataError(f'CTrapezoid has width < height ({width} < {height})') + if ctrapezoid_type in range(0, 4) and width < height: + raise InvalidDataError('CTrapezoid has width < height' + ' ({} < {})'.format(width, height)) if ctrapezoid_type in range(4, 8) and width < 2 * height: - raise InvalidDataError(f'CTrapezoid has width < 2*height ({width} < 2 * {height})') + raise InvalidDataError('CTrapezoid has width < 2*height' + ' ({} < 2 * {})'.format(width, height)) if ctrapezoid_type in range(8, 12) and width > height: - raise InvalidDataError(f'CTrapezoid has width > height ({width} > {height})') + raise InvalidDataError('CTrapezoid has width > height' + ' ({} > {})'.format(width, height)) if ctrapezoid_type in range(12, 16) and 2 * width > height: - raise InvalidDataError(f'CTrapezoid has 2*width > height ({width} > 2 * {height})') + raise InvalidDataError('CTrapezoid has 2*width > height' + ' ({} > 2 * {})'.format(width, height)) - if ctrapezoid_type is not None and ctrapezoid_type not in range(0, 26): # noqa: PIE808 - raise InvalidDataError(f'CTrapezoid has invalid type: {ctrapezoid_type}') + if ctrapezoid_type is not None and ctrapezoid_type not in range(0, 26): + raise InvalidDataError('CTrapezoid has invalid type: ' + '{}'.format(ctrapezoid_type)) class Circle(Record, GeometryMixin): """ Circle record (ID 27) - """ - layer: int | None - datatype: int | None - x: int | None - y: int | None - repetition: repetition_t | None - radius: int | None - properties: list['Property'] - def __init__( - self, - radius: int | None = None, - layer: int | None = None, - datatype: int | None = None, - x: int | None = None, - y: int | None = None, - repetition: repetition_t | None = None, - properties: list['Property'] | None = None, - ) -> None: + Attributes: + radius (Optional[int]): None means reuse modal + layer (Optional[int]): None means reuse modal + datatype (Optional[int]): None means reuse modal + x (Optional[int]): x-offset, None means reuse modal + y (Optional[int]): y-offset, None means reuse modal + repetition (Optional[repetition_t]): Repetition, if any + properties (List[Property]): List of property records associate with this record. + """ + layer: Optional[int] = None + datatype: Optional[int] = None + x: Optional[int] = None + y: Optional[int] = None + repetition: Optional[repetition_t] = None + radius: Optional[int] = None + properties: List['Property'] + + def __init__(self, + radius: int = None, + layer: int = None, + datatype: int = None, + x: int = None, + y: int = None, + repetition: repetition_t = None, + properties: Optional[List['Property']] = None): """ Args: radius: Radius. Default `None` (reuse modal). @@ -2456,14 +2493,14 @@ class Circle(Record, GeometryMixin): def get_radius(self) -> int: return verify_modal(self.radius) - def merge_with_modals(self, modals: Modals) -> None: + def merge_with_modals(self, modals: Modals): adjust_coordinates(self, modals, 'geometry_x', 'geometry_y') adjust_repetition(self, modals) adjust_field(self, 'layer', modals, 'layer') adjust_field(self, 'datatype', modals, 'datatype') adjust_field(self, 'radius', modals, 'circle_radius') - def deduplicate_with_modals(self, modals: Modals) -> None: + def deduplicate_with_modals(self, modals: Modals): dedup_coordinates(self, modals, 'geometry_x', 'geometry_y') dedup_repetition(self, modals) dedup_field(self, 'layer', modals, 'layer') @@ -2471,57 +2508,58 @@ class Circle(Record, GeometryMixin): dedup_field(self, 'radius', modals, 'circle_radius') @staticmethod - def read(stream: IO[bytes], record_id: int) -> 'Circle': + def read(stream: io.BufferedIOBase, record_id: int) -> 'Circle': if record_id != 27: - raise InvalidDataError(f'Invalid record id for Circle: {record_id}') + raise InvalidDataError('Invalid record id for Circle: ' + '{}'.format(record_id)) - z0, z1, has_radius, xx, yy, rr, dd, ll = read_bool_byte(stream) + z0, z1, has_radius, x, y, r, d, l = read_bool_byte(stream) if z0 or z1: raise InvalidDataError('Malformed circle header') - optional: dict[str, Any] = {} - if ll: + optional: Dict[str, Any] = {} + if l: optional['layer'] = read_uint(stream) - if dd: + if d: optional['datatype'] = read_uint(stream) if has_radius: optional['radius'] = read_uint(stream) - if xx: + if x: optional['x'] = read_sint(stream) - if yy: + if y: optional['y'] = read_sint(stream) - if rr: + if r: optional['repetition'] = read_repetition(stream) record = Circle(**optional) - logger.debug(f'Record ending at 0x{stream.tell():x}:\n {record}') + logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record - def write(self, stream: IO[bytes]) -> int: - ss = self.radius is not None - xx = self.x is not None - yy = self.y is not None - rr = self.repetition is not None - dd = self.datatype is not None - ll = self.layer is not None + def write(self, stream: io.BufferedIOBase) -> int: + s = self.radius is not None + x = self.x is not None + y = self.y is not None + r = self.repetition is not None + d = self.datatype is not None + l = self.layer is not None size = write_uint(stream, 27) - size += write_bool_byte(stream, (0, 0, ss, xx, yy, rr, dd, ll)) - if ll: + size += write_bool_byte(stream, (0, 0, s, x, y, r, d, l)) + if l: size += write_uint(stream, self.layer) # type: ignore - if dd: + if d: size += write_uint(stream, self.datatype) # type: ignore - if ss: + if s: size += write_uint(stream, self.radius) # type: ignore - if xx: + if x: size += write_sint(stream, self.x) # type: ignore - if yy: + if y: size += write_sint(stream, self.y) # type: ignore - if rr: + if r: size += self.repetition.write(stream) # type: ignore return size -def adjust_repetition(record: HasRepetition, modals: Modals) -> None: +def adjust_repetition(record, modals: Modals): """ Merge the record's repetition entry with the one in the modals @@ -2537,12 +2575,13 @@ def adjust_repetition(record: HasRepetition, modals: Modals) -> None: if isinstance(record.repetition, ReuseRepetition): if modals.repetition is None: raise InvalidDataError('Unfillable repetition') - record.repetition = copy.copy(modals.repetition) + else: + record.repetition = copy.copy(modals.repetition) else: modals.repetition = copy.copy(record.repetition) -def adjust_field(record: Record, r_field: str, modals: Modals, m_field: str) -> None: +def adjust_field(record, r_field: str, modals: Modals, m_field: str): """ Merge `record.r_field` with `modals.m_field` @@ -2563,10 +2602,10 @@ def adjust_field(record: Record, r_field: str, modals: Modals, m_field: str) -> if m is not None: setattr(record, r_field, copy.copy(m)) else: - raise InvalidDataError(f'Unfillable field: {m_field}') + raise InvalidDataError('Unfillable field: {}'.format(m_field)) -def adjust_coordinates(record: HasXY, modals: Modals, mx_field: str, my_field: str) -> None: +def adjust_coordinates(record, modals: Modals, mx_field: str, my_field: str): """ Merge `record.x` and `record.y` with `modals.mx_field` and `modals.my_field`, taking into account the value of `modals.xy_relative`. @@ -2600,7 +2639,7 @@ def adjust_coordinates(record: HasXY, modals: Modals, mx_field: str, my_field: s # TODO: Clarify the docs on the dedup_* functions -def dedup_repetition(record: HasRepetition, modals: Modals) -> None: +def dedup_repetition(record, modals: Modals): """ Deduplicate the record's repetition entry with the one in the modals. Update the one in the modals if they are different. @@ -2627,7 +2666,7 @@ def dedup_repetition(record: HasRepetition, modals: Modals) -> None: modals.repetition = record.repetition -def dedup_field(record: Record, r_field: str, modals: Modals, m_field: str) -> None: +def dedup_field(record, r_field: str, modals: Modals, m_field: str): """ Deduplicate `record.r_field` using `modals.m_field` Update the `modals.m_field` if they are different. @@ -2641,26 +2680,26 @@ def dedup_field(record: Record, r_field: str, modals: Modals, m_field: str) -> N Args: InvalidDataError: if both fields are `None` """ - rr = getattr(record, r_field) - mm = getattr(modals, m_field) - if rr is not None: + r = getattr(record, r_field) + m = getattr(modals, m_field) + if r is not None: if m_field in ('polygon_point_list', 'path_point_list'): if _USE_NUMPY: - equal = numpy.array_equal(mm, rr) + equal = numpy.array_equal(m, r) else: - equal = (mm is not None) and all(tuple(mmm) == tuple(rrr) for mmm, rrr in zip(mm, rr, strict=True)) + equal = (m is not None) and all(tuple(mm) == tuple(rr) for mm, rr in zip(m, r)) else: - equal = (mm is not None) and mm == rr + equal = (m is not None) and m == r if equal: setattr(record, r_field, None) else: - setattr(modals, m_field, rr) - elif mm is None: + setattr(modals, m_field, r) + elif m is None: raise InvalidDataError('Unfillable field') -def dedup_coordinates(record: HasXY, modals: Modals, mx_field: str, my_field: str) -> None: +def dedup_coordinates(record, modals: Modals, mx_field: str, my_field: str): """ Deduplicate `record.x` and `record.y` using `modals.mx_field` and `modals.my_field`, taking into account the value of `modals.xy_relative`. @@ -2683,18 +2722,20 @@ def dedup_coordinates(record: HasXY, modals: Modals, mx_field: str, my_field: st if modals.xy_relative: record.x -= mx setattr(modals, mx_field, record.x) - elif record.x == mx: - record.x = None else: - setattr(modals, mx_field, record.x) + if record.x == mx: + record.x = None + else: + setattr(modals, mx_field, record.x) if record.y is not None: my = getattr(modals, my_field) if modals.xy_relative: record.y -= my setattr(modals, my_field, record.y) - elif record.y == my: - record.y = None else: - setattr(modals, my_field, record.y) + if record.y == my: + record.y = None + else: + setattr(modals, my_field, record.y) diff --git a/fatamorgana/test/build_testfiles.py b/fatamorgana/test/build_testfiles.py index 50511ba..0dedfc4 100644 --- a/fatamorgana/test/build_testfiles.py +++ b/fatamorgana/test/build_testfiles.py @@ -1,10 +1,10 @@ -""" +''' Build files equivalent to the test cases used by KLayout. -""" +''' +# type: ignore -from typing import IO -from collections.abc import Callable -from pathlib import Path +from typing import Callable +from io import BufferedIOBase from . import ( @@ -12,13 +12,12 @@ from . import ( test_files_circles, test_files_ctrapezoids, test_files_trapezoids, test_files_placements, test_files_paths, test_files_modals, test_files_polygons, test_files_rectangles, test_files_empty, - test_files_texts, test_files_cells, - ) + test_files_texts, test_files_cells) -def build_file(num: str, func: Callable[[IO[bytes]], IO[bytes]]) -> None: - with Path('t' + num + '.oas').open('wb') as ff: - func(ff) +def build_file(num: str, func: Callable[[BufferedIOBase], BufferedIOBase]) -> None: + with open('t' + num + '.oas', 'wb') as f: + func(f) def write_all_files() -> None: diff --git a/fatamorgana/test/test_files_cblocks.py b/fatamorgana/test/test_files_cblocks.py index 18c76fa..93ff0ac 100644 --- a/fatamorgana/test/test_files_cblocks.py +++ b/fatamorgana/test/test_files_cblocks.py @@ -1,11 +1,17 @@ -# mypy: disable-error-code="union-attr" -from typing import IO -from io import BytesIO +# type: ignore +from typing import List, Tuple, Iterable +from itertools import chain +from io import BytesIO, BufferedIOBase +import struct + +import pytest # type: ignore +import numpy from numpy.testing import assert_equal from .utils import HEADER, FOOTER -from ..basic import write_uint, write_bstring, write_byte +from ..basic import write_uint, write_sint, read_uint, read_sint, write_bstring, write_byte, PathExtensionScheme +from ..basic import InvalidRecordError, InvalidDataError from ..main import OasisLayout @@ -26,9 +32,9 @@ def base_tests(layout: OasisLayout) -> None: assert not layout.cells[0].properties -def write_file_1(buf: IO[bytes]) -> IO[bytes]: - """ - """ +def write_file_1(buf: BufferedIOBase) -> BufferedIOBase: + ''' + ''' buf.write(HEADER) write_uint(buf, 14) # CELL record (explicit) @@ -92,105 +98,86 @@ def test_file_1() -> None: assert geometry[1].height == 610 assert geometry[1].width == 680 - assert_equal(geometry[2].point_list, [ - [-30, -360], - [480, -50], - [180, 430], - [-630, -20], - ]) + assert_equal(geometry[2].point_list, + [[-30, -360], [480, -50], [180, 430], [-630, -20]]) - assert_equal(geometry[3].point_list, [ - [-30, -400], - [450, 40], - [70, -220], - [10, 210], - [740, -20], - [0, 660], - [570, 10], - [50, 500], - [630, 20], - [10, 100], - [-810, 10], - [20, -470], - [-660, 0], - [20, -470], - [-620, 10], - [0, 610], - [610, -10], - [0, -100], - [210, 10], - [40, 820], - [-1340, 60], - [30, -1370], - ]) + assert_equal(geometry[3].point_list, + [[-30, -400], + [450, 40], + [70, -220], + [10, 210], + [740, -20], + [0, 660], + [570, 10], + [50, 500], + [630, 20], + [10, 100], + [-810, 10], + [20, -470], + [-660, 0], + [20, -470], + [-620, 10], + [0, 610], + [610, -10], + [0, -100], + [210, 10], + [40, 820], + [-1340, 60], + [30, -1370]]) - assert_equal(geometry[4].point_list, [ - [40, -760], - [490, -50], - [110, 800], - [-640, 10], - ]) + assert_equal(geometry[4].point_list, + [[40, -760], [490, -50], [110, 800], [-640, 10]]) - assert_equal(geometry[5].point_list, [ - [140, -380], - [340, -10], - [30, -100], - [-320, 20], - [130, -460], - [-480, -20], - [-210, 910], - [370, 40], - ]) + assert_equal(geometry[5].point_list, + [[140, -380], + [340, -10], + [30, -100], + [-320, 20], + [130, -460], + [-480, -20], + [-210, 910], + [370, 40]]) - assert_equal(geometry[6].point_list, [ - [720, -20], - [20, 20], - [690, 0], - [-10, 650], - [-20, 30], - [-90, -10], - [10, 70], - [470, -30], - [20, -120], - [-320, 0], - [40, -790], - [-90, -20], - [-60, 140], - [-1390, 50], - [10, 30], - ]) + assert_equal(geometry[6].point_list, + [[720, -20], + [20, 20], + [690, 0], + [-10, 650], + [-20, 30], + [-90, -10], + [10, 70], + [470, -30], + [20, -120], + [-320, 0], + [40, -790], + [-90, -20], + [-60, 140], + [-1390, 50], + [10, 30]]) - assert_equal(geometry[7].point_list, [ - [150, -830], - [-1320, 40], - [-70, 370], - [310, -30], - [10, 220], - [250, -40], - [40, -220], - [340, 10], - [-20, 290], - [-1070, 20], - [0, 230], - [1380, -60], - ]) + assert_equal(geometry[7].point_list, + [[150, -830], + [-1320, 40], + [-70, 370], + [310, -30], + [10, 220], + [250, -40], + [40, -220], + [340, 10], + [-20, 290], + [-1070, 20], + [0, 230], + [1380, -60]]) - assert_equal(geometry[8].point_list, [ - [330, 0], - [-10, 480], - [620, -20], - [-10, 330], - [-930, 60], - [0, -850], - ]) + assert_equal(geometry[8].point_list, + [[330, 0], [-10, 480], [620, -20], [-10, 330], [-930, 60], [0, -850]]) - assert_equal(geometry[9].point_list, [ - [-140, -410], - [10, -140], - [270, 0], - [130, 1030], - [-500, 50], - [10, -330], - [210, -10], - [10, -190], - ]) + assert_equal(geometry[9].point_list, + [[-140, -410], + [10, -140], + [270, 0], + [130, 1030], + [-500, 50], + [10, -330], + [210, -10], + [10, -190]]) diff --git a/fatamorgana/test/test_files_cells.py b/fatamorgana/test/test_files_cells.py index ba286a7..9239994 100644 --- a/fatamorgana/test/test_files_cells.py +++ b/fatamorgana/test/test_files_cells.py @@ -1,11 +1,14 @@ -# mypy: disable-error-code="union-attr" -from typing import IO -from io import BytesIO +# type: ignore -import pytest +from typing import List, Tuple, Iterable +from itertools import chain +from io import BytesIO, BufferedIOBase +import struct + +import pytest # type: ignore from .utils import HEADER, FOOTER -from ..basic import write_uint, write_bstring +from ..basic import write_uint, write_sint, read_uint, read_sint, write_bstring from ..basic import InvalidRecordError, InvalidDataError from ..main import OasisLayout @@ -23,10 +26,10 @@ def base_tests(layout: OasisLayout) -> None: assert not layout.layers -def write_file_1(buf: IO[bytes]) -> IO[bytes]: - """ +def write_file_1(buf: BufferedIOBase) -> BufferedIOBase: + ''' Single cell with explicit name 'XYZ' - """ + ''' buf.write(HEADER) write_uint(buf, 14) # CELL record (explicit) @@ -48,10 +51,10 @@ def test_file_1() -> None: assert not layout.cellnames -def write_file_2(buf: IO[bytes]) -> IO[bytes]: - """ +def write_file_2(buf: BufferedIOBase) -> BufferedIOBase: + ''' Two cellnames ('XYZ', 'ABC') and two cells with name references. - """ + ''' buf.write(HEADER) write_uint(buf, 3) # CELLNAME record (implicit id 0) @@ -85,10 +88,10 @@ def test_file_2() -> None: assert layout.cells[1].name == 1 -def write_file_3(buf: IO[bytes]) -> IO[bytes]: - """ +def write_file_3(buf: BufferedIOBase) -> BufferedIOBase: + ''' Invalid file, contains a mix of explicit and implicit cellnames - """ + ''' buf.write(HEADER) write_uint(buf, 4) # CELLNAME record (explicit id) @@ -113,13 +116,13 @@ def test_file_3() -> None: buf.seek(0) with pytest.raises(InvalidRecordError): - _layout = OasisLayout.read(buf) + layout = OasisLayout.read(buf) -def write_file_4(buf: IO[bytes]) -> IO[bytes]: - """ +def write_file_4(buf: BufferedIOBase) -> BufferedIOBase: + ''' Two cells referencing two names with explicit ids (unsorted) - """ + ''' buf.write(HEADER) write_uint(buf, 4) # CELLNAME record (explicit id) @@ -155,10 +158,10 @@ def test_file_4() -> None: assert layout.cells[1].name == 1 -def write_file_5(buf: IO[bytes]) -> IO[bytes]: - """ +def write_file_5(buf: BufferedIOBase) -> BufferedIOBase: + ''' Reference to non-existent cell name. - """ + ''' buf.write(HEADER) write_uint(buf, 4) # CELLNAME record (explicit id) @@ -196,10 +199,10 @@ def test_file_5() -> None: #TODO add optional error checking for this case -def write_file_6(buf: IO[bytes]) -> IO[bytes]: - """ +def write_file_6(buf: BufferedIOBase) -> BufferedIOBase: + ''' Cellname with invalid n-string. - """ + ''' buf.write(HEADER) write_uint(buf, 4) # CELLNAME record (explicit id) @@ -226,7 +229,7 @@ def test_file_6() -> None: buf.seek(0) with pytest.raises(InvalidDataError): - _layout = OasisLayout.read(buf) + layout = OasisLayout.read(buf) #base_tests(layout) #assert len(layout.cellnames) == 2 @@ -237,10 +240,10 @@ def test_file_6() -> None: #assert layout.cells[1].name == 1 -def write_file_7(buf: IO[bytes]) -> IO[bytes]: - """ +def write_file_7(buf: BufferedIOBase) -> BufferedIOBase: + ''' Unused cellname. - """ + ''' buf.write(HEADER) write_uint(buf, 4) # CELLNAME record (explicit id) diff --git a/fatamorgana/test/test_files_circles.py b/fatamorgana/test/test_files_circles.py index 39e54a0..35c7a14 100644 --- a/fatamorgana/test/test_files_circles.py +++ b/fatamorgana/test/test_files_circles.py @@ -1,9 +1,17 @@ -# mypy: disable-error-code="union-attr" -from typing import IO -from io import BytesIO +# type: ignore + +from typing import List, Tuple, Iterable +from itertools import chain +from io import BytesIO, BufferedIOBase +import struct + +import pytest # type: ignore +import numpy +from numpy.testing import assert_equal from .utils import HEADER, FOOTER -from ..basic import write_uint, write_sint, write_bstring, write_byte +from ..basic import write_uint, write_sint, read_uint, read_sint, write_bstring, write_byte, PathExtensionScheme +from ..basic import InvalidRecordError, InvalidDataError from ..main import OasisLayout @@ -24,57 +32,57 @@ def base_tests(layout: OasisLayout) -> None: assert not layout.cells[0].properties -def write_file_1(buf: IO[bytes]) -> IO[bytes]: - """ - """ +def write_file_1(buf: BufferedIOBase) -> BufferedIOBase: + ''' + ''' buf.write(HEADER) - write_uint(buf, 14) # CELL record (explicit) - write_bstring(buf, b'A') # Cell name + write_uint(buf, 14) # CELL record (explicit) + write_bstring(buf, b'A') # Cell name - write_uint(buf, 27) # CIRCLE record - write_byte(buf, 0b0011_1011) # 00rX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 150) # radius - write_sint(buf, -100) # geometry-x (absolute) - write_sint(buf, 200) # geometry-y (absolute) + write_uint(buf, 27) # CIRCLE record + write_byte(buf, 0b0011_1011) # 00rX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 150) # radius + write_sint(buf, -100) # geometry-x (absolute) + write_sint(buf, 200) # geometry-y (absolute) - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record - write_uint(buf, 27) # CIRCLE record - write_byte(buf, 0b0000_1000) # 00rX_YRDL - write_sint(buf, 400) # geometry-y (relative) + write_uint(buf, 27) # CIRCLE record + write_byte(buf, 0b0000_1000) # 00rX_YRDL + write_sint(buf, 400) # geometry-y (relative) - write_uint(buf, 27) # CIRCLE record - write_byte(buf, 0b0010_1000) # 00rX_YRDL - write_uint(buf, 0) # radius - write_sint(buf, 400) # geometry-y (relative) + write_uint(buf, 27) # CIRCLE record + write_byte(buf, 0b0010_1000) # 00rX_YRDL + write_uint(buf, 0) # radius + write_sint(buf, 400) # geometry-y (relative) - write_uint(buf, 27) # CIRCLE record - write_byte(buf, 0b0010_1000) # 00rX_YRDL - write_uint(buf, 1) # radius - write_sint(buf, 400) # geometry-y (relative) + write_uint(buf, 27) # CIRCLE record + write_byte(buf, 0b0010_1000) # 00rX_YRDL + write_uint(buf, 1) # radius + write_sint(buf, 400) # geometry-y (relative) - write_uint(buf, 27) # CIRCLE record - write_byte(buf, 0b0010_1000) # 00rX_YRDL - write_uint(buf, 6) # radius - write_sint(buf, 400) # geometry-y (relative) + write_uint(buf, 27) # CIRCLE record + write_byte(buf, 0b0010_1000) # 00rX_YRDL + write_uint(buf, 6) # radius + write_sint(buf, 400) # geometry-y (relative) - write_uint(buf, 27) # CIRCLE record - write_byte(buf, 0b0010_1000) # 00rX_YRDL - write_uint(buf, 20) # radius - write_sint(buf, 400) # geometry-y (relative) + write_uint(buf, 27) # CIRCLE record + write_byte(buf, 0b0010_1000) # 00rX_YRDL + write_uint(buf, 20) # radius + write_sint(buf, 400) # geometry-y (relative) - write_uint(buf, 27) # CIRCLE record - write_byte(buf, 0b0010_1100) # 00rX_YRDL - write_uint(buf, 100) # radius - write_sint(buf, 400) # geometry-y (relative) - write_uint(buf, 1) # repetition (3x4 matrix) - write_uint(buf, 1) # (repetition) x-dimension - write_uint(buf, 2) # (repetition) y-dimension - write_uint(buf, 400) # (repetition) x-spacing - write_uint(buf, 300) # (repetition) y-spacing + write_uint(buf, 27) # CIRCLE record + write_byte(buf, 0b0010_1100) # 00rX_YRDL + write_uint(buf, 100) # radius + write_sint(buf, 400) # geometry-y (relative) + write_uint(buf, 1) # repetition (3x4 matrix) + write_uint(buf, 1) # (repetition) x-dimension + write_uint(buf, 2) # (repetition) y-dimension + write_uint(buf, 400) # (repetition) x-spacing + write_uint(buf, 300) # (repetition) y-spacing buf.write(FOOTER) return buf diff --git a/fatamorgana/test/test_files_ctrapezoids.py b/fatamorgana/test/test_files_ctrapezoids.py index 37fc80f..b98794a 100644 --- a/fatamorgana/test/test_files_ctrapezoids.py +++ b/fatamorgana/test/test_files_ctrapezoids.py @@ -1,9 +1,17 @@ -# mypy: disable-error-code="union-attr" -from typing import IO -from io import BytesIO +# type: ignore + +from typing import List, Tuple, Iterable +from itertools import chain +from io import BytesIO, BufferedIOBase +import struct + +import pytest # type: ignore +import numpy +from numpy.testing import assert_equal from .utils import HEADER, FOOTER -from ..basic import write_uint, write_sint, write_bstring, write_byte +from ..basic import write_uint, write_sint, read_uint, read_sint, write_bstring, write_byte, PathExtensionScheme +from ..basic import InvalidRecordError, InvalidDataError from ..main import OasisLayout @@ -20,34 +28,34 @@ def base_tests(layout: OasisLayout) -> None: assert not layout.layers -def write_file_1(buf: IO[bytes]) -> IO[bytes]: - """ - """ +def write_file_1(buf: BufferedIOBase) -> BufferedIOBase: + ''' + ''' buf.write(HEADER) - write_uint(buf, 14) # CELL record (explicit) - write_bstring(buf, b'A') # Cell name + write_uint(buf, 14) # CELL record (explicit) + write_bstring(buf, b'A') # Cell name - write_uint(buf, 26) # CTRAPEZOID record - write_byte(buf, 0b1111_1011) # TWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 24) # ctrapezoid type - write_uint(buf, 100) # width - write_uint(buf, 200) # height - write_sint(buf, -100) # geometry-x (absolute) - write_sint(buf, 200) # geometry-y (absolute) + write_uint(buf, 26) # CTRAPEZOID record + write_byte(buf, 0b1111_1011) # TWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 24) # ctrapezoid type + write_uint(buf, 100) # width + write_uint(buf, 200) # height + write_sint(buf, -100) # geometry-x (absolute) + write_sint(buf, 200) # geometry-y (absolute) - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record - write_uint(buf, 26) # CTRAPEZOID record - write_byte(buf, 0b0000_1000) # TWHX_YRDL - write_sint(buf, 400) # geometry-y (relative) + write_uint(buf, 26) # CTRAPEZOID record + write_byte(buf, 0b0000_1000) # TWHX_YRDL + write_sint(buf, 400) # geometry-y (relative) - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0000_0011) # SWHX_YRDL - write_uint(buf, 2) # layer - write_uint(buf, 3) # datatype + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0000_0011) # SWHX_YRDL + write_uint(buf, 2) # layer + write_uint(buf, 3) # datatype h = [250, 100] v = [100, 250] @@ -58,34 +66,33 @@ def write_file_1(buf: IO[bytes]) -> IO[bytes]: + [0b10] * 4 + [0b01] * 2 + [0b10] * 2 - + [0b11, 0b10] - ) + + [0b11, 0b10]) - for t, (x, x_en) in enumerate(zip(wh, wh_en, strict=True)): - write_uint(buf, 26) # CTRAPEZOID record + for t, (x, x_en) in enumerate(zip(wh, wh_en)): + write_uint(buf, 26) # CTRAPEZOID record write_byte(buf, 0b1000_1011 | (x_en << 5)) # TWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, t) # ctrapezoid type + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, t) # ctrapezoid type if x_en & 0b10: - write_uint(buf, x[0]) # width + write_uint(buf, x[0]) # width if x_en & 0b01: - write_uint(buf, x[1]) # height - write_sint(buf, 400) # geometry-y (relative) + write_uint(buf, x[1]) # height + write_sint(buf, 400) # geometry-y (relative) - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0000_0011) # SWHX_YRDL - write_uint(buf, 2) # layer - write_uint(buf, 3) # datatype + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0000_0011) # SWHX_YRDL + write_uint(buf, 2) # layer + write_uint(buf, 3) # datatype - write_uint(buf, 26) # CTRAPEZOID record - write_byte(buf, 0b0000_1100) # TWHX_YRDL - write_sint(buf, 400) # geometry-y (relative) - write_uint(buf, 1) # repetition (3x4 matrix) - write_uint(buf, 1) # (repetition) x-dimension - write_uint(buf, 2) # (repetition) y-dimension - write_uint(buf, 400) # (repetition) x-spacing - write_uint(buf, 300) # (repetition) y-spacing + write_uint(buf, 26) # CTRAPEZOID record + write_byte(buf, 0b0000_1100) # TWHX_YRDL + write_sint(buf, 400) # geometry-y (relative) + write_uint(buf, 1) # repetition (3x4 matrix) + write_uint(buf, 1) # (repetition) x-dimension + write_uint(buf, 2) # (repetition) y-dimension + write_uint(buf, 400) # (repetition) x-spacing + write_uint(buf, 300) # (repetition) y-spacing buf.write(FOOTER) return buf @@ -135,12 +142,13 @@ def test_file_1() -> None: assert gg.width == [250, None][is_ctrapz], msg elif ct_type in range(22, 24) or ct_type == 25: assert gg.height == [100, None][is_ctrapz], msg - elif ct_type < 8 or 16 <= ct_type < 25 or ct_type >= 26: - assert gg.width == 250, msg - assert gg.height == 100, msg else: - assert gg.width == 100, msg - assert gg.height == 250, msg + if ct_type < 8 or 16 <= ct_type < 25 or 26 <= ct_type : + assert gg.width == 250, msg + assert gg.height == 100, msg + else: + assert gg.width == 100, msg + assert gg.height == 250, msg elif ii < 3 and ii % 2: assert gg.ctrapezoid_type == 24, msg elif ii == 55: @@ -152,48 +160,48 @@ def test_file_1() -> None: assert geometry[55].repetition.b_vector == [0, 300] -def write_file_2(buf: IO[bytes]) -> IO[bytes]: - """ - """ +def write_file_2(buf: BufferedIOBase) -> BufferedIOBase: + ''' + ''' buf.write(HEADER) write_uint(buf, 14) # CELL record (explicit) write_bstring(buf, b'A') # Cell name # Shouldn't access (undefined) height modal, despite not having a height. - write_uint(buf, 26) # CTRAPEZOID record - write_byte(buf, 0b1101_1011) # TWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 16) # ctrapezoid type - write_uint(buf, 200) # width - write_sint(buf, -100) # geometry-x (absolute) - write_sint(buf, 200) # geometry-y (absolute) + write_uint(buf, 26) # CTRAPEZOID record + write_byte(buf, 0b1101_1011) # TWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 16) # ctrapezoid type + write_uint(buf, 200) # width + write_sint(buf, -100) # geometry-x (absolute) + write_sint(buf, 200) # geometry-y (absolute) - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record - write_uint(buf, 26) # CTRAPEZOID record - write_byte(buf, 0b0000_1000) # TWHX_YRDL - write_sint(buf, 400) # geometry-y (relative) + write_uint(buf, 26) # CTRAPEZOID record + write_byte(buf, 0b0000_1000) # TWHX_YRDL + write_sint(buf, 400) # geometry-y (relative) - write_uint(buf, 14) # CELL record (explicit) - write_bstring(buf, b'B') # Cell name + write_uint(buf, 14) # CELL record (explicit) + write_bstring(buf, b'B') # Cell name # Shouldn't access (undefined) width modal, despite not having a width. - write_uint(buf, 26) # CTRAPEZOID record - write_byte(buf, 0b1011_1011) # TWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 20) # ctrapezoid type - write_uint(buf, 200) # height - write_sint(buf, -100) # geometry-x (absolute) - write_sint(buf, 200) # geometry-y (absolute) + write_uint(buf, 26) # CTRAPEZOID record + write_byte(buf, 0b1011_1011) # TWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 20) # ctrapezoid type + write_uint(buf, 200) # height + write_sint(buf, -100) # geometry-x (absolute) + write_sint(buf, 200) # geometry-y (absolute) - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record - write_uint(buf, 26) # CTRAPEZOID record - write_byte(buf, 0b0000_1000) # TWHX_YRDL - write_sint(buf, 400) # geometry-y (relative) + write_uint(buf, 26) # CTRAPEZOID record + write_byte(buf, 0b0000_1000) # TWHX_YRDL + write_sint(buf, 400) # geometry-y (relative) buf.write(FOOTER) return buf diff --git a/fatamorgana/test/test_files_empty.py b/fatamorgana/test/test_files_empty.py index a0fdbee..1fbb63c 100644 --- a/fatamorgana/test/test_files_empty.py +++ b/fatamorgana/test/test_files_empty.py @@ -1,9 +1,14 @@ -from typing import IO -from io import BytesIO +# type: ignore + +from typing import List, Tuple, Iterable +from itertools import chain +from io import BytesIO, BufferedIOBase import struct +import pytest # type: ignore + from .utils import MAGIC_BYTES, FOOTER -from ..basic import write_uint, write_bstring +from ..basic import write_uint, write_sint, read_uint, read_sint, write_bstring from ..main import OasisLayout @@ -21,12 +26,12 @@ def base_tests(layout: OasisLayout) -> None: assert not layout.layers -def write_file_1(buf: IO[bytes]) -> IO[bytes]: - """ +def write_file_1(buf: BufferedIOBase) -> BufferedIOBase: + ''' File contains one PAD record. 1000 units/micron Offset table inside START. - """ + ''' buf.write(MAGIC_BYTES) write_uint(buf, 1) # START record @@ -54,12 +59,13 @@ def test_file_1() -> None: assert layout.unit == 1000 -def write_file_2(buf: IO[bytes]) -> IO[bytes]: - """ + +def write_file_2(buf: BufferedIOBase) -> BufferedIOBase: + ''' File contains no records. 1/2 unit/micron Offset table inside START. - """ + ''' buf.write(MAGIC_BYTES) write_uint(buf, 1) # START record @@ -85,12 +91,12 @@ def test_file_2() -> None: assert layout.unit == 0.5 -def write_file_3(buf: IO[bytes]) -> IO[bytes]: - """ +def write_file_3(buf: BufferedIOBase) -> BufferedIOBase: + ''' File contains no records. 10/4 unit/micron Offset table inside START. - """ + ''' buf.write(MAGIC_BYTES) write_uint(buf, 1) # START record @@ -117,12 +123,12 @@ def test_file_3() -> None: assert layout.unit == 10 / 4 -def write_file_4(buf: IO[bytes]) -> IO[bytes]: - """ +def write_file_4(buf: BufferedIOBase) -> BufferedIOBase: + ''' File contains no records. 12.5 unit/micron (float32) Offset table inside START. - """ + ''' buf.write(MAGIC_BYTES) write_uint(buf, 1) # START record @@ -148,12 +154,12 @@ def test_file_4() -> None: assert layout.unit == 12.5 -def write_file_5(buf: IO[bytes]) -> IO[bytes]: - """ +def write_file_5(buf: BufferedIOBase) -> BufferedIOBase: + ''' File contains no records. 12.5 unit/micron (float64) Offset table inside START. - """ + ''' buf.write(MAGIC_BYTES) write_uint(buf, 1) # START record diff --git a/fatamorgana/test/test_files_layernames.py b/fatamorgana/test/test_files_layernames.py index 72e9af6..56ddc23 100644 --- a/fatamorgana/test/test_files_layernames.py +++ b/fatamorgana/test/test_files_layernames.py @@ -1,19 +1,24 @@ -from typing import IO -from collections.abc import Sequence +# type: ignore -from io import BytesIO +from typing import List, Tuple, Iterable, Sequence +from itertools import chain +from io import BytesIO, BufferedIOBase + +import pytest # type: ignore +import numpy +from numpy.testing import assert_equal from .utils import HEADER, FOOTER -from ..basic import write_uint, write_sint, write_bstring, write_byte +from ..basic import write_uint, write_sint, read_uint, read_sint, write_bstring, write_byte, PathExtensionScheme +from ..basic import InvalidRecordError, InvalidDataError from ..main import OasisLayout -LAYERS = [ - (1, 2), (1, 5), (1, 6), (1, 8), - (5, 2), (5, 5), (5, 6), (5, 8), - (6, 2), (6, 5), (6, 6), (6, 8), - (7, 2), (7, 5), (7, 6), (7, 8), - ] +LAYERS = [(1, 2), (1, 5), (1, 6), (1, 8), + (5, 2), (5, 5), (5, 6), (5, 8), + (6, 2), (6, 5), (6, 6), (6, 8), + (7, 2), (7, 5), (7, 6), (7, 8), + ] def base_tests(layout: OasisLayout) -> None: assert layout.version.string == '1.0' @@ -27,77 +32,77 @@ def base_tests(layout: OasisLayout) -> None: assert not layout.cellnames assert len(layout.cells) == 1 - assert layout.cells[0].name.string == 'A' # type: ignore + assert layout.cells[0].name.string == 'A' assert not layout.cells[0].properties -def write_names_geom(buf: IO[bytes], short: bool = False) -> IO[bytes]: - write_uint(buf, 11) # LAYERNAME record (geometry) - write_bstring(buf, b'AA') # name - write_uint(buf, 0) # all layers - write_uint(buf, 0) # all datatypes +def write_names_geom(buf: BufferedIOBase, short: bool = False) -> BufferedIOBase: + write_uint(buf, 11) # LAYERNAME record (geometry) + write_bstring(buf, b'AA') # name + write_uint(buf, 0) # all layers + write_uint(buf, 0) # all datatypes - write_uint(buf, 11) # LAYERNAME record (geometry) - write_bstring(buf, b'L5A') # name - write_uint(buf, 1) # layer <=5 - write_uint(buf, 5) # (...) - write_uint(buf, 0) # all datatypes + write_uint(buf, 11) # LAYERNAME record (geometry) + write_bstring(buf, b'L5A') # name + write_uint(buf, 1) # layer <=5 + write_uint(buf, 5) # (...) + write_uint(buf, 0) # all datatypes - write_uint(buf, 11) # LAYERNAME record (geometry) - write_bstring(buf, b'H5A') # name - write_uint(buf, 2) # layer >=5 - write_uint(buf, 5) # (...) - write_uint(buf, 0) # all datatypes + write_uint(buf, 11) # LAYERNAME record (geometry) + write_bstring(buf, b'H5A') # name + write_uint(buf, 2) # layer >=5 + write_uint(buf, 5) # (...) + write_uint(buf, 0) # all datatypes - write_uint(buf, 11) # LAYERNAME record (geometry) - write_bstring(buf, b'E5A') # name - write_uint(buf, 3) # layer ==5 - write_uint(buf, 5) # (...) - write_uint(buf, 0) # all datatypes + write_uint(buf, 11) # LAYERNAME record (geometry) + write_bstring(buf, b'E5A') # name + write_uint(buf, 3) # layer ==5 + write_uint(buf, 5) # (...) + write_uint(buf, 0) # all datatypes - write_uint(buf, 11) # LAYERNAME record (geometry) - write_bstring(buf, b'I56A') # name - write_uint(buf, 4) # layer 5 to 6 - write_uint(buf, 5) # (...) - write_uint(buf, 6) # (...) - write_uint(buf, 0) # all datatypes + write_uint(buf, 11) # LAYERNAME record (geometry) + write_bstring(buf, b'I56A') # name + write_uint(buf, 4) # layer 5 to 6 + write_uint(buf, 5) # (...) + write_uint(buf, 6) # (...) + write_uint(buf, 0) # all datatypes if short: return buf - write_uint(buf, 11) # LAYERNAME record (geometry) - write_bstring(buf, b'E5L4') # name - write_uint(buf, 3) # layer ==5 - write_uint(buf, 5) # (...) - write_uint(buf, 1) # datatype <=4 - write_uint(buf, 4) # (...) + write_uint(buf, 11) # LAYERNAME record (geometry) + write_bstring(buf, b'E5L4') # name + write_uint(buf, 3) # layer ==5 + write_uint(buf, 5) # (...) + write_uint(buf, 1) # datatype <=4 + write_uint(buf, 4) # (...) - write_uint(buf, 11) # LAYERNAME record (geometry) - write_bstring(buf, b'E5H4') # name - write_uint(buf, 3) # layer ==5 - write_uint(buf, 5) # (...) - write_uint(buf, 2) # datatype >=4 - write_uint(buf, 4) # (...) + write_uint(buf, 11) # LAYERNAME record (geometry) + write_bstring(buf, b'E5H4') # name + write_uint(buf, 3) # layer ==5 + write_uint(buf, 5) # (...) + write_uint(buf, 2) # datatype >=4 + write_uint(buf, 4) # (...) - write_uint(buf, 11) # LAYERNAME record (geometry) - write_bstring(buf, b'E5E4') # name - write_uint(buf, 3) # layer ==5 - write_uint(buf, 5) # (...) - write_uint(buf, 3) # datatype ==4 - write_uint(buf, 4) # (...) + write_uint(buf, 11) # LAYERNAME record (geometry) + write_bstring(buf, b'E5E4') # name + write_uint(buf, 3) # layer ==5 + write_uint(buf, 5) # (...) + write_uint(buf, 3) # datatype ==4 + write_uint(buf, 4) # (...) - write_uint(buf, 11) # LAYERNAME record (geometry) - write_bstring(buf, b'E5I47') # name - write_uint(buf, 3) # layer ==5 - write_uint(buf, 5) # (...) - write_uint(buf, 4) # datatype 4 to 7 - write_uint(buf, 4) # (...) - write_uint(buf, 7) # (...) + write_uint(buf, 11) # LAYERNAME record (geometry) + write_bstring(buf, b'E5I47') # name + write_uint(buf, 3) # layer ==5 + write_uint(buf, 5) # (...) + write_uint(buf, 4) # datatype 4 to 7 + write_uint(buf, 4) # (...) + write_uint(buf, 7) # (...) return buf -def write_names_text(buf: IO[bytes], prefix: bytes = b'') -> IO[bytes]: +def write_names_text(buf: BufferedIOBase, prefix: bytes = b'') -> BufferedIOBase: write_uint(buf, 12) # LAYERNAME record (geometry) write_bstring(buf, prefix + b'AA') # name write_uint(buf, 0) # all layers @@ -122,14 +127,14 @@ def write_names_text(buf: IO[bytes], prefix: bytes = b'') -> IO[bytes]: write_uint(buf, 0) # all datatypes write_uint(buf, 12) # LAYERNAME record (geometry) - write_bstring(buf, prefix + b'I56A') # name + write_bstring(buf, prefix + b'I56A') # name write_uint(buf, 4) # layer 5 to 6 write_uint(buf, 5) # (...) write_uint(buf, 6) # (...) write_uint(buf, 0) # all datatypes return buf -def write_geom(buf: IO[bytes]) -> IO[bytes]: +def write_geom(buf: BufferedIOBase) -> BufferedIOBase: for ll, dt in LAYERS: write_uint(buf, 27) # CIRCLE record write_byte(buf, 0b0011_1011) # 00rX_YRDL @@ -141,7 +146,7 @@ def write_geom(buf: IO[bytes]) -> IO[bytes]: return buf -def write_text(buf: IO[bytes]) -> IO[bytes]: +def write_text(buf: BufferedIOBase) -> BufferedIOBase: for ll, dt in LAYERS: write_uint(buf, 19) # TEXT record write_byte(buf, 0b0101_1011) # 0CNX_YRTL @@ -155,8 +160,7 @@ def write_text(buf: IO[bytes]) -> IO[bytes]: def name_test(layers: Sequence, is_textlayer: bool) -> None: for ii, nn in enumerate(layers): - msg = f'Fail on layername {ii}' - assert is_textlayer == nn.is_textlayer, msg + assert is_textlayer == nn.is_textlayer, f'Fail on layername {ii}' assert nn.nstring.string == ['AA', 'L5A', 'H5A', 'E5A', 'I56A', 'E5L4', 'E5H4', 'E5E4', 'E5I47'][ii], msg @@ -168,8 +172,7 @@ def name_test(layers: Sequence, is_textlayer: bool) -> None: def name_test_text(layers: Sequence) -> None: for ii, nn in enumerate(layers): - msg = f'Fail on layername {ii}' - assert nn.is_textlayer, msg + assert nn.is_textlayer, f'Fail on layername {ii}' assert nn.nstring.string == ['TAA', 'TL5A', 'TH5A', 'TE5A', 'TI56A'][ii], msg assert nn.layer_interval[0] == [None, None, 5, 5, 5][ii], msg @@ -206,9 +209,9 @@ def elem_test_text(geometry: Sequence) -> None: assert not gg.properties, msg -def write_file_1(buf: IO[bytes]) -> IO[bytes]: - """ - """ +def write_file_1(buf: BufferedIOBase) -> BufferedIOBase: + ''' + ''' buf.write(HEADER) write_names_geom(buf) @@ -236,9 +239,9 @@ def test_file_1() -> None: name_test(layout.layers, is_textlayer=False) -def write_file_2(buf: IO[bytes]) -> IO[bytes]: - """ - """ +def write_file_2(buf: BufferedIOBase) -> BufferedIOBase: + ''' + ''' buf.write(HEADER) write_names_text(buf) @@ -266,9 +269,9 @@ def test_file_2() -> None: name_test(layout.layers, is_textlayer=True) -def write_file_3(buf: IO[bytes]) -> IO[bytes]: - """ - """ +def write_file_3(buf: BufferedIOBase) -> BufferedIOBase: + ''' + ''' buf.write(HEADER) write_names_text(buf, prefix=b'T') write_names_geom(buf, short=True) @@ -282,9 +285,9 @@ def write_file_3(buf: IO[bytes]) -> IO[bytes]: return buf -def write_file_4(buf: IO[bytes]) -> IO[bytes]: - """ - """ +def write_file_4(buf: BufferedIOBase) -> BufferedIOBase: + ''' + ''' buf.write(HEADER) write_uint(buf, 14) # CELL record (explicit) diff --git a/fatamorgana/test/test_files_modals.py b/fatamorgana/test/test_files_modals.py index fddc576..0021bf1 100644 --- a/fatamorgana/test/test_files_modals.py +++ b/fatamorgana/test/test_files_modals.py @@ -1,9 +1,17 @@ -# mypy: disable-error-code="union-attr" -from typing import IO -from io import BytesIO +# type: ignore + +from typing import List, Tuple, Iterable +from itertools import chain +from io import BytesIO, BufferedIOBase +import struct + +import pytest # type: ignore +import numpy +from numpy.testing import assert_equal from .utils import HEADER, FOOTER -from ..basic import write_uint, write_sint, write_bstring, write_byte +from ..basic import write_uint, write_sint, read_uint, read_sint, write_bstring, write_byte, PathExtensionScheme +from ..basic import InvalidRecordError, InvalidDataError from ..main import OasisLayout @@ -20,156 +28,156 @@ def base_tests(layout: OasisLayout) -> None: assert not layout.layers -def write_file_1(buf: IO[bytes]) -> IO[bytes]: - """ - """ +def write_file_1(buf: BufferedIOBase) -> BufferedIOBase: + ''' + ''' buf.write(HEADER) - write_uint(buf, 14) # CELL record (explicit) - write_bstring(buf, b'A') # Cell name + write_uint(buf, 14) # CELL record (explicit) + write_bstring(buf, b'A') # Cell name # RECTANGLE 0 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0110_0011) # SWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 10) # width - write_uint(buf, 20) # height + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0110_0011) # SWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 10) # width + write_uint(buf, 20) # height # TEXT 1 - write_uint(buf, 19) # TEXT record - write_byte(buf, 0b0100_0011) # 0CNX_YRTL - write_bstring(buf, b'A') # text string - write_uint(buf, 2) # layer - write_uint(buf, 1) # datatype + write_uint(buf, 19) # TEXT record + write_byte(buf, 0b0100_0011) # 0CNX_YRTL + write_bstring(buf, b'A') # text string + write_uint(buf, 2) # layer + write_uint(buf, 1) # datatype # RECTANGLE 2 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0001_1000) # SWHX_YRDL - write_sint(buf, 100) # geometry-x (absolute) - write_sint(buf, -100) # geometry-y (absolute) + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0001_1000) # SWHX_YRDL + write_sint(buf, 100) # geometry-x (absolute) + write_sint(buf, -100) # geometry-y (absolute) # TEXT 3 - write_uint(buf, 19) # TEXT record - write_byte(buf, 0b0001_1000) # 0CNX_YRTL - write_sint(buf, 100) # text-x (absolute) - write_sint(buf, -100) # text-y (absolute) + write_uint(buf, 19) # TEXT record + write_byte(buf, 0b0001_1000) # 0CNX_YRTL + write_sint(buf, 100) # text-x (absolute) + write_sint(buf, -100) # text-y (absolute) # RECTANGLE 4 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0001_1000) # SWHX_YRDL - write_sint(buf, 200) # geometry-x (absolute) - write_sint(buf, -200) # geometry-y (absolute) + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0001_1000) # SWHX_YRDL + write_sint(buf, 200) # geometry-x (absolute) + write_sint(buf, -200) # geometry-y (absolute) # TEXT 5 - write_uint(buf, 19) # TEXT record - write_byte(buf, 0b0001_1000) # 0CNX_YRTL - write_sint(buf, 200) # text-x (absolute) - write_sint(buf, -200) # text-y (absolute) + write_uint(buf, 19) # TEXT record + write_byte(buf, 0b0001_1000) # 0CNX_YRTL + write_sint(buf, 200) # text-x (absolute) + write_sint(buf, -200) # text-y (absolute) - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record # RECTANGLE 6 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0001_1000) # SWHX_YRDL - write_sint(buf, 100) # geometry-x (relative) - write_sint(buf, -100) # geometry-y (relative) + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0001_1000) # SWHX_YRDL + write_sint(buf, 100) # geometry-x (relative) + write_sint(buf, -100) # geometry-y (relative) # TEXT 7 - write_uint(buf, 19) # TEXT record - write_byte(buf, 0b0001_1000) # 0CNX_YRTL - write_sint(buf, 100) # text-x (relative) - write_sint(buf, -100) # text-y (relative) + write_uint(buf, 19) # TEXT record + write_byte(buf, 0b0001_1000) # 0CNX_YRTL + write_sint(buf, 100) # text-x (relative) + write_sint(buf, -100) # text-y (relative) - write_uint(buf, 14) # CELL record (explicit) - write_bstring(buf, b'B') # Cell name + write_uint(buf, 14) # CELL record (explicit) + write_bstring(buf, b'B') # Cell name # RECTANGLE 0 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0110_0011) # SWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 20) # width - write_uint(buf, 10) # height + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0110_0011) # SWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 20) # width + write_uint(buf, 10) # height # TEXT 1 - write_uint(buf, 19) # TEXT record - write_byte(buf, 0b0100_0011) # 0CNX_YRTL - write_bstring(buf, b'B') # text string - write_uint(buf, 2) # layer - write_uint(buf, 1) # datatype + write_uint(buf, 19) # TEXT record + write_byte(buf, 0b0100_0011) # 0CNX_YRTL + write_bstring(buf, b'B') # text string + write_uint(buf, 2) # layer + write_uint(buf, 1) # datatype # RECTANGLE 2 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0001_1000) # SWHX_YRDL - write_sint(buf, 100) # geometry-x (absolute) - write_sint(buf, 100) # geometry-y (absolute) + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0001_1000) # SWHX_YRDL + write_sint(buf, 100) # geometry-x (absolute) + write_sint(buf, 100) # geometry-y (absolute) # TEXT 3 - write_uint(buf, 19) # TEXT record - write_byte(buf, 0b0001_1000) # 0CNX_YRTL - write_sint(buf, 100) # text-x (absolute) - write_sint(buf, 100) # text-y (absolute) + write_uint(buf, 19) # TEXT record + write_byte(buf, 0b0001_1000) # 0CNX_YRTL + write_sint(buf, 100) # text-x (absolute) + write_sint(buf, 100) # text-y (absolute) # RECTANGLE 4 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0001_1000) # SWHX_YRDL - write_sint(buf, 200) # geometry-x (absolute) - write_sint(buf, 200) # geometry-y (absolute) + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0001_1000) # SWHX_YRDL + write_sint(buf, 200) # geometry-x (absolute) + write_sint(buf, 200) # geometry-y (absolute) # TEXT 5 - write_uint(buf, 19) # TEXT record - write_byte(buf, 0b0001_1000) # 0CNX_YRTL - write_sint(buf, 200) # text-x (absolute) - write_sint(buf, 200) # text-y (absolute) + write_uint(buf, 19) # TEXT record + write_byte(buf, 0b0001_1000) # 0CNX_YRTL + write_sint(buf, 200) # text-x (absolute) + write_sint(buf, 200) # text-y (absolute) - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record # RECTANGLE 6 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0001_1000) # SWHX_YRDL - write_sint(buf, 100) # geometry-x (relative) - write_sint(buf, 100) # geometry-y (relative) + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0001_1000) # SWHX_YRDL + write_sint(buf, 100) # geometry-x (relative) + write_sint(buf, 100) # geometry-y (relative) # TEXT 7 - write_uint(buf, 19) # TEXT record - write_byte(buf, 0b0001_1000) # 0CNX_YRTL - write_sint(buf, 100) # text-x (relative) - write_sint(buf, 100) # text-y (relative) + write_uint(buf, 19) # TEXT record + write_byte(buf, 0b0001_1000) # 0CNX_YRTL + write_sint(buf, 100) # text-x (relative) + write_sint(buf, 100) # text-y (relative) # PLACEMENT 0 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b1000_0000) # CNXY_RAAF - write_bstring(buf, b'A') # Cell reference + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b1000_0000) # CNXY_RAAF + write_bstring(buf, b'A') # Cell reference # PLACEMENT 1 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0011_0000) # CNXY_RAAF - write_sint(buf, 50) # placement-x (relative) - write_sint(buf, 50) # placement-y (relative) + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0011_0000) # CNXY_RAAF + write_sint(buf, 50) # placement-x (relative) + write_sint(buf, 50) # placement-y (relative) - write_uint(buf, 14) # CELL record (explicit) - write_bstring(buf, b'TOP') # Cell name + write_uint(buf, 14) # CELL record (explicit) + write_bstring(buf, b'TOP') # Cell name # PLACEMENT 0 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b1000_0000) # CNXY_RAAF - write_bstring(buf, b'B') # Cell reference + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b1000_0000) # CNXY_RAAF + write_bstring(buf, b'B') # Cell reference # RECTANGLE 0 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0110_0011) # SWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 50) # width - write_uint(buf, 5) # height + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0110_0011) # SWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 50) # width + write_uint(buf, 5) # height # TEXT 1 - write_uint(buf, 19) # TEXT record - write_byte(buf, 0b0100_0011) # 0CNX_YRTL - write_bstring(buf, b'TOP') # text string - write_uint(buf, 2) # layer - write_uint(buf, 1) # datatype + write_uint(buf, 19) # TEXT record + write_byte(buf, 0b0100_0011) # 0CNX_YRTL + write_bstring(buf, b'TOP') # text string + write_uint(buf, 2) # layer + write_uint(buf, 1) # datatype buf.write(FOOTER) return buf diff --git a/fatamorgana/test/test_files_paths.py b/fatamorgana/test/test_files_paths.py index 1dc1def..a359817 100644 --- a/fatamorgana/test/test_files_paths.py +++ b/fatamorgana/test/test_files_paths.py @@ -1,11 +1,17 @@ -# mypy: disable-error-code="union-attr" -from typing import IO -from io import BytesIO +# type: ignore +from typing import List, Tuple, Iterable +from itertools import chain +from io import BytesIO, BufferedIOBase +import struct + +import pytest # type: ignore +import numpy from numpy.testing import assert_equal from .utils import HEADER, FOOTER -from ..basic import write_uint, write_sint, write_bstring, write_byte, PathExtensionScheme +from ..basic import write_uint, write_sint, read_uint, read_sint, write_bstring, write_byte, PathExtensionScheme +from ..basic import InvalidRecordError, InvalidDataError from ..main import OasisLayout @@ -26,113 +32,113 @@ def base_tests(layout: OasisLayout) -> None: assert not layout.cells[0].properties -def write_file_1(buf: IO[bytes]) -> IO[bytes]: - """ - """ +def write_file_1(buf: BufferedIOBase) -> BufferedIOBase: + ''' + ''' buf.write(HEADER) - write_uint(buf, 14) # CELL record (explicit) - write_bstring(buf, b'ABC') # Cell name + write_uint(buf, 14) # CELL record (explicit) + write_bstring(buf, b'ABC') # Cell name # PATH 0 - write_uint(buf, 22) # PATH record - write_byte(buf, 0b1111_1011) # EWPX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 10) # half-width - write_byte(buf, 0b0000_1111) # extension-scheme 0000_SSEE - write_sint(buf, 5) # (extension-scheme) start - write_sint(buf, -5) # (extension-scheme) end - write_uint(buf, 0) # pointlist: 1-delta, horiz-fisrt - write_uint(buf, 3) # (pointlist) dimension - write_sint(buf, 150) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, -50) # (pointlist) - write_sint(buf, 0) # geometry-x (absolute) - write_sint(buf, 100) # geometry-y (absolute) + write_uint(buf, 22) # PATH record + write_byte(buf, 0b1111_1011) # EWPX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 10) # half-width + write_byte(buf, 0b0000_1111) # extension-scheme 0000_SSEE + write_sint(buf, 5) # (extension-scheme) start + write_sint(buf, -5) # (extension-scheme) end + write_uint(buf, 0) # pointlist: 1-delta, horiz-fisrt + write_uint(buf, 3) # (pointlist) dimension + write_sint(buf, 150) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, -50) # (pointlist) + write_sint(buf, 0) # geometry-x (absolute) + write_sint(buf, 100) # geometry-y (absolute) - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record # PATH 1 - write_uint(buf, 22) # PATH record - write_byte(buf, 0b1110_1011) # EWPX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 10) # half-width - write_byte(buf, 0b0000_0000) # extension-scheme 0000_SSEE - write_uint(buf, 0) # pointlist: 1-delta, horiz-fisrt - write_uint(buf, 3) # (pointlist) dimension - write_sint(buf, 150) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, -50) # (pointlist) - write_sint(buf, 200) # geometry-y (relative) + write_uint(buf, 22) # PATH record + write_byte(buf, 0b1110_1011) # EWPX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 10) # half-width + write_byte(buf, 0b0000_0000) # extension-scheme 0000_SSEE + write_uint(buf, 0) # pointlist: 1-delta, horiz-fisrt + write_uint(buf, 3) # (pointlist) dimension + write_sint(buf, 150) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, -50) # (pointlist) + write_sint(buf, 200) # geometry-y (relative) # PATH 2 - write_uint(buf, 22) # PATH record - write_byte(buf, 0b1110_1001) # EWPX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 10) # half-width - write_byte(buf, 0b0000_0100) # extension-scheme 0000_SSEE - write_uint(buf, 0) # pointlist: 1-delta, horiz-fisrt - write_uint(buf, 3) # (pointlist) dimension - write_sint(buf, 150) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, -50) # (pointlist) - write_sint(buf, 200) # geometry-y (relative) + write_uint(buf, 22) # PATH record + write_byte(buf, 0b1110_1001) # EWPX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 10) # half-width + write_byte(buf, 0b0000_0100) # extension-scheme 0000_SSEE + write_uint(buf, 0) # pointlist: 1-delta, horiz-fisrt + write_uint(buf, 3) # (pointlist) dimension + write_sint(buf, 150) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, -50) # (pointlist) + write_sint(buf, 200) # geometry-y (relative) # PATH 3 - write_uint(buf, 22) # PATH record - write_byte(buf, 0b1110_1010) # EWPX_YRDL - write_uint(buf, 2) # datatype - write_uint(buf, 12) # half-width - write_byte(buf, 0b0000_0101) # extension-scheme 0000_SSEE - write_uint(buf, 0) # pointlist: 1-delta, horiz-fisrt - write_uint(buf, 3) # (pointlist) dimension - write_sint(buf, 150) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, -50) # (pointlist) - write_sint(buf, 200) # geometry-y (relative) + write_uint(buf, 22) # PATH record + write_byte(buf, 0b1110_1010) # EWPX_YRDL + write_uint(buf, 2) # datatype + write_uint(buf, 12) # half-width + write_byte(buf, 0b0000_0101) # extension-scheme 0000_SSEE + write_uint(buf, 0) # pointlist: 1-delta, horiz-fisrt + write_uint(buf, 3) # (pointlist) dimension + write_sint(buf, 150) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, -50) # (pointlist) + write_sint(buf, 200) # geometry-y (relative) # PATH 4 - write_uint(buf, 22) # PATH record - write_byte(buf, 0b1010_1011) # EWPX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_byte(buf, 0b0000_1010) # extension-scheme 0000_SSEE - write_uint(buf, 0) # pointlist: 1-delta, horiz-fisrt - write_uint(buf, 3) # (pointlist) dimension - write_sint(buf, 150) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, -50) # (pointlist) - write_sint(buf, 200) # geometry-y (relative) + write_uint(buf, 22) # PATH record + write_byte(buf, 0b1010_1011) # EWPX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_byte(buf, 0b0000_1010) # extension-scheme 0000_SSEE + write_uint(buf, 0) # pointlist: 1-delta, horiz-fisrt + write_uint(buf, 3) # (pointlist) dimension + write_sint(buf, 150) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, -50) # (pointlist) + write_sint(buf, 200) # geometry-y (relative) # PATH 5 - write_uint(buf, 22) # PATH record - write_byte(buf, 0b0000_1011) # EWPX_YRDL - write_uint(buf, 2) # layer - write_uint(buf, 3) # datatype - write_sint(buf, 200) # geometry-y (relative) + write_uint(buf, 22) # PATH record + write_byte(buf, 0b0000_1011) # EWPX_YRDL + write_uint(buf, 2) # layer + write_uint(buf, 3) # datatype + write_sint(buf, 200) # geometry-y (relative) # PATH 6 - write_uint(buf, 22) # PATH record - write_byte(buf, 0b0000_1111) # EWPX_YRDL - write_uint(buf, 2) # layer - write_uint(buf, 3) # datatype - write_sint(buf, 200) # geometry-y (relative) - write_uint(buf, 1) # repetition (3x4 matrix) - write_uint(buf, 1) # (repetition) x-dimension - write_uint(buf, 2) # (repetition) y-dimension - write_uint(buf, 200) # (repetition) x-spacing - write_uint(buf, 300) # (repetition) y-spacing + write_uint(buf, 22) # PATH record + write_byte(buf, 0b0000_1111) # EWPX_YRDL + write_uint(buf, 2) # layer + write_uint(buf, 3) # datatype + write_sint(buf, 200) # geometry-y (relative) + write_uint(buf, 1) # repetition (3x4 matrix) + write_uint(buf, 1) # (repetition) x-dimension + write_uint(buf, 2) # (repetition) y-dimension + write_uint(buf, 200) # (repetition) x-spacing + write_uint(buf, 300) # (repetition) y-spacing - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record # PATH 7 - write_uint(buf, 22) # PATH record - write_byte(buf, 0b0001_0101) # EWPX_YRDL - write_uint(buf, 1) # layer - write_sint(buf, 1000) # geometry-x (relative) - write_uint(buf, 0) # repetition (reuse) + write_uint(buf, 22) # PATH record + write_byte(buf, 0b0001_0101) # EWPX_YRDL + write_uint(buf, 1) # layer + write_sint(buf, 1000) # geometry-x (relative) + write_uint(buf, 0) # repetition (reuse) buf.write(FOOTER) return buf @@ -182,7 +188,7 @@ def test_file_1() -> None: else: assert gg.half_width == 12, msg - assert len(gg.point_list) == 3, msg # type: ignore + assert len(gg.point_list) == 3, msg assert_equal(gg.point_list, [[150, 0], [0, 50], [-50, 0]], err_msg=msg) if ii >= 4: diff --git a/fatamorgana/test/test_files_placements.py b/fatamorgana/test/test_files_placements.py index ce4c8aa..5059be9 100644 --- a/fatamorgana/test/test_files_placements.py +++ b/fatamorgana/test/test_files_placements.py @@ -1,12 +1,17 @@ -# mypy: disable-error-code="union-attr" -from typing import IO, cast -from io import BytesIO +# type: ignore +from typing import List, Tuple, Iterable +from itertools import chain +from io import BytesIO, BufferedIOBase +import struct + +import pytest # type: ignore +import numpy from numpy.testing import assert_equal from .utils import HEADER, FOOTER -from ..basic import write_uint, write_sint, write_bstring, write_byte, write_float32, write_float64 -from ..records import Rectangle +from ..basic import write_uint, write_sint, read_uint, read_sint, write_bstring, write_byte, PathExtensionScheme +from ..basic import InvalidRecordError, InvalidDataError, write_float32, write_float64 from ..main import OasisLayout @@ -22,136 +27,136 @@ def base_tests(layout: OasisLayout) -> None: assert not layout.layers -def write_rectangle(buf: IO[bytes], pos: tuple[int, int] = (300, -400)) -> None: - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0111_1011) # SWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 100) # width - write_uint(buf, 200) # height - write_sint(buf, pos[0]) # geometry-x (absolute) - write_sint(buf, pos[1]) # geometry-y (absolute) +def write_rectangle(buf: BufferedIOBase, pos: Tuple[int, int] = (300, -400)) -> None: + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0111_1011) # SWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 100) # width + write_uint(buf, 200) # height + write_sint(buf, pos[0]) # geometry-x (absolute) + write_sint(buf, pos[1]) # geometry-y (absolute) -def write_file_1(buf: IO[bytes]) -> IO[bytes]: - """ - """ +def write_file_1(buf: BufferedIOBase) -> BufferedIOBase: + ''' + ''' buf.write(HEADER) - write_uint(buf, 14) # CELL record (explicit) - write_bstring(buf, b'A') # Cell name + write_uint(buf, 14) # CELL record (explicit) + write_bstring(buf, b'A') # Cell name write_rectangle(buf) - write_uint(buf, 14) # CELL record (explicit) + write_uint(buf, 14) # CELL record (explicit) write_bstring(buf, b'TOP') # Cell name - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record # PLACEMENT 0 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b1011_0000) # CNXY_RAAF - write_bstring(buf, b'A') # cell reference - write_sint(buf, -300) # placement-x (relative) - write_sint(buf, 400) # placement-y (relative) + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b1011_0000) # CNXY_RAAF + write_bstring(buf, b'A') # cell reference + write_sint(buf, -300) # placement-x (relative) + write_sint(buf, 400) # placement-y (relative) # PLACEMENT 1 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0011_0000) # CNXY_RAAF - write_sint(buf, 0) # placement-x (relative) - write_sint(buf, 400) # placement-y (relative) + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0011_0000) # CNXY_RAAF + write_sint(buf, 0) # placement-x (relative) + write_sint(buf, 400) # placement-y (relative) # PLACEMENT 2 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0001_0000) # CNXY_RAAF - write_sint(buf, 400) # placement-y (relative) + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0001_0000) # CNXY_RAAF + write_sint(buf, 400) # placement-y (relative) # PLACEMENT 3 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0010_0000) # CNXY_RAAF - write_sint(buf, 300) # placement-x (relative) + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0010_0000) # CNXY_RAAF + write_sint(buf, 300) # placement-x (relative) - write_uint(buf, 15) # XYABSOLUTE record + write_uint(buf, 15) # XYABSOLUTE record # PLACEMENT 4 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0011_0001) # CNXY_RAAF - write_sint(buf, 700) # placement-x (absolute) - write_sint(buf, 400) # placement-y (absolute) + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0011_0001) # CNXY_RAAF + write_sint(buf, 700) # placement-x (absolute) + write_sint(buf, 400) # placement-y (absolute) - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record # PLACEMENT 5 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0001_0010) # CNXY_RAAF - write_sint(buf, 1000) # placement-y (relative) + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0001_0010) # CNXY_RAAF + write_sint(buf, 1000) # placement-y (relative) # PLACEMENT 6 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0001_0011) # CNXY_RAAF - write_sint(buf, 1000) # placement-y (relative) + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0001_0011) # CNXY_RAAF + write_sint(buf, 1000) # placement-y (relative) - write_uint(buf, 15) # XYABSOLUTE record + write_uint(buf, 15) # XYABSOLUTE record # PLACEMENT 7 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0011_1111) # CNXY_RAAF - write_sint(buf, 2000) # placement-x (absolute) - write_sint(buf, 0) # placement-y (absolute) - write_uint(buf, 1) # repetition (3x4 matrix) - write_uint(buf, 1) # (repetition) x-dimension - write_uint(buf, 2) # (repetition) y-dimension - write_uint(buf, 300) # (repetition) x-spacing - write_uint(buf, 300) # (repetition) y-spacing + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0011_1111) # CNXY_RAAF + write_sint(buf, 2000) # placement-x (absolute) + write_sint(buf, 0) # placement-y (absolute) + write_uint(buf, 1) # repetition (3x4 matrix) + write_uint(buf, 1) # (repetition) x-dimension + write_uint(buf, 2) # (repetition) y-dimension + write_uint(buf, 300) # (repetition) x-spacing + write_uint(buf, 300) # (repetition) y-spacing - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record # PLACEMENT 8 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0011_1111) # CNXY_RAAF - write_sint(buf, 2000) # placement-x (relative) - write_sint(buf, 0) # placement-y (relative) - write_uint(buf, 0) # repetition (reuse) + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0011_1111) # CNXY_RAAF + write_sint(buf, 2000) # placement-x (relative) + write_sint(buf, 0) # placement-y (relative) + write_uint(buf, 0) # repetition (reuse) # PLACEMENT 9 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0011_1111) # CNXY_RAAF - write_sint(buf, 2000) # placement-x (relative) - write_sint(buf, 0) # placement-y (relative) - write_uint(buf, 2) # repetition (3 cols.) - write_uint(buf, 1) # (repetition) count - write_uint(buf, 320) # (repetition) spacing + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0011_1111) # CNXY_RAAF + write_sint(buf, 2000) # placement-x (relative) + write_sint(buf, 0) # placement-y (relative) + write_uint(buf, 2) # repetition (3 cols.) + write_uint(buf, 1) # (repetition) count + write_uint(buf, 320) # (repetition) spacing # PLACEMENT 10 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0011_1111) # CNXY_RAAF - write_sint(buf, 2000) # placement-x (relative) - write_sint(buf, 0) # placement-y (relative) - write_uint(buf, 3) # repetition (4 rows) - write_uint(buf, 2) # (repetition) count - write_uint(buf, 310) # (repetition) spacing + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0011_1111) # CNXY_RAAF + write_sint(buf, 2000) # placement-x (relative) + write_sint(buf, 0) # placement-y (relative) + write_uint(buf, 3) # repetition (4 rows) + write_uint(buf, 2) # (repetition) count + write_uint(buf, 310) # (repetition) spacing # PLACEMENT 11 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0011_1111) # CNXY_RAAF - write_sint(buf, 2000) # placement-x (relative) - write_sint(buf, 0) # placement-y (relative) - write_uint(buf, 4) # repetition (4 arbitrary cols.) - write_uint(buf, 2) # (repetition) dimension - write_uint(buf, 320) # (repetition) spacing - write_uint(buf, 330) # (repetition) spacing - write_uint(buf, 340) # (repetition) spacing + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0011_1111) # CNXY_RAAF + write_sint(buf, 2000) # placement-x (relative) + write_sint(buf, 0) # placement-y (relative) + write_uint(buf, 4) # repetition (4 arbitrary cols.) + write_uint(buf, 2) # (repetition) dimension + write_uint(buf, 320) # (repetition) spacing + write_uint(buf, 330) # (repetition) spacing + write_uint(buf, 340) # (repetition) spacing # PLACEMENT 12 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0011_1111) # CNXY_RAAF - write_sint(buf, 2000) # placement-x (relative) - write_sint(buf, 0) # placement-y (relative) - write_uint(buf, 8) # repetition (3x4 matrix, arbitrary vectors) - write_uint(buf, 1) # (repetition) n-dimension - write_uint(buf, 2) # (repetition) m-dimension - write_uint(buf, 310 << 2 | 0b01) # (repetition) n-displacement g-delta: (310, 320) - write_sint(buf, 320) # (repetition g-delta) + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0011_1111) # CNXY_RAAF + write_sint(buf, 2000) # placement-x (relative) + write_sint(buf, 0) # placement-y (relative) + write_uint(buf, 8) # repetition (3x4 matrix, arbitrary vectors) + write_uint(buf, 1) # (repetition) n-dimension + write_uint(buf, 2) # (repetition) m-dimension + write_uint(buf, 310 << 2 | 0b01) # (repetition) n-displacement g-delta: (310, 320) + write_sint(buf, 320) # (repetition g-delta) write_uint(buf, 330 << 4 | 0b1010) # (repetition) m-displacement g-delta: 330-northwest (-330, 330) buf.write(FOOTER) @@ -174,7 +179,7 @@ def test_file_1() -> None: assert not layout.cells[1].properties assert not layout.cells[1].geometry - geometry = cast(list[Rectangle], layout.cells[0].geometry) + geometry = layout.cells[0].geometry assert len(geometry) == 1 assert geometry[0].layer == 1 assert geometry[0].datatype == 2 @@ -202,19 +207,19 @@ def test_file_1() -> None: if ii < 3: assert pp.y == 400 * (ii + 1), msg - elif ii >= 7: + elif 7 <= ii: assert pp.y == 0, msg if ii < 4 or ii == 5: - assert not bool(pp.flip), msg + assert pp.flip == False, msg else: - assert bool(pp.flip), msg + assert pp.flip == True, msg if ii < 5: assert pp.angle == 0, msg elif ii in (5, 6): assert pp.angle == 90, msg - elif ii >= 7: + elif 7 <= ii: assert pp.angle == 270, msg if ii < 7: @@ -249,9 +254,9 @@ def test_file_1() -> None: assert placements[12].repetition.b_vector == [-330, 330] -def write_file_common(buf: IO[bytes], variant: int) -> IO[bytes]: - """ - """ +def write_file_common(buf: BufferedIOBase, variant: int) -> BufferedIOBase: + ''' + ''' assert variant in (2, 3, 5, 7), 'Error in test definition!' buf.write(HEADER) @@ -281,49 +286,49 @@ def write_file_common(buf: IO[bytes], variant: int) -> IO[bytes]: # PLACEMENT 0 write_uint(buf, 17) # PLACEMENT (simple) if variant == 2: - write_byte(buf, 0b1011_0000) # CNXY_RAAF - write_bstring(buf, b'A') # cell reference + write_byte(buf, 0b1011_0000) # CNXY_RAAF + write_bstring(buf, b'A') # cell reference else: - write_byte(buf, 0b1111_0000) # CNXY_RAAF - write_uint(buf, 0) # cell reference - write_sint(buf, -300) # placement-x (relative) - write_sint(buf, 400) # placement-y (relative) + write_byte(buf, 0b1111_0000) # CNXY_RAAF + write_uint(buf, 0) # cell reference + write_sint(buf, -300) # placement-x (relative) + write_sint(buf, 400) # placement-y (relative) # PLACEMENT 1 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0011_0000) # CNXY_RAAF - write_sint(buf, 0) # placement-x (relative) - write_sint(buf, 400) # placement-y (relative) + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0011_0000) # CNXY_RAAF + write_sint(buf, 0) # placement-x (relative) + write_sint(buf, 400) # placement-y (relative) # PLACEMENT 2 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0001_0000) # CNXY_RAAF - write_sint(buf, 400) # placement-y (relative) + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0001_0000) # CNXY_RAAF + write_sint(buf, 400) # placement-y (relative) # PLACEMENT 3 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0010_0000) # CNXY_RAAF - write_sint(buf, 300) # placement-x (relative) + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0010_0000) # CNXY_RAAF + write_sint(buf, 300) # placement-x (relative) - write_uint(buf, 15) # XYABSOLUTE record + write_uint(buf, 15) # XYABSOLUTE record # PLACEMENT 4 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0011_0001) # CNXY_RAAF - write_sint(buf, 700) # placement-x (absolute) - write_sint(buf, 400) # placement-y (absolute) + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0011_0001) # CNXY_RAAF + write_sint(buf, 700) # placement-x (absolute) + write_sint(buf, 400) # placement-y (absolute) - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record # PLACEMENT 5 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0001_0010) # CNXY_RAAF - write_sint(buf, 1000) # placement-y (relative) + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0001_0010) # CNXY_RAAF + write_sint(buf, 1000) # placement-y (relative) # PLACEMENT 6 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0001_0011) # CNXY_RAAF - write_sint(buf, 1000) # placement-y (relative) + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0001_0011) # CNXY_RAAF + write_sint(buf, 1000) # placement-y (relative) if variant == 2: write_uint(buf, 14) # CELL record (explicit) @@ -497,9 +502,9 @@ def common_tests(layout: OasisLayout, variant: int) -> None: assert pp.y == 400 * (ii + 1), msg if ii in (4, 6): - assert bool(pp.flip), msg + assert pp.flip == True, msg else: - assert not bool(pp.flip), msg + assert pp.flip == False, msg if ii in (5, 6): assert pp.angle == 90, msg @@ -515,78 +520,78 @@ def common_tests(layout: OasisLayout, variant: int) -> None: assert placements[6].y == 2400 -def write_file_4(buf: IO[bytes]) -> IO[bytes]: - """ - """ +def write_file_4(buf: BufferedIOBase) -> BufferedIOBase: + ''' + ''' buf.write(HEADER) - write_uint(buf, 3) # CELLNAME record (implicit id 0) + write_uint(buf, 3) # CELLNAME record (implicit id 0) write_bstring(buf, b'A') - write_uint(buf, 3) # CELLNAME record (implicit id 1) + write_uint(buf, 3) # CELLNAME record (implicit id 1) write_bstring(buf, b'TOP') - write_uint(buf, 13) # CELL record (name ref.) - write_uint(buf, 1) # Cell name 1 (TOP) + write_uint(buf, 13) # CELL record (name ref.) + write_uint(buf, 1) # Cell name 1 (TOP) - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record # PLACEMENT 0 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b1111_1000) # CNXY_RAAF - write_uint(buf, 0) # cell reference - write_sint(buf, -300) # placement-x (relative) - write_sint(buf, 400) # placement-y (relative) - write_uint(buf, 1) # repetition (3x4 matrix) - write_uint(buf, 1) # (repetition) x-dimension - write_uint(buf, 2) # (repetition) y-dimension - write_uint(buf, 20) # (repetition) x-spacing - write_uint(buf, 30) # (repetition) y-spacing + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b1111_1000) # CNXY_RAAF + write_uint(buf, 0) # cell reference + write_sint(buf, -300) # placement-x (relative) + write_sint(buf, 400) # placement-y (relative) + write_uint(buf, 1) # repetition (3x4 matrix) + write_uint(buf, 1) # (repetition) x-dimension + write_uint(buf, 2) # (repetition) y-dimension + write_uint(buf, 20) # (repetition) x-spacing + write_uint(buf, 30) # (repetition) y-spacing # PLACEMENT 1 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0011_1000) # CNXY_RAAF - write_sint(buf, 0) # placement-x (relative) - write_sint(buf, 400) # placement-y (relative) - write_uint(buf, 0) # repetition (reuse) + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0011_1000) # CNXY_RAAF + write_sint(buf, 0) # placement-x (relative) + write_sint(buf, 400) # placement-y (relative) + write_uint(buf, 0) # repetition (reuse) # PLACEMENT 2 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0001_1000) # CNXY_RAAF - write_sint(buf, 400) # placement-y (relative) - write_uint(buf, 0) # repetition (reuse) + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0001_1000) # CNXY_RAAF + write_sint(buf, 400) # placement-y (relative) + write_uint(buf, 0) # repetition (reuse) # PLACEMENT 3 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0010_1000) # CNXY_RAAF - write_sint(buf, 300) # placement-x (relative) - write_uint(buf, 0) # repetition (reuse) + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0010_1000) # CNXY_RAAF + write_sint(buf, 300) # placement-x (relative) + write_uint(buf, 0) # repetition (reuse) - write_uint(buf, 15) # XYABSOLUTE record + write_uint(buf, 15) # XYABSOLUTE record # PLACEMENT 4 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0011_1001) # CNXY_RAAF - write_sint(buf, 700) # placement-x (absolute) - write_sint(buf, 400) # placement-y (absolute) - write_uint(buf, 0) # repetition (reuse) + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0011_1001) # CNXY_RAAF + write_sint(buf, 700) # placement-x (absolute) + write_sint(buf, 400) # placement-y (absolute) + write_uint(buf, 0) # repetition (reuse) - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record # PLACEMENT 5 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0001_1010) # CNXY_RAAF - write_sint(buf, 1000) # placement-y (relative) - write_uint(buf, 0) # repetition (reuse) + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0001_1010) # CNXY_RAAF + write_sint(buf, 1000) # placement-y (relative) + write_uint(buf, 0) # repetition (reuse) # PLACEMENT 6 - write_uint(buf, 17) # PLACEMENT (simple) - write_byte(buf, 0b0001_1011) # CNXY_RAAF - write_sint(buf, 1000) # placement-y (relative) - write_uint(buf, 0) # repetition (reuse) + write_uint(buf, 17) # PLACEMENT (simple) + write_byte(buf, 0b0001_1011) # CNXY_RAAF + write_sint(buf, 1000) # placement-y (relative) + write_uint(buf, 0) # repetition (reuse) - write_uint(buf, 13) # CELL record (name ref.) - write_uint(buf, 0) # Cell name 0 (A) + write_uint(buf, 13) # CELL record (name ref.) + write_uint(buf, 0) # Cell name 0 (A) write_rectangle(buf) @@ -594,89 +599,89 @@ def write_file_4(buf: IO[bytes]) -> IO[bytes]: return buf -def write_file_6(buf: IO[bytes]) -> IO[bytes]: - """ - """ +def write_file_6(buf: BufferedIOBase) -> BufferedIOBase: + ''' + ''' buf.write(HEADER) - write_uint(buf, 14) # CELL record (explicit) - write_bstring(buf, b'TOPTOP') # Cell name + write_uint(buf, 14) # CELL record (explicit) + write_bstring(buf, b'TOPTOP') # Cell name - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record - write_uint(buf, 18) # PLACEMENT (mag 0.5, manhattan) - write_byte(buf, 0b1011_0110) # CNXY_RMAF - write_bstring(buf, b'TOP') # Cell reference - write_uint(buf, 6) # magnitude, float32 - write_float32(buf, 0.5) # (magnitude) - write_uint(buf, 7) # angle, float64 - write_float64(buf, 90.0) # (angle) - write_sint(buf, 100) # placement-x (relative) - write_sint(buf, 0) # placement-y (relative) + write_uint(buf, 18) # PLACEMENT (mag 0.5, manhattan) + write_byte(buf, 0b1011_0110) # CNXY_RMAF + write_bstring(buf, b'TOP') # Cell reference + write_uint(buf, 6) # magnitude, float32 + write_float32(buf, 0.5) # (magnitude) + write_uint(buf, 7) # angle, float64 + write_float64(buf, 90.0) # (angle) + write_sint(buf, 100) # placement-x (relative) + write_sint(buf, 0) # placement-y (relative) - write_uint(buf, 18) # PLACEMENT (no mag, manhattan) - write_byte(buf, 0b0011_0000) # CNXY_RMAF - write_sint(buf, 100) # placement-x (relative) - write_sint(buf, 1000) # placement-y (relative) + write_uint(buf, 18) # PLACEMENT (no mag, manhattan) + write_byte(buf, 0b0011_0000) # CNXY_RMAF + write_sint(buf, 100) # placement-x (relative) + write_sint(buf, 1000) # placement-y (relative) - write_uint(buf, 14) # CELL record (explicit) - write_bstring(buf, b'TOP') # Cell name + write_uint(buf, 14) # CELL record (explicit) + write_bstring(buf, b'TOP') # Cell name - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record # PLACEMENT 0 - write_uint(buf, 18) # PLACEMENT (mag 0.5, manhattan) - write_byte(buf, 0b1011_0110) # CNXY_RMAF - write_bstring(buf, b'A') # Cell reference - write_uint(buf, 6) # magnitude, float32 - write_float32(buf, 0.5) # (magnitude) - write_uint(buf, 7) # angle, float64 - write_float64(buf, 0.0) # (angle) - write_sint(buf, -150) # placement-x (relative) - write_sint(buf, 200) # placement-y (relative) + write_uint(buf, 18) # PLACEMENT (mag 0.5, manhattan) + write_byte(buf, 0b1011_0110) # CNXY_RMAF + write_bstring(buf, b'A') # Cell reference + write_uint(buf, 6) # magnitude, float32 + write_float32(buf, 0.5) # (magnitude) + write_uint(buf, 7) # angle, float64 + write_float64(buf, 0.0) # (angle) + write_sint(buf, -150) # placement-x (relative) + write_sint(buf, 200) # placement-y (relative) # PLACEMENT 1 - write_uint(buf, 18) # PLACEMENT (no mag, manhattan) - write_byte(buf, 0b0011_0000) # CNXY_RMAF - write_sint(buf, -150) # placement-x (relative) - write_sint(buf, 600) # placement-y (relative) + write_uint(buf, 18) # PLACEMENT (no mag, manhattan) + write_byte(buf, 0b0011_0000) # CNXY_RMAF + write_sint(buf, -150) # placement-x (relative) + write_sint(buf, 600) # placement-y (relative) # PLACEMENT 2 - write_uint(buf, 18) # PLACEMENT (no mag, manhattan) - write_byte(buf, 0b0001_0000) # CNXY_RMAF - write_sint(buf, 400) # placement-y (relative) + write_uint(buf, 18) # PLACEMENT (no mag, manhattan) + write_byte(buf, 0b0001_0000) # CNXY_RMAF + write_sint(buf, 400) # placement-y (relative) # PLACEMENT 3 - write_uint(buf, 18) # PLACEMENT (no mag, manhattan) - write_byte(buf, 0b0010_0000) # CNXY_RMAF - write_sint(buf, 300) # placement-x (relative) + write_uint(buf, 18) # PLACEMENT (no mag, manhattan) + write_byte(buf, 0b0010_0000) # CNXY_RMAF + write_sint(buf, 300) # placement-x (relative) - write_uint(buf, 15) # XYABSOLUTE record + write_uint(buf, 15) # XYABSOLUTE record # PLACEMENT 4 - write_uint(buf, 18) # PLACEMENT (no mag, manhattan) - write_byte(buf, 0b0011_0001) # CNXY_RMAF - write_sint(buf, 700) # placement-x (absolute) - write_sint(buf, 400) # placement-y (absolute) + write_uint(buf, 18) # PLACEMENT (no mag, manhattan) + write_byte(buf, 0b0011_0001) # CNXY_RMAF + write_sint(buf, 700) # placement-x (absolute) + write_sint(buf, 400) # placement-y (absolute) - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record # PLACEMENT 5 - write_uint(buf, 18) # PLACEMENT (no mag, manhattan) - write_byte(buf, 0b0001_0010) # CNXY_RMAF - write_uint(buf, 0) # angle (uint, positive) - write_uint(buf, 90) # (angle) - write_sint(buf, 1000) # placement-y (relative) + write_uint(buf, 18) # PLACEMENT (no mag, manhattan) + write_byte(buf, 0b0001_0010) # CNXY_RMAF + write_uint(buf, 0) # angle (uint, positive) + write_uint(buf, 90) # (angle) + write_sint(buf, 1000) # placement-y (relative) # PLACEMENT 6 - write_uint(buf, 18) # PLACEMENT (no mag, manhattan) - write_byte(buf, 0b0001_0011) # CNXY_RMAF - write_uint(buf, 1) # angle (uint, negative) - write_uint(buf, 90) # (angle) - write_sint(buf, 1000) # placement-y (relative) + write_uint(buf, 18) # PLACEMENT (no mag, manhattan) + write_byte(buf, 0b0001_0011) # CNXY_RMAF + write_uint(buf, 1) # angle (uint, negative) + write_uint(buf, 90) # (angle) + write_sint(buf, 1000) # placement-y (relative) - write_uint(buf, 14) # CELL record (explicit) - write_bstring(buf, b'A') # Cell name + write_uint(buf, 14) # CELL record (explicit) + write_bstring(buf, b'A') # Cell name write_rectangle(buf) @@ -747,76 +752,76 @@ def test_file_6() -> None: assert pp.y == [0, 1000][ii], msg -def write_file_8(buf: IO[bytes]) -> IO[bytes]: - """ - """ +def write_file_8(buf: BufferedIOBase) -> BufferedIOBase: + ''' + ''' buf.write(HEADER) - write_uint(buf, 14) # CELL record (explicit) - write_bstring(buf, b'TOPTOP') # Cell name + write_uint(buf, 14) # CELL record (explicit) + write_bstring(buf, b'TOPTOP') # Cell name - write_uint(buf, 15) # XYABSOLUTE record + write_uint(buf, 15) # XYABSOLUTE record - write_uint(buf, 18) # PLACEMENT (mag 0.5, arbitrary angle) - write_byte(buf, 0b1011_0110) # CNXY_RMAF - write_bstring(buf, b'TOP') # Cell reference - write_uint(buf, 6) # magnitude, float32 - write_float32(buf, 0.5) # (magnitude) - write_uint(buf, 7) # angle, float64 - write_float64(buf, 22.5) # (angle) - write_sint(buf, 100) # placement-x (absolute) - write_sint(buf, 0) # placement-y (absolute) + write_uint(buf, 18) # PLACEMENT (mag 0.5, arbitrary angle) + write_byte(buf, 0b1011_0110) # CNXY_RMAF + write_bstring(buf, b'TOP') # Cell reference + write_uint(buf, 6) # magnitude, float32 + write_float32(buf, 0.5) # (magnitude) + write_uint(buf, 7) # angle, float64 + write_float64(buf, 22.5) # (angle) + write_sint(buf, 100) # placement-x (absolute) + write_sint(buf, 0) # placement-y (absolute) - write_uint(buf, 18) # PLACEMENT (mag 1.0, manhattan) - write_byte(buf, 0b1011_0110) # CNXY_RMAF - write_bstring(buf, b'TOP') # Cell reference - write_uint(buf, 6) # magnitude, float32 - write_float32(buf, 1.0) # (magnitude) - write_uint(buf, 7) # angle, float64 - write_float64(buf, 0.0) # (angle) - write_sint(buf, 1100) # placement-x (absolute) - write_sint(buf, 0) # placement-y (absolute) + write_uint(buf, 18) # PLACEMENT (mag 1.0, manhattan) + write_byte(buf, 0b1011_0110) # CNXY_RMAF + write_bstring(buf, b'TOP') # Cell reference + write_uint(buf, 6) # magnitude, float32 + write_float32(buf, 1.0) # (magnitude) + write_uint(buf, 7) # angle, float64 + write_float64(buf, 0.0) # (angle) + write_sint(buf, 1100) # placement-x (absolute) + write_sint(buf, 0) # placement-y (absolute) - write_uint(buf, 14) # CELL record (explicit) - write_bstring(buf, b'TOP') # Cell name + write_uint(buf, 14) # CELL record (explicit) + write_bstring(buf, b'TOP') # Cell name - write_uint(buf, 18) # PLACEMENT (mag 2.0, manhattan) - write_byte(buf, 0b1011_0110) # CNXY_RMAF - write_bstring(buf, b'A') # Cell reference - write_uint(buf, 6) # magnitude, float32 - write_float32(buf, 2.0) # (magnitude) - write_uint(buf, 7) # angle, float64 - write_float64(buf, 0.0) # (angle) - write_sint(buf, -100) # placement-x (absolute) - write_sint(buf, 100) # placement-y (absolute) + write_uint(buf, 18) # PLACEMENT (mag 2.0, manhattan) + write_byte(buf, 0b1011_0110) # CNXY_RMAF + write_bstring(buf, b'A') # Cell reference + write_uint(buf, 6) # magnitude, float32 + write_float32(buf, 2.0) # (magnitude) + write_uint(buf, 7) # angle, float64 + write_float64(buf, 0.0) # (angle) + write_sint(buf, -100) # placement-x (absolute) + write_sint(buf, 100) # placement-y (absolute) - write_uint(buf, 18) # PLACEMENT (mag 1.0, arbitrary angle) - write_byte(buf, 0b1011_0110) # CNXY_RMAF - write_bstring(buf, b'A') # Cell reference - write_uint(buf, 6) # magnitude, float32 - write_float32(buf, 1.0) # (magnitude) - write_uint(buf, 7) # angle, float64 - write_float64(buf, 45.0) # (angle) - write_sint(buf, -150) # placement-x (absolute) - write_sint(buf, 1100) # placement-y (absolute) + write_uint(buf, 18) # PLACEMENT (mag 1.0, arbitrary angle) + write_byte(buf, 0b1011_0110) # CNXY_RMAF + write_bstring(buf, b'A') # Cell reference + write_uint(buf, 6) # magnitude, float32 + write_float32(buf, 1.0) # (magnitude) + write_uint(buf, 7) # angle, float64 + write_float64(buf, 45.0) # (angle) + write_sint(buf, -150) # placement-x (absolute) + write_sint(buf, 1100) # placement-y (absolute) - write_uint(buf, 18) # PLACEMENT (mag 0.5, arbitrary angle) - write_byte(buf, 0b1011_1111) # CNXY_RMAF - write_bstring(buf, b'A') # Cell reference - write_uint(buf, 6) # magnitude, float32 - write_float32(buf, 0.5) # (magnitude) - write_uint(buf, 7) # angle, float64 - write_float64(buf, 135.0) # (angle) - write_sint(buf, -200) # placement-x (absolute) - write_sint(buf, 2100) # placement-y (absolute) - write_uint(buf, 1) # repetition (3x4 matrix) - write_uint(buf, 1) # (repetition) x-dimension - write_uint(buf, 2) # (repetition) y-dimension - write_uint(buf, 200) # (repetition) x-spacing - write_uint(buf, 300) # (repetition) y-spacing + write_uint(buf, 18) # PLACEMENT (mag 0.5, arbitrary angle) + write_byte(buf, 0b1011_1111) # CNXY_RMAF + write_bstring(buf, b'A') # Cell reference + write_uint(buf, 6) # magnitude, float32 + write_float32(buf, 0.5) # (magnitude) + write_uint(buf, 7) # angle, float64 + write_float64(buf, 135.0) # (angle) + write_sint(buf, -200) # placement-x (absolute) + write_sint(buf, 2100) # placement-y (absolute) + write_uint(buf, 1) # repetition (3x4 matrix) + write_uint(buf, 1) # (repetition) x-dimension + write_uint(buf, 2) # (repetition) y-dimension + write_uint(buf, 200) # (repetition) x-spacing + write_uint(buf, 300) # (repetition) y-spacing - write_uint(buf, 14) # CELL record (explicit) - write_bstring(buf, b'A') # Cell name + write_uint(buf, 14) # CELL record (explicit) + write_bstring(buf, b'A') # Cell name write_rectangle(buf, pos=(30, -40)) @@ -843,7 +848,7 @@ def test_file_8() -> None: assert not layout.cells[2].properties assert not layout.cells[2].placements - geometry = cast(list[Rectangle], layout.cells[2].geometry) + geometry = layout.cells[2].geometry assert len(geometry) == 1 assert geometry[0].layer == 1 assert geometry[0].datatype == 2 diff --git a/fatamorgana/test/test_files_polygons.py b/fatamorgana/test/test_files_polygons.py index 7ac0102..fc5d0c2 100644 --- a/fatamorgana/test/test_files_polygons.py +++ b/fatamorgana/test/test_files_polygons.py @@ -1,12 +1,17 @@ -# mypy: disable-error-code="union-attr, arg-type" -from typing import IO -from io import BytesIO +# type: ignore +from typing import List, Tuple, Iterable +from itertools import chain +from io import BytesIO, BufferedIOBase +import struct + +import pytest # type: ignore import numpy from numpy.testing import assert_equal from .utils import HEADER, FOOTER -from ..basic import write_uint, write_sint, write_bstring, write_byte +from ..basic import write_uint, write_sint, read_uint, read_sint, write_bstring, write_byte +from ..basic import InvalidRecordError, InvalidDataError from ..main import OasisLayout @@ -80,11 +85,8 @@ def common_tests(layout: OasisLayout) -> None: for ii in range(4): msg = f'Fail on poly {ii}' assert len(geometry[0].point_list) == 6, msg - assert_equal( - geometry[0].point_list, - [[150, 0], [0, 50], [-50, 0], [0, 50], [-100, 0], [0, -100]], - err_msg=msg, - ) + assert_equal(geometry[0].point_list, [[150, 0], [0, 50], [-50, 0], [0, 50], + [-100, 0], [0, -100]], err_msg=msg) assert len(geometry[4].point_list) == 6 assert_equal(geometry[4].point_list, [[0, 150], [50, 0], [0, -50], [50, 0], [0, -100], [-100, 0]]) @@ -95,10 +97,8 @@ def common_tests(layout: OasisLayout) -> None: assert len(geometry[7].point_list) == 9 assert_equal(geometry[7].point_list, [[25, 0], [50, 50], [0, 50], [-50, 50], [-50, 0], [-50, -50], [10, -75], [25, -25], [40, 0]]) assert len(geometry[8].point_list) == 9 - assert_equal( - geometry[8].point_list, - numpy.cumsum([[25, 0], [50, 50], [0, 50], [-50, 50], [-50, 0], [-50, -50], [10, -75], [25, -25], [45, -575]], axis=0), - ) + assert_equal(geometry[8].point_list, + numpy.cumsum([[25, 0], [50, 50], [0, 50], [-50, 50], [-50, 0], [-50, -50], [10, -75], [25, -25], [45, -575]], axis=0)) for ii in range(9, 12): msg = f'Fail on poly {ii}' @@ -106,57 +106,57 @@ def common_tests(layout: OasisLayout) -> None: assert_equal(geometry[ii].point_list, [[0, 150], [50, 0], [0, -50], [50, 0], [0, -100], [-100, 0]], err_msg=msg) -def write_file_common(buf: IO[bytes], variant: int) -> IO[bytes]: - """ - """ +def write_file_common(buf: BufferedIOBase, variant: int) -> BufferedIOBase: + ''' + ''' assert variant in (1, 3), 'Error in test!!' buf.write(HEADER) if variant == 3: - write_uint(buf, 7) # PROPNAME record (implict id 0) - write_bstring(buf, b'PROP0') # property name + write_uint(buf, 7) # PROPNAME record (implict id 0) + write_bstring(buf, b'PROP0') # property name - write_uint(buf, 14) # CELL record (explicit) - write_bstring(buf, b'ABC') # Cell name + write_uint(buf, 14) # CELL record (explicit) + write_bstring(buf, b'ABC') # Cell name # POLYGON 0 - write_uint(buf, 21) # POLYGON record - write_byte(buf, 0b0011_1011) # 00PX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 0) # pointlist: 1-delta, horiz-fisrt - write_uint(buf, 4) # (pointlist) dimension - write_sint(buf, 150) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, -50) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, 0) # geometry-x (absolute) - write_sint(buf, 100) # geometry-y (absolute) + write_uint(buf, 21) # POLYGON record + write_byte(buf, 0b0011_1011) # 00PX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 0) # pointlist: 1-delta, horiz-fisrt + write_uint(buf, 4) # (pointlist) dimension + write_sint(buf, 150) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, -50) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, 0) # geometry-x (absolute) + write_sint(buf, 100) # geometry-y (absolute) if variant == 3: # PROPERTY 0 - write_uint(buf, 28) # PROPERTY record (explicit) - write_byte(buf, 0b0001_0110) # UUUU_VCNS - write_uint(buf, 0) # propname id - write_uint(buf, 2) # property value (real: positive reciprocal) - write_uint(buf, 5) # (real) 1/5 + write_uint(buf, 28) # PROPERTY record (explicit) + write_byte(buf, 0b0001_0110) # UUUU_VCNS + write_uint(buf, 0) # propname id + write_uint(buf, 2) # property value (real: positive reciprocal) + write_uint(buf, 5) # (real) 1/5 write_uint(buf, 16) # XYRELATIVE record # Polygon 1 - write_uint(buf, 21) # POLYGON record - write_byte(buf, 0b0011_1011) # 00PX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 0) # pointlist: 1-delta, horiz-fisrt - write_uint(buf, 4) # (pointlist) dimension - write_sint(buf, 150) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, -50) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, -200) # geometry-x (relative) - write_sint(buf, 300) # geometry-y (relative) + write_uint(buf, 21) # POLYGON record + write_byte(buf, 0b0011_1011) # 00PX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 0) # pointlist: 1-delta, horiz-fisrt + write_uint(buf, 4) # (pointlist) dimension + write_sint(buf, 150) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, -50) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, -200) # geometry-x (relative) + write_sint(buf, 300) # geometry-y (relative) if variant == 3: # PROPERTY 1 @@ -165,55 +165,55 @@ def write_file_common(buf: IO[bytes], variant: int) -> IO[bytes]: write_uint(buf, 15) # XYABSOLUTE record # Polygon 2 - write_uint(buf, 21) # POLYGON record - write_byte(buf, 0b0011_0011) # 00PX_YRDL - write_uint(buf, 2) # layer - write_uint(buf, 3) # datatype - write_uint(buf, 0) # pointlist: 1-delta, horiz-fisrt - write_uint(buf, 4) # (pointlist) dimension - write_sint(buf, 150) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, -50) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, 0) # geometry-x (absolute) + write_uint(buf, 21) # POLYGON record + write_byte(buf, 0b0011_0011) # 00PX_YRDL + write_uint(buf, 2) # layer + write_uint(buf, 3) # datatype + write_uint(buf, 0) # pointlist: 1-delta, horiz-fisrt + write_uint(buf, 4) # (pointlist) dimension + write_sint(buf, 150) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, -50) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, 0) # geometry-x (absolute) if variant == 3: # PROPERTY 2 write_uint(buf, 29) # PROPERTY record (repeat) # Polygon 3 - write_uint(buf, 21) # POLYGON record - write_byte(buf, 0b0000_1000) # 00PX_YRDL - write_sint(buf, 1000) # geometry-y (absolute) + write_uint(buf, 21) # POLYGON record + write_byte(buf, 0b0000_1000) # 00PX_YRDL + write_sint(buf, 1000) # geometry-y (absolute) if variant == 3: # PROPERTY 3 write_uint(buf, 29) # PROPERTY record (repeat) # Polygon 4 - write_uint(buf, 21) # POLYGON record - write_byte(buf, 0b0011_0011) # 00PX_YRDL - write_uint(buf, 2) # layer - write_uint(buf, 3) # datatype - write_uint(buf, 1) # pointlist: 1-delta, vert-fisrt - write_uint(buf, 4) # (pointlist) dimension - write_sint(buf, 150) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, -50) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, 200) # geometry-x (absolute) + write_uint(buf, 21) # POLYGON record + write_byte(buf, 0b0011_0011) # 00PX_YRDL + write_uint(buf, 2) # layer + write_uint(buf, 3) # datatype + write_uint(buf, 1) # pointlist: 1-delta, vert-fisrt + write_uint(buf, 4) # (pointlist) dimension + write_sint(buf, 150) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, -50) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, 200) # geometry-x (absolute) if variant == 3: # PROPERTY 4 write_uint(buf, 29) # PROPERTY record (repeat) # Polygon 5 - write_uint(buf, 21) # POLYGON record - write_byte(buf, 0b0011_0011) # 00PX_YRDL - write_uint(buf, 2) # layer - write_uint(buf, 3) # datatype - write_uint(buf, 2) # pointlist: 2-delta - write_uint(buf, 7) # (pointlist) dimension + write_uint(buf, 21) # POLYGON record + write_byte(buf, 0b0011_0011) # 00PX_YRDL + write_uint(buf, 2) # layer + write_uint(buf, 3) # datatype + write_uint(buf, 2) # pointlist: 2-delta + write_uint(buf, 7) # (pointlist) dimension write_uint(buf, 150 << 2 | 0b00) # (pointlist) write_uint(buf, 50 << 2 | 0b01) # (pointlist) write_uint(buf, 50 << 2 | 0b10) # (pointlist) @@ -228,12 +228,12 @@ def write_file_common(buf: IO[bytes], variant: int) -> IO[bytes]: write_uint(buf, 29) # PROPERTY record (repeat) # Polygon 6 - write_uint(buf, 21) # POLYGON record - write_byte(buf, 0b0011_0011) # 00PX_YRDL - write_uint(buf, 2) # layer - write_uint(buf, 3) # datatype - write_uint(buf, 3) # pointlist: 3-delta - write_uint(buf, 8) # (pointlist) dimension + write_uint(buf, 21) # POLYGON record + write_byte(buf, 0b0011_0011) # 00PX_YRDL + write_uint(buf, 2) # layer + write_uint(buf, 3) # datatype + write_uint(buf, 3) # pointlist: 3-delta + write_uint(buf, 8) # (pointlist) dimension write_uint(buf, 25 << 3 | 0b000) # (pointlist) write_uint(buf, 50 << 3 | 0b100) # (pointlist) write_uint(buf, 50 << 3 | 0b001) # (pointlist) @@ -249,12 +249,12 @@ def write_file_common(buf: IO[bytes], variant: int) -> IO[bytes]: write_uint(buf, 29) # PROPERTY record (repeat) # Polygon 7 - write_uint(buf, 21) # POLYGON record - write_byte(buf, 0b0011_0011) # 00PX_YRDL - write_uint(buf, 2) # layer - write_uint(buf, 3) # datatype - write_uint(buf, 4) # pointlist: g-delta - write_uint(buf, 8) # (pointlist) dimension + write_uint(buf, 21) # POLYGON record + write_byte(buf, 0b0011_0011) # 00PX_YRDL + write_uint(buf, 2) # layer + write_uint(buf, 3) # datatype + write_uint(buf, 4) # pointlist: g-delta + write_uint(buf, 8) # (pointlist) dimension write_uint(buf, 25 << 4 | 0b0000) # (pointlist) write_uint(buf, 50 << 4 | 0b1000) # (pointlist) write_uint(buf, 50 << 4 | 0b0010) # (pointlist) @@ -263,7 +263,7 @@ def write_file_common(buf: IO[bytes], variant: int) -> IO[bytes]: write_uint(buf, 50 << 4 | 0b0100) # (pointlist) write_uint(buf, 50 << 4 | 0b1100) # (pointlist) write_uint(buf, 10 << 2 | 0b01) # (pointlist) - write_sint(buf, -75) + write_sint(buf, -75 ) write_uint(buf, 25 << 4 | 0b1110) # (pointlist) write_sint(buf, 900) # geometry-x (absolute) @@ -272,12 +272,12 @@ def write_file_common(buf: IO[bytes], variant: int) -> IO[bytes]: write_uint(buf, 29) # PROPERTY record (repeat) # Polygon 8 - write_uint(buf, 21) # POLYGON record - write_byte(buf, 0b0011_0011) # 00PX_YRDL - write_uint(buf, 2) # layer - write_uint(buf, 3) # datatype - write_uint(buf, 5) # pointlist: double g-delta - write_uint(buf, 8) # (pointlist) dimension + write_uint(buf, 21) # POLYGON record + write_byte(buf, 0b0011_0011) # 00PX_YRDL + write_uint(buf, 2) # layer + write_uint(buf, 3) # datatype + write_uint(buf, 5) # pointlist: double g-delta + write_uint(buf, 8) # (pointlist) dimension write_uint(buf, 25 << 4 | 0b0000) # (pointlist) write_uint(buf, 50 << 4 | 0b1000) # (pointlist) write_uint(buf, 50 << 4 | 0b0010) # (pointlist) @@ -286,7 +286,7 @@ def write_file_common(buf: IO[bytes], variant: int) -> IO[bytes]: write_uint(buf, 50 << 4 | 0b0100) # (pointlist) write_uint(buf, 50 << 4 | 0b1100) # (pointlist) write_uint(buf, 10 << 2 | 0b01) # (pointlist) - write_sint(buf, -75) + write_sint(buf, -75 ) write_uint(buf, 25 << 4 | 0b1110) # (pointlist) write_sint(buf, 1100) # geometry-x (absolute) @@ -295,62 +295,62 @@ def write_file_common(buf: IO[bytes], variant: int) -> IO[bytes]: write_uint(buf, 29) # PROPERTY record (repeat) # Polygon 9 - write_uint(buf, 21) # POLYGON record - write_byte(buf, 0b0011_1111) # 00PX_YRDL - write_uint(buf, 2) # layer - write_uint(buf, 3) # datatype - write_uint(buf, 1) # pointlist: 1-delta (vert. first) - write_uint(buf, 4) # (pointlist) dimension - write_sint(buf, 150) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, -50) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, 0) # geometry-x (absolute) - write_sint(buf, 2000) # geometry-y (absolute) - write_uint(buf, 1) # repetition (3x4 matrix) - write_uint(buf, 1) # (repetition) x-dimension - write_uint(buf, 2) # (repetition) y-dimension - write_uint(buf, 200) # (repetition) x-spacing - write_uint(buf, 300) # (repetition) y-spacing + write_uint(buf, 21) # POLYGON record + write_byte(buf, 0b0011_1111) # 00PX_YRDL + write_uint(buf, 2) # layer + write_uint(buf, 3) # datatype + write_uint(buf, 1) # pointlist: 1-delta (vert. first) + write_uint(buf, 4) # (pointlist) dimension + write_sint(buf, 150) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, -50) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, 0) # geometry-x (absolute) + write_sint(buf, 2000) # geometry-y (absolute) + write_uint(buf, 1) # repetition (3x4 matrix) + write_uint(buf, 1) # (repetition) x-dimension + write_uint(buf, 2) # (repetition) y-dimension + write_uint(buf, 200) # (repetition) x-spacing + write_uint(buf, 300) # (repetition) y-spacing if variant == 3: # PROPERTY 9 write_uint(buf, 29) # PROPERTY record (repeat) - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record # Polygon 10 - write_uint(buf, 21) # POLYGON record - write_byte(buf, 0b0011_0110) # 00PX_YRDL - write_uint(buf, 1) # datatype - write_uint(buf, 1) # pointlist: 1-delta (vert. first) - write_uint(buf, 4) # (pointlist) dimension - write_sint(buf, 150) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, -50) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, 1000) # geometry-x (relative) - write_uint(buf, 0) # repetition (reuse) + write_uint(buf, 21) # POLYGON record + write_byte(buf, 0b0011_0110) # 00PX_YRDL + write_uint(buf, 1) # datatype + write_uint(buf, 1) # pointlist: 1-delta (vert. first) + write_uint(buf, 4) # (pointlist) dimension + write_sint(buf, 150) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, -50) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, 1000) # geometry-x (relative) + write_uint(buf, 0) # repetition (reuse) if variant == 3: # PROPERTY 10 write_uint(buf, 29) # PROPERTY record (repeat) # Polygon 11 - write_uint(buf, 21) # POLYGON record - write_byte(buf, 0b0011_0110) # 00PX_YRDL - write_uint(buf, 1) # datatype - write_uint(buf, 1) # pointlist: 1-delta (vert. first) - write_uint(buf, 4) # (pointlist) dimension - write_sint(buf, 150) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, -50) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, 1000) # geometry-x (relative) - write_uint(buf, 6) # repetition (3 rows) - write_uint(buf, 1) # (repetition) dimension - write_uint(buf, 200) # (repetition) y-delta - write_uint(buf, 300) # (repetition) y-delta + write_uint(buf, 21) # POLYGON record + write_byte(buf, 0b0011_0110) # 00PX_YRDL + write_uint(buf, 1) # datatype + write_uint(buf, 1) # pointlist: 1-delta (vert. first) + write_uint(buf, 4) # (pointlist) dimension + write_sint(buf, 150) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, -50) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, 1000) # geometry-x (relative) + write_uint(buf, 6) # repetition (3 rows) + write_uint(buf, 1) # (repetition) dimension + write_uint(buf, 200) # (repetition) y-delta + write_uint(buf, 300) # (repetition) y-delta if variant == 3: # PROPERTY 11 @@ -375,9 +375,9 @@ def test_file_1() -> None: assert not gg.properties, f'Fail on polygon {ii}' -def write_file_2(buf: IO[bytes]) -> IO[bytes]: - """ - """ +def write_file_2(buf: BufferedIOBase) -> BufferedIOBase: + ''' + ''' buf.write(HEADER) write_uint(buf, 14) # CELL record (explicit) @@ -386,21 +386,21 @@ def write_file_2(buf: IO[bytes]) -> IO[bytes]: write_uint(buf, 15) # XYRELATIVE record # POLYGON 0 - write_uint(buf, 21) # POLYGON record - write_byte(buf, 0b0011_0011) # 00PX_YRDL - write_uint(buf, 2) # layer - write_uint(buf, 3) # datatype - write_uint(buf, 4) # pointlist: g-delta - write_uint(buf, 8002) # (pointlist) dimension - write_uint(buf, 1000 << 2 | 0b11) # (pointlist) - write_sint(buf, 0) # (pointlist) + write_uint(buf, 21) # POLYGON record + write_byte(buf, 0b0011_0011) # 00PX_YRDL + write_uint(buf, 2) # layer + write_uint(buf, 3) # datatype + write_uint(buf, 4) # pointlist: g-delta + write_uint(buf, 8002) # (pointlist) dimension + write_uint(buf, 1000 << 2 | 0b11) # (pointlist) + write_sint(buf, 0) # (pointlist) for _ in range(4000): - write_uint(buf, 10 << 2 | 0b01) # (pointlist) - write_sint(buf, 20) # (pointlist) - write_uint(buf, 10 << 2 | 0b11) # (pointlist) - write_sint(buf, 20) # (pointlist) - write_uint(buf, 1000 << 2 | 0b01) # (pointlist) - write_sint(buf, 0) # (pointlist) + write_uint(buf, 10 << 2 | 0b01) # (pointlist) + write_sint(buf, 20) # (pointlist) + write_uint(buf, 10 << 2 | 0b11) # (pointlist) + write_sint(buf, 20) # (pointlist) + write_uint(buf, 1000 << 2 | 0b01) # (pointlist) + write_sint(buf, 0) # (pointlist) write_sint(buf, 0) # geometry-x (absolute) buf.write(FOOTER) @@ -425,8 +425,7 @@ def test_file_2() -> None: assert_equal(poly.point_list, ([[-1000, 0]] + [[(-1) ** nn * 10, 20] for nn in range(8000)] - + [[1000, 0], [0, -20 * 8000]]), - ) + + [[1000, 0], [0, -20 * 8000]])) def test_file_3() -> None: @@ -445,7 +444,7 @@ def test_file_3() -> None: for ii, gg in enumerate(geometry): msg = f'Fail on polygon {ii}' assert len(gg.properties) == 1, msg - assert gg.properties[0].name == 0, msg # type: ignore + assert gg.properties[0].name == 0, msg assert len(gg.properties[0].values) == 1, msg - assert gg.properties[0].values[0] * 5 == 1, msg # type: ignore + assert gg.properties[0].values[0] * 5 == 1, msg diff --git a/fatamorgana/test/test_files_properties.py b/fatamorgana/test/test_files_properties.py index 98fdede..a49d7e1 100644 --- a/fatamorgana/test/test_files_properties.py +++ b/fatamorgana/test/test_files_properties.py @@ -1,13 +1,17 @@ -# mypy: disable-error-code="union-attr, index, arg-type" -from typing import IO -from io import BytesIO +# type: ignore -import pytest +from typing import List, Tuple, Iterable +from itertools import chain +from io import BytesIO, BufferedIOBase +import struct + +import pytest # type: ignore +import numpy from numpy.testing import assert_equal from .utils import HEADER, FOOTER -from ..basic import write_uint, write_sint, write_bstring, write_byte -from ..basic import InvalidDataError +from ..basic import write_uint, write_sint, read_uint, read_sint, write_bstring, write_byte, PathExtensionScheme +from ..basic import InvalidRecordError, InvalidDataError from ..main import OasisLayout @@ -23,12 +27,12 @@ def base_tests(layout: OasisLayout) -> None: assert not layout.layers -def write_file_common(buf: IO[bytes], variant: int) -> IO[bytes]: - """ - """ +def write_file_common(buf: BufferedIOBase, variant: int) -> BufferedIOBase: + ''' + ''' include_repetitions = variant in (2, 5) - def var_byte(buf: IO[bytes], byte: int) -> None: + def var_byte(buf, byte): if include_repetitions: byte |= 0b0100 write_byte(buf, byte) @@ -50,6 +54,7 @@ def write_file_common(buf: IO[bytes], variant: int) -> IO[bytes]: write_uint(buf, 7) # PROPNAME record (implicit id 1) write_bstring(buf, b'PROP1') + write_uint(buf, 14) # CELL record (explicit) write_bstring(buf, b'A') # Cell name @@ -69,29 +74,11 @@ def write_file_common(buf: IO[bytes], variant: int) -> IO[bytes]: write_uint(buf, 300) # (repetition) x-spacing write_uint(buf, 320) # (repetition) y-spacing - write_uint(buf, 28) # PROPERTY record - write_byte(buf, 0b0000_0100) # UUUU_VCNS + write_uint(buf, 28) # PROPERTY record + write_byte(buf, 0b0000_0100) # UUUU_VCNS write_bstring(buf, b'PROPX') # RECTANGLE 1 - write_uint(buf, 20) # RECTANGLE record - var_byte(buf, 0b0111_1011) # SWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 100) # width - write_uint(buf, 200) # height - write_sint(buf, 0) # geometry-x (relative) - write_sint(buf, 1000) # geometry-y (relative) - if include_repetitions: - write_uint(buf, 0) # repetition (reuse) - - write_uint(buf, 28) # PROPERTY record - write_byte(buf, 0b0001_0110) # UUUU_VCNS - write_uint(buf, 0) # propname id - write_uint(buf, 1) # property value 0 (real type 1, negative int) - write_uint(buf, 5) # (real 1) - - # RECTANGLE 2 write_uint(buf, 20) # RECTANGLE record var_byte(buf, 0b0111_1011) # SWHX_YRDL write_uint(buf, 1) # layer @@ -103,20 +90,60 @@ def write_file_common(buf: IO[bytes], variant: int) -> IO[bytes]: if include_repetitions: write_uint(buf, 0) # repetition (reuse) - write_uint(buf, 28) # PROPERTY record - write_byte(buf, 0b0100_0110) # UUUU_VCNS - write_uint(buf, 0) # propname id - write_uint(buf, 8) # prop value 0 (unsigned int) - write_uint(buf, 25) # (prop value) - write_uint(buf, 9) # prop value 1 (signed int) - write_sint(buf, -124) # (prop value) - write_uint(buf, 10) # prop value 2 (a-string) + write_uint(buf, 28) # PROPERTY record + write_byte(buf, 0b0001_0110) # UUUU_VCNS + write_uint(buf, 0) # propname id + write_uint(buf, 1) # property value 0 (real type 1, negative int) + write_uint(buf, 5) # (real 1) + + # RECTANGLE 2 + write_uint(buf, 20) # RECTANGLE record + var_byte(buf, 0b0111_1011) # SWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 100) # width + write_uint(buf, 200) # height + write_sint(buf, 0) # geometry-x (relative) + write_sint(buf, 1000) # geometry-y (relative) + if include_repetitions: + write_uint(buf, 0) # repetition (reuse) + + write_uint(buf, 28) # PROPERTY record + write_byte(buf, 0b0100_0110) # UUUU_VCNS + write_uint(buf, 0) # propname id + write_uint(buf, 8) # prop value 0 (unsigned int) + write_uint(buf, 25) # (prop value) + write_uint(buf, 9) # prop value 1 (signed int) + write_sint(buf, -124) # (prop value) + write_uint(buf, 10) # prop value 2 (a-string) write_bstring(buf, b'PROP_VALUE2') - write_uint(buf, 13) # prop value 3 (propstring ref.) + write_uint(buf, 13) # prop value 3 (propstring ref.) write_uint(buf, 12) # RECTANGLE 3 write_uint(buf, 20) # RECTANGLE record + var_byte(buf, 0b0111_1011) # SWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 100) # width + write_uint(buf, 200) # height + write_sint(buf, 0) # geometry-x (relative) + write_sint(buf, 1000) # geometry-y (relative) + if include_repetitions: + write_uint(buf, 0) # repetition (reuse) + + write_uint(buf, 28) # PROPERTY record + write_byte(buf, 0b1111_0000) # UUUU_VCNS + write_uint(buf, 3) # number of values + write_uint(buf, 0) # prop value 0 (unsigned int) + write_uint(buf, 25) # (prop value) + write_uint(buf, 9) # prop value 1 (signed int) + write_sint(buf, -124) # (prop value) + write_uint(buf, 14) # prop value 2 (propstring ref.) + write_uint(buf, 13) + + # RECTANGLE 4 + write_uint(buf, 20) # RECTANGLE record var_byte(buf, 0b0111_1011) # SWHX_YRDL write_uint(buf, 1) # layer write_uint(buf, 2) # datatype @@ -127,36 +154,14 @@ def write_file_common(buf: IO[bytes], variant: int) -> IO[bytes]: if include_repetitions: write_uint(buf, 0) # repetition (reuse) - write_uint(buf, 28) # PROPERTY record - write_byte(buf, 0b1111_0000) # UUUU_VCNS - write_uint(buf, 3) # number of values - write_uint(buf, 0) # prop value 0 (unsigned int) - write_uint(buf, 25) # (prop value) - write_uint(buf, 9) # prop value 1 (signed int) - write_sint(buf, -124) # (prop value) - write_uint(buf, 14) # prop value 2 (propstring ref.) - write_uint(buf, 13) + write_uint(buf, 28) # PROPERTY record + write_byte(buf, 0b0000_1000) # UUUU_VCNS - # RECTANGLE 4 - write_uint(buf, 20) # RECTANGLE record - var_byte(buf, 0b0111_1011) # SWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 100) # width - write_uint(buf, 200) # height - write_sint(buf, 0) # geometry-x (relative) - write_sint(buf, 1000) # geometry-y (relative) - if include_repetitions: - write_uint(buf, 0) # repetition (reuse) - - write_uint(buf, 28) # PROPERTY record - write_byte(buf, 0b0000_1000) # UUUU_VCNS - - write_uint(buf, 15) # XYABSOLUTE record + write_uint(buf, 15) # XYABSOLUTE record # TEXT 5 write_uint(buf, 19) # TEXT record - var_byte(buf, 0b0101_1011) # 0CNX_YRTL + var_byte(buf, 0b0101_1011) # 0CNX_YRTL write_bstring(buf, b'A') # text-string write_uint(buf, 2) # text-layer write_uint(buf, 1) # text-datatype @@ -168,47 +173,47 @@ def write_file_common(buf: IO[bytes], variant: int) -> IO[bytes]: write_uint(buf, 29) # PROPERTY (reuse) # PATH 6 - write_uint(buf, 22) # PATH record - var_byte(buf, 0b1111_1011) # EWPX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 10) # half-width - write_byte(buf, 0b0000_1111) # extension-scheme 0000_SSEE - write_sint(buf, 5) # (extension-scheme) - write_sint(buf, -5) # (extension-scheme) - write_uint(buf, 0) # pointlist (1-delta, horiz. first) - write_uint(buf, 3) # (pointlist) dimension - write_sint(buf, 150) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, -50) # (pointlist) - write_sint(buf, 2000) # geometry-x (absolute) - write_sint(buf, 0) # geometry-y (absolute) + write_uint(buf, 22) # PATH record + var_byte(buf, 0b1111_1011) # EWPX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 10) # half-width + write_byte(buf, 0b0000_1111) # extension-scheme 0000_SSEE + write_sint(buf, 5) # (extension-scheme) + write_sint(buf, -5) # (extension-scheme) + write_uint(buf, 0) # pointlist (1-delta, horiz. first) + write_uint(buf, 3) # (pointlist) dimension + write_sint(buf, 150) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, -50) # (pointlist) + write_sint(buf, 2000) # geometry-x (absolute) + write_sint(buf, 0) # geometry-y (absolute) if include_repetitions: - write_uint(buf, 0) # repetition (reuse) + write_uint(buf, 0) # repetition (reuse) - write_uint(buf, 29) # PROPERTY (reuse) + write_uint(buf, 29) # PROPERTY (reuse) # POLYGON 7 - write_uint(buf, 21) # POLYGON record - var_byte(buf, 0b0011_1011) # 00PX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 0) # pointlist (1-delta, horiz. first) - write_uint(buf, 4) # (pointlist) dimension - write_sint(buf, 150) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, -50) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, 3000) # geometry-x (absolute) - write_sint(buf, 0) # geometry-y (absolute) + write_uint(buf, 21) # POLYGON record + var_byte(buf, 0b0011_1011) # 00PX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 0) # pointlist (1-delta, horiz. first) + write_uint(buf, 4) # (pointlist) dimension + write_sint(buf, 150) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, -50) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, 3000) # geometry-x (absolute) + write_sint(buf, 0) # geometry-y (absolute) if include_repetitions: - write_uint(buf, 0) # repetition (reuse) + write_uint(buf, 0) # repetition (reuse) - write_uint(buf, 29) # PROPERTY (reuse) + write_uint(buf, 29) # PROPERTY (reuse) - write_uint(buf, 28) # PROPERTY record - write_byte(buf, 0b0000_0110) # UUUU_VCNS - write_uint(buf, 1) # propname id + write_uint(buf, 28) # PROPERTY record + write_byte(buf, 0b0000_0110) # UUUU_VCNS + write_uint(buf, 1) # propname id if variant == 5: write_uint(buf, 10) # PROPSTRING (explicit id) @@ -354,9 +359,9 @@ def test_file_5() -> None: assert gg.repetition.b_vector == [0, 320], msg -def write_file_3(buf: IO[bytes]) -> IO[bytes]: - """ - """ +def write_file_3(buf: BufferedIOBase) -> BufferedIOBase: + ''' + ''' buf.write(HEADER) write_uint(buf, 10) # PROPSTRING (explicit id) @@ -370,141 +375,141 @@ def write_file_3(buf: IO[bytes]) -> IO[bytes]: write_uint(buf, 7) # PROPNAME record (implicit id 0) write_bstring(buf, b'S_GDS_PROPERTY') - # ** CELL ** + write_uint(buf, 14) # CELL record (explicit) write_bstring(buf, b'A') # Cell name write_uint(buf, 16) # XYRELATIVE record # RECTANGLE 0 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0111_1011) # SWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 100) # width - write_uint(buf, 200) # height - write_sint(buf, 0) # geometry-x (relative) - write_sint(buf, 1000) # geometry-y (relative) + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0111_1011) # SWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 100) # width + write_uint(buf, 200) # height + write_sint(buf, 0) # geometry-x (relative) + write_sint(buf, 1000) # geometry-y (relative) - write_uint(buf, 28) # PROPERTY record - write_byte(buf, 0b0010_0111) # UUUU_VCNS - write_uint(buf, 0) # propname id - write_uint(buf, 8) # property value 0 (unsigned int) - write_uint(buf, 25) # (...) - write_uint(buf, 10) # property value 1 (a-string) + write_uint(buf, 28) # PROPERTY record + write_byte(buf, 0b0010_0111) # UUUU_VCNS + write_uint(buf, 0) # propname id + write_uint(buf, 8) # property value 0 (unsigned int) + write_uint(buf, 25) # (...) + write_uint(buf, 10) # property value 1 (a-string) write_bstring(buf, b'PROP_VALUE2') # RECTANGLE 1 - write_uint(buf, 20) # RECTANGLE record + write_uint(buf, 20) # RECTANGLE record write_byte(buf, 0b0111_1011) # SWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 100) # width - write_uint(buf, 200) # height - write_sint(buf, 0) # geometry-x (relative) - write_sint(buf, 1000) # geometry-y (relative) + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 100) # width + write_uint(buf, 200) # height + write_sint(buf, 0) # geometry-x (relative) + write_sint(buf, 1000) # geometry-y (relative) - write_uint(buf, 28) # PROPERTY record - write_byte(buf, 0b1111_0001) # UUUU_VCNS - write_uint(buf, 2) # number of values - write_uint(buf, 8) # property value 0 (unsigned int) - write_uint(buf, 10) # (...) - write_uint(buf, 14) # property value 1 (prop-string ref.) - write_uint(buf, 13) # (...) + write_uint(buf, 28) # PROPERTY record + write_byte(buf, 0b1111_0001) # UUUU_VCNS + write_uint(buf, 2) # number of values + write_uint(buf, 8) # property value 0 (unsigned int) + write_uint(buf, 10) # (...) + write_uint(buf, 14) # property value 1 (prop-string ref.) + write_uint(buf, 13) # (...) # RECTANGLE 2 - write_uint(buf, 20) # RECTANGLE record + write_uint(buf, 20) # RECTANGLE record write_byte(buf, 0b0111_1011) # SWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 100) # width - write_uint(buf, 200) # height - write_sint(buf, 0) # geometry-x (relative) - write_sint(buf, 1000) # geometry-y (relative) + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 100) # width + write_uint(buf, 200) # height + write_sint(buf, 0) # geometry-x (relative) + write_sint(buf, 1000) # geometry-y (relative) - write_uint(buf, 28) # PROPERTY record - write_byte(buf, 0b0000_1001) # UUUU_VCNS + write_uint(buf, 28) # PROPERTY record + write_byte(buf, 0b0000_1001) # UUUU_VCNS # RECTANGLE 3 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0111_1011) # SWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 100) # width - write_uint(buf, 200) # height - write_sint(buf, 0) # geometry-x (relative) - write_sint(buf, 1000) # geometry-y (relative) + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0111_1011) # SWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 100) # width + write_uint(buf, 200) # height + write_sint(buf, 0) # geometry-x (relative) + write_sint(buf, 1000) # geometry-y (relative) - write_uint(buf, 29) # PROPERTY (reuse) + write_uint(buf, 29) # PROPERTY (reuse) # RECTANGLE 4 - write_uint(buf, 20) # RECTANGLE record + write_uint(buf, 20) # RECTANGLE record write_byte(buf, 0b0111_1011) # SWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 100) # width - write_uint(buf, 200) # height - write_sint(buf, 0) # geometry-x (relative) - write_sint(buf, 1000) # geometry-y (relative) + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 100) # width + write_uint(buf, 200) # height + write_sint(buf, 0) # geometry-x (relative) + write_sint(buf, 1000) # geometry-y (relative) - write_uint(buf, 28) # PROPERTY record - write_byte(buf, 0b0000_1001) # UUUU_VCNS + write_uint(buf, 28) # PROPERTY record + write_byte(buf, 0b0000_1001) # UUUU_VCNS - write_uint(buf, 28) # PROPERTY record - write_byte(buf, 0b0010_0111) # UUUU_VCNS - write_uint(buf, 0) # propname id - write_uint(buf, 8) # prop value 0 (unsigned int) - write_uint(buf, 25) # (...) - write_uint(buf, 10) # prop-value 1 (a-string) + write_uint(buf, 28) # PROPERTY record + write_byte(buf, 0b0010_0111) # UUUU_VCNS + write_uint(buf, 0) # propname id + write_uint(buf, 8) # prop value 0 (unsigned int) + write_uint(buf, 25) # (...) + write_uint(buf, 10) # prop-value 1 (a-string) write_bstring(buf, b'PROP_VALUE2') # (...) write_uint(buf, 15) # XYABSOLUTE record # TEXT 5 - write_uint(buf, 19) # TEXT record - write_byte(buf, 0b0101_1011) # 0CNX_YRTL - write_bstring(buf, b'A') # text-string - write_uint(buf, 2) # text-layer - write_uint(buf, 1) # text-datatype - write_sint(buf, 1000) # geometry-x (absolute) - write_sint(buf, 0) # geometry-y (absolute) + write_uint(buf, 19) # TEXT record + write_byte(buf, 0b0101_1011) # 0CNX_YRTL + write_bstring(buf, b'A') # text-string + write_uint(buf, 2) # text-layer + write_uint(buf, 1) # text-datatype + write_sint(buf, 1000) # geometry-x (absolute) + write_sint(buf, 0) # geometry-y (absolute) - write_uint(buf, 29) # PROPERTY (reuse) + write_uint(buf, 29) # PROPERTY (reuse) # PATH 6 - write_uint(buf, 22) # PATH record + write_uint(buf, 22) # PATH record write_byte(buf, 0b1111_1011) # EWPX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 10) # half-width - write_byte(buf, 0b0000_1111) # extension-scheme 0000_SSEE - write_sint(buf, 5) # (extension-scheme) - write_sint(buf, -5) # (extension-scheme) - write_uint(buf, 0) # pointlist (1-delta, horiz. first) - write_uint(buf, 3) # (pointlist) dimension - write_sint(buf, 150) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, -50) # (pointlist) - write_sint(buf, 2000) # geometry-x (absolute) - write_sint(buf, 0) # geometry-y (absolute) + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 10) # half-width + write_byte(buf, 0b0000_1111) # extension-scheme 0000_SSEE + write_sint(buf, 5) # (extension-scheme) + write_sint(buf, -5) # (extension-scheme) + write_uint(buf, 0) # pointlist (1-delta, horiz. first) + write_uint(buf, 3) # (pointlist) dimension + write_sint(buf, 150) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, -50) # (pointlist) + write_sint(buf, 2000) # geometry-x (absolute) + write_sint(buf, 0) # geometry-y (absolute) - write_uint(buf, 29) # PROPERTY (reuse) + write_uint(buf, 29) # PROPERTY (reuse) # POLYGON 7 - write_uint(buf, 21) # POLYGON record + write_uint(buf, 21) # POLYGON record write_byte(buf, 0b0011_1011) # 00PX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 0) # pointlist (1-delta, horiz. first) - write_uint(buf, 4) # (pointlist) dimension - write_sint(buf, 150) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, -50) # (pointlist) - write_sint(buf, 50) # (pointlist) - write_sint(buf, 3000) # geometry-x (absolute) - write_sint(buf, 0) # geometry-y (absolute) + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 0) # pointlist (1-delta, horiz. first) + write_uint(buf, 4) # (pointlist) dimension + write_sint(buf, 150) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, -50) # (pointlist) + write_sint(buf, 50) # (pointlist) + write_sint(buf, 3000) # geometry-x (absolute) + write_sint(buf, 0) # geometry-y (absolute) - write_uint(buf, 29) # PROPERTY (reuse) + write_uint(buf, 29) # PROPERTY (reuse) buf.write(FOOTER) return buf @@ -579,9 +584,9 @@ def test_file_3() -> None: assert geometry[ii].properties[0].values[1].string == 'PROP_VALUE2', msg -def write_file_4_6(buf: IO[bytes], variant: int) -> IO[bytes]: - """ - """ +def write_file_4_6(buf: BufferedIOBase, variant: int) -> BufferedIOBase: + ''' + ''' buf.write(HEADER) write_uint(buf, 10) # PROPSTRING (explicit id) @@ -600,180 +605,180 @@ def write_file_4_6(buf: IO[bytes], variant: int) -> IO[bytes]: write_bstring(buf, b'A') # Cell name # RECTANGLE 0 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0111_1011) # SWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 100) # width - write_uint(buf, 200) # height - write_sint(buf, 300) # geometry-x (relative) - write_sint(buf, -400) # geometry-y (relative) + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0111_1011) # SWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 100) # width + write_uint(buf, 200) # height + write_sint(buf, 300) # geometry-x (relative) + write_sint(buf, -400) # geometry-y (relative) + - # ** CELL ** write_uint(buf, 14) # CELL record (explicit) write_bstring(buf, b'TOP') # Cell name # PLACEMENT 0 - write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) - write_byte(buf, 0b1011_0000) # CNXY_RAAF - write_bstring(buf, b'A') # cell name - write_sint(buf, -300) # placement-x - write_sint(buf, 400) # placement-y + write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) + write_byte(buf, 0b1011_0000) # CNXY_RAAF + write_bstring(buf, b'A') # cell name + write_sint(buf, -300) # placement-x + write_sint(buf, 400) # placement-y - write_uint(buf, 28) # PROPERTY record - write_byte(buf, 0b0010_0111) # UUUU_VCNS - write_uint(buf, 0) # propname-id - write_uint(buf, 8) # prop-value 0 (unsigned int) - write_uint(buf, 25) # (...) - write_uint(buf, 10) # prop-value 1 (a-string) + write_uint(buf, 28) # PROPERTY record + write_byte(buf, 0b0010_0111) # UUUU_VCNS + write_uint(buf, 0) # propname-id + write_uint(buf, 8) # prop-value 0 (unsigned int) + write_uint(buf, 25) # (...) + write_uint(buf, 10) # prop-value 1 (a-string) write_bstring(buf, b'PROP_VALUE2') if variant == 6: - write_uint(buf, 28) # PROPERTY record - write_byte(buf, 0b0010_0111) # UUUU_VCNS - write_uint(buf, 0) # propname-id - write_uint(buf, 8) # prop-value 0 (unsigned int) - write_uint(buf, 26) # (...) - write_uint(buf, 10) # prop-value 1 (a-string) + write_uint(buf, 28) # PROPERTY record + write_byte(buf, 0b0010_0111) # UUUU_VCNS + write_uint(buf, 0) # propname-id + write_uint(buf, 8) # prop-value 0 (unsigned int) + write_uint(buf, 26) # (...) + write_uint(buf, 10) # prop-value 1 (a-string) write_bstring(buf, b'PROP_VALUE26') # PLACEMENT 1 - write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) - write_byte(buf, 0b0011_0000) # CNXY_RAAF - write_sint(buf, 0) # placement-x + write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) + write_byte(buf, 0b0011_0000) # CNXY_RAAF + write_sint(buf, 0) # placement-x if variant == 4: write_sint(buf, 200) # placement-y else: write_sint(buf, 400) # placement-y - write_uint(buf, 28) # PROPERTY record - write_byte(buf, 0b1111_0001) # UUUU_VCNS - write_uint(buf, 2) # number of values - write_uint(buf, 8) # prop-value 0 (unsigned int) - write_uint(buf, 10) # (...) - write_uint(buf, 14) # prop-value 1 (prop-string ref.) - write_uint(buf, 13) # (...) + write_uint(buf, 28) # PROPERTY record + write_byte(buf, 0b1111_0001) # UUUU_VCNS + write_uint(buf, 2) # number of values + write_uint(buf, 8) # prop-value 0 (unsigned int) + write_uint(buf, 10) # (...) + write_uint(buf, 14) # prop-value 1 (prop-string ref.) + write_uint(buf, 13) # (...) # PLACEMENT 2 - write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) - write_byte(buf, 0b0001_0000) # CNXY_RAAF - write_sint(buf, 400) # placement-y + write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) + write_byte(buf, 0b0001_0000) # CNXY_RAAF + write_sint(buf, 400) # placement-y - write_uint(buf, 28) # PROPERTY record - write_byte(buf, 0b0000_1001) # UUUU_VCNS + write_uint(buf, 28) # PROPERTY record + write_byte(buf, 0b0000_1001) # UUUU_VCNS # PLACEMENT 3 - write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) - write_byte(buf, 0b0010_0000) # CNXY_RAAF - write_sint(buf, 300) # placement-x + write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) + write_byte(buf, 0b0010_0000) # CNXY_RAAF + write_sint(buf, 300) # placement-x - write_uint(buf, 29) # PROPERTY (reuse) + write_uint(buf, 29) # PROPERTY (reuse) - write_uint(buf, 15) # XYABSOLUTE record + write_uint(buf, 15) # XYABSOLUTE record # PLACEMENT 4 - write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) - write_byte(buf, 0b0011_0001) # CNXY_RAAF - write_sint(buf, 700) # placement-x (absolute) - write_sint(buf, 400) # placement-y (absolute) + write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) + write_byte(buf, 0b0011_0001) # CNXY_RAAF + write_sint(buf, 700) # placement-x (absolute) + write_sint(buf, 400) # placement-y (absolute) - write_uint(buf, 28) # PROPERTY record - write_byte(buf, 0b0000_1001) # UUUU_VCNS + write_uint(buf, 28) # PROPERTY record + write_byte(buf, 0b0000_1001) # UUUU_VCNS - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record # PLACEMENT 5 - write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) - write_byte(buf, 0b0001_0010) # CNXY_RAAF - write_sint(buf, 1000) # placement-y (relative) + write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) + write_byte(buf, 0b0001_0010) # CNXY_RAAF + write_sint(buf, 1000) # placement-y (relative) - write_uint(buf, 28) # PROPERTY record - write_byte(buf, 0b0010_0111) # UUUU_VCNS - write_uint(buf, 0) # propname-id - write_uint(buf, 8) # prop-value 0 (unsigned int) - write_uint(buf, 25) # (...) - write_uint(buf, 10) # prop-value 1 (a-string) + write_uint(buf, 28) # PROPERTY record + write_byte(buf, 0b0010_0111) # UUUU_VCNS + write_uint(buf, 0) # propname-id + write_uint(buf, 8) # prop-value 0 (unsigned int) + write_uint(buf, 25) # (...) + write_uint(buf, 10) # prop-value 1 (a-string) write_bstring(buf, b'PROP_VALUE2') # PLACEMENT 6 - write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) - write_byte(buf, 0b0001_0011) # CNXY_RAAF - write_sint(buf, 1000) # placement-y (relative) + write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) + write_byte(buf, 0b0001_0011) # CNXY_RAAF + write_sint(buf, 1000) # placement-y (relative) - write_uint(buf, 29) # PROPERTY (reuse) + write_uint(buf, 29) # PROPERTY (reuse) - write_uint(buf, 15) # XYABSOLUTE record + write_uint(buf, 15) # XYABSOLUTE record # PLACEMENT 7 - write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) - write_byte(buf, 0b0011_1111) # CNXY_RAAF - write_sint(buf, 2000) # placement-x (absolute) - write_sint(buf, 0) # placement-y (absolute) - write_uint(buf, 1) # repetition (3x4 matrix) - write_uint(buf, 1) # (repetition) x-dimension - write_uint(buf, 2) # (repetition) y-dimension - write_uint(buf, 300) # (repetition) x-spacing - write_uint(buf, 300) # (repetition) y-spacing + write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) + write_byte(buf, 0b0011_1111) # CNXY_RAAF + write_sint(buf, 2000) # placement-x (absolute) + write_sint(buf, 0) # placement-y (absolute) + write_uint(buf, 1) # repetition (3x4 matrix) + write_uint(buf, 1) # (repetition) x-dimension + write_uint(buf, 2) # (repetition) y-dimension + write_uint(buf, 300) # (repetition) x-spacing + write_uint(buf, 300) # (repetition) y-spacing - write_uint(buf, 29) # PROPERTY (reuse) + write_uint(buf, 29) # PROPERTY (reuse) - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record # PLACEMENT 8 - write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) - write_byte(buf, 0b0011_1111) # CNXY_RAAF - write_sint(buf, 2000) # placement-x - write_sint(buf, 0) # placement-y - write_uint(buf, 0) # repetition (reuse) + write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) + write_byte(buf, 0b0011_1111) # CNXY_RAAF + write_sint(buf, 2000) # placement-x + write_sint(buf, 0) # placement-y + write_uint(buf, 0) # repetition (reuse) - write_uint(buf, 29) # PROPERTY (reuse) + write_uint(buf, 29) # PROPERTY (reuse) # PLACEMENT 9 - write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) - write_byte(buf, 0b0011_1111) # CNXY_RAAF - write_sint(buf, 2000) # placement-x - write_sint(buf, 0) # placement-y - write_uint(buf, 2) # repetition (3 cols.) - write_uint(buf, 1) # (repetition) dimension - write_uint(buf, 320) # (repetition) offset + write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) + write_byte(buf, 0b0011_1111) # CNXY_RAAF + write_sint(buf, 2000) # placement-x + write_sint(buf, 0) # placement-y + write_uint(buf, 2) # repetition (3 cols.) + write_uint(buf, 1) # (repetition) dimension + write_uint(buf, 320) # (repetition) offset - write_uint(buf, 29) # PROPERTY (reuse) + write_uint(buf, 29) # PROPERTY (reuse) # PLACEMENT 10 - write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) - write_byte(buf, 0b0011_1111) # CNXY_RAAF - write_sint(buf, 2000) # placement-x - write_sint(buf, 0) # placement-y - write_uint(buf, 3) # repetition (4 rows) - write_uint(buf, 2) # (repetition) dimension - write_uint(buf, 310) # (repetition) offset + write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) + write_byte(buf, 0b0011_1111) # CNXY_RAAF + write_sint(buf, 2000) # placement-x + write_sint(buf, 0) # placement-y + write_uint(buf, 3) # repetition (4 rows) + write_uint(buf, 2) # (repetition) dimension + write_uint(buf, 310) # (repetition) offset - write_uint(buf, 29) # PROPERTY (reuse) + write_uint(buf, 29) # PROPERTY (reuse) # PLACEMENT 11 - write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) - write_byte(buf, 0b0011_1111) # CNXY_RAAF - write_sint(buf, 2000) # placement-x - write_sint(buf, 0) # placement-y - write_uint(buf, 4) # repetition (4 arbitrary cols.) - write_uint(buf, 2) # (repetition) dimension - write_uint(buf, 320) # (repetition) - write_uint(buf, 330) # (repetition) - write_uint(buf, 340) # (repetition) + write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) + write_byte(buf, 0b0011_1111) # CNXY_RAAF + write_sint(buf, 2000) # placement-x + write_sint(buf, 0) # placement-y + write_uint(buf, 4) # repetition (4 arbitrary cols.) + write_uint(buf, 2) # (repetition) dimension + write_uint(buf, 320) # (repetition) + write_uint(buf, 330) # (repetition) + write_uint(buf, 340) # (repetition) - write_uint(buf, 29) # PROPERTY (reuse) + write_uint(buf, 29) # PROPERTY (reuse) # PLACEMENT 12 - write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) - write_byte(buf, 0b0011_1111) # CNXY_RAAF - write_sint(buf, 2000) # placement-x - write_sint(buf, 0) # placement-y - write_uint(buf, 8) # repetition (3x4 matrix, arbitrary vectors) - write_uint(buf, 1) # (repetition) n-dimension - write_uint(buf, 2) # (repetition) m-dimension - write_uint(buf, 310 << 2 | 0b01) # (repetition) n-displacement g-delta (310, 320) + write_uint(buf, 17) # PLACEMENT record (no mag, manhattan) + write_byte(buf, 0b0011_1111) # CNXY_RAAF + write_sint(buf, 2000) # placement-x + write_sint(buf, 0) # placement-y + write_uint(buf, 8) # repetition (3x4 matrix, arbitrary vectors) + write_uint(buf, 1) # (repetition) n-dimension + write_uint(buf, 2) # (repetition) m-dimension + write_uint(buf, 310 << 2 | 0b01) # (repetition) n-displacement g-delta (310, 320) write_sint(buf, 320) - write_uint(buf, 330 << 4 | 0b1010) # (repetition) m-dispalcement g-delta 330/northwest = (-330, 330) + write_uint(buf, 330 << 4 | 0b1010) # (repetition) m-dispalcement g-delta 330/northwest = (-330, 330) write_uint(buf, 29) # PROPERTY (reuse) @@ -786,8 +791,8 @@ def write_file_4_6(buf: IO[bytes], variant: int) -> IO[bytes]: def test_file_4() -> None: - """ - """ + ''' + ''' buf = write_file_4_6(BytesIO(), 4) buf.seek(0) @@ -823,7 +828,7 @@ def test_file_4() -> None: assert pp.x == [-300, 0, 0, 300, 700, 700, 700, 2000, 4000, 6000, 8000, 10000, 12000][ii], msg assert pp.y == [400, 200, 400, 400, 400, 1400, 2400, 0, 0, 0, 0, 0, 0][ii], msg - if ii == 4 or ii >= 6: + if ii == 4 or 6 <= ii: assert pp.flip, msg else: assert not pp.flip, msg @@ -855,8 +860,8 @@ def test_file_4() -> None: def test_file_6() -> None: - """ - """ + ''' + ''' buf = write_file_4_6(BytesIO(), 6) buf.seek(0) @@ -892,7 +897,7 @@ def test_file_6() -> None: assert pp.x == [-300, 0, 0, 300, 700, 700, 700, 2000, 4000, 6000, 8000, 10000, 12000][ii], msg assert pp.y == [400, 400, 400, 400, 400, 1400, 2400, 0, 0, 0, 0, 0, 0][ii], msg - if ii == 4 or ii >= 6: + if ii == 4 or 6 <= ii: assert pp.flip, msg else: assert not pp.flip, msg @@ -927,21 +932,21 @@ def test_file_6() -> None: assert placements[ii].properties[0].values[1].string == 'PROP_VALUE2', msg -def write_file_7_8_9(buf: IO[bytes], variant: int) -> IO[bytes]: - """ - """ +def write_file_7_8_9(buf: BufferedIOBase, variant: int) -> BufferedIOBase: + ''' + ''' buf.write(HEADER) - write_uint(buf, 28) # PROPERTY record - write_byte(buf, 0b0001_0100) # UUUU_VCNS + write_uint(buf, 28) # PROPERTY record + write_byte(buf, 0b0001_0100) # UUUU_VCNS write_bstring(buf, b'FileProp1') # property name - write_uint(buf, 10) # prop-value 0 (a-string) + write_uint(buf, 10) # prop-value 0 (a-string) write_bstring(buf, b'FileProp1Value') - write_uint(buf, 28) # PROPERTY record - write_byte(buf, 0b0001_0110) # UUUU_VCNS - write_uint(buf, 13) # prop-name reference - write_uint(buf, 10) # prop-value 0 (a-string) + write_uint(buf, 28) # PROPERTY record + write_byte(buf, 0b0001_0110) # UUUU_VCNS + write_uint(buf, 13) # prop-name reference + write_uint(buf, 10) # prop-value 0 (a-string) write_bstring(buf, b'FileProp1Value') write_uint(buf, 8) # PROPNAME record (explicit id) @@ -952,69 +957,70 @@ def write_file_7_8_9(buf: IO[bytes], variant: int) -> IO[bytes]: write_uint(buf, 28) # PROPERTY record if variant == 8: # Will give an error since the value modal variable is reset by PROPNAME_ID - write_byte(buf, 0b0001_1110) # UUUU_VCNS + write_byte(buf, 0b0001_1110) # UUUU_VCNS else: - write_byte(buf, 0b0001_0110) # UUUU_VCNS - write_uint(buf, 13) # prop-name reference + write_byte(buf, 0b0001_0110) # UUUU_VCNS + write_uint(buf, 13) # prop-name reference if variant != 8: - write_uint(buf, 8) # prop-value 0 (unsigned int) - write_uint(buf, 17) # (...) + write_uint(buf, 8) # prop-value 0 (unsigned int) + write_uint(buf, 17) # (...) write_uint(buf, 10) # PROPSTRING (explicit id) write_bstring(buf, b'FileProp2Value') write_uint(buf, 12) # id # associated with PROPSTRING? - write_uint(buf, 28) # PROPERTY record + write_uint(buf, 28) # PROPERTY record if variant == 9: # Will give an error since the value modal variable is unset - write_byte(buf, 0b0001_1110) # UUUU_VCNS + write_byte(buf, 0b0001_1110) # UUUU_VCNS else: - write_byte(buf, 0b0001_0110) # UUUU_VCNS - write_uint(buf, 13) # prop-name reference + write_byte(buf, 0b0001_0110) # UUUU_VCNS + write_uint(buf, 13) # prop-name reference if variant != 9: - write_uint(buf, 8) # prop-value 0 (unsigned int) - write_uint(buf, 42) # (...) + write_uint(buf, 8) # prop-value 0 (unsigned int) + write_uint(buf, 42) # (...) write_uint(buf, 3) # CELLNAME record (implicit id 0) write_bstring(buf, b'A') # associated with cell A, through CELLNAME # TODO - write_uint(buf, 28) # PROPERTY record - write_byte(buf, 0b0001_0100) # UUUU_VCNS - write_bstring(buf, b'CellProp0') # prop name - write_uint(buf, 10) # prop-value 0 (a-string) + write_uint(buf, 28) # PROPERTY record + write_byte(buf, 0b0001_0100) # UUUU_VCNS + write_bstring(buf, b'CellProp0') # prop name + write_uint(buf, 10) # prop-value 0 (a-string) write_bstring(buf, b'CPValue0') - # ** CELL ** + write_uint(buf, 13) # CELL record (name ref.) write_uint(buf, 0) # Cell name 0 (XYZ) # associated with cell A - write_uint(buf, 28) # PROPERTY record - write_byte(buf, 0b0001_0100) # UUUU_VCNS - write_bstring(buf, b'CellProp1') # prop name - write_uint(buf, 10) # prop-value 0 (a-string) + write_uint(buf, 28) # PROPERTY record + write_byte(buf, 0b0001_0100) # UUUU_VCNS + write_bstring(buf, b'CellProp1') # prop name + write_uint(buf, 10) # prop-value 0 (a-string) write_bstring(buf, b'CPValue') - write_uint(buf, 28) # PROPERTY record - write_byte(buf, 0b0001_1100) # UUUU_VCNS - write_bstring(buf, b'CellProp2') # prop name + write_uint(buf, 28) # PROPERTY record + write_byte(buf, 0b0001_1100) # UUUU_VCNS + write_bstring(buf, b'CellProp2') # prop name # RECTANGLE 0 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0111_1011) # SWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 100) # width - write_uint(buf, 200) # height - write_sint(buf, 300) # geometry-x - write_sint(buf, -400) # geometry-y + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0111_1011) # SWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 100) # width + write_uint(buf, 200) # height + write_sint(buf, 300) # geometry-x + write_sint(buf, -400) # geometry-y buf.write(FOOTER) return buf + def test_file_7() -> None: buf = write_file_7_8_9(BytesIO(), 7) @@ -1059,20 +1065,20 @@ def test_file_7() -> None: def test_file_8() -> None: - """ - """ + ''' + ''' buf = write_file_7_8_9(BytesIO(), 8) buf.seek(0) with pytest.raises(InvalidDataError): - _layout = OasisLayout.read(buf) + layout = OasisLayout.read(buf) def test_file_9() -> None: - """ - """ + ''' + ''' buf = write_file_7_8_9(BytesIO(), 9) buf.seek(0) with pytest.raises(InvalidDataError): - _layout = OasisLayout.read(buf) + layout = OasisLayout.read(buf) diff --git a/fatamorgana/test/test_files_rectangles.py b/fatamorgana/test/test_files_rectangles.py index 204b9b3..f47503f 100644 --- a/fatamorgana/test/test_files_rectangles.py +++ b/fatamorgana/test/test_files_rectangles.py @@ -1,9 +1,15 @@ -# mypy: disable-error-code="union-attr" -from typing import IO -from io import BytesIO +# type: ignore + +from typing import List, Tuple, Iterable +from itertools import chain +from io import BytesIO, BufferedIOBase +import struct + +import pytest # type: ignore from .utils import HEADER, FOOTER -from ..basic import write_uint, write_sint, write_bstring, write_byte +from ..basic import write_uint, write_sint, read_uint, read_sint, write_bstring, write_byte +from ..basic import InvalidRecordError, InvalidDataError from ..main import OasisLayout @@ -74,157 +80,157 @@ def base_tests(layout: OasisLayout) -> None: assert geometry[10].repetition.x_displacements == [200, 300] -def write_file_common(buf: IO[bytes], variant: int) -> IO[bytes]: - """ - """ +def write_file_common(buf: BufferedIOBase, variant: int) -> BufferedIOBase: + ''' + ''' assert variant in (1, 2), 'Error in test!!' buf.write(HEADER) if variant == 2: - write_uint(buf, 7) # PROPNAME record (implict id 0) - write_bstring(buf, b'PROP0') # property name + write_uint(buf, 7) # PROPNAME record (implict id 0) + write_bstring(buf, b'PROP0') # property name write_uint(buf, 14) # CELL record (explicit) write_bstring(buf, b'ABC') # Cell name # RECTANGLE 0 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0111_1011) # SWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 100) # width - write_uint(buf, 200) # height - write_sint(buf, 300) # geometry-x (absolute) - write_sint(buf, -400) # geometry-y (absolute) + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0111_1011) # SWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 100) # width + write_uint(buf, 200) # height + write_sint(buf, 300) # geometry-x (absolute) + write_sint(buf, -400) # geometry-y (absolute) if variant == 2: # PROPERTY 0 - write_uint(buf, 28) # PROPERTY record (explicit) - write_byte(buf, 0b0001_0110) # UUUU_VCNS - write_uint(buf, 0) # propname id - write_uint(buf, 2) # property value (real: positive reciprocal) - write_uint(buf, 5) # (real) 1/5 + write_uint(buf, 28) # PROPERTY record (explicit) + write_byte(buf, 0b0001_0110) # UUUU_VCNS + write_uint(buf, 0) # propname id + write_uint(buf, 2) # property value (real: positive reciprocal) + write_uint(buf, 5) # (real) 1/5 write_uint(buf, 16) # XYRELATIVE record # RECTANGLE 1 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0111_1011) # SWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 100) # width - write_uint(buf, 200) # height - write_sint(buf, 100) # geometry-x (relative) - write_sint(buf, -100) # geometry-y (relative) + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0111_1011) # SWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 100) # width + write_uint(buf, 200) # height + write_sint(buf, 100) # geometry-x (relative) + write_sint(buf, -100) # geometry-y (relative) if variant == 2: # PROPERTY 1 write_uint(buf, 29) # PROPERTY record (repeat) - write_uint(buf, 15) # XYABSOLUTE record + write_uint(buf, 15) # XYABSOLUTE record # RECTANGLE 2 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0111_1011) # SWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 100) # width - write_uint(buf, 200) # height - write_sint(buf, 600) # geometry-x (absolute) - write_sint(buf, -300) # geometry-y (absolute) + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0111_1011) # SWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 100) # width + write_uint(buf, 200) # height + write_sint(buf, 600) # geometry-x (absolute) + write_sint(buf, -300) # geometry-y (absolute) if variant == 2: # PROPERTY 2 write_uint(buf, 29) # PROPERTY record (repeat) # RECTANGLE 3 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0111_0011) # SWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 100) # width - write_uint(buf, 200) # height - write_sint(buf, 800) # geometry-x (absolute) + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0111_0011) # SWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 100) # width + write_uint(buf, 200) # height + write_sint(buf, 800) # geometry-x (absolute) if variant == 2: # PROPERTY 3 write_uint(buf, 29) # PROPERTY record (repeat) # RECTANGLE 4 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0110_1011) # SWHX_YRDL - write_uint(buf, 2) # layer - write_uint(buf, 3) # datatype - write_uint(buf, 100) # width - write_uint(buf, 200) # height - write_sint(buf, -600) # geometry-y (absolute) + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0110_1011) # SWHX_YRDL + write_uint(buf, 2) # layer + write_uint(buf, 3) # datatype + write_uint(buf, 100) # width + write_uint(buf, 200) # height + write_sint(buf, -600) # geometry-y (absolute) if variant == 2: # PROPERTY 4 write_uint(buf, 29) # PROPERTY record (repeat) # RECTANGLE 5 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0110_1000) # SWHX_YRDL - write_uint(buf, 100) # width - write_uint(buf, 200) # height - write_sint(buf, -900) # geometry-y (absolute) + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0110_1000) # SWHX_YRDL + write_uint(buf, 100) # width + write_uint(buf, 200) # height + write_sint(buf, -900) # geometry-y (absolute) if variant == 2: # PROPERTY 5 write_uint(buf, 29) # PROPERTY record (repeat) # RECTANGLE 6 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0000_1000) # SWHX_YRDL - write_sint(buf, -1200) # geometry-y (absolute) + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0000_1000) # SWHX_YRDL + write_sint(buf, -1200) # geometry-y (absolute) if variant == 2: # PROPERTY 6 write_uint(buf, 29) # PROPERTY record (repeat) # RECTANGLE 7 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b1100_1000) # SWHX_YRDL - write_uint(buf, 150) # width - write_sint(buf, -1500) # geometry-y (absolute) + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b1100_1000) # SWHX_YRDL + write_uint(buf, 150) # width + write_sint(buf, -1500) # geometry-y (absolute) if variant == 2: # PROPERTY 7 write_uint(buf, 29) # PROPERTY record (repeat) # RECTANGLE 8 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0000_1000) # SWHX_YRDL - write_sint(buf, -1800) # geometry-y (absolute) + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0000_1000) # SWHX_YRDL + write_sint(buf, -1800) # geometry-y (absolute) if variant == 2: # PROPERTY 8 write_uint(buf, 29) # PROPERTY record (repeat) # RECTANGLE 9 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0000_1100) # SWHX_YRDL - write_sint(buf, 500) # geometry-y (absolute) - write_uint(buf, 1) # repetition (3x4 matrix) - write_uint(buf, 1) # (repetition) x-dimension - write_uint(buf, 2) # (repetition) y-dimension - write_uint(buf, 200) # (repetition) x-spacing - write_uint(buf, 300) # (repetition) y-spacing + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0000_1100) # SWHX_YRDL + write_sint(buf, 500) # geometry-y (absolute) + write_uint(buf, 1) # repetition (3x4 matrix) + write_uint(buf, 1) # (repetition) x-dimension + write_uint(buf, 2) # (repetition) y-dimension + write_uint(buf, 200) # (repetition) x-spacing + write_uint(buf, 300) # (repetition) y-spacing if variant == 2: # PROPERTY 9 write_uint(buf, 29) # PROPERTY record (repeat) # RECTANGLE 10 - write_uint(buf, 20) # RECTANGLE record - write_byte(buf, 0b0000_1100) # SWHX_YRDL - write_sint(buf, 2000) # geometry-y (absolute) - write_uint(buf, 4) # repetition (3 arbitrary cols.) - write_uint(buf, 1) # (repetition) dimension - write_uint(buf, 200) # (repetition) x-delta - write_uint(buf, 300) # (repetition) x-delta + write_uint(buf, 20) # RECTANGLE record + write_byte(buf, 0b0000_1100) # SWHX_YRDL + write_sint(buf, 2000) # geometry-y (absolute) + write_uint(buf, 4) # repetition (3 arbitrary cols.) + write_uint(buf, 1) # (repetition) dimension + write_uint(buf, 200) # (repetition) x-delta + write_uint(buf, 300) # (repetition) x-delta if variant == 2: # PROPERTY 10 @@ -267,7 +273,7 @@ def test_file_2() -> None: prop = gg.properties[0] assert prop.name == 0, msg - assert len(prop.values) == 1, msg # type: ignore - assert prop.values[0].numerator == 1, msg # type: ignore - assert prop.values[0].denominator == 5, msg # type: ignore + assert len(prop.values) == 1, msg + assert prop.values[0].numerator == 1, msg + assert prop.values[0].denominator == 5, msg diff --git a/fatamorgana/test/test_files_texts.py b/fatamorgana/test/test_files_texts.py index 8980868..c23877c 100644 --- a/fatamorgana/test/test_files_texts.py +++ b/fatamorgana/test/test_files_texts.py @@ -1,11 +1,14 @@ -# mypy: disable-error-code="union-attr, index" -from typing import IO -from io import BytesIO +# type: ignore -import pytest +from typing import List, Tuple, Iterable +from itertools import chain +from io import BytesIO, BufferedIOBase +import struct + +import pytest # type: ignore from .utils import HEADER, FOOTER -from ..basic import write_uint, write_sint, write_bstring, write_byte +from ..basic import write_uint, write_sint, read_uint, read_sint, write_bstring, write_byte from ..basic import InvalidRecordError, InvalidDataError from ..basic import GridRepetition, ArbitraryRepetition from ..main import OasisLayout @@ -30,8 +33,8 @@ def common_tests(layout: OasisLayout) -> None: geometry = layout.cells[0].geometry - assert geometry[0].layer == 1 - assert geometry[0].datatype == 2 + geometry[0].layer == 1 + geometry[0].datatype == 2 for ii, gg in enumerate(geometry[1:]): assert gg.layer == 2, f'textstring #{ii + 1}' assert gg.datatype == 1, f'textstring #{ii + 1}' @@ -83,12 +86,12 @@ def common_tests(layout: OasisLayout) -> None: assert geometry[13].repetition.b_vector == [-10, 10] assert geometry[14].repetition.a_count == 3 - assert geometry[14].repetition.b_count is None + assert geometry[14].repetition.b_count == None assert geometry[14].repetition.a_vector == [11, 12] assert geometry[14].repetition.b_vector is None assert geometry[15].repetition.a_count == 4 - assert geometry[15].repetition.b_count is None + assert geometry[15].repetition.b_count == None assert geometry[15].repetition.a_vector == [-10, 10] assert geometry[15].repetition.b_vector is None @@ -99,10 +102,10 @@ def common_tests(layout: OasisLayout) -> None: assert geometry[19].repetition.y_displacements == [12, -9] -def write_file_common(buf: IO[bytes], variant: int) -> IO[bytes]: - """ +def write_file_common(buf: BufferedIOBase, variant: int) -> BufferedIOBase: + ''' Single cell with explicit name 'XYZ' - """ + ''' assert variant in (1, 2, 5, 12), 'Error in test!!' buf.write(HEADER) @@ -253,9 +256,9 @@ def write_file_common(buf: IO[bytes], variant: int) -> IO[bytes]: write_uint(buf, 8) # repetition (3x4 matrix w/arb. vectors) write_uint(buf, 1) # (repetition) n-dimension write_uint(buf, 2) # (repetition) m-dimension - write_uint(buf, (10 << 4) | 0b0000) # (repetition) n-displacement g-delta: 10/east = (10, 0) - write_uint(buf, (11 << 2) | 0b11) # (repetition) m-displacement g-delta: (-11, -12) - write_sint(buf, -12) # (repetition g-delta) + write_uint(buf, (10 << 4) | 0b0000) # (repetition) n-displacement g-delta: 10/east = (10, 0) + write_uint(buf, (11 << 2) | 0b11) # (repetition) m-displacement g-delta: (-11, -12) + write_sint(buf, -12) # (repetition g-delta) # TEXT 13 write_uint(buf, 19) # TEXT record @@ -264,9 +267,9 @@ def write_file_common(buf: IO[bytes], variant: int) -> IO[bytes]: write_uint(buf, 8) # repetition (3x4 matrix w/arb. vectors) write_uint(buf, 1) # (repetition) n-dimension write_uint(buf, 2) # (repetition) m-dimension - write_uint(buf, (11 << 2) | 0b01) # (repetition) n-displacement g-delta: (11, 12) + write_uint(buf, (11 << 2) | 0b01) # (repetition) n-displacement g-delta: (11, 12) write_sint(buf, 12) - write_uint(buf, (10 << 4) | 0b1010) # (repetition) n-displacement g-delta: 10/northwest = (-10, 10) + write_uint(buf, (10 << 4) | 0b1010) # (repetition) n-displacement g-delta: 10/northwest = (-10, 10) # TEXT 14 write_uint(buf, 19) # TEXT record @@ -460,11 +463,11 @@ def test_file_12() -> None: assert layout.textstrings[2].string == 'B' -def write_file_3(buf: IO[bytes]) -> IO[bytes]: - """ +def write_file_3(buf: BufferedIOBase) -> BufferedIOBase: + ''' File with one textstring with explicit id, and one with an implicit id. Should fail. - """ + ''' buf.write(HEADER) write_uint(buf, 6) # TEXTSTRING record (explicit id) @@ -494,15 +497,15 @@ def test_file_3() -> None: buf.seek(0) with pytest.raises(InvalidRecordError): - _layout = OasisLayout.read(buf) + layout = OasisLayout.read(buf) -def write_file_4(buf: IO[bytes]) -> IO[bytes]: - """ +def write_file_4(buf: BufferedIOBase) -> BufferedIOBase: + ''' File with a TEXT record that references a non-existent TEXTSTRING TODO add an optional check for valid references - """ + ''' buf.write(HEADER) write_uint(buf, 5) # TEXTSTRING record (implicit id 0) @@ -537,10 +540,10 @@ def test_file_4() -> None: base_tests(layout) -def write_file_6(buf: IO[bytes]) -> IO[bytes]: - """ +def write_file_6(buf: BufferedIOBase) -> BufferedIOBase: + ''' File with TEXT record that uses an un-filled modal for the repetition - """ + ''' buf.write(HEADER) write_uint(buf, 5) # TEXTSTRING record (implicit id 0) @@ -567,13 +570,13 @@ def test_file_6() -> None: buf.seek(0) with pytest.raises(InvalidDataError): - _layout = OasisLayout.read(buf) + layout = OasisLayout.read(buf) -def write_file_7(buf: IO[bytes]) -> IO[bytes]: - """ +def write_file_7(buf: BufferedIOBase) -> BufferedIOBase: + ''' File with TEXT record that uses an un-filled modal for the layer - """ + ''' buf.write(HEADER) write_uint(buf, 5) # TEXTSTRING record (implicit id 0) @@ -598,13 +601,13 @@ def test_file_7() -> None: buf.seek(0) with pytest.raises(InvalidDataError): - _layout = OasisLayout.read(buf) + layout = OasisLayout.read(buf) -def write_file_8(buf: IO[bytes]) -> IO[bytes]: - """ +def write_file_8(buf: BufferedIOBase) -> BufferedIOBase: + ''' File with TEXT record that uses an un-filled modal for the datatype - """ + ''' buf.write(HEADER) write_uint(buf, 5) # TEXTSTRING record (implicit id 0) @@ -629,13 +632,13 @@ def test_file_8() -> None: buf.seek(0) with pytest.raises(InvalidDataError): - _layout = OasisLayout.read(buf) + layout = OasisLayout.read(buf) -def write_file_9(buf: IO[bytes]) -> IO[bytes]: - """ +def write_file_9(buf: BufferedIOBase) -> BufferedIOBase: + ''' File with TEXT record that uses a default modal for the x coordinate - """ + ''' buf.write(HEADER) write_uint(buf, 5) # TEXTSTRING record (implicit id 0) @@ -668,10 +671,10 @@ def test_file_9() -> None: assert text.y == -200 -def write_file_10(buf: IO[bytes]) -> IO[bytes]: - """ +def write_file_10(buf: BufferedIOBase) -> BufferedIOBase: + ''' File with TEXT record that uses a default modal for the y coordinate - """ + ''' buf.write(HEADER) write_uint(buf, 5) # TEXTSTRING record (implicit id 0) @@ -704,10 +707,10 @@ def test_file_10() -> None: assert text.x == 100 -def write_file_11(buf: IO[bytes]) -> IO[bytes]: - """ +def write_file_11(buf: BufferedIOBase) -> BufferedIOBase: + ''' File with TEXT record that uses an un-filled modal for the text string - """ + ''' buf.write(HEADER) write_uint(buf, 5) # TEXTSTRING record (implicit id 0) @@ -732,4 +735,4 @@ def test_file_11() -> None: buf.seek(0) with pytest.raises(InvalidDataError): - _layout = OasisLayout.read(buf) + layout = OasisLayout.read(buf) diff --git a/fatamorgana/test/test_files_trapezoids.py b/fatamorgana/test/test_files_trapezoids.py index 262f449..1bd67e1 100644 --- a/fatamorgana/test/test_files_trapezoids.py +++ b/fatamorgana/test/test_files_trapezoids.py @@ -1,9 +1,17 @@ -# mypy: disable-error-code="union-attr" -from typing import IO -from io import BytesIO +# type: ignore + +from typing import List, Tuple, Iterable +from itertools import chain +from io import BytesIO, BufferedIOBase +import struct + +import pytest # type: ignore +import numpy +from numpy.testing import assert_equal from .utils import HEADER, FOOTER -from ..basic import write_uint, write_sint, write_bstring, write_byte +from ..basic import write_uint, write_sint, read_uint, read_sint, write_bstring, write_byte, PathExtensionScheme +from ..basic import InvalidRecordError, InvalidDataError from ..main import OasisLayout @@ -24,150 +32,150 @@ def base_tests(layout: OasisLayout) -> None: assert not layout.cells[0].properties -def write_file_1(buf: IO[bytes]) -> IO[bytes]: - """ - """ +def write_file_1(buf: BufferedIOBase) -> BufferedIOBase: + ''' + ''' buf.write(HEADER) - write_uint(buf, 14) # CELL record (explicit) - write_bstring(buf, b'ABC') # Cell name + write_uint(buf, 14) # CELL record (explicit) + write_bstring(buf, b'ABC') # Cell name # Trapezoid 0 - write_uint(buf, 23) # TRAPEZOID record - write_byte(buf, 0b0111_1011) # OWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 100) # width - write_uint(buf, 50) # height - write_sint(buf, -20) # delta-a - write_sint(buf, 40) # delta-b - write_sint(buf, 0) # geometry-x (absolute) - write_sint(buf, 100) # geometry-y (absolute) + write_uint(buf, 23) # TRAPEZOID record + write_byte(buf, 0b0111_1011) # OWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 100) # width + write_uint(buf, 50) # height + write_sint(buf, -20) # delta-a + write_sint(buf, 40) # delta-b + write_sint(buf, 0) # geometry-x (absolute) + write_sint(buf, 100) # geometry-y (absolute) - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record # Trapezoid 1 - write_uint(buf, 23) # TRAPEZOID record - write_byte(buf, 0b1010_1011) # OWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 50) # height - write_sint(buf, 20) # delta-a - write_sint(buf, 40) # delta-b - write_sint(buf, 300) # geometry-y (absolute) + write_uint(buf, 23) # TRAPEZOID record + write_byte(buf, 0b1010_1011) # OWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 50) # height + write_sint(buf, 20) # delta-a + write_sint(buf, 40) # delta-b + write_sint(buf, 300) # geometry-y (absolute) # Trapezoid 2 - write_uint(buf, 23) # TRAPEZOID record - write_byte(buf, 0b1100_1001) # OWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 150) # width - write_sint(buf, 20) # delta-a - write_sint(buf, -20) # delta-b - write_sint(buf, 300) # geometry-y (relative) + write_uint(buf, 23) # TRAPEZOID record + write_byte(buf, 0b1100_1001) # OWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 150) # width + write_sint(buf, 20) # delta-a + write_sint(buf, -20) # delta-b + write_sint(buf, 300) # geometry-y (relative) # Trapezoid 3 - write_uint(buf, 23) # TRAPEZOID record - write_byte(buf, 0b0100_1101) # OWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 150) # width - write_sint(buf, 20) # delta-a - write_sint(buf, -20) # delta-b - write_sint(buf, 300) # geometry-y (relative) - write_uint(buf, 1) # repetition (3x4 matrix) - write_uint(buf, 1) # (repetition) x-dimension - write_uint(buf, 2) # (repetition) y-dimension - write_uint(buf, 200) # (repetition) x-spacing - write_uint(buf, 300) # (repetition) y-spacing + write_uint(buf, 23) # TRAPEZOID record + write_byte(buf, 0b0100_1101) # OWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 150) # width + write_sint(buf, 20) # delta-a + write_sint(buf, -20) # delta-b + write_sint(buf, 300) # geometry-y (relative) + write_uint(buf, 1) # repetition (3x4 matrix) + write_uint(buf, 1) # (repetition) x-dimension + write_uint(buf, 2) # (repetition) y-dimension + write_uint(buf, 200) # (repetition) x-spacing + write_uint(buf, 300) # (repetition) y-spacing - write_uint(buf, 15) # XYABSOLUTE record + write_uint(buf, 15) # XYABSOLUTE record # Trapezoid 4 - write_uint(buf, 24) # TRAPEZOID record - write_byte(buf, 0b0111_1011) # OWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 100) # width - write_uint(buf, 50) # height - write_sint(buf, -20) # delta-a - write_sint(buf, 1000) # geometry-x (absolute) - write_sint(buf, 100) # geometry-y (absolute) + write_uint(buf, 24) # TRAPEZOID record + write_byte(buf, 0b0111_1011) # OWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 100) # width + write_uint(buf, 50) # height + write_sint(buf, -20) # delta-a + write_sint(buf, 1000) # geometry-x (absolute) + write_sint(buf, 100) # geometry-y (absolute) - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record # Trapezoid 5 - write_uint(buf, 24) # TRAPEZOID record - write_byte(buf, 0b1010_1011) # OWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 50) # height - write_sint(buf, 20) # delta-a - write_sint(buf, 300) # geometry-y (relative) + write_uint(buf, 24) # TRAPEZOID record + write_byte(buf, 0b1010_1011) # OWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 50) # height + write_sint(buf, 20) # delta-a + write_sint(buf, 300) # geometry-y (relative) # Trapezoid 6 - write_uint(buf, 24) # TRAPEZOID record - write_byte(buf, 0b1100_1001) # OWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 150) # width - write_sint(buf, 20) # delta-a - write_sint(buf, 300) # geometry-y (relative) + write_uint(buf, 24) # TRAPEZOID record + write_byte(buf, 0b1100_1001) # OWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 150) # width + write_sint(buf, 20) # delta-a + write_sint(buf, 300) # geometry-y (relative) # Trapezoid 7 - write_uint(buf, 24) # TRAPEZOID record - write_byte(buf, 0b0100_1101) # OWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 150) # width - write_sint(buf, 20) # delta-a - write_sint(buf, 300) # geometry-y (relative) - write_uint(buf, 1) # repetition (3x4 matrix) - write_uint(buf, 1) # (repetition) x-dimension - write_uint(buf, 2) # (repetition) y-dimension - write_uint(buf, 200) # (repetition) x-spacing - write_uint(buf, 300) # (repetition) y-spacing + write_uint(buf, 24) # TRAPEZOID record + write_byte(buf, 0b0100_1101) # OWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 150) # width + write_sint(buf, 20) # delta-a + write_sint(buf, 300) # geometry-y (relative) + write_uint(buf, 1) # repetition (3x4 matrix) + write_uint(buf, 1) # (repetition) x-dimension + write_uint(buf, 2) # (repetition) y-dimension + write_uint(buf, 200) # (repetition) x-spacing + write_uint(buf, 300) # (repetition) y-spacing - write_uint(buf, 15) # XYABSOLUTE record + write_uint(buf, 15) # XYABSOLUTE record # Trapezoid 8 - write_uint(buf, 25) # TRAPEZOID record - write_byte(buf, 0b0111_1011) # OWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 100) # width - write_uint(buf, 50) # height - write_sint(buf, 40) # delta-b - write_sint(buf, 2000) # geometry-x (absolute) - write_sint(buf, 100) # geometry-y (absolute) + write_uint(buf, 25) # TRAPEZOID record + write_byte(buf, 0b0111_1011) # OWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 100) # width + write_uint(buf, 50) # height + write_sint(buf, 40) # delta-b + write_sint(buf, 2000) # geometry-x (absolute) + write_sint(buf, 100) # geometry-y (absolute) - write_uint(buf, 16) # XYRELATIVE record + write_uint(buf, 16) # XYRELATIVE record # Trapezoid 9 - write_uint(buf, 25) # TRAPEZOID record - write_byte(buf, 0b1010_1011) # OWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 2) # datatype - write_uint(buf, 50) # height - write_sint(buf, 40) # delta-b - write_sint(buf, 300) # geometry-y (relative) + write_uint(buf, 25) # TRAPEZOID record + write_byte(buf, 0b1010_1011) # OWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 2) # datatype + write_uint(buf, 50) # height + write_sint(buf, 40) # delta-b + write_sint(buf, 300) # geometry-y (relative) # Trapezoid 10 - write_uint(buf, 25) # TRAPEZOID record - write_byte(buf, 0b1100_1001) # OWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 150) # width - write_sint(buf, -20) # delta-b - write_sint(buf, 300) # geometry-y (relative) + write_uint(buf, 25) # TRAPEZOID record + write_byte(buf, 0b1100_1001) # OWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 150) # width + write_sint(buf, -20) # delta-b + write_sint(buf, 300) # geometry-y (relative) # Trapezoid 11 - write_uint(buf, 25) # TRAPEZOID record - write_byte(buf, 0b0100_1101) # OWHX_YRDL - write_uint(buf, 1) # layer - write_uint(buf, 150) # width - write_sint(buf, -20) # delta-b - write_sint(buf, 300) # geometry-y (relative) - write_uint(buf, 1) # repetition (3x4 matrix) - write_uint(buf, 1) # (repetition) x-dimension - write_uint(buf, 2) # (repetition) y-dimension - write_uint(buf, 200) # (repetition) x-spacing - write_uint(buf, 300) # (repetition) y-spacing + write_uint(buf, 25) # TRAPEZOID record + write_byte(buf, 0b0100_1101) # OWHX_YRDL + write_uint(buf, 1) # layer + write_uint(buf, 150) # width + write_sint(buf, -20) # delta-b + write_sint(buf, 300) # geometry-y (relative) + write_uint(buf, 1) # repetition (3x4 matrix) + write_uint(buf, 1) # (repetition) x-dimension + write_uint(buf, 2) # (repetition) y-dimension + write_uint(buf, 200) # (repetition) x-spacing + write_uint(buf, 300) # (repetition) y-spacing buf.write(FOOTER) return buf @@ -209,7 +217,7 @@ def test_file_1() -> None: if ii in (0, 4): assert gg.delta_a == -20, msg - elif ii >= 8: + elif 8 <= ii: assert gg.delta_a == 0, msg else: assert gg.delta_a == 20, msg diff --git a/fatamorgana/test/test_int.py b/fatamorgana/test/test_int.py index c974535..5f44053 100644 --- a/fatamorgana/test/test_int.py +++ b/fatamorgana/test/test_int.py @@ -1,6 +1,9 @@ +from typing import List, Tuple, Iterable from itertools import chain from io import BytesIO +import pytest # type: ignore + from ..basic import read_uint, read_sint, write_uint, write_sint diff --git a/fatamorgana/test/utils.py b/fatamorgana/test/utils.py index 7985908..0f60e7c 100644 --- a/fatamorgana/test/utils.py +++ b/fatamorgana/test/utils.py @@ -1,6 +1,12 @@ -from io import BytesIO +from typing import List, Tuple, Iterable +from itertools import chain +from io import BytesIO, BufferedIOBase +import struct -from ..basic import write_uint, write_bstring, write_byte +import pytest # type: ignore + +from ..basic import write_uint, write_sint, read_uint, read_sint, write_bstring, write_byte +from ..main import OasisLayout MAGIC_BYTES = b'%SEMI-OASIS\r\n' diff --git a/pyproject.toml b/pyproject.toml index c91c0d1..146471e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,7 +44,7 @@ classifiers = [ "Topic :: Scientific/Engineering", "Topic :: Scientific/Engineering :: Electronic Design Automation (EDA)", ] -requires-python = ">=3.11" +requires-python = ">=3.8" dynamic = ["version"] dependencies = [ ] @@ -53,38 +53,4 @@ dependencies = [ path = "fatamorgana/__init__.py" [project.optional-dependencies] -numpy = ["numpy>=1.26"] - - -[tool.ruff] -exclude = [ - ".git", - "dist", - ] -line-length = 145 -indent-width = 4 -lint.dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" -lint.select = [ - "NPY", "E", "F", "W", "B", "ANN", "UP", "SLOT", "SIM", "LOG", - "C4", "ISC", "PIE", "PT", "RET", "TCH", "PTH", "INT", - "ARG", "PL", "R", "TRY", - "G010", "G101", "G201", "G202", - "Q002", "Q003", "Q004", - ] -lint.ignore = [ - #"ANN001", # No annotation - "ANN002", # *args - "ANN003", # **kwargs - "ANN401", # Any - "ANN101", # self: Self - "SIM108", # single-line if / else assignment - "RET504", # x=y+z; return x - "PIE790", # unnecessary pass - "ISC003", # non-implicit string concatenation - "C408", # dict(x=y) instead of {'x': y} - "PLR09", # Too many xxx - "PLR2004", # magic number - "PLC0414", # import x as x - "TRY003", # Long exception message - ] - +numpy = ["numpy~=1.21"]