Skip to content

Commit

Permalink
Added load_env
Browse files Browse the repository at this point in the history
  • Loading branch information
Guionardo Furlan committed Jun 24, 2022
1 parent ff1ff50 commit 754df93
Show file tree
Hide file tree
Showing 10 changed files with 404 additions and 66 deletions.
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ verify_ssl = true
flake8 = "*"
autopep8 = "*"
coverage = "*"
twine = "*"
setuptools = "*"

[packages]

Expand Down
363 changes: 313 additions & 50 deletions Pipfile.lock

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# python-package
Template for python package
# PY-GSTOOLS

Tool classes and functions for Guiosoft projects

[![CodeQL](https://github.com/guionardo/py-gstools/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/guionardo/py-gstools/actions/workflows/codeql-analysis.yml)
[![Upload Python Package](https://github.com/guionardo/py-gstools/actions/workflows/python-publish.yml/badge.svg)](https://github.com/guionardo/py-gstools/actions/workflows/python-publish.yml)
![PyPI](https://img.shields.io/pypi/v/py-gstools)
![PyPI - Downloads](https://img.shields.io/pypi/dm/py-gstools)
5 changes: 5 additions & 0 deletions gs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__version__ = '0.1.1'
__tool_name__ = 'py-gstools'
__description__ = 'Tool classes and functions for Guiosoft projects'
__author__ = 'Guionardo Furlan'
__author_email__ = 'guionardo@gmail.com'
2 changes: 2 additions & 0 deletions gs/dotenv/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__all__ = ['load_env']
from .load_dotenv import load_env
37 changes: 37 additions & 0 deletions gs/dotenv/load_dotenv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import logging
import os

logger = logging.getLogger('gs.dotenv')


def load_env(file_name: str = '.env', extra_source: dict = None, verbose: bool = False) -> bool:
"""Load environment variables from a file or dict."""
if not isinstance(extra_source, dict):
extra_source = {}
if extra_source:
if verbose:
logger.info(f'load_env(extra_source={extra_source})')
os.environ.update(extra_source)
return True

try:
with open(file_name) as f:
for line in f:
line = line.strip()
if not line or line.startswith('#') or '=' not in line:
continue
key, value = line.split('=', 1)
extra_source[key.strip()] = value.strip()
if not extra_source:
if verbose:
logger.info(f'load_env(file_name={file_name}) - no data')
else:
if verbose:
logger.info(
f'load_env(file_name={file_name}) - {extra_source}')
os.environ.update(extra_source)
return True
except Exception as exc:
logger.error(f'load_env(file_name={file_name}) - error: {exc}')

return False
18 changes: 9 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def get_definitions(rel_path, *words):
long_description = read('README.md')

_name, _version, _description, _author, _author_email = get_definitions(
os.path.join('src', '__init__.py'),
os.path.join('gs', '__init__.py'),
'tool_name',
'version',
'description',
Expand All @@ -48,15 +48,15 @@ def get_definitions(rel_path, *words):
"Topic :: Software Development :: Build Tools",
"Topic :: Utilities",
"Programming Language :: Python",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8"
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
],
url='CHANGE_THIS',
keywords='CHANGE_THIS',
url='https://github.com/guionardo/py-gstools',
keywords='tools',
project_urls={
"Documentation": "CHANGE_THIS/wiki",
"Source": "CHANGE_THIS",
"Documentation": "https://github.com/guionardo/py-gstools/wiki",
"Source": "https://github.com/guionardo/py-gstools",
},
author=_author,
author_email=_author_email,
Expand All @@ -67,5 +67,5 @@ def get_definitions(rel_path, *words):
install_requires=[
],
zip_safe=True,
python_requires='>=3.6.*'
)
python_requires='>=3.8.*'
)
5 changes: 0 additions & 5 deletions src/__init__.py

This file was deleted.

Empty file added tests/dotenv/__init__.py
Empty file.
28 changes: 28 additions & 0 deletions tests/dotenv/test_dotenv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import unittest
import tempfile

from gs.dotenv import load_env


class TestDotEnv(unittest.TestCase):

def test_just_extra_source(self):

with self.assertLogs('gs.dotenv', level='INFO') as cm:
self.assertTrue(
load_env(extra_source={'TEST_DOTENV': 'test'}, verbose=True))
self.assertEqual(
cm.output, ['INFO:gs.dotenv:load_env(extra_source={\'TEST_DOTENV\': \'test\'})'])

def test_envfile(self):
with tempfile.NamedTemporaryFile('w', prefix='.env', delete=True) as tmp:
tmp.write('TEST_DOTENV=test\n# comment\nTEST_DOTENV2=test2=test3\n')
tmp.flush()
with self.assertLogs('gs.dotenv', level='INFO') as cm:
self.assertTrue(
load_env(file_name=tmp.name, verbose=True))
self.assertEqual(
cm.output, [f"INFO:gs.dotenv:load_env(file_name={tmp.name}) - {{'TEST_DOTENV': 'test', 'TEST_DOTENV2': 'test2=test3'}}"])

def test_unexistent_file(self):
self.assertFalse(load_env(file_name='/tmp/nonexistent.env'))

0 comments on commit 754df93

Please sign in to comment.