Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

Commit

Permalink
Enable specifying the password in config.ini
Browse files Browse the repository at this point in the history
  • Loading branch information
gnn committed Oct 30, 2015
1 parent 05036c1 commit d1ec190
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions oemof_pg/db.py
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()

0 comments on commit d1ec190

Please sign in to comment.