diff --git a/app/db_util.py b/app/db_util.py index 588adf0..6143058 100644 --- a/app/db_util.py +++ b/app/db_util.py @@ -14,7 +14,7 @@ from app.data import CATEGORIES, create_categories, create_tags from app.model import Category, Expense, Tag from app.scrapers import ALL_SCRAPERS -from app.util import DATA_REPO_PATH +from app.util import CONFIG, DATA_REPO_PATH ROOT = Path(__file__).parent.parent DB_NAME = os.getenv("EXPENSES_DB", "expenses.db") @@ -51,22 +51,13 @@ def ensure_categories_created() -> None: def ensure_tags_created() -> None: session = get_sqlalchemy_session() - try: - from conf import TAGS - - tags = TAGS - except ImportError: - tags = [] + tags = cast(list[str], CONFIG.get("tags", [])) return create_tags(session, tags) def get_config_categories() -> list[str]: - try: - from conf import EXTRA_CATEGORIES - - categories = CATEGORIES + EXTRA_CATEGORIES - except ImportError: - categories = CATEGORIES + extra_categories = cast(list[str], CONFIG.get("extra_categories", [])) + categories = CATEGORIES + extra_categories return categories diff --git a/app/util.py b/app/util.py index 1d0743d..2d44998 100644 --- a/app/util.py +++ b/app/util.py @@ -2,23 +2,24 @@ import datetime import io import os -import sys from pathlib import Path import pytz +import toml HERE = Path(__file__).parent ROOT = HERE.parent NUM_MONTHS = 12 DATA_REPO_PATH = Path(os.getenv("DATA_REPO_PATH", Path.cwd() / "data.git")) -sys.path.insert(0, str(DATA_REPO_PATH)) -try: - from conf import TIMEZONE -except ImportError: - TIMEZONE = "Asia/Kolkata" +def get_settings(path: Path) -> dict[str, str | list[str]]: + with open(path) as f: + return toml.load(f)["settings"] + +CONFIG = get_settings(DATA_REPO_PATH / "conf.toml") +TIMEZONE = str(CONFIG.get("timezone", "Asia/Kolkata")) TZINFO = pytz.timezone(TIMEZONE) diff --git a/poetry.lock b/poetry.lock index cbf545f..dd1a7c4 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2450,6 +2450,17 @@ files = [ [package.dependencies] urllib3 = ">=2" +[[package]] +name = "types-toml" +version = "0.10.8.20240310" +description = "Typing stubs for toml" +optional = false +python-versions = ">=3.8" +files = [ + {file = "types-toml-0.10.8.20240310.tar.gz", hash = "sha256:3d41501302972436a6b8b239c850b26689657e25281b48ff0ec06345b8830331"}, + {file = "types_toml-0.10.8.20240310-py3-none-any.whl", hash = "sha256:627b47775d25fa29977d9c70dc0cbab3f314f32c8d8d0c012f2ef5de7aaec05d"}, +] + [[package]] name = "typing-extensions" version = "4.12.2" @@ -2604,4 +2615,4 @@ resolved_reference = "a070f3a349bc0c7ce09ad097b1d3e419b42a6126" [metadata] lock-version = "2.0" python-versions = ">=3.10,<4.0" -content-hash = "7828b2feaa0e0c600ca0044cc2decc22b593942b6c57bc9a570c2161167d707f" +content-hash = "d97cdf01990aa998cd6b04bc04d33167f4cf44e1eb9f4822ec46b946beb769e4" diff --git a/pyproject.toml b/pyproject.toml index 58cf53f..383dd44 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,6 +18,7 @@ pyperclip = "^1.8.2" openpyxl = "^3.1.5" gitpython = "^3.1.43" pytz = "^2024.2" +toml = "^0.10.2" [tool.poetry.group.dev.dependencies] zulint = {git = "https://github.com/zulip/zulint.git"} @@ -28,6 +29,7 @@ types-requests = "^2.32.0.20240914" types-beautifulsoup4 = "^4.12.0.20240907" black = "^24.8.0" gitlint = "^0.19.1" +types-toml = "^0.10.8.20240310" [build-system] requires = ["poetry-core>=1.0.0"] diff --git a/sample/conf.py b/sample/conf.toml similarity index 95% rename from sample/conf.py rename to sample/conf.toml index 7c153c2..196ba7c 100644 --- a/sample/conf.py +++ b/sample/conf.toml @@ -1,14 +1,14 @@ # Specify the scrapers to be used -SCRAPERS = [ +[settings] +scrapers = [ "axis", "axis-cc", "cash", "sbi", ] - -# List of additional user defined categories -EXTRA_CATEGORIES = [ +# List of additional user-defined categories +extra_categories = [ # Food & Dining "Groceries", "Alcohol", @@ -42,7 +42,7 @@ "Stationery", "Hobby Education", # Medical Expenses - "Medication ", + "Medication", "Doctor Visit", "Health insurance", "Supplements", @@ -114,7 +114,7 @@ ] # Define TAGS to be added -TAGS = [ +tags = [ "Nationals 2022", "Trip to Paris 2022", "Birthday 2022", diff --git a/scripts/run-sample.sh b/scripts/run-sample.sh index d0a5684..fca473f 100755 --- a/scripts/run-sample.sh +++ b/scripts/run-sample.sh @@ -2,17 +2,17 @@ set -xeuo pipefail export EXPENSES_DB='sample-expenses.db' +export DATA_REPO_PATH="sample/git" HERE=$(dirname "${0}") -pushd "${HERE}/.." -alembic upgrade head - # Make a temporary git repository from the sample data -export DATA_REPO_PATH="sample/git" git init "${DATA_REPO_PATH}" cp sample/*.* "${DATA_REPO_PATH}/" +pushd "${HERE}/.." +alembic upgrade head + ./scripts/update.py --no-fetch if [ -z "${1:-}" ]; then streamlit run app/app.py diff --git a/scripts/update.py b/scripts/update.py index 24d11e0..e6bb21f 100755 --- a/scripts/update.py +++ b/scripts/update.py @@ -9,11 +9,12 @@ # 3rd-party from alembic.config import main as alembic_main -from app.db_util import ensure_categories_created, ensure_tags_created # Local +from app.db_util import ensure_categories_created, ensure_tags_created from app.parse_util import parse_data from app.scrapers import ALL_SCRAPERS +from app.util import CONFIG if __name__ == "__main__": import argparse @@ -37,15 +38,10 @@ ensure_categories_created() ensure_tags_created() - if not args.scrapers: - try: - from conf import SCRAPERS + scrapers_to_use = sorted(set(args.scrapers)) if args.scrapers else CONFIG.get("scrapers", []) - scrapers_to_use = SCRAPERS - except ImportError: - parser.error("Please define scrapers in conf.py or use --scrapers to specify them.") - else: - scrapers_to_use = sorted(set(args.scrapers)) + if not scrapers_to_use: + parser.error("Please define scrapers in conf.py or use --scrapers to specify them.") # Fetch data if not skipped if not args.no_fetch: