Move Abstract into its own file

This commit is contained in:
Jan Petykiewicz 2023-01-23 22:42:31 -08:00 committed by jan
parent a1073eca6b
commit 090e86644a
4 changed files with 73 additions and 47 deletions

View File

@ -1,3 +1,4 @@
from .builder import Builder, Abstract from .builder import Builder
from .abstract import Abstract
from .utils import ell from .utils import ell
from .tools import Tool from .tools import Tool

View File

@ -0,0 +1,59 @@
from typing import Dict, Union, Optional
from typing import MutableMapping, TYPE_CHECKING
import copy
import logging
from ..pattern import Pattern
from ..library import MutableLibrary
from ..ports import PortList, Port
from .tools import Tool
if TYPE_CHECKING:
from .builder import Builder
logger = logging.getLogger(__name__)
class Abstract(PortList):
__slots__ = ('name', 'ports')
name: str
""" Name of the pattern this device references """
ports: Dict[str, Port]
""" Uniquely-named ports which can be used to snap instances together"""
def __init__(
self,
name: str,
ports: Dict[str, Port],
) -> None:
self.name = name
self.ports = copy.deepcopy(ports)
def build(
self,
library: MutableLibrary,
tools: Union[None, Tool, MutableMapping[Optional[str], Tool]] = None,
) -> 'Builder':
"""
Begin building a new device around an instance of the current device
(rather than modifying the current device).
Returns:
The new `Builder` object.
"""
pat = Pattern(ports=self.ports)
pat.ref(self.name)
new = Builder(library=library, pattern=pat, tools=tools) # TODO should Ref have tools?
return new
# TODO do we want to store a Ref instead of just a name? then we can translate/rotate/mirror...
def __repr__(self) -> str:
s = f'<Abstract {self.name} ['
for name, port in self.ports.items():
s += f'\n\t{name}: {port}'
s += ']>'
return s

View File

@ -14,6 +14,7 @@ from ..error import PortError, BuildError
from ..ports import PortList, Port from ..ports import PortList, Port
from .tools import Tool from .tools import Tool
from .utils import ell from .utils import ell
from .abstract import Abstract
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -22,50 +23,6 @@ logger = logging.getLogger(__name__)
BB = TypeVar('BB', bound='Builder') BB = TypeVar('BB', bound='Builder')
class Abstract(PortList):
__slots__ = ('name', 'ports')
name: str
""" Name of the pattern this device references """
ports: Dict[str, Port]
""" Uniquely-named ports which can be used to snap instances together"""
def __init__(
self,
name: str,
ports: Dict[str, Port],
) -> None:
self.name = name
self.ports = copy.deepcopy(ports)
def build(
self,
library: MutableLibrary,
tools: Union[None, Tool, MutableMapping[Optional[str], Tool]] = None,
) -> 'Builder':
"""
Begin building a new device around an instance of the current device
(rather than modifying the current device).
Returns:
The new `Builder` object.
"""
pat = Pattern(ports=self.ports)
pat.ref(self.name)
new = Builder(library=library, pattern=pat, tools=tools) # TODO should Ref have tools?
return new
# TODO do we want to store a Ref instead of just a name? then we can translate/rotate/mirror...
def __repr__(self) -> str:
s = f'<Abstract {self.name} ['
for name, port in self.ports.items():
s += f'\n\t{name}: {port}'
s += ']>'
return s
class Builder(PortList): class Builder(PortList):
""" """
TODO DOCUMENT Builder TODO DOCUMENT Builder

View File

@ -19,10 +19,10 @@ from .error import LibraryError, PatternError
from .utils import rotation_matrix_2d, normalize_mirror from .utils import rotation_matrix_2d, normalize_mirror
from .shapes import Shape, Polygon from .shapes import Shape, Polygon
from .label import Label from .label import Label
from .builder.abstract import Abstract
if TYPE_CHECKING: if TYPE_CHECKING:
from .pattern import Pattern from .pattern import Pattern
from .builder import Abstract
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -42,7 +42,16 @@ class Library(Mapping[str, Pattern], metaclass=ABCMeta):
#__contains__, keys, items, values, get, __eq__, __ne__ supplied by Mapping #__contains__, keys, items, values, get, __eq__, __ne__ supplied by Mapping
def abstract(self, name: str) -> 'Abstract': def abstract(self, name: str) -> Abstract:
"""
Return an `Abstract` (name & ports) for the pattern in question.
Args:
name: The pattern name
Returns:
An `Abstract` object for the pattern
"""
return Abstract(name=name, ports=self[name].ports) return Abstract(name=name, ports=self[name].ports)
def __repr__(self) -> str: def __repr__(self) -> str: