Some refactoring and started second bot

This commit is contained in:
2023-11-27 22:04:50 +03:00
parent b14eeb8342
commit a848733422
8 changed files with 108 additions and 63 deletions

View File

@@ -1,21 +1,15 @@
import os
import tomllib
from typing import List, Self, TypeVar
from typing import List, Optional
from attr import dataclass
from dotenv import load_dotenv
from neuroapi.types import Singleton
from .types._helpers import *
class _Singleton:
_instances = {}
def __new__(cls) -> Self:
if cls not in cls._instances:
cls._instances[cls] = super(_Singleton, cls).__new__(cls)
return cls._instances[cls]
@dataclass
class Settings:
time: List[str]
@@ -31,13 +25,21 @@ class Settings:
result['time'] = from_list(from_str, self.time)
return result
class Config(_Singleton):
class Config(Singleton):
api_url: str
settings: Settings
token: Optional[str]
proxy_token: Optional[str]
def __init__(self):
load_dotenv(os.path.join(os.path.dirname(__file__), '..', '.env'))
if not os.path.exists(os.path.join(os.path.dirname(__file__), '..', 'settings.toml')): raise Exception('Settings.toml must be in root folder')
with open(os.path.join(os.path.dirname(__file__), '..', 'settings.toml'), 'rb') as f:
settings = tomllib.load(f)
self.settings = Settings.from_dict(settings)
self.api_url = os.environ.get('API_URL')
self.api_url = os.environ.get('API_URL')
self.token = os.environ.get('TOKEN')
if self.token == '':
self.token = None
self.proxy_token = os.environ.get('PROXY_TOKEN')
if self.proxy_token == '':
self.proxy_token = None

View File

@@ -1,4 +1,5 @@
from ._post import Post
from ._image import Image
from ._admin import Admin
from ._image import Image
from ._post import Post
from ._singleton import Singleton
from ._user import User

View File

@@ -0,0 +1,10 @@
from typing import Self
class Singleton:
_instances = {}
def __new__(cls, *args, **kwargs) -> Self:
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__new__(cls)
return cls._instances[cls]