read_dotenv()   B
last analyzed

Complexity

Conditions 6

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
c 1
b 0
f 0
dl 0
loc 12
rs 8
1
import os
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
from contextlib import closing
3
4
5
def set_env_file(path: str):
6
    try:
7
        for env_key, env_value in read_dotenv(path):
8
            os.environ.setdefault(env_key, env_value)
9
    except FileNotFoundError:
10
        pass
11
12
13
def read_dotenv(dotenv_path: str):
14
    with closing(open(dotenv_path, 'r')) as file:
15
        for line in file:
16
            line = line.rstrip(os.linesep)
17
18
            if not line or line.startswith('#') or '=' not in line:
19
                continue
20
21
            env_key, env_value = line.split('=', 1)
22
            env_value = env_value.strip("'").strip('"')
23
24
            yield env_key, env_value
25