| Conditions | 5 |
| Total Lines | 68 |
| Code Lines | 29 |
| 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 as option |
||
| 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 | ) |
||
| 125 |