Skip to content

Commit

Permalink
Merge pull request #10 from HeySlava/5-split-configpy-and-configurati…
Browse files Browse the repository at this point in the history
…on-file

Split config dataclass and credintials
  • Loading branch information
HeySlava authored Oct 6, 2023
2 parents 6fdb0c8 + 422af32 commit b6e0612
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 23 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
.env
*.sqlite
config.py
config.ini
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ FROM python:3.10-slim-bullseye

WORKDIR /app

COPY config.ini config.ini
COPY requirements.txt requirements.txt

ENV PATH=/venv/bin:$PATH
Expand All @@ -10,6 +11,6 @@ RUN :\
&& pip install --no-cache-dir pip -U wheel setuptools -r requirements.txt \
&& :

COPY ./bot .
COPY ./bot ./bot

CMD ["python", "main.py"]
CMD ["python", "bot/main.py"]
37 changes: 37 additions & 0 deletions bot/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import configparser
from dataclasses import dataclass
from pathlib import Path

import pytz

_CONFIG_INI_PATH = Path(__file__).absolute().parent.parent / 'config.ini'
_default_token = '22:aa-F-6eNoKY65R-omFA'
_admin_id = 123

configreader = configparser.ConfigParser()
if not _CONFIG_INI_PATH.exists():
token = _default_token
admin = _admin_id
else:
configreader.read(_CONFIG_INI_PATH)
try:
admin = int(configreader['config']['admin'])
token = configreader['config']['token']
except (KeyError, ValueError):
print(f"You didn't setup your {_CONFIG_INI_PATH}. See example.config.ini")
raise SystemExit(1)


@dataclass
class Config:
token: str = token
admin: int = admin
conn_str: str = 'sqlite:///./db/budget.sqlite'
migration_script_location: Path = Path(__file__).parent / 'migrations'
alembic_config_path: Path = Path(__file__).parent / 'alembic.ini'
tz = pytz.timezone('Asia/Yerevan')
last: int = 10
replenishment_name: str = '_replenishemnt'


config = Config()
19 changes: 0 additions & 19 deletions bot/config.py.example

This file was deleted.

3 changes: 3 additions & 0 deletions example.config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[config]
admin = 123
token = 01:AJCDoV-F-6e5R-oA

0 comments on commit b6e0612

Please sign in to comment.