hooks.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. from __future__ import with_statement
  4. import threading
  5. _LOCK = threading.RLock()
  6. _HOOKS = {}
  7. def install_hook(name, fct):
  8. """
  9. Install one of the following hook
  10. modbus_rtu.RtuMaster.before_open((master,))
  11. modbus_rtu.RtuMaster.after_close((master,)
  12. modbus_rtu.RtuMaster.before_send((master, request)) returns modified request or None
  13. modbus_rtu.RtuMaster.after_recv((master, response)) returns modified response or None
  14. modbus_rtu.RtuServer.before_close((server, ))
  15. modbus_rtu.RtuServer.after_close((server, ))
  16. modbus_rtu.RtuServer.before_open((server, ))
  17. modbus_rtu.RtuServer.after_open(((server, ))
  18. modbus_rtu.RtuServer.after_read((server, request)) returns modified request or None
  19. modbus_rtu.RtuServer.before_write((server, response)) returns modified response or None
  20. modbus_rtu.RtuServer.after_write((server, response))
  21. modbus_rtu.RtuServer.on_error((server, excpt))
  22. modbus_tcp.TcpMaster.before_connect((master, ))
  23. modbus_tcp.TcpMaster.after_connect((master, ))
  24. modbus_tcp.TcpMaster.before_close((master, ))
  25. modbus_tcp.TcpMaster.after_close((master, ))
  26. modbus_tcp.TcpMaster.before_send((master, request))
  27. modbus_tcp.TcpServer.after_send((master, request))
  28. modbus_tcp.TcpMaster.after_recv((master, response))
  29. modbus_tcp.TcpServer.on_connect((server, client, address))
  30. modbus_tcp.TcpServer.on_disconnect((server, sock))
  31. modbus_tcp.TcpServer.after_recv((server, sock, request)) returns modified request or None
  32. modbus_tcp.TcpServer.before_send((server, sock, response)) returns modified response or None
  33. modbus_tcp.TcpServer.on_error((server, sock, excpt))
  34. modbus_rtu_over_tcp.RtuOverTcpMaster.after_recv((master, response))
  35. modbus.Master.before_send((master, request)) returns modified request or None
  36. modbus.Master.after_send((master))
  37. modbus.Master.after_recv((master, response)) returns modified response or None
  38. modbus.Slave.handle_request((slave, request_pdu)) returns modified response or None
  39. modbus.Slave.handle_write_multiple_coils_request((slave, request_pdu))
  40. modbus.Slave.handle_write_multiple_registers_request((slave, request_pdu)) returns modified response or None
  41. modbus.Slave.handle_write_single_register_request((slave, request_pdu)) returns modified response or None
  42. modbus.Slave.handle_write_single_coil_request((slave, request_pdu)) returns modified response or None
  43. modbus.Slave.handle_read_input_registers_request((slave, request_pdu)) returns modified response or None
  44. modbus.Slave.handle_read_holding_registers_request((slave, request_pdu)) returns modified response or None
  45. modbus.Slave.handle_read_discrete_inputs_request((slave, request_pdu)) returns modified response or None
  46. modbus.Slave.handle_read_coils_request((slave, request_pdu)) returns modified response or None
  47. modbus.Slave.handle_read_write_multiple_registers_request((slave, request_pdu)) returns modified response or None
  48. modbus.Slave.handle_read_exception_status_request((slave, request_pdu)) returns modified response or None
  49. modbus.Slave.on_handle_broadcast((slave, response_pdu)) returns modified response or None
  50. modbus.Slave.on_exception((slave, function_code, excpt))
  51. modbus.Databank.on_error((db, excpt, request_pdu))
  52. modbus.ModbusBlock.setitem((self, slice, value))
  53. modbus.Server.before_handle_request((server, request)) returns modified request or None
  54. modbus.Server.after_handle_request((server, response)) returns modified response or None
  55. modbus.Server.on_exception((server, excpt))
  56. """
  57. with _LOCK:
  58. try:
  59. _HOOKS[name].append(fct)
  60. except KeyError:
  61. _HOOKS[name] = [fct]
  62. def uninstall_hook(name, fct=None):
  63. """remove the function from the hooks"""
  64. with _LOCK:
  65. if fct:
  66. _HOOKS[name].remove(fct)
  67. else:
  68. del _HOOKS[name][:]
  69. def call_hooks(name, args):
  70. """call the function associated with the hook and pass the given args"""
  71. with _LOCK:
  72. try:
  73. for fct in _HOOKS[name]:
  74. retval = fct(args)
  75. if retval is not None:
  76. return retval
  77. except KeyError:
  78. pass
  79. return None