diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..0042015 --- /dev/null +++ b/.flake8 @@ -0,0 +1,29 @@ +[flake8] +ignore = + # E501 line too long + E501, + # W391 newlines at EOF + W391, + # E241 multiple spaces after comma + E241, + # E302 expected 2 newlines + E302, + # W503 line break before binary operator (to be deprecated) + W503, + # E265 block comment should start with '# ' + E265, + # E123 closing bracket does not match indentation of opening bracket's line + E123, + # E124 closing bracket does not match visual indentation + E124, + # E221 multiple spaces before operator + E221, + # E201 whitespace after '[' + E201, + # E741 ambiguous variable name 'I' + E741, + + +per-file-ignores = + # F401 import without use + */__init__.py: F401, diff --git a/.gitignore b/.gitignore index f0e9ec8..27d5b19 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,9 @@ *.pyc __pycache__ *.idea +.mypy_cache/ build dist fatamorgana.egg-info +docs diff --git a/MANIFEST.in b/MANIFEST.in index c28ab72..3d18ec7 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,2 +1,3 @@ include README.md include LICENSE.md +include fatamorgana/VERSION diff --git a/README.md b/README.md index 92325cd..fc5f845 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# fatamorgana +# fatamorgana **fatamorgana** is a Python package for reading and writing OASIS format layout files. @@ -6,7 +6,7 @@ **Capabilities:** * This package is a work-in-progress and is largely untested -- it works for - the tasks I usually use it for, but I can't guarantee I've even + the tasks I usually use it for, but I can't guarantee I've even tried the features you happen to use! Use at your own risk! * Interfaces and datastructures are subject to change! * That said the following work for me: @@ -26,12 +26,12 @@ Install with pip from PyPi (preferred): ```bash -pip install fatamorgana +pip3 install fatamorgana ``` Install directly from git repository: ```bash -pip install git+https://mpxd.net/code/jan/fatamorgana.git@release +pip3 install git+https://mpxd.net/code/jan/fatamorgana.git@release ``` ## Documentation @@ -53,7 +53,7 @@ Read an OASIS file and write it back out: with open('test.oas', 'rb') as f: layout = fatamorgana.OasisLayout.read(f) - + with open('test_write.oas', 'wb') as f: layout.write(f) ``` diff --git a/fatamorgana/VERSION.py b/fatamorgana/VERSION.py new file mode 100644 index 0000000..511362f --- /dev/null +++ b/fatamorgana/VERSION.py @@ -0,0 +1,4 @@ +""" VERSION defintion. THIS FILE IS MANUALLY PARSED BY setup.py and REQUIRES A SPECIFIC FORMAT """ +__version__ = ''' +0.11 +''' diff --git a/fatamorgana/__init__.py b/fatamorgana/__init__.py index 0e8485b..4e414c1 100644 --- a/fatamorgana/__init__.py +++ b/fatamorgana/__init__.py @@ -16,13 +16,26 @@ Dependencies: - Python 3.5 or later - - numpy (optional, no additional functionality) + - numpy (optional, faster but no additional functionality) + + To get started, try: + ```python3 + import fatamorgana + help(fatamorgana.OasisLayout) + ``` """ +import pathlib + from .main import OasisLayout, Cell, XName -from .basic import NString, AString, Validation, OffsetTable, OffsetEntry, \ - EOFError, SignedError, InvalidDataError, InvalidRecordError +from .basic import ( + NString, AString, Validation, OffsetTable, OffsetEntry, + EOFError, SignedError, InvalidDataError, InvalidRecordError, + UnfilledModalError, + ReuseRepetition, GridRepetition, ArbitraryRepetition + ) __author__ = 'Jan Petykiewicz' -version = '0.4' +from .VERSION import __version__ +version = __version__ diff --git a/fatamorgana/basic.py b/fatamorgana/basic.py index 4db5ba0..2176cb5 100644 --- a/fatamorgana/basic.py +++ b/fatamorgana/basic.py @@ -2,26 +2,27 @@ This module contains all datatypes and parsing/writing functions for all abstractions below the 'record' or 'block' level. """ +from typing import List, Tuple, Type, Union, Optional, Any, Sequence from fractions import Fraction -from typing import List, Tuple, Type from enum import Enum import math import struct import io +import warnings try: import numpy _USE_NUMPY = True -except: +except ImportError: _USE_NUMPY = False ''' Type definitions ''' -real_t = int or float or Fraction -repetition_t = 'ReuseRepetition' or 'GridRepetition' or 'ArbitraryRepetition' -property_value_t = int or bytes or 'AString' or 'NString' or' PropStringReference' or float or Fraction +real_t = Union[int, float, Fraction] +repetition_t = Union['ReuseRepetition', 'GridRepetition', 'ArbitraryRepetition'] +property_value_t = Union[int, bytes, 'AString', 'NString', 'PropStringReference', float, Fraction] class FatamorganaError(Exception): @@ -58,6 +59,12 @@ class InvalidRecordError(FatamorganaError): """ pass +class UnfilledModalError(FatamorganaError): + """ + Attempted to call .get_var(), but var() was None! + """ + pass + class PathExtensionScheme(Enum): """ @@ -68,11 +75,10 @@ class PathExtensionScheme(Enum): Arbitrary = 3 - ''' Constants ''' -MAGIC_BYTES = b'%SEMI-OASIS\r\n' # type: bytes +MAGIC_BYTES: bytes = b'%SEMI-OASIS\r\n' ''' @@ -83,10 +89,15 @@ 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. - :param stream: Stream to read from. - :param n: Number of bytes to read. - :return: The bytes that were read. - :raises: EOFError if not enough bytes could be read. + Args: + stream: Stream to read from. + n: Number of bytes to read. + + Returns: + The bytes that were read. + + Raises: + EOFError if not enough bytes could be read. """ b = stream.read(n) if len(b) != n: @@ -98,8 +109,11 @@ def read_byte(stream: io.BufferedIOBase) -> int: """ Read a single byte and return it. - :param stream: Stream to read from. - :return: The byte that was read. + Args: + stream: Stream to read from. + + Returns: + The byte that was read. """ return _read(stream, 1)[0] @@ -108,65 +122,90 @@ def write_byte(stream: io.BufferedIOBase, n: int) -> int: """ Write a single byte to the stream. - :param stream: Stream to read from. - :return: The number of bytes writen (1). + Args: + stream: Stream to read from. + + Returns: + The number of bytes writen (1). """ return stream.write(bytes((n,))) +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. + + Args: + stream: Stream to read from. + + Returns: + A list of 8 booleans corresponding to the bits (MSB first). + """ + byte = _read(stream, 1)[0] + bits = [bool((byte >> i) & 0x01) for i in reversed(range(8))] + return bits + +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. + + Args: + stream: Stream to write to. + bits: A list of 8 booleans corresponding to the bits (MSB first). + + Returns: + Number of bytes written (1). + + Raises: + InvalidDataError if didn't receive 8 bits. + """ + if len(bits) != 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 + return stream.write(bytes((byte,))) + + if _USE_NUMPY: - def read_bool_byte(stream: io.BufferedIOBase) -> List[bool]: + 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. - :param stream: Stream to read from. - :return: A list of 8 booleans corresponding to the bits (MSB first). + Args: + stream: Stream to read from. + + Returns: + A list of 8 booleans corresponding to the bits (MSB first). """ byte_arr = _read(stream, 1) return numpy.unpackbits(numpy.frombuffer(byte_arr, dtype=numpy.uint8)) - def write_bool_byte(stream: io.BufferedIOBase, bits: Tuple[bool]) -> 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. - :param stream: Stream to write to. - :param bits: A list of 8 booleans corresponding to the bits (MSB first). - :return: Number of bytes written (1). - :raises: InvalidDataError if didn't receive 8 bits. + Args: + stream: Stream to write to. + bits: A list of 8 booleans corresponding to the bits (MSB first). + + Returns: + Number of bytes written (1). + + Raises: + InvalidDataError if didn't receive 8 bits. """ if len(bits) != 8: raise InvalidDataError('write_bool_byte received {} bits, requires 8'.format(len(bits))) - return stream.write(numpy.packbits(bits)) + return stream.write(numpy.packbits(bits)[0]) + + read_bool_byte = _np_read_bool_byte + write_bool_byte = _np_write_bool_byte else: - def 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. - - :param stream: Stream to read from. - :return: A list of 8 booleans corresponding to the bits (MSB first). - """ - byte = _read(1)[0] - bits = [(byte >> i) & 0x01 for i in reversed(range(8))] - return bits - - def write_bool_byte(stream: io.BufferedIOBase, bits: Tuple[bool]) -> int: - """ - Pack 8 booleans into a byte, and write it to the stream. - - :param stream: Stream to write to. - :param bits: A list of 8 booleans corresponding to the bits (MSB first). - :return: Number of bytes written (1). - :raises: InvalidDataError if didn't receive 8 bits. - """ - if len(bits) != 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 - return stream.write(bytes((byte))) - + read_bool_byte = _py_read_bool_byte + write_bool_byte = _py_write_bool_byte def read_uint(stream: io.BufferedIOBase) -> int: """ @@ -177,8 +216,11 @@ def read_uint(stream: io.BufferedIOBase) -> int: - Remaining bits of each byte form the binary representation of the integer, but are stored _least significant group first_. - :param stream: Stream to read from. - :return: The integer's value. + Args: + stream: Stream to read from. + + Returns: + The integer's value. """ result = 0 i = 0 @@ -194,12 +236,17 @@ def read_uint(stream: io.BufferedIOBase) -> int: def write_uint(stream: io.BufferedIOBase, n: int) -> int: """ Write an unsigned integer to the stream. - See format details in read_uint(...). + See format details in `read_uint()`. - :param stream: Stream to write to. - :param n: Value to write. - :return: The number of bytes written. - :raises: SignedError if n is negative. + Args: + stream: Stream to write to. + n: Value to write. + + Returns: + The number of bytes written. + + Raises: + SignedError: if `n` is negative. """ if n < 0: raise SignedError('uint must be positive: {}'.format(n)) @@ -226,8 +273,11 @@ def decode_sint(uint: int) -> int: - The LSB is treated as the sign bit - The remainder of the bits encodes the absolute value - :param uint: Unsigned integer to decode from. - :return: The decoded signed integer. + Args: + uint: Unsigned integer to decode from. + + Returns: + The decoded signed integer. """ return (uint >> 1) * (1 - 2 * (0x01 & uint)) @@ -235,10 +285,13 @@ def decode_sint(uint: int) -> int: def encode_sint(sint: int) -> int: """ Encode a signed integer into its corresponding unsigned integer form. - See decode_sint() for format details. + See `decode_sint()` for format details. - :param int: The signed integer to encode. - :return: Unsigned integer encoding for the input. + Args: + int: The signed integer to encode. + + Returns: + Unsigned integer encoding for the input. """ return (abs(sint) << 1) | (sint < 0) @@ -246,10 +299,13 @@ def encode_sint(sint: int) -> int: def read_sint(stream: io.BufferedIOBase) -> int: """ Read a signed integer from the stream. - See decode_sint() for format details. + See `decode_sint()` for format details. - :param stream: Stream to read from. - :return: The integer's value. + Args: + stream: Stream to read from. + + Returns: + The integer's value. """ return decode_sint(read_uint(stream)) @@ -257,11 +313,14 @@ def read_sint(stream: io.BufferedIOBase) -> int: def write_sint(stream: io.BufferedIOBase, n: int) -> int: """ Write a signed integer to the stream. - See decode_sint() for format details. + See `decode_sint()` for format details. - :param stream: Stream to write to. - :param n: Value to write. - :return: The number of bytes written. + Args: + stream: Stream to write to. + n: Value to write. + + Returns: + The number of bytes written. """ return write_uint(stream, encode_sint(n)) @@ -273,8 +332,11 @@ def read_bstring(stream: io.BufferedIOBase) -> bytes: - length: uint - data: bytes - :param stream: Stream to read from. - :return: Bytes containing the binary string. + Args: + stream: Stream to read from. + + Returns: + Bytes containing the binary string. """ length = read_uint(stream) return _read(stream, length) @@ -283,11 +345,14 @@ def read_bstring(stream: io.BufferedIOBase) -> bytes: def write_bstring(stream: io.BufferedIOBase, bstring: bytes): """ Write a binary string to the stream. - See read_bstring() for format details. + See `read_bstring()` for format details. - :param stream: Stream to write to. - :param bstring: Binary string to write. - :return: The number of bytes written. + Args: + stream: Stream to write to. + bstring: Binary string to write. + + Returns: + The number of bytes written. """ write_uint(stream, len(bstring)) return stream.write(bstring) @@ -300,8 +365,11 @@ def read_ratio(stream: io.BufferedIOBase) -> Fraction: - numerator: uint - denominator: uint - :param stream: Stream to read from. - :return: Fraction object containing the read value. + Args: + stream: Stream to read from. + + Returns: + Fraction object containing the read value. """ numer = read_uint(stream) denom = read_uint(stream) @@ -311,12 +379,17 @@ def read_ratio(stream: io.BufferedIOBase) -> Fraction: def write_ratio(stream: io.BufferedIOBase, r: Fraction) -> int: """ Write an unsigned ratio to the stream. - See read_ratio() for format details. + See `read_ratio()` for format details. - :param stream: Stream to write to. - :param r: Ratio to write (Fraction object). - :return: The number of bytes written. - :raises: SignedError if r is negative. + Args: + stream: Stream to write to. + r: Ratio to write (`Fraction` object). + + Returns: + The number of bytes written. + + Raises: + SignedError: if r is negative. """ if r < 0: raise SignedError('Ratio must be unsigned: {}'.format(r)) @@ -329,8 +402,11 @@ def read_float32(stream: io.BufferedIOBase) -> float: """ Read a 32-bit float from the stream. - :param stream: Stream to read from. - :return: The value read. + Args: + stream: Stream to read from. + + Returns: + The value read. """ b = _read(stream, 4) return struct.unpack(" int: """ Write a 32-bit float to the stream. - :param stream: Stream to write to. - :param f: Value to write. - :return: The number of bytes written (4). + Arsg: + stream: Stream to write to. + f: Value to write. + + Returns: + The number of bytes written (4). """ b = struct.pack(" float: """ Read a 64-bit float from the stream. - :param stream: Stream to read from. - :return: The value read. + Args: + stream: Stream to read from. + + Returns: + The value read. """ b = _read(stream, 8) return struct.unpack(" int: """ Write a 64-bit float to the stream. - :param stream: Stream to write to. - :param f: Value to write. - :return: The number of bytes written (8). + Args: + stream: Stream to write to. + f: Value to write. + + Returns: + The number of bytes written (8). """ b = struct.pack(" real_t: 6: 32-bit float 7: 64-bit float - :param stream: Stream to read from. - :param real_type: Type of real number to read. If None (default), - the type is read from the stream. - :return: The value read. - :raises: InvalidDataError if real_type is invalid. + Args: + stream: Stream to read from. + real_type: Type of real number to read. If `None` (default), + the type is read from the stream. + + Returns: + The value read. + + Raises: + InvalidDataError: if real_type is invalid. """ if real_type is None: @@ -430,10 +520,14 @@ def write_real(stream: io.BufferedIOBase, Since python has no 32-bit floats, the force_float32 parameter will perform the cast at write-time if set to True (default False). - :param stream: Stream to write to. - :param r: Value to write. - :param float32: - :return: The number of bytes written. + Args: + stream: Stream to write to. + r: Value to write. + force_float32: If `True`, casts r to float32 when writing. + Default `False`. + + Returns: + The number of bytes written. """ size = 0 if isinstance(r, int): @@ -461,15 +555,16 @@ class NString: Class for handling "name strings", which hold one or more printable ASCII characters (0x21 to 0x7e, inclusive). - __init__ can be called with either a string or bytes object; - subsequent reading/writing should use the .string and - .bytes properties. + `__init__` can be called with either a string or bytes object; + subsequent reading/writing should use the `string` and + `bytes` properties. """ - _string = None # type: str + _string: str - def __init__(self, string_or_bytes: bytes or str): + def __init__(self, string_or_bytes: Union[bytes, str]): """ - :param string_or_bytes: Content of the Nstring. + Args: + string_or_bytes: Content of the `NString`. """ if isinstance(string_or_bytes, str): self.string = string_or_bytes @@ -492,8 +587,8 @@ class NString: @bytes.setter def bytes(self, bstring: bytes): - if len(bstring) == 0 or not all(0x21 <= c <= 0x7e for c in bstring): - raise InvalidDataError('Invalid n-string {}'.format(bstring)) + if len(bstring) == 0 or not all(0x21 <= c <= 0x7e for c in bstring): + raise InvalidDataError('Invalid n-string {!r}'.format(bstring)) self._string = bstring.decode('ascii') @staticmethod @@ -501,9 +596,14 @@ class NString: """ Create an NString object by reading a bstring from the provided stream. - :param stream: Stream to read from. - :return: Resulting NString. - :raises: InvalidDataError + Args: + stream: Stream to read from. + + Returns: + Resulting NString. + + Raises: + InvalidDataError """ return NString(read_bstring(stream)) @@ -511,26 +611,37 @@ class NString: """ Write this NString to a stream. - :param stream: Stream to write to. - :return: Number of bytes written. + Args: + stream: Stream to write to. + + Returns: + Number of bytes written. """ return write_bstring(stream, self.bytes) - def __eq__(self, other: 'NString') -> bool: + def __eq__(self, other: Any) -> bool: return isinstance(other, type(self)) and self.string == other.string def __repr__(self) -> str: return '[N]' + self._string + def __str__(self) -> str: + return self._string + def read_nstring(stream: io.BufferedIOBase) -> str: """ Read a name string from the provided stream. - See NString for constraints on name strings. + See `NString` for constraints on name strings. - :param stream: Stream to read from. - :return: Resulting string. - :raises: InvalidDataError + Args: + stream: Stream to read from. + + Returns: + Resulting string. + + Raises: + InvalidDataError """ return NString.read(stream).string @@ -538,12 +649,17 @@ def read_nstring(stream: io.BufferedIOBase) -> str: def write_nstring(stream: io.BufferedIOBase, string: str) -> int: """ Write a name string to a stream. - See NString for constraints on name strings. + See `NString` for constraints on name strings. - :param stream: Stream to write to. - :param string: String to write. - :return: Number of bytes written. - :raises: InvalidDataError + Args: + stream: Stream to write to. + string: String to write. + + Returns: + Number of bytes written. + + Raises: + InvalidDataError """ return NString(string).write(stream) @@ -553,15 +669,16 @@ class AString: Class for handling "ascii strings", which hold zero or more ASCII characters (0x20 to 0x7e, inclusive). - __init__ can be called with either a string or bytes object; - subsequent reading/writing should use the .string and - .bytes properties. + `__init__` can be called with either a string or bytes object; + subsequent reading/writing should use the `string` and + `bytes` properties. """ - _string = None # type: str + _string: str - def __init__(self, string_or_bytes: bytes or str): + def __init__(self, string_or_bytes: Union[bytes, str]): """ - :param string_or_bytes: Content of the AString. + Args: + string_or_bytes: Content of the AString. """ if isinstance(string_or_bytes, str): self.string = string_or_bytes @@ -585,44 +702,60 @@ class AString: @bytes.setter def bytes(self, bstring: bytes): if not all(0x20 <= c <= 0x7e for c in bstring): - raise InvalidDataError('Invalid a-string {}'.format(bstring)) + raise InvalidDataError('Invalid a-string {!r}'.format(bstring)) self._string = bstring.decode('ascii') @staticmethod def read(stream: io.BufferedIOBase) -> 'AString': """ - Create an AString object by reading a bstring from the provided stream. + Create an `AString` object by reading a bstring from the provided stream. - :param stream: Stream to read from. - :return: Resulting AString. - :raises: InvalidDataError + Args: + stream: Stream to read from. + + Returns: + Resulting `AString`. + + Raises: + InvalidDataError """ return AString(read_bstring(stream)) def write(self, stream: io.BufferedIOBase) -> int: """ - Write this AString to a stream. + Write this `AString` to a stream. - :param stream: Stream to write to. - :return: Number of bytes written. + Args: + stream: Stream to write to. + + Returns: + Number of bytes written. """ return write_bstring(stream, self.bytes) - def __eq__(self, other: 'AString') -> bool: + def __eq__(self, other: Any) -> bool: return isinstance(other, type(self)) and self.string == other.string def __repr__(self) -> str: return '[A]' + self._string + def __str__(self) -> str: + return self._string + def read_astring(stream: io.BufferedIOBase) -> str: """ Read an ASCII string from the provided stream. - See AString for constraints on ASCII strings. + See `AString` for constraints on ASCII strings. - :param stream: Stream to read from. - :return: Resulting string. - :raises: InvalidDataError + Args: + stream: Stream to read from. + + Returns: + Resulting string. + + Raises: + InvalidDataError """ return AString.read(stream).string @@ -632,10 +765,15 @@ def write_astring(stream: io.BufferedIOBase, string: str) -> int: Write an ASCII string to a stream. See AString for constraints on ASCII strings. - :param stream: Stream to write to. - :param string: String to write. - :return: Number of bytes written. - :raises: InvalidDataError + Args: + stream: Stream to write to. + string: String to write. + + Returns: + Number of bytes written. + + Raises: + InvalidDataError """ return AString(string).write(stream) @@ -644,19 +782,20 @@ class ManhattanDelta: """ Class representing an axis-aligned ("Manhattan") vector. - Has properties - .vertical (boolean, true if aligned along y-axis) - .value (int, signed length of the vector) + Attributes: + vertical (bool): `True` if aligned along y-axis + value (int): signed length of the vector """ vertical = None # type: bool value = None # type: int def __init__(self, x: int, y: int): """ - One of x or y _must_ be zero! + One of `x` or `y` _must_ be zero! - :param x: x-displacement - :param y: y-displacement + Args: + x: x-displacement + y: y-displacement """ x = int(x) y = int(y) @@ -673,7 +812,8 @@ class ManhattanDelta: """ Return a list representation of this vector. - :return: [x, y] + Returns: + `[x, y]` """ xy = [0, 0] xy[self.vertical] = self.value @@ -682,9 +822,10 @@ class ManhattanDelta: def as_uint(self) -> int: """ Return this vector encoded as an unsigned integer. - See ManhattanDelta.from_uint() for format details. + See `ManhattanDelta.from_uint()` for format details. - :return: uint encoding of this vector. + Returns: + uint encoding of this vector. """ return (encode_sint(self.value) << 1) | self.vertical @@ -696,42 +837,51 @@ class ManhattanDelta: The LSB of the encoded object is 1 if the vector is aligned to the y-axis, or 0 if aligned to the x-axis. The remaining bits are used to encode a signed integer containing - the signed length of the vector (see encode_sint() for format details). + the signed length of the vector (see `encode_sint()` for format details). - :param n: Unsigned integer representation of a ManhattanDelta vector. - :return: The ManhattanDelta object that was encoded by n. + Args: + n: Unsigned integer representation of a `ManhattanDelta` vector. + + Returns: + The `ManhattanDelta` object that was encoded by `n`. """ d = ManhattanDelta(0, 0) d.value = decode_sint(n >> 1) - d.vertical = n & 0x01 + d.vertical = bool(n & 0x01) return d @staticmethod def read(stream: io.BufferedIOBase) -> 'ManhattanDelta': """ - Read a ManhattanDelta object from the provided stream. + Read a `ManhattanDelta` object from the provided stream. - See .from_uint() for format details. + See `ManhattanDelta.from_uint()` for format details. - :param stream: The stream to read from. - :return: The ManhattanDelta object that was read from the stream. + Args: + stream: The stream to read from. + + Returns: + The `ManhattanDelta` object that was read from the stream. """ n = read_uint(stream) return ManhattanDelta.from_uint(n) def write(self, stream: io.BufferedIOBase) -> int: """ - Write a ManhattanDelta object to the provided stream. + Write a `ManhattanDelta` object to the provided stream. - See .from_uint() for format details. + See `ManhattanDelta.from_uint()` for format details. - :param stream: The stream to write to. - :return: The number of bytes written. + Args: + stream: The stream to write to. + + Returns: + The number of bytes written. """ return write_uint(stream, self.as_uint()) - def __eq__(self, other: 'ManhattanDelta') -> bool: - return hasattr(other, as_list) and self.as_list() == other.as_list() + def __eq__(self, other: Any) -> bool: + return hasattr(other, 'as_list') and self.as_list() == other.as_list() def __repr__(self) -> str: return '{}'.format(self.as_list()) @@ -741,9 +891,9 @@ class OctangularDelta: """ Class representing an axis-aligned or 45-degree ("Octangular") vector. - Has properties - .proj_mag (int, projection of the vector onto the x or y axis (non-zero)) - .octangle (int, bitfield: + 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 @@ -756,17 +906,17 @@ class OctangularDelta: 0: +x, 1: +y, 2: -x, 3: -y, 4: +x+y, 5: -x+y, 6: +x-y, 7: -x-y - ) """ - proj_mag = None # type: int - octangle = None # type: int + proj_mag: int + octangle: int def __init__(self, x: int, y: int): """ - Either abs(x)==abs(y), x==0, or y==0 _must_ be true! + Either `abs(x)==abs(y)`, `x==0`, or `y==0` _must_ be true! - :param x: x-displacement - :param y: y-displacement + Args: + x: x-displacement + y: y-displacement """ x = int(x) y = int(y) @@ -788,7 +938,8 @@ class OctangularDelta: """ Return a list representation of this vector. - :return: [x, y] + Returns: + `[x, y]` """ if self.octangle < 4: xy = [0, 0] @@ -807,24 +958,28 @@ class OctangularDelta: def as_uint(self) -> int: """ Return this vector encoded as an unsigned integer. - See OctangularDelta.from_uint() for format details. + See `OctangularDelta.from_uint()` for format details. - :return: uint encoding of this vector. + Returns: + uint encoding of this vector. """ return (self.proj_mag << 3) | self.octangle @staticmethod def from_uint(n: int) -> 'OctangularDelta': """ - Construct an OctangularDelta object from its unsigned integer encoding. + Construct an `OctangularDelta` object from its unsigned integer encoding. - The low 3 bits are equal to .proj_mag, as specified in the class + The low 3 bits are equal to `proj_mag`, as specified in the class docstring. The remaining bits are used to encode an unsigned integer containing the length of the vector. - :param n: Unsigned integer representation of an OctangularDelta vector. - :return: The OctangularDelta object that was encoded by n. + Args: + n: Unsigned integer representation of an `OctangularDelta` vector. + + Returns: + The `OctangularDelta` object that was encoded by `n`. """ d = OctangularDelta(0, 0) d.proj_mag = n >> 3 @@ -834,29 +989,35 @@ class OctangularDelta: @staticmethod def read(stream: io.BufferedIOBase) -> 'OctangularDelta': """ - Read an OctangularDelta object from the provided stream. + Read an `OctangularDelta` object from the provided stream. - See .from_uint() for format details. + See `OctangularDelta.from_uint()` for format details. - :param stream: The stream to read from. - :return: The OctangularDelta object that was read from the stream. + Args: + stream: The stream to read from. + + Returns: + The `OctangularDelta` object that was read from the stream. """ n = read_uint(stream) return OctangularDelta.from_uint(n) def write(self, stream: io.BufferedIOBase) -> int: """ - Write an OctangularDelta object to the provided stream. + Write an `OctangularDelta` object to the provided stream. - See .from_uint() for format details. + See `OctangularDelta.from_uint()` for format details. - :param stream: The stream to write to. - :return: The number of bytes written. + Args: + stream: The stream to write to. + + Returns: + The number of bytes written. """ return write_uint(stream, self.as_uint()) - def __eq__(self, other: 'OctangularDelta') -> bool: - return hasattr(other, as_list) and self.as_list() == other.as_list() + def __eq__(self, other: Any) -> bool: + return hasattr(other, 'as_list') and self.as_list() == other.as_list() def __repr__(self) -> str: return '{}'.format(self.as_list()) @@ -866,17 +1027,18 @@ class Delta: """ Class representing an arbitrary vector - Has properties - .x (int) - .y (int) + Attributes + x (int): x-displacement + y (int): y-displacement """ - x = None # type: int - y = None # type: int + x: int + y: int def __init__(self, x: int, y: int): """ - :param x: x-displacement - :param y: y-displacement + Args: + x: x-displacement + y: y-displacement """ x = int(x) y = int(y) @@ -887,25 +1049,29 @@ class Delta: """ Return a list representation of this vector. - :return: [x, y] + Returns: + `[x, y]` """ return [self.x, self.y] @staticmethod def read(stream: io.BufferedIOBase) -> 'Delta': """ - Read a Delta object from the provided stream. + Read a `Delta` object from the provided stream. The format consists of one or two unsigned integers. The LSB of the first integer is 1 if a second integer is present. If two integers are present, the remaining bits of the first - integer are an encoded signed integer (see encode_sint()), and + integer are an encoded signed integer (see `encode_sint()`), and the second integer is an encoded signed_integer. Otherwise, the remaining bits of the first integer are an encoded - OctangularData (see OctangularData.from_uint()). + `OctangularData` (see `OctangularData.from_uint()`). - :param stream: The stream to read from. - :return: The Delta object that was read from the stream. + Args: + stream: The stream to read from. + + Returns: + The `Delta` object that was read from the stream. """ n = read_uint(stream) if (n & 0x01) == 0: @@ -917,12 +1083,15 @@ class Delta: def write(self, stream: io.BufferedIOBase) -> int: """ - Write a Delta object to the provided stream. + Write a `Delta` object to the provided stream. - See .from_uint() for format details. + See `Delta.from_uint()` for format details. - :param stream: The stream to write to. - :return: The number of bytes written. + Args: + stream: The stream to write to. + + Returns: + The number of bytes written. """ 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) @@ -931,8 +1100,8 @@ class Delta: size += write_uint(stream, encode_sint(self.y)) return size - def __eq__(self, other: 'Delta') -> bool: - return hasattr(other, as_list) and self.as_list() == other.as_list() + def __eq__(self, other: Any) -> bool: + return hasattr(other, 'as_list') and self.as_list() == other.as_list() def __repr__(self) -> str: return '{}'.format(self.as_list()) @@ -942,8 +1111,14 @@ def read_repetition(stream: io.BufferedIOBase) -> repetition_t: """ Read a repetition entry from the given stream. - :param stream: Stream to read from. - :return: The repetition entry. + Args: + stream: Stream to read from. + + Returns: + The repetition entry. + + Raises: + InvalidDataError: if an unexpected repetition type is read """ rtype = read_uint(stream) if rtype == 0: @@ -952,15 +1127,20 @@ def read_repetition(stream: io.BufferedIOBase) -> repetition_t: return GridRepetition.read(stream, rtype) elif rtype in (4, 5, 6, 7, 10, 11): return ArbitraryRepetition.read(stream, rtype) + else: + raise InvalidDataError('Unexpected repetition type: {}'.format(rtype)) def write_repetition(stream: io.BufferedIOBase, repetition: repetition_t) -> int: """ Write a repetition entry to the given stream. - :param stream: Stream to write to. - :param repetition: The repetition entry to write. - :return: The number of bytes written. + Args: + stream: Stream to write to. + repetition: The repetition entry to write. + + Returns: + The number of bytes written. """ return repetition.write(stream) @@ -977,7 +1157,7 @@ class ReuseRepetition: def write(self, stream: io.BufferedIOBase) -> int: return write_uint(stream, 0) - def __eq__(self, other: 'ReuseRepetition') -> bool: + def __eq__(self, other: Any) -> bool: return isinstance(other, ReuseRepetition) def __repr__(self) -> str: @@ -991,58 +1171,55 @@ class GridRepetition: two lattice vectors, and the extent of the grid is stored as the number of elements along each lattice vector. - This class has properties - .a_vector ([xa: int, ya: int], vector specifying a center-to-center - displacement between adjacent elements in the grid.) - .b_vector ([xb: int, yb: int] or None, a second displacement, present if - a 2D grid is being specified.) - .a_count (int >= 1, number of elements along the grid axis specified by - .a_vector) - .b_count (int >= 1 or None, number of elements along the grid axis - specified by .b_vector) + 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`. """ - a_vector = None # type: List[int] - b_vector = None # type: List[int] or None - a_count = None # type: int - b_count = None # type: int or None + a_vector: List[int] + b_vector: Optional[List[int]] = None + a_count: int + b_count: Optional[int] = None def __init__(self, a_vector: List[int], a_count: int, - b_vector: List[int] = None, - b_count: int = None): + b_vector: Optional[List[int]] = None, + b_count: Optional[int] = None): """ - :param a_vector: First lattice vector, of the form [x, y]. - Specifies center-to-center spacing between adjacent elements. - :param a_count: Number of elements in the a_vector direction. - :param b_vector: Second lattice vector, of the form [x, y]. - Specifies center-to-center spacing between adjacent elements. - Can be omitted when specifying a 1D array. - :param b_count: Number of elements in the b_vector direction. - Should be omitted if b_vector was omitted. - :raises: InvalidDataError if b_* inputs conflict with each other - or a_count < 1. - """ - self.a_vector = a_vector - self.b_vector = b_vector - self.a_count = a_count - self.b_count = b_count + Args: + a_vector: First lattice vector, of the form `[x, y]`. + Specifies center-to-center spacing between adjacent elements. + a_count: Number of elements in the a_vector direction. + b_vector: Second lattice vector, of the form `[x, y]`. + Specifies center-to-center spacing between adjacent elements. + Can be omitted when specifying a 1D array. + b_count: Number of elements in the `b_vector` direction. + Should be omitted if `b_vector` was omitted. - 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: + Raises: + InvalidDataError: if `b_count` and `b_vector` inputs conflict + with each other or if `a_count < 1`. + """ + if b_vector is None or b_count is None: + if b_vector is not None or b_count is not None: raise InvalidDataError('Repetition has only one of' 'b_vector and b_count') else: - if self.b_count < 1: + if b_count < 1: raise InvalidDataError('Repetition has too-small b_count') - if self.b_count < 2: - self.b_count = None - self.b_vector = None - print('Warning: removed b_count and b_vector since b_count == 1') - # TODO: warn here + if b_count < 2: + b_count = None + b_vector = None + warnings.warn('Removed b_count and b_vector since b_count == 1') - if self.a_count < 2: - raise InvalidDataError('Repetition has too-small x-count: ' + if a_count < 2: + raise InvalidDataError('Repetition has too-small a_count: ' '{}'.format(a_count)) self.a_vector = a_vector self.b_vector = b_vector @@ -1052,14 +1229,21 @@ class GridRepetition: @staticmethod def read(stream: io.BufferedIOBase, repetition_type: int) -> 'GridRepetition': """ - Read a GridRepetition from a stream. + Read a `GridRepetition` from a stream. - :param stream: Stream to read from. - :param repetition_type: Repetition type as defined in OASIS repetition spec. - Valid types are 1, 2, 3, 8, 9. - :return: GridRepetition object read from stream. - :raises InvalidDataError if repetition_type is invalid. + Args: + stream: Stream to read from. + repetition_type: Repetition type as defined in OASIS repetition spec. + Valid types are 1, 2, 3, 8, 9. + + Returns: + `GridRepetition` object read from stream. + + Raises: + InvalidDataError: if `repetition_type` is invalid. """ + nb: Optional[int] + b_vector: Optional[List[int]] if repetition_type == 1: na = read_uint(stream) + 2 nb = read_uint(stream) + 2 @@ -1092,14 +1276,19 @@ class GridRepetition: def write(self, stream: io.BufferedIOBase) -> int: """ - Write the GridRepetition to a stream. + Write the `GridRepetition` to a stream. - A minimal representation is written (e.g., if b_count==1, + A minimal representation is written (e.g., if `b_count==1`, a 1D grid is written) - :param stream: Stream to write to. - :return: Number of bytes written. - :raises: InvalidDataError if repetition is malformed. + Args: + stream: Stream to write to. + + Returns: + Number of bytes written. + + Raises: + InvalidDataError: if repetition is malformed. """ 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: @@ -1138,12 +1327,20 @@ class GridRepetition: size += Delta(*self.b_vector).write(stream) return size - def __eq__(self, other: 'GridRepetition') -> bool: - return isinstance(other, type(self)) and \ - self.a_count == other.a_count and \ - self.b_count == other.b_count and \ - self.a_vector == other.a_vector and \ - self.b_vector == other.b_vector + def __eq__(self, other: Any) -> bool: + if not isinstance(other, type(self)): + return False + if self.a_count != other.a_count or self.b_count != other.b_count: + return False + if any(self.a_vector[ii] != other.a_vector[ii] for ii in range(2)): + return False + if self.b_vector is None and other.b_vector is None: + 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)): + return False + return True def __repr__(self) -> str: return 'GridRepetition: ({} : {} | {} : {})'.format(self.a_count, self.a_vector, @@ -1155,20 +1352,21 @@ class ArbitraryRepetition: Class representing a repetition entry denoting a 1D or 2D array of arbitrarily-spaced elements. - Properties: - .x_displacements (List[int], x-displacements between elements) - .y_displacements (List[int], y-displacements between elements) + Attributes: + x_displacements (List[int]): x-displacements between consecutive elements + y_displacements (List[int]): y-displacements between consecutive elements """ - x_displacements = None # type: List[int] - y_displacements = None # type: List[int] + x_displacements: List[int] + y_displacements: List[int] def __init__(self, x_displacements: List[int], y_displacements: List[int]): """ - :param x_displacements: x-displacements between consecutive elements - :param y_displacements: y-displacements between consecutive elements + Args: + x_displacements: x-displacements between consecutive elements + y_displacements: y-displacements between consecutive elements """ self.x_displacements = x_displacements self.y_displacements = y_displacements @@ -1176,13 +1374,18 @@ class ArbitraryRepetition: @staticmethod def read(stream: io.BufferedIOBase, repetition_type: int) -> 'ArbitraryRepetition': """ - Read an ArbitraryRepetition from a stream. + Read an `ArbitraryRepetition` from a stream. - :param stream: Stream to read from. - :param repetition_type: Repetition type as defined in OASIS repetition spec. - Valid types are 4, 5, 6, 7, 10, 11. - :return: ArbitraryRepetition object read from stream. - :raises InvalidDataError if repetition_type is invalid. + Args: + stream: Stream to read from. + repetition_type: Repetition type as defined in OASIS repetition spec. + Valid types are 4, 5, 6, 7, 10, 11. + + Returns: + `ArbitraryRepetition` object read from stream. + + Raises: + InvalidDataError: if `repetition_type` is invalid. """ if repetition_type == 4: n = read_uint(stream) + 1 @@ -1225,14 +1428,17 @@ class ArbitraryRepetition: def write(self, stream: io.BufferedIOBase) -> int: """ - Write the ArbitraryRepetition to a stream. + Write the `ArbitraryRepetition` to a stream. A minimal representation is attempted; common factors in the displacements will be factored out, and lists of zeroes will be omitted. - :param stream: Stream to write to. - :return: Number of bytes written. + Args: + stream: Stream to write to. + + Returns: + Number of bytes written. """ def get_gcd(vals: List[int]) -> int: """ @@ -1285,9 +1491,10 @@ class ArbitraryRepetition: for x, y in zip(self.x_displacements, self.y_displacements)) return size - - def __eq__(self, other: 'ArbitraryRepetition') -> bool: - return isinstance(other, type(self)) and self.x_displacements == other.x_displacements and self.y_displacements == other.y_displacements + def __eq__(self, other: Any) -> bool: + return (isinstance(other, type(self)) + and self.x_displacements == other.x_displacements + and self.y_displacements == other.y_displacements) def __repr__(self) -> str: return 'ArbitraryRepetition: x{} y{})'.format(self.x_displacements, self.y_displacements) @@ -1297,8 +1504,14 @@ def read_point_list(stream: io.BufferedIOBase) -> List[List[int]]: """ Read a point list from a stream. - :param stream: Stream to read from. - :return: Point list of the form [[x0, y0], [x1, y1], ...] + Args: + stream: Stream to read from. + + Returns: + Point list of the form `[[x0, y0], [x1, y1], ...]` + + Raises: + InvalidDataError: if an invalid list type is read. """ list_type = read_uint(stream) list_len = read_uint(stream) @@ -1343,43 +1556,42 @@ def read_point_list(stream: io.BufferedIOBase) -> List[List[int]]: elif list_type == 5: deltas = [Delta.read(stream).as_list() for _ in range(list_len)] if _USE_NUMPY: - delta_x, delta_y = zip(*deltas) - x = numpy.cumsum(delta_x) - y = numpy.cumsum(delta_y) - points = list(zip(x, y)) + points = numpy.cumsum(deltas, axis=0) else: points = [] x = 0 y = 0 - for _ in range(list_len): - delta = Delta.read(stream) - x += delta.x - y += delta.y + for delta in deltas: + x += delta[0] + y += delta[1] points.append([x, y]) else: - raise Exception('Invalid point list type') + raise InvalidDataError('Invalid point list type') return points def write_point_list(stream: io.BufferedIOBase, - points: List[List[int]], + points: List[Sequence[int]], fast: bool = False, implicit_closed: bool = True ) -> int: """ Write a point list to a stream. - :param stream: Stream to write to. - :param points: List of points, of the form [[x0, y0], [x1, y1], ...] - :param fast: If True, avoid searching for a compact representation for - the point list. - :param implicit_closed: Set to True if the list represents an implicitly - closed polygon, i.e. there is an implied line segment from points[-1] - to points[0]. If False, such segments are ignored, which can result in a - more compact representation for non-closed paths (e.g. a Manhattan - path with non-colinear endpoints). If unsure, use the default. - Default True. - :return: Number of bytes written. + Args: + stream: Stream to write to. + points: List of points, of the form `[[x0, y0], [x1, y1], ...]` + fast: If `True`, avoid searching for a compact representation for + the point list. + implicit_closed: Set to True if the list represents an implicitly + closed polygon, i.e. there is an implied line segment from `points[-1]` + to `points[0]`. If False, such segments are ignored, which can result in + a more compact representation for non-closed paths (e.g. a Manhattan + path with non-colinear endpoints). If unsure, use the default. + Default `True`. + + Returns: + Number of bytes written. """ # If we're in a hurry, just write the points as arbitrary Deltas if fast: @@ -1420,6 +1632,7 @@ def write_point_list(stream: io.BufferedIOBase, return size # Try writing a bunch of Manhattan or Octangular deltas + deltas: Union[List[ManhattanDelta], List[OctangularDelta], List[Delta]] list_type = None try: deltas = [ManhattanDelta(x, y) for x, y in points] @@ -1460,7 +1673,8 @@ def write_point_list(stream: io.BufferedIOBase, previous = [0, 0] diff = [] for point in points: - d = [point[0] - previous[0], point[1] - previous[1]] + d = [point[0] - previous[0], + point[1] - previous[1]] previous = point diff.append(d) @@ -1481,12 +1695,12 @@ class PropStringReference: """ Reference to a property string. - Properties: - .ref (int, ID of the target) - .ref_type (Type, Type of the target: bytes, NString, or AString) + Attributes: + ref (int): ID of the target + ref_type (Type): Type of the target: `bytes`, `NString`, or `AString` """ - ref = None # type: int - reference_type = None # type: Type + ref: int + reference_type: Type def __init__(self, ref: int, ref_type: Type): """ @@ -1496,7 +1710,7 @@ class PropStringReference: self.ref = ref self.ref_type = ref_type - def __eq__(self, other: 'PropStringReference') -> bool: + def __eq__(self, other: Any) -> bool: return isinstance(other, type(self)) and self.ref == other.ref and self.reference_type == other.reference_type def __repr__(self) -> str: @@ -1514,17 +1728,23 @@ def read_property_value(stream: io.BufferedIOBase) -> property_value_t: 0...7: real number; property value type is reused for real number type 8: unsigned integer 9: signed integer - 10: ASCII string (AString) - 11: binary string (bytes) - 12: name string (NString) - 13: PropstringReference to AString - 14: PropstringReference to bstring (i.e., to bytes) - 15: PropstringReference to NString + 10: ASCII string (`AString`) + 11: binary string (`bytes`) + 12: name string (`NString`) + 13: `PropStringReference` to `AString` + 14: `PropStringReference` to `bstring` (i.e., to `bytes`) + 15: `PropStringReference` to `NString` - :param stream: Stream to read from. - :return: Value of the property, depending on type. - :raises: InvalidDataError if an invalid type is read. + Args: + stream: Stream to read from. + + Returns: + Value of the property, depending on type. + + Raises: + InvalidDataError: if an invalid type is read. """ + ref_type: Type prop_type = read_uint(stream) if 0 <= prop_type <= 7: return read_real(stream, prop_type) @@ -1563,18 +1783,21 @@ def write_property_value(stream: io.BufferedIOBase, """ Write a property value to a stream. - See read_property_value() for format details. + See `read_property_value()` for format details. - :param stream: Stream to write to. - :param value: Property value to write. Can be an integer, a real number, - bytes (bstring), NString, AString, or a PropstringReference. - :param force_real: If True and value is an integer, writes an integer- - valued real number instead of a plain integer. Default False. - :param force_signed_int: If True and value is a positive integer, - writes a signed integer. Default false. - :param force_float32: If True and value is a float, writes a 32-bit - float (real number) instead of a 64-bit float. - :return: Number of bytes written. + Args: + stream: Stream to write to. + value: Property value to write. Can be an integer, a real number, + `bytes` (`bstring`), `NString`, `AString`, or a `PropstringReference`. + force_real: If `True` and value is an integer, writes an integer- + valued real number instead of a plain integer. Default `False`. + force_signed_int: If `True` and value is a positive integer, + writes a signed integer. Default `False`. + force_float32: If `True` and value is a float, writes a 32-bit + float (real number) instead of a 64-bit float. + + Returns: + Number of bytes written. """ if isinstance(value, int) and not force_real: if force_signed_int or value < 0: @@ -1583,7 +1806,7 @@ def write_property_value(stream: io.BufferedIOBase, else: size = write_uint(stream, 8) size += write_uint(stream, value) - elif isinstance(value, real_t): + elif isinstance(value, (Fraction, float, int)): size = write_real(stream, value, force_float32) elif isinstance(value, AString): size = write_uint(stream, 10) @@ -1607,7 +1830,7 @@ def write_property_value(stream: io.BufferedIOBase, return size -def read_interval(stream: io.BufferedIOBase) -> Tuple[int or 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. @@ -1620,10 +1843,16 @@ def read_interval(stream: io.BufferedIOBase) -> Tuple[int or None]: type 3: a, a (unsigned integer a) type 4: a, b (unsigned integers a, b) - :param stream: Stream to read from. - :return: (lower, upper), where - lower can be None if there is an implicit lower bound of 0 - upper can be None if there is no upper bound (inf) + Args: + stream: Stream to read from. + + Returns: + `(lower, upper)`, where + `lower` can be `None` if there is an implicit lower bound of `0` + `upper` can be `None` if there is no upper bound (`inf`) + + Raises: + InvalidDataError: On malformed data. """ interval_type = read_uint(stream) if interval_type == 0: @@ -1637,20 +1866,25 @@ def read_interval(stream: io.BufferedIOBase) -> Tuple[int or None]: return v, v elif interval_type == 4: return read_uint(stream), read_uint(stream) + else: + raise InvalidDataError('Unrecognized interval type: {}'.format(interval_type)) def write_interval(stream: io.BufferedIOBase, - min_bound: int or None = None, - max_bound: int or None = None + 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. + Used for layer data; see `read_interval()` for format details. - :param stream: Stream to write to. - :param min_bound: Lower bound on the interval, can be None (implicit 0, default) - :param max_bound: Upper bound on the interval, can be None (unbounded, default) - :return: Number of bytes written. + Args: + stream: Stream to write to. + min_bound: Lower bound on the interval, can be None (implicit 0, default) + max_bound: Upper bound on the interval, can be None (unbounded, default) + + Returns: + Number of bytes written. """ if min_bound is None: if max_bound is None: @@ -1660,8 +1894,10 @@ def write_interval(stream: io.BufferedIOBase, 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, 3) + size = write_uint(stream, 4) size += write_uint(stream, min_bound) size += write_uint(stream, max_bound) return size @@ -1671,33 +1907,31 @@ class OffsetEntry: """ Entry for the file's offset table. - Properties: - .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 mode 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.) + 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 = False # type: bool - offset = 0 # type: int + strict: bool = False + offset: int = 0 def __init__(self, strict: bool = False, offset: int = 0): """ - :param strict: True if the records referenced are written in - strict mode (see class docstring). Default False. - :param offset: Offset from the start of the file for the - referenced records; may be 0 if records are absent. - Default 0. + Args: + strict: `True` if the records referenced are written in + strict mode (see class docstring). Default `False`. + offset: Offset from the start of the file for the + referenced records; may be `0` if records are absent. + Default `0`. """ self.strict = strict self.offset = offset @@ -1707,8 +1941,11 @@ class OffsetEntry: """ Read an offset entry from a stream. - :param stream: Stream to read from. - :return: Offset entry that was read. + Args: + stream: Stream to read from. + + Returns: + Offset entry that was read. """ entry = OffsetEntry() entry.strict = read_uint(stream) > 0 @@ -1719,8 +1956,11 @@ class OffsetEntry: """ Write this offset entry to a stream. - :param stream: Stream to write to. - :return: Number of bytes written + Args: + stream: Stream to write to. + + Returns: + Number of bytes written """ return write_uint(stream, self.strict) + write_uint(stream, self.offset) @@ -1742,37 +1982,38 @@ class OffsetTable: which are stored in the above order in the file's offset table. - Proerties: - .cellnames (OffsetEntry) - .textstrings (OffsetEntry) - .propnames (OffsetEntry) - .propstrings (OffsetEntry) - .layernames (OffsetEntry) - .xnames (OffsetEntry) + 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 = None # type: OffsetEntry - textstrings= None # type: OffsetEntry - propnames = None # type: OffsetEntry - propstrings = None # type: OffsetEntry - layernames = None # type: OffsetEntry - xnames = None # type: OffsetEntry + cellnames: OffsetEntry + textstrings: OffsetEntry + propnames: OffsetEntry + propstrings: OffsetEntry + layernames: OffsetEntry + xnames: OffsetEntry def __init__(self, - cellnames: OffsetEntry = None, - textstrings: OffsetEntry = None, - propnames: OffsetEntry = None, - propstrings: OffsetEntry = None, - layernames: OffsetEntry = None, - xnames: OffsetEntry = None): + 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. + All parameters default to a non-strict entry with offset `0`. - :param cellnames: OffsetEntry for CellName records. - :param textstrings: OffsetEntry for TextString records. - :param propnames: OffsetEntry for PropName records. - :param propstrings: OffsetEntry for PropString records. - :param layernames: OffsetEntry for LayerNamerecords. - :param xnames: OffsetEntry for XName records. + Args: + cellnames: `OffsetEntry` for `CellName` records. + textstrings: `OffsetEntry` for `TextString` records. + propnames: `OffsetEntry` for `PropName` records. + propstrings: `OffsetEntry` for `PropString` records. + layernames: `OffsetEntry` for `LayerName` records. + xnames: `OffsetEntry` for `XName` records. """ if cellnames is None: cellnames = OffsetEntry() @@ -1780,7 +2021,7 @@ class OffsetTable: textstrings = OffsetEntry() if propnames is None: propnames = OffsetEntry() - if propstrings is None: + if propstrings is None: propstrings = OffsetEntry() if layernames is None: layernames = OffsetEntry() @@ -1800,8 +2041,11 @@ class OffsetTable: Read an offset table from a stream. See class docstring for format details. - :param stream: Stream to read from. - :return: The offset table that was read. + Args: + stream: Stream to read from. + + Returns: + The offset table that was read. """ table = OffsetTable() table.cellnames = OffsetEntry.read(stream) @@ -1817,8 +2061,11 @@ class OffsetTable: Write this offset table to a stream. See class docstring for format details. - :param stream: Stream to write to. - :return: Number of bytes written. + Args: + stream: Stream to write to. + + Returns: + Number of bytes written. """ size = self.cellnames.write(stream) size += self.textstrings.write(stream) @@ -1837,21 +2084,29 @@ def read_u32(stream: io.BufferedIOBase) -> int: """ Read a 32-bit unsigned integer (little endian) from a stream. - :param stream: Stream to read from. - :return: The integer that was read. + Args: + stream: Stream to read from. + + Returns: + The integer that was read. """ b = _read(stream, 4) - return struct.unpack(' int: """ Write a 32-bit unsigned integer (little endian) to a stream. - :param stream: Stream to write to. - :param n: Integer to write. - :return: The number of bytes written (4). - :raises: SignedError if n is negative. + Args: + stream: Stream to write to. + n: Integer to write. + + Returns: + The number of bytes written (4). + + Raises: + SignedError: if `n` is negative. """ if n < 0: raise SignedError('Negative u32: {}'.format(n)) @@ -1867,20 +2122,22 @@ class Validation: The checksum is calculated using the entire file, excluding the final 4 bytes (the value of the checksum itself). - Properties: - .checksum_type (int, 0: No checksum, 1: crc32, 2: checksum32) - .checksum (int or None, value of the checksum) - + Attributes: + checksum_type (int): `0` for no checksum, `1` for crc32, `2` for checksum32 + checksum (Optional[int]): value of the checksum """ - checksum_type = None # type: int - checksum = None # type: int or None + checksum_type: int + checksum: Optional[int] = None def __init__(self, checksum_type: int, checksum: int = None): """ - :param checksum_type: 0,1,2 (No checksum, crc32, checksum32) - :param checksum: Value of the checksum, or None. - :raises: InvalidDataError if checksum_type is invalid, or - unexpected checksum is present. + Args: + checksum_type: 0,1,2 (No checksum, crc32, checksum32) + checksum: Value of the checksum, or None. + + Raises: + InvalidDataError: if `checksum_type` is invalid, or + unexpected `checksum` is present. """ if checksum_type < 0 or checksum_type > 2: raise InvalidDataError('Invalid validation type') @@ -1895,9 +2152,14 @@ class Validation: Read a validation entry from a stream. See class docstring for format details. - :param stream: Stream to read from. - :return: The validation entry that was read. - :raises: InvalidDataError if an invalid validation type was encountered. + Args: + stream: Stream to read from. + + Returns: + The validation entry that was read. + + Raises: + InvalidDataError: if an invalid validation type was encountered. """ checksum_type = read_uint(stream) if checksum_type == 0: @@ -1915,15 +2177,27 @@ class Validation: Write this validation entry to a stream. See class docstring for format details. - :param stream: Stream to write to. - :return: Number of bytes written. + Args: + stream: Stream to write to. + + Returns: + Number of bytes written. + + Raises: + InvalidDataError: if the checksum type can't be handled. """ if self.checksum_type == 0: return write_uint(stream, 0) + 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) elif self.checksum_type == 2: return write_uint(stream, 2) + write_u32(stream, self.checksum) + else: + raise InvalidDataError('Unrecognized checksum type: ' + '{}'.format(self.checksum_type)) def __repr__(self) -> str: return 'Validation(type: {} sum: {})'.format(self.checksum_type, self.checksum) @@ -1933,8 +2207,11 @@ def write_magic_bytes(stream: io.BufferedIOBase) -> int: """ Write the magic byte sequence to a stream. - :param stream: Stream to write to. - :return: Number of bytes written. + Args: + stream: Stream to write to. + + Returns: + Number of bytes written. """ return stream.write(MAGIC_BYTES) @@ -1942,13 +2219,15 @@ def write_magic_bytes(stream: io.BufferedIOBase) -> int: def read_magic_bytes(stream: io.BufferedIOBase): """ Read the magic byte sequence from a stream. - Raise an InvalidDataError if it was not found. + Raise an `InvalidDataError` if it was not found. - :param stream: Stream to read from. - :raises: InvalidDataError if the sequence was not found. + Args: + stream: Stream to read from. + + Raises: + InvalidDataError: if the sequence was not found. """ magic = _read(stream, len(MAGIC_BYTES)) if magic != MAGIC_BYTES: raise InvalidDataError('Could not read magic bytes, ' - 'found {} : {}'.format(magic, magic.decode())) - + 'found {!r} : {}'.format(magic, magic.decode())) diff --git a/fatamorgana/main.py b/fatamorgana/main.py index 3cc721c..ea2ffa8 100644 --- a/fatamorgana/main.py +++ b/fatamorgana/main.py @@ -3,18 +3,22 @@ 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 List, Dict, Union, Optional, Type import io import logging from . import records -from .records import Modals -from .basic import OffsetEntry, OffsetTable, NString, AString, real_t, Validation, \ - read_magic_bytes, write_magic_bytes, read_uint, EOFError, \ - InvalidDataError, InvalidRecordError +from .records import Modals, Record +from .basic import ( + OffsetEntry, OffsetTable, NString, AString, real_t, Validation, + read_magic_bytes, write_magic_bytes, read_uint, EOFError, + InvalidRecordError, + ) __author__ = 'Jan Petykiewicz' +#logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) @@ -23,17 +27,21 @@ class FileModals: """ File-scoped modal variables """ - cellname_implicit = None # type: bool or None - propname_implicit = None # type: bool or None - xname_implicit = None # type: bool or None - textstring_implicit = None # type: bool or None - propstring_implicit = None # type: bool or None - cellname_implicit = None # type: bool or 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 - within_cell = False # type: bool - within_cblock = False # type: bool - end_has_offset_table = None # type: bool - started = False # type: bool + 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]): + self.property_target = property_target class OasisLayout: @@ -43,49 +51,50 @@ class OasisLayout: Names and strings are stored in dicts, indexed by reference number. Layer names and properties are stored directly using their associated record objects. - Cells are stored using Cell objects (different from Cell record objects). + Cells are stored using `Cell` objects (different from `records.Cell` + record objects). - Properties: - File properties: - .version AString: Version string ('1.0') - .unit real number: grid steps per micron - .validation Validation: checksum data + Attributes: + (File properties) + version (AString): Version string ('1.0') + unit (real_t): grid steps per micron + validation (Validation): checksum data - Names: - .cellnames Dict[int, NString] - .propnames Dict[int, NString] - .xnames Dict[int, XName] + (Names) + cellnames (Dict[int, CellName]): Cell names + propnames (Dict[int, NString]): Property names + xnames (Dict[int, XName]): Custom names - Strings: - .textstrings Dict[int, AString] - .propstrings Dict[int, AString] + (Strings) + textstrings (Dict[int, AString]): Text strings + propstrings (Dict[int, AString]): Property strings - Data: - .layers List[records.LayerName] - .properties List[records.Property] - .cells List[Cell] + (Data) + layers (List[records.LayerName]): Layer definitions + properties (List[records.Property]): Property values + cells (List[Cell]): Layout cells """ - version = None # type: AString - unit = None # type: real_t - validation = None # type: Validation + version: AString + unit: real_t + validation: Validation - properties = None # type: List[records.Property] - cells = None # type: List[Cell] + properties: List[records.Property] + cells: List['Cell'] - cellnames = None # type: Dict[int, NString] - propnames = None # type: Dict[int, NString] - xnames = None # type: Dict[int, XName] - - textstrings = None # type: Dict[int, AString] - propstrings = None # type: Dict[int, AString] - layers = None # type: List[records.LayerName] + cellnames: Dict[int, 'CellName'] + propnames: Dict[int, NString] + xnames: Dict[int, 'XName'] + textstrings: Dict[int, AString] + propstrings: Dict[int, AString] + layers: List[records.LayerName] def __init__(self, unit: real_t, validation: Validation = None): """ - :param unit: Real number (i.e. int, float, or Fraction), grid steps per micron. - :param validation: Validation object containing checksum data. - Default creates a Validation object of the "no checksum" type. + Args: + unit: Real number (i.e. int, float, or `Fraction`), grid steps per micron. + validation: `Validation` object containing checksum data. + Default creates a `Validation` object of the "no checksum" type. """ if validation is None: validation = Validation(0) @@ -105,14 +114,17 @@ class OasisLayout: @staticmethod def read(stream: io.BufferedIOBase) -> 'OasisLayout': """ - Read an entire .oas file into an OasisLayout object. + Read an entire .oas file into an `OasisLayout` object. - :param stream: Stream to read from. - :return: New OasisLayout object. + Args: + stream: Stream to read from. + + Returns: + New `OasisLayout` object. """ - file_state = FileModals() + layout = OasisLayout(unit=-1) # dummy unit modals = Modals() - layout = OasisLayout(unit=None) + file_state = FileModals(layout.properties) read_magic_bytes(stream) @@ -127,15 +139,20 @@ class OasisLayout: ) -> bool: """ Read a single record of unspecified type from a stream, adding its - contents into this OasisLayout object. + contents into this `OasisLayout` object. - :param stream: Stream to read from. - :param modals: Modal variable data, used to fill unfilled record - fields and updated using filled record fields. - :param file_state: File status data. - :return: True if EOF was reached without error, False otherwise. - :raises: InvalidRecordError from unexpected records; - InvalidDataError from within record parsers. + Args: + stream: Stream to read from. + modals: Modal variable data, used to fill unfilled record + fields and updated using filled record fields. + file_state: File status data. + + Returns: + `True` if EOF was reached without error, `False` otherwise. + + Raises: + InvalidRecordError: from unexpected records + InvalidDataError: from within record parsers """ try: record_id = read_uint(stream) @@ -147,6 +164,8 @@ class OasisLayout: logger.info('read_record of type {} at position 0x{:x}'.format(record_id, stream.tell())) + record: Record + # CBlock if record_id == 34: if file_state.within_cblock: @@ -185,16 +204,19 @@ class OasisLayout: raise InvalidRecordError('Unknown record id: {}'.format(record_id)) if record_id == 0: - # Pad + ''' Pad ''' pass elif record_id == 1: + ''' Start ''' record = records.Start.read(stream, record_id) record.merge_with_modals(modals) self.unit = record.unit self.version = record.version file_state.end_has_offset_table = record.offset_table is None + file_state.property_target = self.properties # TODO Offset table strict check elif record_id == 2: + ''' End ''' record = records.End.read(stream, record_id, file_state.end_has_offset_table) record.merge_with_modals(modals) self.validation = record.validation @@ -202,6 +224,7 @@ class OasisLayout: raise InvalidRecordError('Stream continues past End record') return True elif record_id in (3, 4): + ''' CellName ''' implicit = record_id == 3 if file_state.cellname_implicit is None: file_state.cellname_implicit = implicit @@ -213,8 +236,12 @@ class OasisLayout: key = record.reference_number if key is None: key = len(self.cellnames) - self.cellnames[key] = record.nstring + + cellname = CellName.from_record(record) + self.cellnames[key] = cellname + file_state.property_target = cellname.properties elif record_id in (5, 6): + ''' TextString ''' implicit = record_id == 5 if file_state.textstring_implicit is None: file_state.textstring_implicit = implicit @@ -228,6 +255,7 @@ class OasisLayout: key = len(self.textstrings) self.textstrings[key] = record.astring elif record_id in (7, 8): + ''' PropName ''' implicit = record_id == 7 if file_state.propname_implicit is None: file_state.propname_implicit = implicit @@ -241,6 +269,7 @@ class OasisLayout: key = len(self.propnames) self.propnames[key] = record.nstring elif record_id in (9, 10): + ''' PropString ''' implicit = record_id == 9 if file_state.propstring_implicit is None: file_state.propstring_implicit = implicit @@ -254,17 +283,17 @@ class OasisLayout: key = len(self.propstrings) self.propstrings[key] = record.astring elif record_id in (11, 12): + ''' LayerName ''' record = records.LayerName.read(stream, record_id) record.merge_with_modals(modals) self.layers.append(record) elif record_id in (28, 29): + ''' Property ''' record = records.Property.read(stream, record_id) record.merge_with_modals(modals) - if not file_state.within_cell: - self.properties.append(record) - else: - self.cells[-1].properties.append(record) + file_state.property_target.append(record) elif record_id in (30, 31): + ''' XName ''' implicit = record_id == 30 if file_state.xname_implicit is None: file_state.xname_implicit = implicit @@ -277,25 +306,34 @@ class OasisLayout: if key is None: key = len(self.xnames) self.xnames[key] = XName.from_record(record) + # TODO: do anything with property target? # # Cell and elements # elif record_id in (13, 14): + ''' Cell ''' record = records.Cell.read(stream, record_id) record.merge_with_modals(modals) - self.cells.append(Cell(record.name)) + cell = Cell(record.name) + self.cells.append(cell) + file_state.property_target = cell.properties elif record_id in (15, 16): + ''' XYMode ''' record = records.XYMode.read(stream, record_id) record.merge_with_modals(modals) elif record_id in (17, 18): + ''' Placement ''' record = records.Placement.read(stream, record_id) record.merge_with_modals(modals) self.cells[-1].placements.append(record) + file_state.property_target = record.properties elif record_id in _GEOMETRY: + ''' Geometry ''' record = _GEOMETRY[record_id].read(stream, record_id) record.merge_with_modals(modals) self.cells[-1].geometry.append(record) + file_state.property_target = record.properties else: raise InvalidRecordError('Unknown record id: {}'.format(record_id)) return False @@ -304,26 +342,33 @@ class OasisLayout: """ Write this object in OASIS fromat to a stream. - :param stream: Stream to write to. - :return: Number of bytes written. - :raises: InvalidDataError if contained records are invalid. + Args: + stream: Stream to write to. + + Returns: + Number of bytes written. + + Raises: + InvalidDataError: if contained records are invalid. """ modals = Modals() size = 0 size += write_magic_bytes(stream) size += records.Start(self.unit, self.version).dedup_write(stream, modals) + size += sum(p.dedup_write(stream, modals) for p in self.properties) cellnames_offset = OffsetEntry(False, size) - size += sum(records.CellName(name, refnum).dedup_write(stream, modals) - for refnum, name in self.cellnames.items()) + for refnum, cn in self.cellnames.items(): + size += records.CellName(cn.nstring, refnum).dedup_write(stream, modals) + size += sum(p.dedup_write(stream, modals) for p in cn.properties) propnames_offset = OffsetEntry(False, size) size += sum(records.PropName(name, refnum).dedup_write(stream, modals) for refnum, name in self.propnames.items()) xnames_offset = OffsetEntry(False, size) - size += sum(records.XName(x.attribute, x.string, refnum).dedup_write(stream, modals) + size += sum(records.XName(x.attribute, x.bstring, refnum).dedup_write(stream, modals) for refnum, x in self.xnames.items()) textstrings_offset = OffsetEntry(False, size) @@ -337,8 +382,6 @@ class OasisLayout: layernames_offset = OffsetEntry(False, size) size += sum(r.dedup_write(stream, modals) for r in self.layers) - size += sum(p.dedup_write(stream, modals) for p in self.properties) - size += sum(c.dedup_write(stream, modals) for c in self.cells) offset_table = OffsetTable( @@ -357,58 +400,110 @@ class Cell: """ Representation of an OASIS cell. - Properties: - .name NString or int (CellName reference number) + Attributes: + name (Union[NString, int]): name or "CellName reference" number - .properties List of records.Property - .placements List of records.Placement - .geometry List of geometry record objectes + properties (List[records.Property]): Properties of this cell + placements (List[records.Placement]): Placement record objects + geometry: (List[records.geometry_t]): Geometry record objectes """ - name = None # type: NString or int - properties = None # type: List[records.Property] - placements = None # type: List[records.Placement] - geometry = None # type: List[records.geometry_t] + name: Union[NString, int] + properties: List[records.Property] + placements: List[records.Placement] + geometry: List[records.geometry_t] - def __init__(self, name: NString or int): - """ - :param name: NString or int (CellName reference number) - """ - self.name = name - self.properties = [] - self.placements = [] - self.geometry = [] + 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.BufferedIOBase, modals: Modals) -> int: """ Write this cell to a stream, using the provided modal variables to deduplicate any repeated data. - :param stream: Stream to write to. - :param modals: Modal variables to use for deduplication. - :return: Number of bytes written. - :raises: InvalidDataError if contained records are invalid. + Args: + stream: Stream to write to. + modals: Modal variables to use for deduplication. + + Returns: + Number of bytes written. + + Raises: + InvalidDataError: if contained records are invalid. """ size = records.Cell(self.name).dedup_write(stream, modals) size += sum(p.dedup_write(stream, modals) for p in self.properties) - size += sum(p.dedup_write(stream, modals) for p in self.placements) - size += sum(g.dedup_write(stream, modals) for g in self.geometry) + for placement in self.placements: + size += placement.dedup_write(stream, modals) + size += sum(p.dedup_write(stream, modals) for p in placement.properties) + for shape in self.geometry: + size += shape.dedup_write(stream, modals) + size += sum(p.dedup_write(stream, modals) for p in shape.properties) return size +class CellName: + """ + Representation of a CellName. + + This class is effectively a simplified form of a `records.CellName`, + with the reference data stripped out. + """ + nstring: NString + properties: List[records.Property] + + def __init__(self, + nstring: Union[NString, str], + properties: Optional[List[records.Property]] = None): + """ + Args: + nstring: The contained string. + properties: Properties which apply to this CellName's cell, but + are placed following the CellName record. + """ + if isinstance(nstring, NString): + self.nstring = nstring + else: + self.nstring = NString(nstring) + self.properties = [] if properties is None else properties + + @staticmethod + def from_record(record: records.CellName) -> 'CellName': + """ + Create an `CellName` object from a `records.CellName` record. + + Args: + record: CellName record to use. + + Returns: + A new `CellName` object. + """ + return CellName(record.nstring) + + class XName: """ Representation of an XName. - This class is effectively a simplified form of a records.XName, + This class is effectively a simplified form of a `records.XName`, with the reference data stripped out. """ - attribute = None # type: int - bstring = None # type: bytes + attribute: int + bstring: bytes def __init__(self, attribute: int, bstring: bytes): """ - :param attribute: Attribute number. - :param bstring: Binary data. + Args: + attribute: Attribute number. + bstring: Binary data. """ self.attribute = attribute self.bstring = bstring @@ -416,16 +511,19 @@ class XName: @staticmethod def from_record(record: records.XName) -> 'XName': """ - Create an XName object from a records.XName record. + Create an `XName` object from a `records.XName` record. - :param record: XName record to use. - :return: XName object. + Args: + record: XName record to use. + + Returns: + a new `XName` object. """ return XName(record.attribute, record.bstring) # Mapping from record id to record class. -_GEOMETRY = { +_GEOMETRY: Dict[int, Type[records.geometry_t]] = { 19: records.Text, 20: records.Rectangle, 21: records.Polygon, diff --git a/fatamorgana/py.typed b/fatamorgana/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/fatamorgana/records.py b/fatamorgana/records.py index f76682e..cdf361a 100644 --- a/fatamorgana/records.py +++ b/fatamorgana/records.py @@ -10,8 +10,8 @@ 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 List, Dict, Tuple, Union, Optional, Sequence, Any, TypeVar from abc import ABCMeta, abstractmethod -from typing import List, Dict, Tuple import copy import math import zlib @@ -19,55 +19,62 @@ import io import logging import pprint from warnings import warn -from .basic import AString, NString, repetition_t, property_value_t, real_t, \ - ReuseRepetition, OffsetTable, Validation, read_point_list, read_property_value, \ - read_bstring, read_uint, read_sint, read_real, read_repetition, read_interval, \ - write_bstring, write_uint, write_sint, write_real, write_interval, write_point_list, \ - write_property_value, read_bool_byte, write_bool_byte, read_byte, write_byte, \ - InvalidDataError, PathExtensionScheme +from .basic import ( + AString, NString, repetition_t, property_value_t, real_t, + ReuseRepetition, OffsetTable, Validation, read_point_list, read_property_value, + read_bstring, read_uint, read_sint, read_real, read_repetition, read_interval, + write_bstring, write_uint, write_sint, write_real, write_interval, write_point_list, + write_property_value, read_bool_byte, write_bool_byte, read_byte, write_byte, + InvalidDataError, UnfilledModalError, PathExtensionScheme, _USE_NUMPY, + ) + +if _USE_NUMPY: + import numpy logger = logging.getLogger(__name__) + ''' Type definitions ''' -geometry_t = 'Text' or 'Rectangle' or 'Polygon' or 'Path' or 'Trapezoid' or \ - 'CTrapezoid' or 'Circle' or 'XElement' or 'XGeometry' -pathextension_t = Tuple['PathExtensionScheme' or int] +geometry_t = Union['Text', 'Rectangle', 'Polygon', 'Path', 'Trapezoid', + 'CTrapezoid', 'Circle', 'XElement', 'XGeometry'] +pathextension_t = Tuple['PathExtensionScheme', Optional[int]] +point_list_t = Sequence[Sequence[int]] class Modals: """ - Modal variables, used to store data about previously-written ori + Modal variables, used to store data about previously-written or -read records. """ - repetition = None # type: repetition_t or None - placement_x = 0 # type: int - placement_y = 0 # type: int - placement_cell = None # type: int or NString or None - layer = None # type: int or None - datatype = None # type: int or None - text_layer = None # type: int or None - text_datatype = None # type: int or None - text_x = 0 # type: int - text_y = 0 # type: int - text_string = None # type: AString or int or None - geometry_x = 0 # type: int - geometry_y = 0 # type: int - xy_relative = False # type: bool - geometry_w = None # type: int or None - geometry_h = None # type: int or None - polygon_point_list = None # type: List[List[int]] or None - path_halfwidth = None # type: int or None - path_point_list = None # type: List[List[int]] or None - path_extension_start = None # type: pathextension_t or None - path_extension_end = None # type: pathextension_t or None - ctrapezoid_type = None # type: int or None - circle_radius = None # type: int or None - property_value_list = None # type: List[property_value_t] or None - property_name = None # type: int or NString or None - property_is_standard = None # type: bool or None + repetition: Optional[repetition_t] = None + placement_x: int = 0 + placement_y: int = 0 + 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: Union[AString, int, None] = None + geometry_x: int = 0 + geometry_y: int = 0 + xy_relative: bool = False + 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): self.reset() @@ -76,9 +83,9 @@ class Modals: """ Resets all modal variables to their default values. Default values are: - 0 for placement_{x,y}, text_{x,y}, geometry_{x,y} - False for xy_relative - Undefined (None) for all others + `0` for placement_{x,y}, text_{x,y}, geometry_{x,y} + `False` for xy_relative + Undefined (`None`) for all others """ self.repetition = None self.placement_x = 0 @@ -97,7 +104,7 @@ class Modals: self.geometry_w = None self.geometry_h = None self.polygon_point_list = None - self.path_halfwidth = None + self.path_half_width = None self.path_point_list = None self.path_extension_start = None self.path_extension_end = None @@ -108,6 +115,13 @@ class Modals: self.property_is_standard = None +T = TypeVar('T') +def verify_modal(var: Optional[T]) -> T: + if var is None: + raise UnfilledModalError + return var + + ''' Records @@ -124,7 +138,8 @@ class Record(metaclass=ABCMeta): Copy all defined values from this record into the modal variables. Fill all undefined values in this record from the modal variables. - :param modals: Modal variables to merge with. + Args: + modals: Modal variables to merge with. """ pass @@ -137,7 +152,8 @@ class Record(metaclass=ABCMeta): used instead. Update the modal variables using the remaining (unequal) values. - :param modals: Modal variables to deduplicate with. + Args: + modals: Modal variables to deduplicate with. """ pass @@ -148,12 +164,17 @@ class Record(metaclass=ABCMeta): Read a record of this type from a stream. This function does not merge with modal variables. - :param stream: Stream to read from. - :param record_id: Record id of the record to read. The + Args: + stream: Stream to read from. + record_id: Record id of the record to read. The record id is often used to specify which variant of the record is stored. - :return: The record that was read. - :raises: InvalidDataError if the record is malformed. + + Returns: + The record that was read. + + Raises: + InvalidDataError: if the record is malformed. """ pass @@ -163,20 +184,30 @@ class Record(metaclass=ABCMeta): Write this record to a stream as-is. This function does not merge or deduplicate with modal variables. - :param stream: Stream to write to. - :return: Number of bytes written. - :raises: InvalidDataError if the record contains invalid data. + Args: + stream: Stream to write to. + + Returns: + Number of bytes written. + + Raises: + InvalidDataError: if the record contains invalid data. """ pass def dedup_write(self, stream: io.BufferedIOBase, modals: Modals) -> int: """ - Run .deduplicate_with_modals() and then .write() to the stream. + Run `.deduplicate_with_modals()` and then `.write()` to the stream. - :param stream: Stream to write to. - :param modals: Modal variables to merge with. - :return: Number of bytes written - :raises: InvalidDataError if the record contains invalid data. + Args: + stream: Stream to write to. + modals: Modal variables to merge with. + + Returns: + Number of bytes written. + + Raises: + InvalidDataError: if the record contains invalid data. """ # TODO logging #print(type(self), stream.tell()) @@ -187,7 +218,8 @@ class Record(metaclass=ABCMeta): """ Perform a deep copy of this record. - :return: A deep copy of this record. + Returns: + A deep copy of this record. """ return copy.deepcopy(self) @@ -195,18 +227,49 @@ class Record(metaclass=ABCMeta): return '{}: {}'.format(self.__class__, pprint.pformat(self.__dict__)) +class GeometryMixin(metaclass=ABCMeta): + """ + Mixin defining common functions for geometry records + """ + x: Optional[int] + y: Optional[int] + layer: Optional[int] + datatype: Optional[int] + + def get_x(self) -> int: + return verify_modal(self.x) + + def get_y(self) -> int: + return verify_modal(self.y) + + def get_xy(self) -> Tuple[int, int]: + return (self.get_x(), self.get_y()) + + def get_layer(self) -> int: + return verify_modal(self.layer) + + def get_datatype(self) -> int: + return verify_modal(self.datatype) + + def get_layer_tuple(self) -> Tuple[int, int]: + return (self.get_layer(), self.get_datatype()) + + def read_refname(stream: io.BufferedIOBase, - is_present: bool, - is_reference: bool - ) -> None or int or NString: + is_present: Union[bool, int], + is_reference: Union[bool, int] + ) -> Union[None, int, NString]: """ Helper function for reading a possibly-absent, possibly-referenced NString. - :param stream: Stream to read from. - :param is_present: If False, read nothing and return None - :param is_reference: If True, read a uint (reference id), - otherwise read an NString. - :return: None, reference id, or NString + Args: + stream: Stream to read from. + is_present: If `False`, read nothing and return `None` + is_reference: If `True`, read a uint (reference id), + otherwise read an `NString`. + + Returns: + `None`, reference id, or `NString` """ if not is_present: return None @@ -217,17 +280,20 @@ def read_refname(stream: io.BufferedIOBase, def read_refstring(stream: io.BufferedIOBase, - is_present: bool, - is_reference: bool - ) -> None or int or AString: + is_present: Union[bool, int], + is_reference: Union[bool, int], + ) -> Union[None, int, AString]: """ - Helper function for reading a possibly-absent, possibly-referenced AString. + Helper function for reading a possibly-absent, possibly-referenced `AString`. - :param stream: Stream to read from. - :param is_present: If False, read nothing and return None - :param is_reference: If True, read a uint (reference id), - otherwise read an AString. - :return: None, reference id, or AString + Args: + stream: Stream to read from. + is_present: If `False`, read nothing and return `None` + is_reference: If `True`, read a uint (reference id), + otherwise read an `AString`. + + Returns: + `None`, reference id, or `AString` """ if not is_present: return None @@ -264,14 +330,14 @@ class XYMode(Record): """ XYMode record (ID 15, 16) - Properties: - .relative (bool, default False) + Attributes: + relative (bool): default `False` """ - relative = False # type: bool + relative: bool = False @property def absolute(self) -> bool: - return not relative + return not self.relative @absolute.setter def absolute(self, b: bool): @@ -279,7 +345,8 @@ class XYMode(Record): def __init__(self, relative: bool): """ - :param relative: True if the mode is 'relative', False if 'absolute'. + Args: + relative: `True` if the mode is 'relative', `False` if 'absolute'. """ self.relative = relative @@ -305,25 +372,26 @@ class Start(Record): """ Start Record (ID 1) - Properties: - .version (AString, "1.0") - .unit (positive real number, grid steps per micron) - .offset_table (OffsetTable or None, if None then table must be - placed in the End record) + 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 = None # type: AString - unit = None # type: real_t - offset_table = None # type: OffsetTable + version: AString + unit: real_t + offset_table: Optional[OffsetTable] = None def __init__(self, unit: real_t, - version: AString or str = None, - offset_table: OffsetTable = None): + version: Union[AString, str] = None, + offset_table: Optional[OffsetTable] = None): """ - :param unit: Grid steps per micron (positive real number) - :param version: Version string, default "1.0" - :param offset_table: OffsetTable for the file, or None to place - it in the End record instead. + Args + unit: Grid steps per micron (positive real number) + version: Version string, default "1.0" + offset_table: `OffsetTable` for the file, or `None` to place + it in the `End` record instead. """ if unit <= 0: raise InvalidDataError('Non-positive unit: {}'.format(unit)) @@ -360,6 +428,7 @@ class Start(Record): version = AString.read(stream) unit = read_real(stream) has_offset_table = read_uint(stream) == 0 + offset_table: Optional[OffsetTable] if has_offset_table: offset_table = OffsetTable.read(stream) else: @@ -384,21 +453,22 @@ class End(Record): The end record is always padded to a total length of 256 bytes. - Properties: - .offset_table (OffsetTable or None, None if offset table was - written into the Start record instead) - .validation (Validation object) + Attributes: + offset_table (Optional[OffsetTable]): `None` if offset table was + written into the `Start` record instead + validation (Validation): object containing checksum """ - offset_table = None # type: OffsetTable or None - validation = None # type: Validation + offset_table: Optional[OffsetTable] = None + validation: Validation def __init__(self, validation: Validation, - offset_table: OffsetTable = None): + offset_table: Optional[OffsetTable] = None): """ - :param validation: Validation object for this file. - :param offset_table: OffsetTable, or None if the Start record - contained an OffsetTable. Default None. + Args: + validation: `Validation` object for this file. + offset_table: `OffsetTable`, or `None` if the `Start` record + contained an `OffsetTable`. Default `None`. """ self.validation = validation self.offset_table = offset_table @@ -417,10 +487,10 @@ class End(Record): if record_id != 2: raise InvalidDataError('Invalid record id for End {}'.format(record_id)) if has_offset_table: - offset_table = OffsetTable.read(stream) + offset_table: Optional[OffsetTable] = OffsetTable.read(stream) else: offset_table = None - _padding_string = read_bstring(stream) + _padding_string = read_bstring(stream) # noqa validation = Validation.read(stream) record = End(validation, offset_table) logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) @@ -447,23 +517,24 @@ class CBlock(Record): """ CBlock (Compressed Block) record (ID 34) - Properties: - .compression_type (int, 0 for zlib) - .decompressed_byte_count (int) - .compressed_bytes (bytes) + Attributes: + compression_type (int): `0` for zlib + decompressed_byte_count (int): size after decompressing + compressed_bytes (bytes): compressed data """ - compression_type = None # type: int - decompressed_byte_count = None # type: int - compressed_bytes = None # type: bytes + compression_type: int + decompressed_byte_count: int + compressed_bytes: bytes def __init__(self, compression_type: int, decompressed_byte_count: int, compressed_bytes: bytes): """ - :param compression_type: 0 (zlib) - :param decompressed_byte_count: Number of bytes in the decompressed data. - :param compressed_bytes: The compressed data. + Args: + compression_type: `0` (zlib) + decompressed_byte_count: Number of bytes in the decompressed data. + compressed_bytes: The compressed data. """ if compression_type != 0: raise InvalidDataError('CBlock: Invalid compression scheme ' @@ -506,11 +577,16 @@ class CBlock(Record): """ Create a CBlock record from uncompressed data. - :param decompressed_bytes: Uncompressed data (one or more non-CBlock records) - :param compression_type: Compression type (0: zlib). Default 0 - :param compression_args Passed as kwargs to zlib.compressobj(). Default {}. - :return: CBlock object constructed from the data. - :raises: InvalidDataError if invalid compression_type. + Args: + decompressed_bytes: Uncompressed data (one or more non-CBlock records) + compression_type: Compression type (0: zlib). Default `0` + compression_args: Passed as kwargs to `zlib.compressobj()`. Default `{}`. + + Returns: + CBlock object constructed from the data. + + Raises: + InvalidDataError: if invalid `compression_type`. """ if compression_args is None: compression_args = {} @@ -518,8 +594,8 @@ class CBlock(Record): if compression_type == 0: count = len(decompressed_bytes) compressor = zlib.compressobj(wbits=-zlib.MAX_WBITS, **compression_args) - compressed_bytes = compressor.compress(decompressed_bytes) + \ - compressor.flush() + compressed_bytes = (compressor.compress(decompressed_bytes) + + compressor.flush()) else: raise InvalidDataError('Unknown compression type: ' '{}'.format(compression_type)) @@ -530,17 +606,22 @@ class CBlock(Record): """ Decompress the contents of this CBlock. - :param decompression_args: Passed as kwargs to zlib.decompressobj(). - :return: Decompressed bytes object. - :raises: InvalidDataError if data is malformed or compression type is + Args: + decompression_args: Passed as kwargs to `zlib.decompressobj()`. + + Returns: + Decompressed `bytes` object. + + Raises: + InvalidDataError: if data is malformed or compression type is unknonwn. """ if decompression_args is None: decompression_args = {} if self.compression_type == 0: decompressor = zlib.decompressobj(wbits=-zlib.MAX_WBITS, **decompression_args) - decompressed_bytes = decompressor.decompress(self.compressed_bytes) + \ - decompressor.flush() + decompressed_bytes = (decompressor.decompress(self.compressed_bytes) + + decompressor.flush()) if len(decompressed_bytes) != self.decompressed_byte_count: raise InvalidDataError('Decompressed data length does not match!') else: @@ -553,20 +634,21 @@ class CellName(Record): """ CellName record (ID 3, 4) - Properties: - .nstring (NString) - .reference_number (int or None) + Attributes: + nstring (NString): name + reference_number (Optional[int]): `None` results in implicit assignment """ - nstring = None # type: NString - reference_number = None # type: int or None + nstring: NString + reference_number: Optional[int] = None def __init__(self, - nstring: NString or str, + nstring: Union[NString, str], reference_number: int = None): """ - :param nstring: The contained string. - :param reference_number: Reference id number for the string. - Default is to use an implicitly-assigned number. + Args: + nstring: The contained string. + reference_number: Reference id number for the string. + Default is to use an implicitly-assigned number. """ if isinstance(nstring, NString): self.nstring = nstring @@ -587,7 +669,7 @@ class CellName(Record): '{}'.format(record_id)) nstring = NString.read(stream) if record_id == 4: - reference_number = read_uint(stream) + reference_number: Optional[int] = read_uint(stream) else: reference_number = None record = CellName(nstring, reference_number) @@ -606,20 +688,21 @@ class PropName(Record): """ PropName record (ID 7, 8) - Properties: - .nstring (NString) - .reference_number (int or None) + Attributes: + nstring (NString): name + reference_number (Optional[int]): `None` results in implicit assignment """ - nstring = None # type: NString - reference_number = None # type: int or None + nstring: NString + reference_number: Optional[int] = None def __init__(self, - nstring: NString or str, + nstring: Union[NString, str], reference_number: int = None): """ - :param nstring: The contained string. - :param reference_number: Reference id number for the string. - Default is to use an implicitly-assigned number. + Args: + nstring: The contained string. + reference_number: Reference id number for the string. + Default is to use an implicitly-assigned number. """ if isinstance(nstring, NString): self.nstring = nstring @@ -640,7 +723,7 @@ class PropName(Record): '{}'.format(record_id)) nstring = NString.read(stream) if record_id == 8: - reference_number = read_uint(stream) + reference_number: Optional[int] = read_uint(stream) else: reference_number = None record = PropName(nstring, reference_number) @@ -660,20 +743,21 @@ class TextString(Record): """ TextString record (ID 5, 6) - Properties: - .astring (AString) - .reference_number (int or None) + Attributes: + astring (AString): string data + reference_number (Optional[int]): `None` results in implicit assignment """ - astring = None # type: AString - reference_number = None # type: int or None + astring: AString + reference_number: Optional[int] = None def __init__(self, - string: AString or str, + string: Union[AString, str], reference_number: int = None): """ - :param string: The contained string. - :param reference_number: Reference id number for the string. - Default is to use an implicitly-assigned number. + Args: + string: The contained string. + reference_number: Reference id number for the string. + Default is to use an implicitly-assigned number. """ if isinstance(string, AString): self.astring = string @@ -694,7 +778,7 @@ class TextString(Record): '{}'.format(record_id)) astring = AString.read(stream) if record_id == 6: - reference_number = read_uint(stream) + reference_number: Optional[int] = read_uint(stream) else: reference_number = None record = TextString(astring, reference_number) @@ -714,20 +798,21 @@ class PropString(Record): """ PropString record (ID 9, 10) - Properties: - .astring (AString) - .reference_number (int or None) + Attributes: + astring (AString): string data + reference_number (Optional[int]): `None` results in implicit assignment """ - astring = None # type: AString - reference_number = None # type: int or None + astring: AString + reference_number: Optional[int] = None def __init__(self, - string: AString or str, + string: Union[AString, str], reference_number: int = None): """ - :param string: The contained string. - :param reference_number: Reference id number for the string. - Default is to use an implicitly-assigned number. + Args: + string: The contained string. + reference_number: Reference id number for the string. + Default is to use an implicitly-assigned number. """ if isinstance(string, AString): self.astring = string @@ -748,7 +833,7 @@ class PropString(Record): '{}'.format(record_id)) astring = AString.read(stream) if record_id == 10: - reference_number = read_uint(stream) + reference_number: Optional[int] = read_uint(stream) else: reference_number = None record = PropString(astring, reference_number) @@ -768,31 +853,30 @@ class LayerName(Record): """ LayerName record (ID 11, 12) - Properties: - .nstring (NString) - .layer_interval (Tuple, (int or None, int or None), - bounds on the interval) - .type_interval (Tuple, (int or None, int or None), - bounds on the interval) - .is_textlayer (bool) + 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 = None # type: NString, - layer_interval = None # type: Tuple - type_interval = None # type: Tuple - is_textlayer = None # type: bool + nstring: NString + layer_interval: Tuple + type_interval: Tuple + is_textlayer: bool def __init__(self, - nstring: NString or str, + nstring: Union[NString, str], layer_interval: Tuple, type_interval: Tuple, is_textlayer: bool): """ - :param nstring: The layer name. - :param layer_interval: Tuple (int or None, int or None) giving bounds - (or lack of thereof) on the layer number. - :param type_interval: Tuple (int or None, int or None) giving bounds - (or lack of thereof) on the type number. - :param is_textlayer: True if the layer is a text layer. + Args: + nstring: The layer name. + 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): self.nstring = nstring @@ -834,36 +918,45 @@ class Property(Record): """ LayerName record (ID 28, 29) - Properties: - .name (NString or int or None, - int is an explicit reference, - None is a flag to use Modal) - .values (List of property values or None) - .is_standard (bool, whether this is a standard property) + 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 = None # type: NString or int or None, - values = None # type: List[property_value_t] or None - is_standard = None # type: bool or None + name: Optional[Union[NString, int]] = None + values: Optional[List[property_value_t]] = None + is_standard: Optional[bool] = None def __init__(self, - name: NString or str or int = None, - values: List[property_value_t] = None, - is_standard: bool = None): + name: Union[NString, str, int, None] = None, + values: Optional[List[property_value_t]] = None, + is_standard: Optional[bool] = None): """ - :param name: Property name, reference number, or None (i.e. use modal) - Default None. - :param values: List of property values, or None (i.e. use modal) - Default None. - :param is_standard: True if this is a standard property. None to use modal. - Default None. + Args: + name: Property name, reference number, or `None` (i.e. use modal) + Default `None. + values: List of property values, or `None` (i.e. use modal) + Default `None`. + is_standard: `True` if this is a standard property. `None` to use modal. + Default `None`. """ - if isinstance(name, str): - self.name = NString(name) - else: + 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) -> Union[NString, int]: + return verify_modal(self.name) # type: ignore + + 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): adjust_field(self, 'name', modals, 'property_name') adjust_field(self, 'values', modals, 'property_value_list') @@ -883,7 +976,7 @@ class Property(Record): if record_id == 29: record = Property() else: - byte = read_byte(stream) #UUUUVCNS + byte = read_byte(stream) # UUUUVCNS u = 0x0f & (byte >> 4) v = 0x01 & (byte >> 3) c = 0x01 & (byte >> 2) @@ -896,12 +989,13 @@ class Property(Record): value_count = u else: value_count = read_uint(stream) - values = [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 u != 0: raise InvalidDataError('Malformed property record header') - record = Property(name, values, s) + record = Property(name, values, bool(s)) logger.debug('Record ending at 0x{:x}:\n {}'.format(stream.tell(), record)) return record @@ -931,13 +1025,13 @@ class Property(Record): size += write_byte(stream, (u << 4) | (v << 3) | (c << 2) | (n << 1) | s) if c: if n: - size += write_uint(stream, self.name) + size += write_uint(stream, self.name) # type: ignore else: - size += self.name.write(stream) + size += self.name.write(stream) # type: ignore if not v: if u == 0x0f: - size += write_uint(stream, self.name) - size += sum(write_property_value(stream, p) for p in self.values) + 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 @@ -945,24 +1039,25 @@ class XName(Record): """ XName record (ID 30, 31) - Properties: - .attribute (int) - .bstring (bytes) - .reference_number (int or None, None means to use implicity numbering) + Attributes: + attribute (int): Attribute number + bstring (bytes): XName data + reference_number (Optional[int]): None means to use implicit numbering """ - attribute = None # type: int - bstring = None # type: bytes - reference_number = None # type: int or None + attribute: int + bstring: bytes + reference_number: Optional[int] = None def __init__(self, attribute: int, bstring: bytes, reference_number: int = None): """ - :param attribute: Attribute number. - :param bstring: Binary XName data. - :param reference_number: Reference number for this XName. - Default None (implicit). + Args: + attribute: Attribute number. + bstring: Binary XName data. + reference_number: Reference number for this `XName`. + Default `None` (implicit). """ self.attribute = attribute self.bstring = bstring @@ -982,7 +1077,7 @@ class XName(Record): attribute = read_uint(stream) bstring = read_bstring(stream) if record_id == 31: - reference_number = read_uint(stream) + reference_number: Optional[int] = read_uint(stream) else: reference_number = None record = XName(attribute, bstring, reference_number) @@ -1003,20 +1098,27 @@ class XElement(Record): """ XElement record (ID 32) - Properties: - .attribute (int) - .bstring (bytes) + Attributes: + attribute (int): Attribute number. + bstring (bytes): XElement data. """ - attribute = None # type: int - bstring = None # type: bytes + attribute: int + bstring: bytes + properties: List['Property'] - def __init__(self, attribute: int, bstring: bytes): + def __init__(self, + attribute: int, + bstring: bytes, + properties: Optional[List['Property']] = None): """ - :param attribute: Attribute number. - :param bstring: Binary data for this XElement. + Args: + attribute: Attribute number. + bstring: Binary data for this XElement. + properties: List of property records associated with this record. """ self.attribute = attribute self.bstring = bstring + self.properties = [] if properties is None else properties def merge_with_modals(self, modals: Modals): pass @@ -1042,43 +1144,48 @@ class XElement(Record): return size -class XGeometry(Record): +class XGeometry(Record, GeometryMixin): """ XGeometry record (ID 33) - Properties: - .attribute (int) - .bstring (bytes) - .layer (int or None, None means reuse modal) - .datatype (int or None, None means reuse modal) - .x (int or None, None means reuse modal) - .y (int or None, None means reuse modal) - .repetition (reptetition or None) + 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 = None # type: int - bstring = None # type: bytes - layer = None # type: int or None - datatype = None # type: int or None - x = None # type: int or None - y = None # type: int or None - repetition = None # type: repetition_t or None + 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: List['Property'] def __init__(self, attribute: int, bstring: bytes, - layer: int = None, - datatype: int = None, - x: int = None, - y: int = None, - repetition: repetition_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): """ - :param attribute: Attribute number for this XGeometry. - :param bstring: Binary data for this XGeometry. - :param layer: Layer number. Default None (reuse modal). - :param datatype: Datatype number. Default None (reuse modal). - :param x: X-offset. Default None (use modal). - :param y: Y-offset. Default None (use modal). - :param repetition: Repetition. Default None (no repetition). + Args: + attribute: Attribute number for this XGeometry. + bstring: Binary data for this XGeometry. + layer: Layer number. Default `None` (reuse modal). + datatype: Datatype number. Default `None` (reuse modal). + x: X-offset. Default `None` (use modal). + y: Y-offset. Default `None` (use modal). + repetition: Repetition. Default `None` (no repetition). + properties: List of property records associated with this record. """ self.attribute = attribute self.bstring = bstring @@ -1087,6 +1194,7 @@ class XGeometry(Record): self.x = x self.y = y self.repetition = repetition + self.properties = [] if properties is None else properties def merge_with_modals(self, modals: Modals): adjust_coordinates(self, modals, 'geometry_x', 'geometry_y') @@ -1110,7 +1218,7 @@ class XGeometry(Record): if z0 or z1 or z2: raise InvalidDataError('Malformed XGeometry header') attribute = read_uint(stream) - optional = {} + optional: Dict[str, Any] = {} if l: optional['layer'] = read_uint(stream) if d: @@ -1138,16 +1246,16 @@ class XGeometry(Record): size += write_bool_byte(stream, (0, 0, 0, x, y, r, d, l)) size += write_uint(stream, self.attribute) if l: - size += write_uint(stream, self.layer) + size += write_uint(stream, self.layer) # type: ignore if d: - size += write_uint(stream, self.datatype) + size += write_uint(stream, self.datatype) # type: ignore size += write_bstring(stream, self.bstring) if x: - size += write_sint(stream, self.x) + size += write_sint(stream, self.x) # type: ignore if y: - size += write_sint(stream, self.y) + size += write_sint(stream, self.y) # type: ignore if r: - size += self.repetition.write(stream) + size += self.repetition.write(stream) # type: ignore return size @@ -1155,16 +1263,17 @@ class Cell(Record): """ Cell record (ID 13, 14) - Properties: - .name (NString or int specifying CellName reference number) + Attributes: + name (Union[int, NString]): int specifies "CellName reference" number """ - name = None # type: int or NString + name: Union[int, NString] - def __init__(self, name: int or NString): + def __init__(self, name: Union[int, str, NString]): """ - :param name: NString, or an int specifying a CellName reference number. + Args: + name: `NString`, or an int specifying a `CellName` reference number. """ - self.name = name + self.name = name if isinstance(name, (int, NString)) else NString(name) def merge_with_modals(self, modals: Modals): modals.reset() @@ -1174,6 +1283,7 @@ class Cell(Record): @staticmethod 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: @@ -1200,44 +1310,47 @@ class Placement(Record): """ Placement record (ID 17, 18) - Properties: - .attribute (int) - .name (NString, name or - int, CellName reference number or - None, reuse modal) - .magnification (real) - .angle (real, degrees counterclockwise) - .x (int or None, None means reuse modal) - .y (int or None, None means reuse modal) - .repetition (reptetition or None) - .flip (bool) + 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 = None # type: NString or int or None - magnification = None # type: real_t or None - angle = None # type: real_t or None - x = None # type: int or None - y = None # type: int or None - repetition = None # type: repetition_t or None - flip = None # type: bool + 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 + properties: List['Property'] def __init__(self, flip: bool, - name: NString or str or int = None, - magnification: real_t = None, - angle: real_t = None, - x: int = None, - y: int = None, - repetition: repetition_t = None): + 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): """ - :param flip: Whether to perform reflection about the x-axis. - :param name: NString, an int specifying a CellName reference number, - or None (reuse modal). - :param magnification: Magnification factor. Default None (use modal). - :param angle: Rotation angle in degrees, counterclockwise. - Default None (reuse modal). - :param x: X-offset. Default None (use modal). - :param y: Y-offset. Default None (use modal). - :param repetition: Repetition. Default None (no repetition). + Args: + flip: Whether to perform reflection about the x-axis. + name: `NString`, an int specifying a `CellName` reference number, + or `None` (reuse modal). + magnification: Magnification factor. Default `None` (use modal). + angle: Rotation angle in degrees, counterclockwise. + Default `None` (reuse modal). + x: X-offset. Default `None` (use modal). + y: Y-offset. Default `None` (use modal). + repetition: Repetition. Default `None` (no repetition). + properties: List of property records associated with this record. """ self.x = x self.y = y @@ -1245,10 +1358,20 @@ class Placement(Record): self.flip = flip self.magnification = magnification self.angle = angle - if isinstance(name, str): - self.name = NString(name) - else: + 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) -> Union[NString, int]: + return verify_modal(self.name) # type: ignore + + def get_x(self) -> int: + return verify_modal(self.x) + + def get_y(self) -> int: + return verify_modal(self.y) def merge_with_modals(self, modals: Modals): adjust_coordinates(self, modals, 'placement_x', 'placement_y') @@ -1269,7 +1392,7 @@ class Placement(Record): #CNXYRAAF (17) or CNXYRMAF (18) c, n, x, y, r, ma0, ma1, flip = read_bool_byte(stream) - optional = {} + optional: Dict[str, Any] = {} name = read_refname(stream, c, n) if record_id == 17: aa = (ma0 << 1) | ma1 @@ -1300,9 +1423,10 @@ class Placement(Record): r = self.repetition is not None f = self.flip - if self.angle is not None and self.angle % 90 == 0 and \ - self.magnification is None or self.magnification == 1: - aa = int((self.angle / 90) % 4) + 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 = (c, n, x, y, r, aa & 0b10, aa & 0b01, f) m = False a = False @@ -1317,66 +1441,75 @@ class Placement(Record): size += write_bool_byte(stream, bools) if c: if n: - size += write_uint(stream, self.name) + size += write_uint(stream, self.name) # type: ignore else: - size += self.name.write(self) + size += self.name.write(stream) # type: ignore if m: - size += write_real(stream, self.magnification) + size += write_real(stream, self.magnification) # type: ignore if a: - size += write_real(stream, self.angle) + size += write_real(stream, self.angle) # type: ignore if x: - size += write_sint(stream, self.x) + size += write_sint(stream, self.x) # type: ignore if y: - size += write_sint(stream, self.y) + size += write_sint(stream, self.y) # type: ignore if r: - size += self.repetition.write(stream) + size += self.repetition.write(stream) # type: ignore return size -class Text(Record): +class Text(Record, GeometryMixin): """ Text record (ID 19) - Properties: - .string (AString or int or None, None means reuse modal) - .layer (int or None, None means reuse modal) - .datatype (int or None, None means reuse modal) - .x (int or None, None means reuse modal) - .y (int or None, None means reuse modal) - .repetition (reptetition or 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 = None # type: AString or int or None - layer = None # type: int or None - datatype = None # type: int or None - x = None # type: int or None - y = None # type: int or None - repetition = None # type: repetition_t or None + 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: AString or str or int = None, - layer: int = None, - datatype: int = None, - x: int = None, - y: int = None, - repetition: repetition_t = None): + 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): """ - :param string: Text content, or TextString reference number. - Default None (use modal). - :param layer: Layer number. Default None (reuse modal). - :param datatype: Datatype number. Default None (reuse modal). - :param x: X-offset. Default None (use modal). - :param y: Y-offset. Default None (use modal). - :param repetition: Repetition. Default None (no repetition). + Args: + string: Text content, or `TextString` reference number. + Default `None` (use modal). + layer: Layer number. Default `None` (reuse modal). + datatype: Datatype number. Default `None` (reuse modal). + x: X-offset. Default `None` (use modal). + y: Y-offset. Default `None` (use modal). + repetition: Repetition. Default `None` (no repetition). + properties: List of property records associated with this record. """ self.layer = layer self.datatype = datatype self.x = x self.y = y self.repetition = repetition - if isinstance(string, str): - self.string = AString(string) - else: + 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) -> Union[AString, int]: + return verify_modal(self.string) # type: ignore def merge_with_modals(self, modals: Modals): adjust_coordinates(self, modals, 'text_x', 'text_y') @@ -1402,7 +1535,7 @@ class Text(Record): if z0: raise InvalidDataError('Malformed Text header') - optional = {} + optional: Dict[str, Any] = {} string = read_refstring(stream, c, n) if l: optional['layer'] = read_uint(stream) @@ -1432,68 +1565,63 @@ class Text(Record): size += write_bool_byte(stream, (0, c, n, x, y, r, d, l)) if c: if n: - size += write_uint(stream, self.string) + size += write_uint(stream, self.string) # type: ignore else: - size += self.string.write(self) + size += self.string.write(stream) # type: ignore if l: - size += write_uint(stream, self.layer) + size += write_uint(stream, self.layer) # type: ignore if d: - size += write_uint(stream, self.datatype) + size += write_uint(stream, self.datatype) # type: ignore if x: - size += write_sint(stream, self.x) + size += write_sint(stream, self.x) # type: ignore if y: - size += write_sint(stream, self.y) + size += write_sint(stream, self.y) # type: ignore if r: - size += self.repetition.write(stream) + size += self.repetition.write(stream) # type: ignore return size -class Rectangle(Record): +class Rectangle(Record, GeometryMixin): """ Rectangle record (ID 20) - Properties: - .is_square (bool, True if this is a square. - If True, height must be None.) - .width (int or None, None means reuse modal) - .height (int or None, Must be None if .is_square is True. - If .is_square is False, None means reuse modal) - .layer (int or None, None means reuse modal) - .datatype (int or None, None means reuse modal) - .x (int or None, None means use modal) - .y (int or None, None means use modal) - .repetition (reptetition or None) + (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 = None # type: int or None - datatype = None # type: int or None - width = None # type: int or None - height = None # type: int or None - x = None # type: int or None - y = None # type: int or None - repetition = None # type: repetition_t or None - is_square = None # type: bool + 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'] def __init__(self, is_square: bool = False, - layer: int = None, - datatype: int = None, - width: int = None, - height: int = None, - x: int = None, - y: int = None, - repetition: repetition_t = None): - """ - :param is_square: True if this is a square. If True, height must - be None. Default False. - :param layer: Layer number. Default None (reuse modal). - :param datatype: Datatype number. Default None (reuse modal). - :param width: X-width. Default None (reuse modal). - :param height: Y-height. Default None (reuse modal, or use width if - square). Must be None if is_square is True. - :param x: X-offset. Default None (use modal). - :param y: Y-offset. Default None (use modal). - :param repetition: Repetition. Default None (no repetition). - """ + 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 @@ -1504,6 +1632,15 @@ class Rectangle(Record): self.repetition = repetition if is_square and self.height is not None: raise InvalidDataError('Rectangle is square and also has height') + self.properties = [] if properties is None else properties + + def get_width(self) -> int: + return verify_modal(self.width) + + def get_height(self) -> int: + if self.is_square: + return verify_modal(self.width) + return verify_modal(self.height) def merge_with_modals(self, modals: Modals): adjust_coordinates(self, modals, 'geometry_x', 'geometry_y') @@ -1534,7 +1671,7 @@ class Rectangle(Record): '{}'.format(record_id)) is_square, w, h, x, y, r, d, l = read_bool_byte(stream) - optional = {} + optional: Dict[str, Any] = {} if l: optional['layer'] = read_uint(stream) if d: @@ -1566,72 +1703,75 @@ class Rectangle(Record): size = write_uint(stream, 20) size += write_bool_byte(stream, (s, w, h, x, y, r, d, l)) if l: - size += write_uint(stream, self.layer) + size += write_uint(stream, self.layer) # type: ignore if d: - size += write_uint(stream, self.datatype) + size += write_uint(stream, self.datatype) # type: ignore if w: - size += write_uint(stream, self.width) + size += write_uint(stream, self.width) # type: ignore if h: - size += write_uint(stream, self.height) + size += write_uint(stream, self.height) # type: ignore if x: - size += write_sint(stream, self.x) + size += write_sint(stream, self.x) # type: ignore if y: - size += write_sint(stream, self.y) + size += write_sint(stream, self.y) # type: ignore if r: - size += self.repetition.write(stream) + size += self.repetition.write(stream) # type: ignore return size -class Polygon(Record): +class Polygon(Record, GeometryMixin): """ Polygon record (ID 21) - Properties: - .point_list ([[x0, y0], [x1, y1], ...] or None, - list is an implicitly closed path, - vertices are [int, int], - None means reuse modal) - .layer (int or None, None means reuse modal) - .datatype (int or None, None means reuse modal) - .x (int or None, None means reuse modal) - .y (int or None, None means reuse modal) - .repetition (reptetition or None) + 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 = None # type: int or None - datatype = None # type: int or None - x = None # type: int or None - y = None # type: int or None - repetition = None # type: repetition_t or None - point_list = None # type: List[List[int]] or None + 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: List[List[int]] = None, - layer: int = None, - datatype: int = None, - x: int = None, - y: int = None, - repetition: repetition_t = None): - """ - :param point_list: List of vertices [[x0, y0], [x1, y1], ...]. - List forms an implicitly closed path - Default None (reuse modal). - :param layer: Layer number. Default None (reuse modal). - :param datatype: Datatype number. Default None (reuse modal). - :param x: X-offset. Default None (use modal). - :param y: Y-offset. Default None (use modal). - :param repetition: Repetition. Default None (no repetition). - """ + 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 self.y = y self.repetition = repetition self.point_list = point_list + self.properties = [] if properties is None else properties 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): adjust_coordinates(self, modals, 'geometry_x', 'geometry_y') adjust_repetition(self, modals) @@ -1656,7 +1796,7 @@ class Polygon(Record): if z0 or z1: raise InvalidDataError('Invalid polygon header') - optional = {} + optional: Dict[str, Any] = {} if l: optional['layer'] = read_uint(stream) if d: @@ -1684,82 +1824,68 @@ class Polygon(Record): size = write_uint(stream, 21) size += write_bool_byte(stream, (0, 0, p, x, y, r, d, l)) if l: - size += write_uint(stream, self.layer) + size += write_uint(stream, self.layer) # type: ignore if d: - size += write_uint(stream, self.datatype) + size += write_uint(stream, self.datatype) # type: ignore if p: - size += write_point_list(stream, self.point_list, implicit_closed=True, fast=fast) + size += write_point_list(stream, self.point_list, # type: ignore + implicit_closed=True, fast=fast) if x: - size += write_sint(stream, self.x) + size += write_sint(stream, self.x) # type: ignore if y: - size += write_sint(stream, self.y) + size += write_sint(stream, self.y) # type: ignore if r: - size += self.repetition.write(stream) + size += self.repetition.write(stream) # type: ignore return size -class Path(Record): +class Path(Record, GeometryMixin): """ Polygon record (ID 22) - Properties: - .point_list ([[x0, y0], [x1, y1], ...] or None, - vertices are [int, int], - None means reuse modal) - .half_width (int or None, None means reuse modal) - .extension_start (Tuple or None, - None means reuse modal, - Tuple is of the form - (PathExtensionScheme, int or None) - second value is None unless using - PathExtensionScheme.Arbitrary - Value determines extension past start point. - .extension_end Same form as extension_end. Value determines - extension past end point. - .layer (int or None, None means reuse modal) - .datatype (int or None, None means reuse modal) - .x (int or None, None means use modal) - .y (int or None, None means use modal) - .repetition (reptetition or None) + 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 = None # type: int or None - datatype = None # type: int or None - x = None # type: int or None - y = None # type: int or None - repetition = None # type: repetition_t or None - point_list = None # type: List[List[int]] or None - half_width = None # type: int or None - extension_start = None # type: pathextension_t or None - extension_end = None # type: pathextension_t or None + 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'] def __init__(self, - point_list: List[List[int]] = None, - half_width: int = None, - extension_start: pathextension_t = None, - extension_end: pathextension_t = None, - layer: int = None, - datatype: int = None, - x: int = None, - y: int = None, - repetition: repetition_t = None): - """ - :param point_list: List of vertices [[x0, y0], [x1, y1], ...]. - Default None (reuse modal). - :param half_width: Half-width of the path. Default None (reuse modal). - :param extension_start: Specification for path extension at start of path. - None or Tuple: (PathExtensionScheme, int or None). - int is used only for PathExtensionScheme.Arbitrary. - Default None (reuse modal). - :param extension_end: Specification for path extension at end of path. - None or Tuple: (PathExtensionScheme, int or None). - int is used only for PathExtensionScheme.Arbitrary. - Default None (reuse modal). - :param layer: Layer number. Default None (reuse modal). - :param datatype: Datatype number. Default None (reuse modal). - :param x: X-offset. Default None (use modal). - :param y: Y-offset. Default None (use modal). - :param repetition: Repetition. Default None (no repetition). - """ + 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 @@ -1769,6 +1895,19 @@ class Path(Record): self.half_width = half_width self.extension_start = extension_start self.extension_end = extension_end + self.properties = [] if properties is None else properties + + def get_point_list(self) -> point_list_t: + return verify_modal(self.point_list) + + def get_half_width(self) -> int: + return verify_modal(self.half_width) + + def get_extension_start(self) -> pathextension_t: + return verify_modal(self.extension_start) + + def get_extension_end(self) -> pathextension_t: + return verify_modal(self.extension_end) def merge_with_modals(self, modals: Modals): adjust_coordinates(self, modals, 'geometry_x', 'geometry_y') @@ -1797,7 +1936,7 @@ class Path(Record): '{}'.format(record_id)) e, w, p, x, y, r, d, l = read_bool_byte(stream) - optional = {} + optional: Dict[str, Any] = {} if l: optional['layer'] = read_uint(stream) if d: @@ -1809,7 +1948,7 @@ class Path(Record): scheme_end = scheme & 0b11 scheme_start = (scheme >> 2) & 0b11 - def get_pathext(ext_scheme: int) -> pathextension_t: + def get_pathext(ext_scheme: int) -> Optional[pathextension_t]: if ext_scheme == 0: return None elif ext_scheme == 1: @@ -1818,6 +1957,8 @@ class Path(Record): return PathExtensionScheme.HalfWidth, None elif ext_scheme == 3: return PathExtensionScheme.Arbitrary, read_sint(stream) + else: + raise InvalidDataError('Invalid ext_scheme: {}'.format(ext_scheme)) optional['extension_start'] = get_pathext(scheme_start) optional['extension_end'] = get_pathext(scheme_end) @@ -1846,11 +1987,11 @@ class Path(Record): size = write_uint(stream, 21) size += write_bool_byte(stream, (e, w, p, x, y, r, d, l)) if l: - size += write_uint(stream, self.layer) + size += write_uint(stream, self.layer) # type: ignore if d: - size += write_uint(stream, self.datatype) + size += write_uint(stream, self.datatype) # type: ignore if w: - size += write_uint(stream, self.half_width) + size += write_uint(stream, self.half_width) # type: ignore if e: scheme = 0 if self.extension_start is not None: @@ -1859,57 +2000,62 @@ class Path(Record): scheme += self.extension_end[0].value size += write_uint(stream, scheme) if scheme & 0b1100 == 0b1100: - size += write_sint(stream, self.extension_start[1]) + size += write_sint(stream, self.extension_start[1]) # type: ignore if scheme & 0b0011 == 0b0011: - size += write_sint(stream, self.extension_end[1]) + size += write_sint(stream, self.extension_end[1]) # type: ignore if p: - size += write_point_list(stream, self.point_list, implicit_closed=False, fast=fast) + size += write_point_list(stream, self.point_list, # type: ignore + implicit_closed=False, fast=fast) if x: - size += write_sint(stream, self.x) + size += write_sint(stream, self.x) # type: ignore if y: - size += write_sint(stream, self.y) + size += write_sint(stream, self.y) # type: ignore if r: - size += self.repetition.write(stream) + size += self.repetition.write(stream) # type: ignore return size -class Trapezoid(Record): +class Trapezoid(Record, GeometryMixin): """ Trapezoid record (ID 23, 24, 25) - Properties: - .delta_a (int or None, - 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 or None, - 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 (int or None, Bounding box x-width, None means reuse modal) - .height (int or None, Bounding box y-height, None means reuse modal) - .layer (int or None, None means reuse modal) - .datatype (int or None, None means reuse modal) - .x (int or None, None means se modal) - .y (int or None, None means se modal) - .repetition (reptetition or None) + 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 = None # type: int or None - datatype = None # type: int or None - width = None # type: int or None - height = None # type: int or None - x = None # type: int or None - y = None # type: int or None - repetition = None # type: repetition_t or None - delta_a = None # type: int - delta_b = None # type: int - is_vertical = None # type: bool + 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 + delta_b: int = 0 + is_vertical: bool + properties: List['Property'] def __init__(self, is_vertical: bool, @@ -1921,25 +2067,11 @@ class Trapezoid(Record): height: int = None, x: int = None, y: int = None, - repetition: repetition_t = None): + repetition: repetition_t = None, + properties: Optional[List['Property']] = None): """ - :param is_vertical: True if both the left and right sides are aligned - to the y-axis. If the trapezoid is a rectangle, either value - is permitted. - :param delta_a: 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. Default None (reuse modal). - :param delta_b: 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. Default None (reuse modal). - :param layer: Layer number. Default None (reuse modal). - :param datatype: Datatype number. Default None (reuse modal). - :param width: X-width of bounding box. Default None (reuse modal). - :param height: Y-height of bounding box. Default None (reuse modal) - :param x: X-offset. Default None (use modal). - :param y: Y-offset. Default None (use modal). - :param repetition: Repetition. Default None (no repetition). - :raises: InvalidDataError if dimensions are impossible. + Raises: + InvalidDataError: if dimensions are impossible. """ self.is_vertical = is_vertical self.delta_a = delta_a @@ -1951,15 +2083,31 @@ class Trapezoid(Record): self.x = x self.y = y self.repetition = repetition + self.properties = [] if properties is None else properties if self.is_vertical: if height is not None and delta_b - delta_a > height: raise InvalidDataError('Trapezoid: h < delta_b - delta_a' - ' ({} < {} - {})'.format(height, 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)) + + ' ({} < {} - {})'.format(width, delta_b, delta_a)) + + def get_is_vertical(self) -> bool: + return verify_modal(self.is_vertical) + + def get_delta_a(self) -> int: + return verify_modal(self.delta_a) + + def get_delta_b(self) -> int: + return verify_modal(self.delta_b) + + def get_width(self) -> int: + return verify_modal(self.width) + + def get_height(self) -> int: + return verify_modal(self.height) def merge_with_modals(self, modals: Modals): adjust_coordinates(self, modals, 'geometry_x', 'geometry_y') @@ -1984,7 +2132,7 @@ class Trapezoid(Record): '{}'.format(record_id)) is_vertical, w, h, x, y, r, d, l = read_bool_byte(stream) - optional = {} + optional: Dict[str, Any] = {} if l: optional['layer'] = read_uint(stream) if d: @@ -2026,49 +2174,90 @@ class Trapezoid(Record): size = write_uint(stream, record_id) size += write_bool_byte(stream, (v, w, h, x, y, r, d, l)) if l: - size += write_uint(stream, self.layer) + size += write_uint(stream, self.layer) # type: ignore if d: - size += write_uint(stream, self.datatype) + size += write_uint(stream, self.datatype) # type: ignore if w: - size += write_uint(stream, self.width) + size += write_uint(stream, self.width) # type: ignore if h: - size += write_uint(stream, self.height) + size += write_uint(stream, self.height) # type: ignore if record_id != 25: - size += write_sint(stream, self.delta_a) + size += write_sint(stream, self.delta_a) # type: ignore if record_id != 24: - size += write_sint(stream, self.delta_b) + size += write_sint(stream, self.delta_b) # type: ignore if x: - size += write_sint(stream, self.x) + size += write_sint(stream, self.x) # type: ignore if y: - size += write_sint(stream, self.y) + size += write_sint(stream, self.y) # type: ignore if r: - size += self.repetition.write(stream) + size += self.repetition.write(stream) # type: ignore return size -# TODO: CTrapezoid type descriptions -class CTrapezoid(Record): - """ +class CTrapezoid(Record, GeometryMixin): + r""" CTrapezoid record (ID 26) - Properties: - .ctrapezoid_type (int or None, see OASIS spec for details, None means reuse modal) - .width (int or None, Bounding box x-width, None means reuse modal) - .height (int or None, Bounding box y-height, None means reuse modal) - .layer (int or None, None means reuse modal) - .datatype (int or None, None means reuse modal) - .x (int or None, None means se modal) - .y (int or None, None means se modal) - .repetition (reptetition or None) + Compact trapezoid formats. + Two sides are assumed to be parallel to the x- or y-axis, and the remaining + sides form 45 or 90 degree angles with them. + + `ctrapezoid_type` is in `range(0, 26)`, with the following shapes: + ____ ____ _____ ______ + | 0 \ / 2 | / 4 \ / 6 / + |_____\ /_____| /_______\ /_____/ + ______ ______ _________ ______ + | 1 / \ 3 | \ 5 / \ 7 \ + |____/ \____| \_____/ \_____\ + w >= h w >= 2h + + ___ ___ |\ /| /| |\ + |\ /| | | | | | \ / | / | | \ + | \ / | |10 | | 11| |12| |13| |14| |15| + | \ / | | / \ | | | | | | | | | + | 8 | | 9 | | / \ | | / \ | | / \ | + |___| |___| |/ \| |/ \| |/ \| + h >= w h >= w h >= 2w h >= 2w + + __________ + |\ /| /\ |\ /| | 24 | (rect) + | \ / | / \ | \ / | |__________| + |16\ /18| / 20 \ |22\ /23| + |___\ /___| /______\ | / \ | + ____ ____ ______ | / \ | + | / \ | \ / |/ \| _____ + |17/ \19| \ 21 / h = 2w | | (sqr) + | / \ | \ / set h = None | 25 | + |/ \| \/ |_____| + w = h w = 2h w = h + set h = None set w = None set h = None + + + 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 = None # type: int or None - layer = None # type: int or None - datatype = None # type: int or None - width = None # type: int or None - height = None # type: int or None - x = None # type: int or None - y = None # type: int or None - repetition = None # type: repetition_t or None + 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'] def __init__(self, ctrapezoid_type: int = None, @@ -2078,18 +2267,11 @@ class CTrapezoid(Record): height: int = None, x: int = None, y: int = None, - repetition: repetition_t = None): + repetition: repetition_t = None, + properties: Optional[List['Property']] = None): """ - :param ctrapezoid_type: CTrapezoid type; see OASIS format - documentation. Default None (reuse modal). - :param layer: Layer number. Default None (reuse modal). - :param datatype: Datatype number. Default None (reuse modal). - :param width: X-width of bounding box. Default None (reuse modal). - :param height: Y-height of bounding box. Default None (reuse modal) - :param x: X-offset. Default None (use modal). - :param y: Y-offset. Default None (use modal). - :param repetition: Repetition. Default None (no repetition). - :raises: InvalidDataError if dimensions are invalid. + Raises: + InvalidDataError: if dimensions are invalid. """ self.ctrapezoid_type = ctrapezoid_type self.layer = layer @@ -2099,28 +2281,26 @@ class CTrapezoid(Record): self.x = x self.y = y self.repetition = repetition + self.properties = [] if properties is None else properties - if ctrapezoid_type in (20, 21) and width is not None: - 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('CTrapezoid has spurious height entry: ' - '{}'.format(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('CTrapezoid has width < 2*height' - ' ({} < 2 * {})'.format(width, height)) - if ctrapezoid_type in range(8, 12) and width > height: - raise InvalidDataError('CTrapezoid has width > height' - ' ({} > {})'.format(width, height)) - if ctrapezoid_type in range(12, 16) and 2 * width > 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): - raise InvalidDataError('CTrapezoid has invalid type: ' - '{}'.format(ctrapezoid_type)) + self.check_valid() + + def get_ctrapezoid_type(self) -> int: + return verify_modal(self.ctrapezoid_type) + + def get_height(self) -> int: + if self.ctrapezoid_type is None: + return verify_modal(self.height) + if self.ctrapezoid_type in (16, 17, 18, 19, 22, 23, 25): + return verify_modal(self.width) + return verify_modal(self.height) + + def get_width(self) -> int: + if self.ctrapezoid_type is None: + return verify_modal(self.width) + if self.ctrapezoid_type in (20, 21): + return verify_modal(self.height) + return verify_modal(self.width) def merge_with_modals(self, modals: Modals): adjust_coordinates(self, modals, 'geometry_x', 'geometry_y') @@ -2143,6 +2323,8 @@ class CTrapezoid(Record): else: adjust_field(self, 'height', modals, 'geometry_h') + self.check_valid() + def deduplicate_with_modals(self, modals: Modals): dedup_coordinates(self, modals, 'geometry_x', 'geometry_y') dedup_repetition(self, modals) @@ -2166,6 +2348,8 @@ class CTrapezoid(Record): else: dedup_field(self, 'height', modals, 'geometry_h') + self.check_valid() + @staticmethod def read(stream: io.BufferedIOBase, record_id: int) -> 'CTrapezoid': if record_id != 26: @@ -2173,7 +2357,7 @@ class CTrapezoid(Record): '{}'.format(record_id)) t, w, h, x, y, r, d, l = read_bool_byte(stream) - optional = {} + optional: Dict[str, Any] = {} if l: optional['layer'] = read_uint(stream) if d: @@ -2207,42 +2391,74 @@ class CTrapezoid(Record): size = write_uint(stream, 26) size += write_bool_byte(stream, (t, w, h, x, y, r, d, l)) if l: - size += write_uint(stream, self.layer) + size += write_uint(stream, self.layer) # type: ignore if d: - size += write_uint(stream, self.datatype) + size += write_uint(stream, self.datatype) # type: ignore if t: - size += write_uint(stream, self.ctrapezoid_type) + size += write_uint(stream, self.ctrapezoid_type) # type: ignore if w: - size += write_uint(stream, self.width) + size += write_uint(stream, self.width) # type: ignore if h: - size += write_uint(stream, self.height) + size += write_uint(stream, self.height) # type: ignore if x: - size += write_sint(stream, self.x) + size += write_sint(stream, self.x) # type: ignore if y: - size += write_sint(stream, self.y) + size += write_sint(stream, self.y) # type: ignore if r: - size += self.repetition.write(stream) + size += self.repetition.write(stream) # type: ignore return size + def check_valid(self): + ctrapezoid_type = self.ctrapezoid_type + width = self.width + height = self.height -class Circle(Record): + if ctrapezoid_type in (20, 21) and width is not None: + 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('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: + raise InvalidDataError('CTrapezoid has width < height' + ' ({} < {})'.format(width, height)) + if ctrapezoid_type in range(4, 8) and 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('CTrapezoid has width > height' + ' ({} > {})'.format(width, height)) + if ctrapezoid_type in range(12, 16) and 2 * width > 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): + raise InvalidDataError('CTrapezoid has invalid type: ' + '{}'.format(ctrapezoid_type)) + + +class Circle(Record, GeometryMixin): """ Circle record (ID 27) - Properties: - .radius (int or None, None means reuse modal) - .layer (int or None, None means reuse modal) - .datatype (int or None, None means reuse modal) - .x (int or None, None means se modal) - .y (int or None, None means se modal) - .repetition (reptetition or 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 = None # type: int or None - datatype = None # type: int or None - x = None # type: int or None - y = None # type: int or None - repetition = None # type: repetition_t or None - radius = None # type: int or None + 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, @@ -2250,15 +2466,20 @@ class Circle(Record): datatype: int = None, x: int = None, y: int = None, - repetition: repetition_t = None): + repetition: repetition_t = None, + properties: Optional[List['Property']] = None): """ - :param radius: Radius. Default None (reuse modal). - :param layer: Layer number. Default None (reuse modal). - :param datatype: Datatype number. Default None (reuse modal). - :param x: X-offset. Default None (use modal). - :param y: Y-offset. Default None (use modal). - :param repetition: Repetition. Default None (no repetition). - :raises: InvalidDataError if dimensions are invalid. + Args: + radius: Radius. Default `None` (reuse modal). + layer: Layer number. Default `None` (reuse modal). + datatype: Datatype number. Default `None` (reuse modal). + x: X-offset. Default `None` (use modal). + y: Y-offset. Default `None` (use modal). + repetition: Repetition. Default `None` (no repetition). + properties: List of property records associated with this record. + + Raises: + InvalidDataError: if dimensions are invalid. """ self.radius = radius self.layer = layer @@ -2266,6 +2487,10 @@ class Circle(Record): self.x = x self.y = y self.repetition = repetition + self.properties = [] if properties is None else properties + + def get_radius(self) -> int: + return verify_modal(self.radius) def merge_with_modals(self, modals: Modals): adjust_coordinates(self, modals, 'geometry_x', 'geometry_y') @@ -2291,7 +2516,7 @@ class Circle(Record): if z0 or z1: raise InvalidDataError('Malformed circle header') - optional = {} + optional: Dict[str, Any] = {} if l: optional['layer'] = read_uint(stream) if d: @@ -2319,28 +2544,31 @@ class Circle(Record): size = write_uint(stream, 27) size += write_bool_byte(stream, (0, 0, s, x, y, r, d, l)) if l: - size += write_uint(stream, self.layer) + size += write_uint(stream, self.layer) # type: ignore if d: - size += write_uint(stream, self.datatype) + size += write_uint(stream, self.datatype) # type: ignore if s: - size += write_uint(stream, self.radius) + size += write_uint(stream, self.radius) # type: ignore if x: - size += write_sint(stream, self.x) + size += write_sint(stream, self.x) # type: ignore if y: - size += write_sint(stream, self.y) + size += write_sint(stream, self.y) # type: ignore if r: - size += self.repetition.write(stream) + size += self.repetition.write(stream) # type: ignore return size -def adjust_repetition(record: Record, modals: Modals): +def adjust_repetition(record, modals: Modals): """ Merge the record's repetition entry with the one in the modals - :param record: Record to read or modify. - :param modals: Modals to read or modify. - :raises: InvalidDataError if a ReuseRepetition can't be filled - from the modals. + Args: + record: Record to read or modify. + modals: Modals to read or modify. + + Raises: + InvalidDataError: if a `ReuseRepetition` can't be filled + from the modals. """ if record.repetition is not None: if isinstance(record.repetition, ReuseRepetition): @@ -2352,15 +2580,18 @@ def adjust_repetition(record: Record, modals: Modals): modals.repetition = copy.copy(record.repetition) -def adjust_field(record: Record, r_field: str, modals: Modals, m_field: str): +def adjust_field(record, r_field: str, modals: Modals, m_field: str): """ - Merge record.r_field with modals.m_field + Merge `record.r_field` with `modals.m_field` - :param record: Record to read or modify. - :param r_field: Attr of record to access. - :param modals: Modals to read or modify. - :param m_field: Attr of modals to access. - :raises: InvalidDataError if a both fields are None + Args: + record: `Record` to read or modify. + r_field: Attr of record to access. + modals: `Modals` to read or modify. + m_field: Attr of modals to access. + + Raises: + InvalidDataError: if both fields are `None` """ r = getattr(record, r_field) if r is not None: @@ -2373,48 +2604,52 @@ def adjust_field(record: Record, r_field: str, modals: Modals, m_field: str): raise InvalidDataError('Unfillable field: {}'.format(m_field)) -def adjust_coordinates(record: Record, modals: Modals, mx_field: str, my_field: str): +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. + Merge `record.x` and `record.y` with `modals.mx_field` and `modals.my_field`, + taking into account the value of `modals.xy_relative`. - If modals.xy_relative is True and the record has non-None coordinates, - the modal values are added to the record's coordinates. If modals.xy_relative - is False, the coordinates are treated the same way as other fields. + If `modals.xy_relative` is `True` and the record has non-`None` coordinates, + the modal values are added to the record's coordinates. If `modals.xy_relative` + is `False`, the coordinates are treated the same way as other fields. - :param record: Record to read or modify. - :param modals: Modals to read or modify. - :param mx_field: Attr of modals corresponding to record.x - :param my_field: Attr of modals corresponding to record.y - :raises: InvalidDataError if a both fields are None + Args: + record: `Record` to read or modify. + modals: `Modals` to read or modify. + mx_field: Attr of modals corresponding to `record.x` + my_field: Attr of modals corresponding to `record.y` + + Raises: + InvalidDataError: if both fields are `None` """ if record.x is not None: if modals.xy_relative: record.x += getattr(modals, mx_field) - else: - setattr(modals, mx_field, record.x) + setattr(modals, mx_field, record.x) else: record.x = getattr(modals, mx_field) if record.y is not None: if modals.xy_relative: record.y += getattr(modals, my_field) - else: - setattr(modals, my_field, record.y) + setattr(modals, my_field, record.y) else: record.y = getattr(modals, my_field) # TODO: Clarify the docs on the dedup_* functions -def dedup_repetition(record: Record, modals: Modals): +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. - :param record: Record to read or modify. - :param modals: Modals to read or modify. - :raises: InvalidDataError if a ReuseRepetition can't be filled - from the modals. + Args: + record: `Record` to read or modify. + modals: `Modals` to read or modify. + + Raises: + InvalidDataError: if a `ReuseRepetition` can't be filled + from the modals. """ if record.repetition is None: return @@ -2430,21 +2665,32 @@ def dedup_repetition(record: Record, modals: Modals): modals.repetition = record.repetition -def dedup_field(record: Record, r_field: str, modals: Modals, m_field: str): +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. + Deduplicate `record.r_field` using `modals.m_field` + Update the `modals.m_field` if they are different. - :param record: Record to read or modify. - :param r_field: Attr of record to access. - :param modals: Modals to read or modify. - :param m_field: Attr of modals to access. - :raises: InvalidDataError if a both fields are None + Args: + record: `Record` to read or modify. + r_field: Attr of record to access. + modals: `Modals` to read or modify. + m_field: Attr of modals to access. + + Args: + InvalidDataError: if both fields are `None` """ r = getattr(record, r_field) m = getattr(modals, m_field) if r is not None: - if m is not None and m == r: + if m_field in ('polygon_point_list', 'path_point_list'): + if _USE_NUMPY: + equal = numpy.array_equal(m, r) + else: + equal = (m is not None) and all(tuple(mm) == tuple(rr) for mm, rr in zip(m, r)) + else: + equal = (m is not None) and m == r + + if equal: setattr(record, r_field, None) else: setattr(modals, m_field, r) @@ -2452,25 +2698,29 @@ def dedup_field(record: Record, r_field: str, modals: Modals, m_field: str): raise InvalidDataError('Unfillable field') -def dedup_coordinates(record: Record, modals: Modals, mx_field: str, my_field: str): +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. + Deduplicate `record.x` and `record.y` using `modals.mx_field` and `modals.my_field`, + taking into account the value of `modals.xy_relative`. - If modals.xy_relative is True and the record has non-None coordinates, - the modal values are subtracted from the record's coordinates. If modals.xy_relative - is False, the coordinates are treated the same way as other fields. + If `modals.xy_relative` is `True` and the record has non-`None` coordinates, + the modal values are subtracted from the record's coordinates. If `modals.xy_relative` + is `False`, the coordinates are treated the same way as other fields. - :param record: Record to read or modify. - :param modals: Modals to read or modify. - :param mx_field: Attr of modals corresponding to record.x - :param my_field: Attr of modals corresponding to record.y - :raises: InvalidDataError if a both fields are None + Args: + record: `Record` to read or modify. + modals: `Modals` to read or modify. + mx_field: Attr of modals corresponding to `record.x` + my_field: Attr of modals corresponding to `record.y` + + Raises: + InvalidDataError: if both fields are `None` """ if record.x is not None: mx = getattr(modals, mx_field) if modals.xy_relative: record.x -= mx + setattr(modals, mx_field, record.x) else: if record.x == mx: record.x = None @@ -2481,6 +2731,7 @@ def dedup_coordinates(record: Record, modals: Modals, mx_field: str, my_field: s my = getattr(modals, my_field) if modals.xy_relative: record.y -= my + setattr(modals, my_field, record.y) else: if record.y == my: record.y = None diff --git a/setup.py b/setup.py index a8ee066..9727e26 100644 --- a/setup.py +++ b/setup.py @@ -1,19 +1,44 @@ #!/usr/bin/env python3 from setuptools import setup, find_packages -import fatamorgana + with open('README.md', 'r') as f: long_description = f.read() +with open('fatamorgana/VERSION.py', 'rt') as f: + version = f.readlines()[2].strip() + setup(name='fatamorgana', - version=fatamorgana.version, + version=version, description='OASIS layout format parser and writer', long_description=long_description, long_description_content_type='text/markdown', author='Jan Petykiewicz', author_email='anewusername@gmail.com', url='https://mpxd.net/code/jan/fatamorgana', + packages=find_packages(), + package_data={ + 'fatamorgana': ['py.typed'], + }, + install_requires=[ + 'typing', + ], + extras_require={ + 'numpy': ['numpy'], + }, + classifiers=[ + 'Programming Language :: Python :: 3', + 'Development Status :: 3 - Alpha', + 'Environment :: Other Environment', + 'Intended Audience :: Developers', + 'Intended Audience :: Information Technology', + 'Intended Audience :: Manufacturing', + 'Intended Audience :: Science/Research', + 'License :: OSI Approved :: GNU Affero General Public License v3', + 'Topic :: Scientific/Engineering', + 'Topic :: Scientific/Engineering :: Electronic Design Automation (EDA)', + ], keywords=[ 'OASIS', 'layout', @@ -34,28 +59,5 @@ setup(name='fatamorgana', 'polygon', 'gds', ], - classifiers=[ - 'Programming Language :: Python', - 'Programming Language :: Python :: 3', - 'Development Status :: 3 - Alpha', - 'Environment :: Other Environment', - 'Intended Audience :: Developers', - 'Intended Audience :: Information Technology', - 'Intended Audience :: Manufacturing', - 'Intended Audience :: Science/Research', - 'License :: OSI Approved :: GNU Affero General Public License v3', - 'Operating System :: POSIX :: Linux', - 'Operating System :: Microsoft :: Windows', - 'Topic :: Scientific/Engineering', - 'Topic :: Scientific/Engineering :: Electronic Design Automation (EDA)', - 'Topic :: Software Development :: Libraries :: Python Modules', - ], - packages=find_packages(), - install_requires=[ - 'typing', - ], - extras_require={ - 'numpy': ['numpy'], - }, )