handler.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import asyncio
  2. class handler:
  3. """
  4. That is class, which is used to implements new handlers. Handler is
  5. element, which directly store content to log. For example.
  6. To implement property handler, implement:
  7. * add(str) <- Add new content to log,
  8. * open() <- Optional function, create creatr handler for add,
  9. * clean() <- Optional function, clean up resources user by handler.
  10. """
  11. def __init__(self) -> None:
  12. """
  13. That prepare lock for the hndler.
  14. """
  15. self.__lock = asyncio.Lock()
  16. def __del__(self) -> None:
  17. """
  18. That clean up handler when item is removed.
  19. """
  20. self.clean()
  21. def open(self) -> None:
  22. """
  23. That register system resources for handler.
  24. """
  25. pass
  26. async def adding(self, content: str) -> None:
  27. """
  28. That add new content to the the log as new line. It do that
  29. asynchronically.
  30. Parameters
  31. ----------
  32. content : str
  33. Content which must be added to log.
  34. """
  35. async with self.__lock:
  36. await asyncio.to_thread(self.add, content)
  37. def add(self, content: str) -> None:
  38. """
  39. That add new content to the log as new line . It is virtual
  40. function, and must being overwritten.
  41. Parameters
  42. ----------
  43. content : str
  44. Content which must be added to log.
  45. """
  46. raise NotImplementedError()
  47. def clean(self) -> None:
  48. """
  49. That clean up resources used by handler.
  50. """
  51. pass