[Library.flatten] add dangling_ok param

This commit is contained in:
jan 2025-11-19 23:08:17 -08:00
parent 4dc81bd9f7
commit 5fbbaa0648
2 changed files with 13 additions and 5 deletions

View File

@ -378,7 +378,7 @@ class SimpleTool(Tool, metaclass=ABCMeta):
else:
straight_tree = straight_pat_or_tree
top = straight_tree.top()
straight_tree.flatten(top)
straight_tree.flatten(top, dangling_ok=True)
pat.plug(straight_tree[top], pmap, append=True)
if data.ccw is not None:
bend, bport_in, bport_out = self.bend
@ -653,7 +653,7 @@ class AutoTool(Tool, metaclass=ABCMeta):
else:
straight_tree = straight_pat_or_tree
top = straight_tree.top()
straight_tree.flatten(top)
straight_tree.flatten(top, dangling_ok=True)
pat.plug(straight_tree[top], pmap, append=True)
if data.b_transition:
pat.plug(data.b_transition.abstract, {port_names[1]: data.b_transition.our_port_name})
@ -801,7 +801,7 @@ class AutoTool(Tool, metaclass=ABCMeta):
else:
straight_tree = straight_pat_or_tree
top = straight_tree.top()
straight_tree.flatten(top)
straight_tree.flatten(top, dangling_ok=True)
pat.plug(straight_tree[top], pmap, append=True)
if data.b_transition:
pat.plug(data.b_transition.abstract, {port_names[1]: data.b_transition.our_port_name})
@ -813,7 +813,7 @@ class AutoTool(Tool, metaclass=ABCMeta):
else:
sbend_tree = sbend_pat_or_tree
top = sbend_tree.top()
sbend_tree.flatten(top)
sbend_tree.flatten(top, dangling_ok=True)
pat.plug(sbend_tree[top], pmap, append=True, mirrored=data.jog_remaining < 0)
if data.out_transition:
pat.plug(data.out_transition.abstract, {port_names[1]: data.out_transition.our_port_name})

View File

@ -264,6 +264,7 @@ class ILibraryView(Mapping[str, 'Pattern'], metaclass=ABCMeta):
self,
tops: str | Sequence[str],
flatten_ports: bool = False,
dangling_ok: bool = False,
) -> dict[str, 'Pattern']:
"""
Returns copies of all `tops` patterns with all refs
@ -276,6 +277,9 @@ class ILibraryView(Mapping[str, 'Pattern'], metaclass=ABCMeta):
tops: The pattern(s) to flattern.
flatten_ports: If `True`, keep ports from any referenced
patterns; otherwise discard them.
dangling_ok: If `True`, no error will be thrown if any
ref points to a name which is not present in the library.
Default False.
Returns:
{name: flat_pattern} mapping for all flattened patterns.
@ -292,6 +296,8 @@ class ILibraryView(Mapping[str, 'Pattern'], metaclass=ABCMeta):
for target in pat.refs:
if target is None:
continue
if dangling_ok and target not in self:
continue
if target not in flattened:
flatten_single(target)
@ -307,7 +313,9 @@ class ILibraryView(Mapping[str, 'Pattern'], metaclass=ABCMeta):
p.ports.clear()
pat.append(p)
pat.refs.clear()
for target in set(pat.refs.keys()) & set(self.keys()):
del pat.refs[target]
flattened[name] = pat
for top in tops: