Skip to content

Commit

Permalink
Apply black and isort
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaraBuettner committed Mar 23, 2023
1 parent fc74e3a commit 71c9bc2
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 68 deletions.
3 changes: 2 additions & 1 deletion etrago/tools/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"""
import logging

from etrago.tools import db
from pyomo.environ import Constraint
from pypsa.descriptors import expand_series
from pypsa.linopt import define_constraints, define_variables, get_var, linexpr
Expand All @@ -32,6 +31,8 @@
import pandas as pd
import pyomo.environ as po

from etrago.tools import db

logger = logging.getLogger(__name__)

__copyright__ = (
Expand Down
152 changes: 87 additions & 65 deletions etrago/tools/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import configparser as cp
import keyring
import getpass
import os

from sqlalchemy import create_engine
import keyring
import oedialect


def readcfg(filepath, section):
"""
"""
Reads the configuration file. If section is not available, calls
create_oedb_config_file to add the new section to an existing config.ini.
Parameters
----------
filepath : str
Expand All @@ -43,51 +45,60 @@ def readcfg(filepath, section):

cfg = cp.ConfigParser()
cfg.read(filepath)

if not cfg.has_section(section):
print('The section "{sec}" is not in the config file {file}.'
.format(sec=section,
file=filepath))
cfg = create_oedb_config_file(filepath, section)
print(
'The section "{sec}" is not in the config file {file}.'.format(
sec=section, file=filepath
)
)
cfg = create_oedb_config_file(filepath, section)

return cfg


def get_connection_details(section):
"""
Asks the user for the database connection details and returns them as a
ConfigParser-object.
Parameters
----------
None
Returns
-------
cfg : configparser.ConfigParser
Used for configuration file parser language.
"""
print('Please enter your connection details:')
dialect = input('Enter input value for `dialect` (default: psycopg2): ') or 'psycopg2'
username = input('Enter value for `username`: ')
database = input('Enter value for `database`: ')
host = input('Enter value for `host`: ')
port = input('Enter value for `port` (default: 5432): ') or '5432'
print("Please enter your connection details:")
dialect = (
input("Enter input value for `dialect` (default: psycopg2): ")
or "psycopg2"
)
username = input("Enter value for `username`: ")
database = input("Enter value for `database`: ")
host = input("Enter value for `host`: ")
port = input("Enter value for `port` (default: 5432): ") or "5432"

cfg = cp.ConfigParser()
cfg.add_section(section)
cfg.set(section, 'dialect', dialect)
cfg.set(section, 'username', username)
cfg.set(section, 'host', host)
cfg.set(section, 'port', port)
cfg.set(section, 'database', database)
pw = getpass.getpass(prompt="Enter your password/token to " \
"store it in "
"keyring: ".format(database=section))
cfg.set(section, "dialect", dialect)
cfg.set(section, "username", username)
cfg.set(section, "host", host)
cfg.set(section, "port", port)
cfg.set(section, "database", database)
pw = getpass.getpass(
prompt="Enter your password/token to "
"store it in "
"keyring: ".format(database=section)
)
keyring.set_password(section, cfg.get(section, "username"), pw)

return cfg

def create_oedb_config_file(filepath, section='oep'):

def create_oedb_config_file(filepath, section="oep"):
"""
Parameters
Expand All @@ -96,43 +107,46 @@ def create_oedb_config_file(filepath, section='oep'):
Absolute path of config file including the filename itself
section : str
Section in config file which contains connection details
Returns
-------
cfg : configparser.ConfigParser
Used for configuration file parser language.
"""

cfg = get_connection_details(section)

print('Do you want to store the connection details in the config file {file} ?'
.format(file=filepath))
choice = ''
while choice not in ['y', 'n']:
choice = input('(y/n): ')
print(
"Do you want to store the connection details in the config file {file} ?".format(
file=filepath
)
)
choice = ""
while choice not in ["y", "n"]:
choice = input("(y/n): ")

if choice == 'y':
if choice == "y":
# create egoio dir if not existent
base_path = os.path.split(filepath)[0]
if not os.path.isdir(base_path):
os.mkdir(base_path)
print('The directory {path} was created.'.format(path=base_path))
with open(filepath, 'a') as configfile:
print("The directory {path} was created.".format(path=base_path))

with open(filepath, "a") as configfile:
cfg.write(configfile)
pass


print('Template {0} with section "{1}" created.\nYou can manually edit'
' the config file.'
.format(filepath,
section))

print(
'Template {0} with section "{1}" created.\nYou can manually edit'
" the config file.".format(filepath, section)
)
else:
pass

return cfg

def connection(filepath=None, section='oep', readonly=False):

def connection(filepath=None, section="oep", readonly=False):
"""
Instantiate a database connection (for the use with SQLAlchemy).
Expand All @@ -151,50 +165,58 @@ def connection(filepath=None, section='oep', readonly=False):
Set this option to True for creating a read-only and passwordless
engine for accessing the open energy platform.
Default: False.
Returns
-------
conn : sqlalchemy.engine
SQLalchemy engine object containing the connection details
"""

if readonly:
conn = create_engine(
"postgresql+oedialect://openenergy-platform.org")
conn = create_engine("postgresql+oedialect://openenergy-platform.org")
else:
# define default filepath if not provided
if filepath is None:
filepath = os.path.join(os.path.expanduser("~"), '.etrago_database', 'config.ini')
filepath = os.path.join(
os.path.expanduser("~"), ".etrago_database", "config.ini"
)

# does the file exist?
if not os.path.isfile(filepath):
print('DB config file {file} not found. '
'This might be the first run of the tool. '
.format(file=filepath))
print(
"DB config file {file} not found. "
"This might be the first run of the tool. ".format(
file=filepath
)
)
cfg = create_oedb_config_file(filepath, section=section)
else:
cfg = readcfg(filepath, section)

try:
pw = cfg.get(section, "password")
except:
pw = keyring.get_password(section,
cfg.get(section, "username"))
pw = keyring.get_password(section, cfg.get(section, "username"))
if pw is None:
pw = getpass.getpass(prompt='No password found for database "{db}". '
'Enter your password to '
'store it in keyring: '
.format(db=cfg.get(section, 'database')))
pw = getpass.getpass(
prompt='No password found for database "{db}". '
"Enter your password to "
"store it in keyring: ".format(
db=cfg.get(section, "database")
)
)
keyring.set_password(section, cfg.get(section, "username"), pw)

# establish connection and return it
conn = create_engine(
"postgresql+{dialect}://{user}:{password}@{host}:{port}/{db}".format(
dialect=cfg.get(section, 'dialect', fallback='psycopg2'),
user=cfg.get(section, 'username'),
dialect=cfg.get(section, "dialect", fallback="psycopg2"),
user=cfg.get(section, "username"),
password=pw,
host=cfg.get(section, 'host'),
port=cfg.get(section, 'port'),
db=cfg.get(section, 'database')))
host=cfg.get(section, "host"),
port=cfg.get(section, "port"),
db=cfg.get(section, "database"),
)
)

return conn
return conn
2 changes: 1 addition & 1 deletion etrago/tools/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import logging

from etrago.tools import db
from pypsa.components import Network
from sqlalchemy.orm import sessionmaker
import pandas as pd
Expand All @@ -34,6 +33,7 @@
from etrago.cluster.electrical import ehv_clustering, run_spatial_clustering
from etrago.cluster.gas import run_spatial_clustering_gas
from etrago.cluster.snapshot import skip_snapshots, snapshot_clustering
from etrago.tools import db
from etrago.tools.calc_results import calc_etrago_results
from etrago.tools.execute import (
dispatch_disaggregation,
Expand Down
3 changes: 2 additions & 1 deletion etrago/tools/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import math
import os

from etrago.tools import db
from pyomo.environ import Constraint, PositiveReals, Var
from shapely.geometry import LineString, Point
import geopandas as gpd
Expand All @@ -38,6 +37,8 @@
import pypsa
import sqlalchemy.exc

from etrago.tools import db

logger = logging.getLogger(__name__)


Expand Down

0 comments on commit 71c9bc2

Please sign in to comment.