robust.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. """Module implementing error-catching version of send (sendRobust)"""
  2. from grass.pydispatch.dispatcher import Any, Anonymous, liveReceivers, getAllReceivers
  3. from grass.pydispatch.robustapply import robustApply
  4. def sendRobust(signal=Any, sender=Anonymous, *arguments, **named):
  5. """Send signal from sender to all connected receivers catching errors
  6. signal -- (hashable) signal value, see connect for details
  7. sender -- the sender of the signal
  8. if Any, only receivers registered for Any will receive
  9. the message.
  10. if Anonymous, only receivers registered to receive
  11. messages from Anonymous or Any will receive the message
  12. Otherwise can be any python object (normally one
  13. registered with a connect if you actually want
  14. something to occur).
  15. arguments -- positional arguments which will be passed to
  16. *all* receivers. Note that this may raise TypeErrors
  17. if the receivers do not allow the particular arguments.
  18. Note also that arguments are applied before named
  19. arguments, so they should be used with care.
  20. named -- named arguments which will be filtered according
  21. to the parameters of the receivers to only provide those
  22. acceptable to the receiver.
  23. Return a list of tuple pairs [(receiver, response), ... ]
  24. if any receiver raises an error (specifically any subclass of Exception),
  25. the error instance is returned as the result for that receiver.
  26. """
  27. # Call each receiver with whatever arguments it can accept.
  28. # Return a list of tuple pairs [(receiver, response), ... ].
  29. responses = []
  30. for receiver in liveReceivers(getAllReceivers(sender, signal)):
  31. try:
  32. response = robustApply(
  33. receiver, signal=signal, sender=sender, *arguments, **named
  34. )
  35. except Exception as err:
  36. responses.append((receiver, err))
  37. else:
  38. responses.append((receiver, response))
  39. return responses