cit-is-bot-backend/config/main.py

125 строки
2.3 KiB
Python

from configparser import RawConfigParser
import os
import redis
cwd = os.getcwd()
config = RawConfigParser()
config.read(
filenames=os.path.join(
cwd,
'config.ini',
),
)
class Main:
host = config.get(
section='Main',
option='host',
fallback='localhost',
)
port = config.getint(
section='Main',
option='port',
)
title = config.get(
section='Main',
option='title',
fallback='CIT IS Bot',
)
cwd = config.get(
section='Main',
option='cwd',
fallback=os.getcwd(),
)
class Postgres:
host = config.get(
section='Postgres',
option='host',
fallback='localhost',
)
port = config.getint(
section='Postgres',
option='port',
fallback=5432,
)
user = config.get(
section='Postgres',
option='user',
)
password = config.get(
section='Postgres',
option='password',
)
dbname = config.get(
section='Postgres',
option='dbname',
)
class Redis:
host = config.get(
section='Redis',
option='host',
fallback='localhost',
)
port = config.getint(
section='Redis',
option='port',
fallback=6379,
)
db = config.get(
section='Redis',
option='db',
)
password = config.get(
section='Redis',
option='password',
fallback=None,
)
@classmethod
def get(
cls,
key: str,
):
with redis.Redis(
host=cls.host,
port=cls.port,
db=cls.db,
password=cls.password,
decode_responses=True,
) as connection:
return connection.get(
name=key,
)
@classmethod
def set(
cls,
key: str,
value,
):
with redis.Redis(
host=cls.host,
port=cls.port,
db=cls.db,
password=cls.password,
decode_responses=True,
) as connection:
connection.set(
name=key,
value=value,
)
class Telegram:
token = config.get(
section='Telegram',
option='token',
)