-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulate_date_tables.py
More file actions
98 lines (74 loc) · 2.52 KB
/
populate_date_tables.py
File metadata and controls
98 lines (74 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""Populates date-related tables in a SQL Server database.
This module orchestrates the generation of date, pay period, and years dimension
tables and writes them to a SQL Server database. Configuration is read from
a JSON file.
"""
from datetime import date
import json
import logging
from typing import Any
from dates import get_dates
from modules.database import Connection, Database
from pay_periods import get_pay_periods
from years import get_years
CONFIG_FILE = "config.json"
logging.basicConfig(
level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s"
)
def read_config(path: str = CONFIG_FILE) -> dict[str, Any]:
"""Reads configuration from a JSON file.
Args:
path: Path to configuration file.
Returns:
Parsed configuration dictionary.
"""
try:
with open(path, encoding="utf-8") as file:
return json.load(file)
except FileNotFoundError:
logging.error(f"Configuration file not found: {path}")
raise
except json.JSONDecodeError as e:
logging.error(f"Invalid JSON in configuration file: {e}")
raise
def generate_and_store_data(config: dict[str, Any]) -> None:
"""Generates and stores date-related data in the database.
Args:
config: Configuration dictionary.
"""
try:
logging.info("Generating date data...")
dates = get_dates(date(1983, 1, 1), date(2085, 12, 31))
pay_periods = get_pay_periods(dates)
years = get_years(dates, pay_periods)
logging.info("Connecting to database...")
db = Database(
Connection(
host=config["host"],
database=config["database"],
username=config["username"],
password=config["password"],
port=config.get("port", 1433),
)
)
schema = config["schema"]
logging.info("Writing data to database...")
db.write_table(f"{schema}.Dates", dates)
db.write_table(f"{schema}.PayPeriods", pay_periods)
db.write_table(f"{schema}.Years", years)
logging.info("Data successfully written to database.")
except Exception as e:
logging.exception("An error occurred while generating or writing data: %s", e)
raise
def main():
"""Main entry point.
Returns:
Exit status code (0 = success).
"""
try:
generate_and_store_data(read_config())
return 0
except Exception:
return 1
if __name__ == "__main__":
raise SystemExit(main())