Skip to content

Commit

Permalink
Use python config library.
Browse files Browse the repository at this point in the history
  • Loading branch information
dracos committed Mar 22, 2024
1 parent ffa9184 commit e58c05d
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions scripts/division_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""

import re
import sys
from enum import Enum
from pathlib import Path
from typing import cast
Expand All @@ -19,6 +20,12 @@
from rich import print
from rich.prompt import Prompt

repository_path = Path(__file__).parent.parent
sys.path.append(str(repository_path / "commonlib" / "pylib"))

from mysociety import config
config.set_file(repository_path / "conf" / "general")

# suppress warnings about using mysqldb in pandas
filterwarnings(
"ignore",
Expand Down Expand Up @@ -48,24 +55,7 @@ def cli():
pass


def fast_config(config_path: Path) -> dict[str, str]:
"""
There's a more comprehensive config parser in commonlib/pylib/mysociety/config.py
But this is all we need for this function.
"""
pattern = r"define\s*\(\s*['\"](.*?)['\"]\s*,\s*['\"]?(.*?)['\"]?\s*\);"

config_path = Path(__file__).parent.parent / config_path
with config_path.open("r") as f:
content = f.read()

return {key: value for key, value in re.findall(pattern, content)}


def get_twfy_db_connection() -> MySQLdb.Connection:
config_path = Path("conf", "general")
config = fast_config(config_path)

db_connection = cast(
MySQLdb.Connection,
MySQLdb.connect(
Expand Down Expand Up @@ -229,7 +219,6 @@ def export_division_data(verbose: bool = False):
"""
Export division data to publically accessible parquet files
"""
config = fast_config(Path("conf", "general"))
raw_data_dir = Path(config["RAWDATA"])
dest_path = raw_data_dir / "votes"
dest_path.mkdir(parents=True, exist_ok=True)
Expand Down

0 comments on commit e58c05d

Please sign in to comment.