mirror of https://github.com/coddrago/Heroku
commit
8781d89b6c
|
@ -25,12 +25,8 @@ from .inline.types import ( # skipcq: PY-W2000
|
|||
)
|
||||
from . import validators # skipcq: PY-W2000
|
||||
from .pointers import ( # skipcq: PY-W2000
|
||||
PointerBool,
|
||||
PointerInt,
|
||||
PointerStr,
|
||||
PointerList,
|
||||
PointerDict,
|
||||
PointerTuple,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
@ -85,30 +81,6 @@ class Module:
|
|||
send a message to logs with verbosity INFO and exception traceback
|
||||
"""
|
||||
|
||||
def __setattr__(self, attr: str, value: Any):
|
||||
if hasattr(self, attr) and isinstance(
|
||||
getattr(self, attr), (PointerInt, PointerList)
|
||||
):
|
||||
getattr(self, attr).set(value)
|
||||
return
|
||||
|
||||
return object.__setattr__(self, attr, value)
|
||||
|
||||
def __getattribute__(self, attr: str) -> Any:
|
||||
try:
|
||||
object.__getattribute__(self, attr)
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
value = object.__getattribute__(self, attr)
|
||||
if isinstance(value, PointerInt):
|
||||
return value.value
|
||||
|
||||
if isinstance(value, PointerList):
|
||||
return list(value)
|
||||
|
||||
return object.__getattribute__(self, attr)
|
||||
|
||||
|
||||
class Library:
|
||||
"""All external libraries must have a class-inheritant from this class"""
|
||||
|
|
|
@ -12,6 +12,7 @@ import logging
|
|||
import os
|
||||
import time
|
||||
import asyncio
|
||||
import collections
|
||||
|
||||
try:
|
||||
import psycopg2
|
||||
|
@ -32,13 +33,9 @@ from telethon.tl.types import Message
|
|||
from telethon.errors.rpcerrorlist import ChannelsTooMuchError
|
||||
|
||||
from . import utils, main
|
||||
from ._types import (
|
||||
PointerBool,
|
||||
PointerInt,
|
||||
PointerStr,
|
||||
from .pointers import (
|
||||
PointerList,
|
||||
PointerDict,
|
||||
PointerTuple,
|
||||
)
|
||||
|
||||
DATA_DIR = (
|
||||
|
@ -387,12 +384,9 @@ class Database(dict):
|
|||
"""Get a pointer to database key"""
|
||||
value = self.get(owner, key, default)
|
||||
mapping = {
|
||||
int: PointerInt,
|
||||
str: PointerStr,
|
||||
bool: PointerBool,
|
||||
list: PointerList,
|
||||
dict: PointerDict,
|
||||
tuple: PointerTuple,
|
||||
collections.abc.Hashable: lambda v: v,
|
||||
}
|
||||
|
||||
pointer_constructor = next(
|
||||
|
|
|
@ -3,27 +3,6 @@ from typing import Any, Iterable, Optional, SupportsIndex, Union
|
|||
from typing_extensions import Self
|
||||
|
||||
|
||||
class PointerInt(int):
|
||||
"""Pointer to integer saved in database"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
db: "Database", # type: ignore
|
||||
module: str,
|
||||
key: str,
|
||||
default: Optional[Any] = None,
|
||||
):
|
||||
self._db = db
|
||||
self._module = module
|
||||
self._key = key
|
||||
self._default = default
|
||||
self.value = db.get(module, key, default)
|
||||
|
||||
def set(self, value: Any):
|
||||
self._db.set(self._module, self._key, value)
|
||||
self.value = value
|
||||
|
||||
|
||||
class PointerList(list):
|
||||
"""Pointer to list saved in database"""
|
||||
|
||||
|
@ -91,43 +70,6 @@ class PointerList(list):
|
|||
def _save(self):
|
||||
self._db.set(self._module, self._key, list(self))
|
||||
|
||||
def set(self, value: Any):
|
||||
if not isinstance(value, list):
|
||||
raise TypeError(
|
||||
f"Attempted to assign value {value}, which is not a list to pointer"
|
||||
)
|
||||
|
||||
self.clear()
|
||||
self.extend(value)
|
||||
self._save()
|
||||
|
||||
|
||||
class PointerTuple(tuple):
|
||||
"""Pointer to tuple saved in database"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
db: "Database", # type: ignore
|
||||
module: str,
|
||||
key: str,
|
||||
default: Optional[Any] = None,
|
||||
):
|
||||
self._db = db
|
||||
self._module = module
|
||||
self._key = key
|
||||
self._default = default
|
||||
self.set(db.get(module, key, default))
|
||||
|
||||
def set(self, value: Any):
|
||||
if not isinstance(value, tuple):
|
||||
raise TypeError(
|
||||
f"Attempted to assign value {value}, which is not a tuple to pointer"
|
||||
)
|
||||
|
||||
self.clear()
|
||||
self.extend(value)
|
||||
self._save()
|
||||
|
||||
|
||||
class PointerDict(dict):
|
||||
"""Pointer to dict saved in database"""
|
||||
|
@ -182,45 +124,3 @@ class PointerDict(dict):
|
|||
|
||||
def _save(self):
|
||||
self._db.set(self._module, self._key, dict(self))
|
||||
|
||||
def set(self, value: Any):
|
||||
if not isinstance(value, dict):
|
||||
raise TypeError(
|
||||
f"Attempted to assign value {value}, which is not a dict to pointer"
|
||||
)
|
||||
|
||||
self.clear()
|
||||
self.update(value)
|
||||
self._save()
|
||||
|
||||
|
||||
class PointerStr(str):
|
||||
"""Pointer to string saved in database"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
db: "Database", # type: ignore
|
||||
module: str,
|
||||
key: str,
|
||||
default: Optional[Any] = None,
|
||||
):
|
||||
self._db = db
|
||||
self._module = module
|
||||
self._key = key
|
||||
self._default = default
|
||||
self.value = db.get(module, key, default)
|
||||
|
||||
def set(self, value: Any):
|
||||
self.replace(self.center(0), value)
|
||||
self._db.set(self._module, self._key, value)
|
||||
|
||||
|
||||
class PointerBool(PointerInt):
|
||||
def __init__(
|
||||
self,
|
||||
db: "Database", # type: ignore
|
||||
module: str,
|
||||
key: str,
|
||||
default: Optional[Any] = None,
|
||||
):
|
||||
super().__init__(db, module, key, default)
|
||||
|
|
Loading…
Reference in New Issue