Skip to content

Commit

Permalink
config: Use a toml config instead of Python module
Browse files Browse the repository at this point in the history
Closes #15
  • Loading branch information
punchagan committed Oct 2, 2024
1 parent ac8bc89 commit f9768ad
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 39 deletions.
17 changes: 4 additions & 13 deletions app/db_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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


Expand Down
13 changes: 7 additions & 6 deletions app/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
13 changes: 12 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand All @@ -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"]
Expand Down
12 changes: 6 additions & 6 deletions sample/conf.py → sample/conf.toml
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -42,7 +42,7 @@
"Stationery",
"Hobby Education",
# Medical Expenses
"Medication ",
"Medication",
"Doctor Visit",
"Health insurance",
"Supplements",
Expand Down Expand Up @@ -114,7 +114,7 @@
]

# Define TAGS to be added
TAGS = [
tags = [
"Nationals 2022",
"Trip to Paris 2022",
"Birthday 2022",
Expand Down
8 changes: 4 additions & 4 deletions scripts/run-sample.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 5 additions & 9 deletions scripts/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit f9768ad

Please sign in to comment.