generated from guionardo/python-package
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Guionardo Furlan
committed
Jun 24, 2022
1 parent
ff1ff50
commit 754df93
Showing
10 changed files
with
404 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ verify_ssl = true | |
flake8 = "*" | ||
autopep8 = "*" | ||
coverage = "*" | ||
twine = "*" | ||
setuptools = "*" | ||
|
||
[packages] | ||
|
||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
__all__ = ['load_env'] | ||
from .load_dotenv import load_env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |