pointers.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from typing import Any, Iterable, Optional, SupportsIndex, Union
  2. class PointerList(list):
  3. """Pointer to list saved in database"""
  4. def __init__(
  5. self,
  6. db: "Database", # type: ignore
  7. module: str,
  8. key: str,
  9. default: Optional[Any] = None,
  10. ):
  11. self._db = db
  12. self._module = module
  13. self._key = key
  14. self._default = default
  15. super().__init__(db.get(module, key, default))
  16. def __delitem__(self, __i: Union[SupportsIndex, slice]) -> None:
  17. a = super().__delitem__(__i)
  18. self._save()
  19. return a
  20. def __setitem__(self, __i: Union[SupportsIndex, slice], __v: Any) -> None:
  21. a = super().__setitem__(__i, __v)
  22. self._save()
  23. return a
  24. def __iadd__(self, __x: Iterable) -> "Self": # type: ignore
  25. a = super().__iadd__(__x)
  26. self._save()
  27. return a
  28. def __imul__(self, __x: int) -> "Self": # type: ignore
  29. a = super().__imul__(__x)
  30. self._save()
  31. return a
  32. def __str__(self):
  33. return f"PointerList({list(self)})"
  34. def append(self, value: Any):
  35. super().append(value)
  36. self._save()
  37. def extend(self, value: Iterable):
  38. super().extend(value)
  39. self._save()
  40. def insert(self, index: int, value: Any):
  41. super().insert(index, value)
  42. self._save()
  43. def remove(self, value: Any):
  44. super().remove(value)
  45. self._save()
  46. def pop(self, index: int = -1) -> Any:
  47. a = super().pop(index)
  48. self._save()
  49. return a
  50. def clear(self) -> None:
  51. super().clear()
  52. self._save()
  53. def _save(self):
  54. self._db.set(self._module, self._key, list(self))
  55. class PointerDict(dict):
  56. """Pointer to dict saved in database"""
  57. def __init__(
  58. self,
  59. db: "Database", # type: ignore
  60. module: str,
  61. key: str,
  62. default: Optional[Any] = None,
  63. ):
  64. self._db = db
  65. self._module = module
  66. self._key = key
  67. self._default = default
  68. super().__init__(db.get(module, key, default))
  69. def __setitem__(self, key: str, value: Any):
  70. super().__setitem__(key, value)
  71. self._save()
  72. def __delitem__(self, key: str):
  73. super().__delitem__(key)
  74. self._save()
  75. def __str__(self):
  76. return f"PointerDict({dict(self)})"
  77. def update(self, __m: dict) -> None:
  78. super().update(__m)
  79. self._save()
  80. def setdefault(self, key: str, default: Any = None) -> Any:
  81. a = super().setdefault(key, default)
  82. self._save()
  83. return a
  84. def pop(self, key: str, default: Any = None) -> Any:
  85. a = super().pop(key, default)
  86. self._save()
  87. return a
  88. def popitem(self) -> tuple:
  89. a = super().popitem()
  90. self._save()
  91. return a
  92. def clear(self) -> None:
  93. super().clear()
  94. self._save()
  95. def _save(self):
  96. self._db.set(self._module, self._key, dict(self))