This repository has been archived by the owner on Nov 23, 2023. 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.
Enable specifying the password in
config.ini
- Loading branch information
Showing
1 changed file
with
18 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,31 @@ | ||
from configparser import NoOptionError as option, NoSectionError as section | ||
from sqlalchemy import create_engine | ||
import keyring | ||
from . import config as cfg | ||
|
||
|
||
def connection(): | ||
pw = keyring.get_password(cfg.get("postGIS", "database"), | ||
cfg.get("postGIS", "username")) | ||
|
||
if pw is None: | ||
try: pw = cfg.get("postGIS", "pw") | ||
except option: | ||
print("Unable to find the database password in " + | ||
"the oemof config or keyring." + | ||
"\nExiting.") | ||
exit(-1) | ||
except section: | ||
print("Unable to find the 'postGIS' section in oemof's config." + | ||
"\nExiting.") | ||
exit(-1) | ||
|
||
engine = create_engine( | ||
"postgresql+psycopg2://{user}:{passwd}@{host}:{port}/{db}".format( | ||
user=cfg.get("postGIS", "username"), | ||
passwd=keyring.get_password( | ||
cfg.get("postGIS", "database"), | ||
cfg.get("postGIS", "username")), | ||
passwd=pw, | ||
host=cfg.get("postGIS", "host"), | ||
db=cfg.get("postGIS", "database"), | ||
port=int(cfg.get("postGIS", "port")))) | ||
|
||
return engine.connect() |