This repository has been archived by the owner on Feb 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from cisagov/CD-wip-db-config
Add database connection config
- Loading branch information
Showing
2 changed files
with
30 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"""Configuration to connect to a PostgreSQL database.""" | ||
|
||
# Standard Python Libraries | ||
from configparser import ConfigParser | ||
from importlib.resources import files | ||
|
||
REPORT_DB_CONFIG = files("pe_reports").joinpath("data/dbconfig.config") | ||
|
||
|
||
def config(filename=REPORT_DB_CONFIG, section="postgres"): | ||
"""Parse Postgres configuration details from database configuration file.""" | ||
parser = ConfigParser() | ||
|
||
parser.read(filename, encoding="utf-8") | ||
|
||
db = dict() | ||
|
||
if parser.has_section(section): | ||
for key, value in parser.items(section): | ||
db[key] = value | ||
|
||
else: | ||
raise Exception(f"Section {section} not found in {filename}") | ||
|
||
return db |