Add rgetInstanceMatching to utilities, as reminder and for later use.

This commit is contained in:
Jean-Paul Chaput 2021-06-02 14:16:45 +02:00
parent 1afc48fb4c
commit d2e1ab3796
1 changed files with 43 additions and 0 deletions

View File

@ -18,6 +18,7 @@ from Hurricane import Breakpoint
from Hurricane import Box from Hurricane import Box
from Hurricane import Vertical from Hurricane import Vertical
from Hurricane import RoutingPad from Hurricane import RoutingPad
from helpers import trace
def breakpoint ( editor, level, message ): def breakpoint ( editor, level, message ):
@ -96,3 +97,45 @@ def hpathToName ( path ):
if len(s): s += '_' if len(s): s += '_'
s += head.getName() s += head.getName()
return s return s
def getInstanceMatching ( cell, pattern, level=0 ):
"""
getInstanceMatching(): returns first instance with the word being searched.
"""
for instance in cell.getInstances():
name = instance.getName()
if pattern in name:
trace( 550, '\t{} {} match pattern "{}"\n'.format(' '*level,instance,pattern) )
return instance
return None
def rgetInstanceMatching ( cell, path, level=0 ):
"""
Get the instance designated by path (recursively). The path argument can be
either a string of instance names separated by dots or directly a list of
instances names.
It also "reconstructs" the actual full path name of the instances
found recursively.
"""
if isinstance(path,str):
path = path.split( '.' )
elif not isinstance(path, list):
raise ErrorMessage( 1, 'rgetInstanceMatching(): "path" argument is ' \
'neither a string or a list ({})."' \
.format(path) )
# Find instance at this level.
instance = getInstanceMatching( cell, path[0], level )
if instance is None:
raise ErrorMessage( 1, 'rgetInstanceMatching(): No instance "{}" in cell "{}".' \
.format(path[0], cell.getName()) )
iname = instance.getName()
# Last instance (leaf search), return it.
if len(path) == 1:
return instance, iname
# Chew down another level, another brick in the wall
rinstance, rname = rgetInstanceLike( instance.getMasterCell(), path[1:], level+1 )
# Accumulate the names recursively found "level0.level1.level2..."
return rinstance, '{}.{}'.format( iname, rname )