""" Types representing entities of GNU PCB files. """ import collections class Struct: """ Base class for struct-like Python objects """ _attrs = collections.OrderedDict() # attribute name: str -> default value def __init__(self, **kwargs): for name, def_val in self._attrs.items(): setattr(self, name, def_val) for name, val in kwargs.items(): if name not in self._attrs: raise ValueError( f'{type(self).__name__:s} does not have attribute {name:s}' ) setattr(self, name, val) def __repr__(self) -> str: vals = ', '.join([ f'{name:s}={repr(getattr(self, name)):s}' for name in self._attrs.keys() ]) return f'{type(self).__name__:s}({vals:s})' def _compare(self, other) -> int: if self._attrs != other._attrs: raise ValueError(f'{type(self).__name__:s} cannot be' f' compared to {type(other).__name__:s}') for name in self._attrs: s = getattr(self, name) o = getattr(other, name) if s < o: return -1 if s > o: return 1 return 0 def __eq__(self, other) -> bool: return self._compare(other) == 0 def __ne__(self, other) -> bool: return self._compare(other) != 0 def __lt__(self, other) -> bool: return self._compare(other) < 0 def __le__(self, other) -> bool: return self._compare(other) <= 0 def __gt__(self, other) -> bool: return self._compare(other) > 0 def __ge__(self, other) -> bool: return self._compare(other) >= 0 class Element(Struct): """ Element entity in a GNU PCB file. """ _attrs = collections.OrderedDict([('s_flags', []), ('desc', ''), ('name', ''), ('value', ''), ('m_x', 0.0), ('m_y', 0.0), ('t_x', 0.0), ('t_y', 0.0), ('t_dir', 0), ('t_scale', 0.0), ('t_s_flags', []), ('body', [])]) def __init__(self, **kwargs): super().__init__(**kwargs) class ElementArc(Struct): """ ElementArc entity in a GNU PCB file. """ _attrs = collections.OrderedDict([('r_x', 0.0), ('r_y', 0.0), ('width', 0.0), ('height', 0.0), ('start_angle', 0.0), ('end_angle', 0.0), ('thickness', 0.0)]) def __init__(self, **kwargs): super().__init__(**kwargs) class ElementLine(Struct): """ ElementLine entity in a GNU PCB file. """ _attrs = collections.OrderedDict([('r_x1', 0.0), ('r_y1', 0.0), ('r_x2', 0.0), ('r_y2', 0.0), ('thickness', 0.0)]) def __init__(self, **kwargs): super().__init__(**kwargs) class Pad(Struct): """ Pad entity in a GNU PCB file. """ _attrs = collections.OrderedDict([ ('r_x1', 0.0), ('r_y1', 0.0), ('r_x2', 0.0), ('r_y2', 0.0), ('thickness', 0.0), ('clearance', 0.0), ('mask', 0.0), ('name', ''), ('number', ''), ('s_flags', []), ]) def __init__(self, **kwargs): super().__init__(**kwargs) class Pin(Struct): """ Pin entity in a GNU PCB file. """ _attrs = collections.OrderedDict([ ('r_x', 0.0), ('r_y', 0.0), ('thickness', 0.0), ('clearance', 0.0), ('mask', 0.0), ('drill', 0.0), ('name', ''), ('number', ''), ('s_flags', []), ]) def __init__(self, **kwargs): super().__init__(**kwargs)