Skip to content

Commit d1ff1ba

Browse files
committed
Adding ability to define tsrs that are not object specific.
1 parent e92e32a commit d1ff1ba

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/prpy/tsr/tsrlibrary.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def wrapped_func(robot, kinbody, *args, **kw_args):
5454

5555
class TSRLibrary(object):
5656
all_factories = collections.defaultdict(lambda: collections.defaultdict(dict))
57+
generic_kinbody_key = "_*" # Something that is unlikely to be an actual kinbody name
5758

5859
def __init__(self, robot, robot_name=None):
5960
"""
@@ -83,12 +84,19 @@ def __call__(self, kinbody, action_name, *args, **kw_args):
8384
kinbody_name = self.get_object_type(kinbody)
8485
logger.debug('Inferred KinBody name "%s" for TSR.', kinbody_name)
8586

87+
f = None
8688
try:
8789
f = self.all_factories[self.robot_name][kinbody_name][action_name]
88-
except KeyError:
89-
raise KeyError('There is no TSR factory registered for action "{:s}"'
90-
' with robot "{:s}" and object "{:s}".'.format(
91-
action_name, self.robot_name, kinbody_name))
90+
except KeyError, ignored:
91+
pass
92+
93+
if f is None:
94+
try:
95+
f = self.all_factories[self.robot_name][self.generic_kinbody_key][action_name]
96+
except KeyError:
97+
raise KeyError('There is no TSR factory registered for action "{:s}"'
98+
' with robot "{:s}" and object "{:s}".'.format(
99+
action_name, self.robot_name, kinbody_name))
92100

93101
return f(self.robot, kinbody, *args, **kw_args)
94102

@@ -166,6 +174,9 @@ def add_factory(cls, func, robot_name, object_name, action_name):
166174
logger.debug('Adding TSRLibrary factory for robot "%s", object "%s", action "%s".',
167175
robot_name, object_name, action_name)
168176

177+
if object_name is None:
178+
object_name = cls.generic_kinbody_key
179+
169180
if action_name in cls.all_factories[robot_name][object_name]:
170181
logger.warning('Overwriting duplicate TSR factory for action "%s"'
171182
' with robot "%s" and object "%s"',

0 commit comments

Comments
 (0)