db.url()   B
last analyzed

Complexity

Conditions 5

Size

Total Lines 68
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 29
nop 2
dl 0
loc 68
rs 8.7173
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
from configparser import NoOptionError as option
2
from configparser import NoSectionError
3
import getpass
4
5
from sqlalchemy import create_engine
6
import keyring
7
8
from . import config as cfg
9
10
11
__version__ = '0.0.6'
12
13
14
def url(section="postGIS", config_file=None):
15
    """ Retrieve the URL used to connect to the database.
16
17
    Use this if you have your own means of accessing the database and do not
18
    want to use :func:`engine` or :func:`connection`.
19
20
    Parameters
21
    ----------
22
    section : str, optional
23
        The `config.ini` section corresponding to the targeted database.
24
        It should contain all the details that needed to set up a connection.
25
26
    Returns
27
    -------
28
    database URL : str
29
        The URL with which one can connect to the database. Be careful as this
30
        will probably contain sensitive data like the username/password
31
        combination.
32
    config_file : str, optional
33
        Relative of absolute of config.ini. If not specified, it tries to read
34
        from .oemof/config.ini in your HOME dir
35
36
    Notes
37
    -----
38
39
    For documentation on config.ini see the README section on
40
    :ref:`configuring <readme#configuration>` :mod:`oemof.db`.
41
    """
42
43
    cfg.load_config(config_file)
44
45
    try:
46
        pw = keyring.get_password(
47
            cfg.get(section, "database"), cfg.get(section, "username")
48
        )
49
    except NoSectionError:
50
        print(
51
            "There is no section {section} in your config file. Please "
52
            "choose one available section from your config file or "
53
            "specify a new one!".format(section=section)
54
        )
55
        exit(-1)
56
57
    if pw is None:
58
        try:
59
            pw = cfg.get(section, "pw")
60
        except option:
61
            pw = getpass.getpass(
62
                prompt="No password available in your "
63
                "keyring for database {database}. "
64
                "\n\nEnter your password to "
65
                "store it in "
66
                "keyring:".format(database=section)
67
            )
68
            keyring.set_password(section, cfg.get(section, "username"), pw)
69
        except NoSectionError:
70
            print(
71
                "Unable to find the 'postGIS' section in oemof's config."
72
                + "\nExiting."
73
            )
74
            exit(-1)
75
76
    return "postgresql+psycopg2://{user}:{passwd}@{host}:{port}/{db}".format(
77
        user=cfg.get(section, "username"),
78
        passwd=pw,
79
        host=cfg.get(section, "host"),
80
        db=cfg.get(section, "database"),
81
        port=int(cfg.get(section, "port")),
82
    )
83
84
85
def engine(section="postGIS", config_file=None):
86
    """Creates engine object for database access
87
88
    If keyword argument `section` is used it requires an existing config.ini
89
    file at the right location.
90
91
    Parameters
92
    ----------
93
    section : str, optional
94
        Section (in config.ini) of targeted database containing connection
95
        details that are used to set up connection
96
    config_file : str, optional
97
        Relative of absolute of config.ini. If not specified, it tries to read
98
        from .oemof/config.ini in your HOME dir
99
100
    Returns
101
    -------
102
    engine : :class:`sqlalchemy.engine.Engine`
103
        Engine for sqlalchemy
104
105
    Notes
106
    -----
107
108
    For documentation on config.ini see the README section on
109
    :ref:`configuring <readme#configuration>` :mod:`oemof.db`.
110
    """
111
112
    return create_engine(url(section, config_file=config_file))
113
114
115
def connection(section="postGIS", config_file=None):
116
    """Database connection method of sqlalchemy engine object
117
118
    This function purely calls the `connect()` method of the engine object
119
    returned by :py:func:`engine`.
120
121
    For description of parameters see :py:func:`engine`.
122
    """
123
124
    return engine(section=section, config_file=config_file).connect()
125