Skip to content

Commit

Permalink
Fixed __getattr__ and __dir__ on Manipulator (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Koval committed Apr 5, 2015
1 parent e2f0839 commit 4de23f6
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/prpy/base/manipulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,46 @@ def CloneBindings(self, parent):
pass

def __dir__(self):
robot = self.GetRobot()

# We have to manually perform a lookup in InstanceDeduplicator because
# __methods__ bypass __getattribute__.
self = bind.InstanceDeduplicator.get_canonical(self)

# Add planning methods to the tab-completion list.
method_names = set(self.__dict__.keys())
method_names.update(self.GetRobot().planner.get_planning_method_names())
method_names.update(self.GetRobot().actions.get_actions())

if hasattr(robot, 'planner') and robot.planner is not None:
method_names.update(robot.planner.get_planning_method_names())
if hasattr(robot, 'actions') and robot.actions is not None:
method_names.update(robot.actions.get_actions())

return list(method_names)

def __getattr__(self, name):
# We have to manually perform a lookup in InstanceDeduplicator because
# __methods__ bypass __getattribute__.
self = bind.InstanceDeduplicator.get_canonical(self)
robot = self.GetRobot()

# Resolve planner calls through the robot.planner field.
# FIXME: We need to replicate the _PlanWrapper functionality here.
if self.GetRobot().planner.has_planning_method(name):
delegate_method = getattr(self.GetRobot().planner, name)
if (hasattr(robot, 'planner') and robot.planner is not None
and robot.planner.has_planning_method(name)):

delegate_method = getattr(robot.planner, name)
@functools.wraps(delegate_method)
def wrapper_method(*args, **kw_args):
return self._PlanWrapper(delegate_method, args, kw_args)
def wrapper_method(*args, **kwargs):
return self._PlanWrapper(delegate_method, args, kwargs)
return wrapper_method
elif self.GetRobot().actions.has_action(name):
delegate_method = self.GetRobot().actions.get_action(name)

elif (hasattr(robot, 'actions') and robot.actions is not None
and robot.actions.has_action(name)):

delegate_method = robot.actions.get_action(name)
@functools.wraps(delegate_method)
def wrapper_method(obj, *args, **kw_args):
return delegate_method(self.GetRobot(), obj,
manip = self,
*args, **kw_args)
def wrapper_method(obj, *args, **kwargs):
return delegate_method(robot, obj, manip=self, *args, **kwargs)
return wrapper_method

raise AttributeError('{0:s} is missing method "{1:s}".'.format(repr(self), name))
Expand Down

0 comments on commit 4de23f6

Please sign in to comment.