Conditions | 4 |
Total Lines | 62 |
Code Lines | 24 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | from configparser import NoOptionError, NoSectionError |
||
12 | def url(section="postGIS", config_file=None): |
||
13 | """ Retrieve the URL used to connect to the database. |
||
14 | |||
15 | Use this if you have your own means of accessing the database and do not |
||
16 | want to use :func:`engine` or :func:`connection`. |
||
17 | |||
18 | Parameters |
||
19 | ---------- |
||
20 | section : str, optional |
||
21 | The `config.ini` section corresponding to the targeted database. |
||
22 | It should contain all the details that needed to set up a connection. |
||
23 | |||
24 | Returns |
||
25 | ------- |
||
26 | database URL : str |
||
27 | The URL with which one can connect to the database. Be careful as this |
||
28 | will probably contain sensitive data like the username/password |
||
29 | combination. |
||
30 | config_file : str, optional |
||
31 | Relative of absolute of config.ini. If not specified, it tries to read |
||
32 | from .oemof/config.ini in your HOME dir |
||
33 | |||
34 | Notes |
||
35 | ----- |
||
36 | |||
37 | For documentation on config.ini see the README section on |
||
38 | :ref:`configuring <readme#configuration>` :mod:`oemof.db`. |
||
39 | """ |
||
40 | |||
41 | cfg.load_config(config_file) |
||
42 | |||
43 | try: |
||
44 | pw = keyring.get_password( |
||
45 | cfg.get(section, "database"), cfg.get(section, "username") |
||
46 | ) |
||
47 | except NoSectionError: |
||
48 | msg = ( |
||
49 | "There is no section {section} in your config file. Please " |
||
50 | "choose one available section from your config file or " |
||
51 | "specify a new one!".format(section=section) |
||
52 | ) |
||
53 | raise NoSectionError(msg) |
||
54 | |||
55 | if pw is None: |
||
56 | try: |
||
57 | pw = cfg.get(section, "pw") |
||
58 | except NoOptionError: |
||
59 | pw = getpass.getpass( |
||
60 | prompt="No password available in your " |
||
61 | "keyring for database {database}. " |
||
62 | "\n\nEnter your password to " |
||
63 | "store it in " |
||
64 | "keyring:".format(database=section) |
||
65 | ) |
||
66 | keyring.set_password(section, cfg.get(section, "username"), pw) |
||
67 | |||
68 | return "postgresql+psycopg2://{user}:{passwd}@{host}:{port}/{db}".format( |
||
69 | user=cfg.get(section, "username"), |
||
70 | passwd=pw, |
||
71 | host=cfg.get(section, "host"), |
||
72 | db=cfg.get(section, "database"), |
||
73 | port=int(cfg.get(section, "port")), |
||
74 | ) |
||
117 |