pointers.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # ©️ Dan Gazizullin, 2021-2023
  2. # This file is a part of Hikka Userbot
  3. # 🌐 https://github.com/hikariatama/Hikka
  4. # You can redistribute it and/or modify it under the terms of the GNU AGPLv3
  5. # 🔑 https://www.gnu.org/licenses/agpl-3.0.html
  6. import typing
  7. class PointerList(list):
  8. """Pointer to list saved in database"""
  9. def __init__(
  10. self,
  11. db: "Database", # type: ignore
  12. module: str,
  13. key: str,
  14. default: typing.Optional[typing.Any] = None,
  15. ):
  16. self._db = db
  17. self._module = module
  18. self._key = key
  19. self._default = default
  20. super().__init__(db.get(module, key, default))
  21. def __repr__(self):
  22. return f"PointerList({list(self)})"
  23. def __str__(self):
  24. return f"PointerList({list(self)})"
  25. def __delitem__(self, __i: typing.Union[typing.SupportsIndex, slice]) -> None:
  26. a = super().__delitem__(__i)
  27. self._save()
  28. return a
  29. def __setitem__(
  30. self,
  31. __i: typing.Union[typing.SupportsIndex, slice],
  32. __v: typing.Any,
  33. ) -> None:
  34. a = super().__setitem__(__i, __v)
  35. self._save()
  36. return a
  37. def __iadd__(self, __x: typing.Iterable) -> "Self": # type: ignore
  38. a = super().__iadd__(__x)
  39. self._save()
  40. return a
  41. def __imul__(self, __x: int) -> "Self": # type: ignore
  42. a = super().__imul__(__x)
  43. self._save()
  44. return a
  45. def append(self, value: typing.Any):
  46. super().append(value)
  47. self._save()
  48. def extend(self, value: typing.Iterable):
  49. super().extend(value)
  50. self._save()
  51. def insert(self, index: int, value: typing.Any):
  52. super().insert(index, value)
  53. self._save()
  54. def remove(self, value: typing.Any):
  55. super().remove(value)
  56. self._save()
  57. def pop(self, index: int = -1) -> typing.Any:
  58. a = super().pop(index)
  59. self._save()
  60. return a
  61. def clear(self) -> None:
  62. super().clear()
  63. self._save()
  64. def _save(self):
  65. self._db.set(self._module, self._key, list(self))
  66. class PointerDict(dict):
  67. """Pointer to dict saved in database"""
  68. def __init__(
  69. self,
  70. db: "Database", # type: ignore
  71. module: str,
  72. key: str,
  73. default: typing.Optional[typing.Any] = None,
  74. ):
  75. self._db = db
  76. self._module = module
  77. self._key = key
  78. self._default = default
  79. super().__init__(db.get(module, key, default))
  80. def __repr__(self):
  81. return f"PointerDict({dict(self)})"
  82. def __bool__(self) -> bool:
  83. return bool(self._db.get(self._module, self._key, self._default))
  84. def __setitem__(self, key: str, value: typing.Any):
  85. super().__setitem__(key, value)
  86. self._save()
  87. def __delitem__(self, key: str):
  88. super().__delitem__(key)
  89. self._save()
  90. def __str__(self):
  91. return f"PointerDict({dict(self)})"
  92. def update(self, __m: dict) -> None:
  93. super().update(__m)
  94. self._save()
  95. def setdefault(self, key: str, default: typing.Any = None) -> typing.Any:
  96. a = super().setdefault(key, default)
  97. self._save()
  98. return a
  99. def pop(self, key: str, default: typing.Any = None) -> typing.Any:
  100. a = super().pop(key, default)
  101. self._save()
  102. return a
  103. def popitem(self) -> tuple:
  104. a = super().popitem()
  105. self._save()
  106. return a
  107. def clear(self) -> None:
  108. super().clear()
  109. self._save()
  110. def _save(self):
  111. self._db.set(self._module, self._key, dict(self))