Skip to content

Commit

Permalink
added find_dotenv_file method to Env
Browse files Browse the repository at this point in the history
  • Loading branch information
DinisCruz committed Jun 6, 2024
1 parent 8067f9e commit b29a395
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 14 deletions.
20 changes: 11 additions & 9 deletions osbot_utils/utils/Env.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,32 @@ def env_unload_from_file(path):
if key in os.environ: # Remove the environment variable if it exists
del os.environ[key]

def find_dotenv_file(start_path=None, env_file_to_find='.env'):
directories = all_parent_folders(path=start_path, include_path=True) # Define the possible directories to search for the .env file (which is this and all parent folders)
for directory in directories: # Iterate through the directories and load the .env file if found
env_path = os.path.join(directory,env_file_to_find) # Define the path to the .env file
if os.path.exists(env_path): # If we found one
return env_path # return the path to the .env file

def in_github_action():
return os.getenv('GITHUB_ACTIONS') == 'true'

def in_python_debugger():
if sys.gettrace() is not None: # Check for a trace function
return True

pycharm_hosted = os.getenv('PYCHARM_HOSTED') == '1' # Check for PyCharm specific environment variables and other potential indicators
pydevd_load_values_async = os.getenv('PYDEVD_LOAD_VALUES_ASYNC') is not None
if pycharm_hosted and pydevd_load_values_async:
return True

return False

def load_dotenv(dotenv_path=None, override=False):
if dotenv_path: # If a specific dotenv path is provided, load from it
if dotenv_path: # If a specific dotenv path is provided, load from it
env_load_from_file(dotenv_path, override)
else:
directories = all_parent_folders(include_path=True) # Define the possible directories to search for the .env file (which is this and all parent folders)
for directory in directories: # Iterate through the directories and load the .env file if found
env_path = os.path.join(directory, '.env') # Define the path to the .env file
if os.path.exists(env_path): # If we found one
env_load_from_file(env_path, override) # Process it
break # Stop after loading the first .env file # Stop after loading the first .env file
env_file = find_dotenv_file()
if env_file:
env_load_from_file(env_file, override) # Process it


def not_in_github_action():
Expand Down
7 changes: 6 additions & 1 deletion osbot_utils/utils/Files.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,10 @@ def path_combine(path1, path2):
return abspath(join(parent_path,sub_path))

@staticmethod
def parent_folder(path):
def parent_folder(path, use_full_path=False):
if path:
if use_full_path:
path = file_full_path(path)
return os.path.dirname(path)

@staticmethod
Expand Down Expand Up @@ -429,6 +431,8 @@ def write_gz(path=None, contents=None):
return path

# todo: refactor the methods above into static methods (as bellow)
def absolute_path(path):
return abspath(path)

def all_parent_folders(path=None, include_path=False):
if path is None:
Expand Down Expand Up @@ -498,6 +502,7 @@ def stream_to_file(stream, path=None):
file_exists = Files.exists
file_extension = Files.file_extension
file_extension_fix = Files.file_extension_fix
file_full_path = absolute_path
file_lines = Files.lines
file_lines_gz = Files.lines_gz
file_md5 = Files.contents_md5
Expand Down
53 changes: 49 additions & 4 deletions tests/utils/test_Env.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import os
from os.path import abspath
from unittest import TestCase

from osbot_utils.testing.Temp_File import Temp_File
from osbot_utils.testing.Temp_Folder import Temp_Folder

from osbot_utils.utils.Dev import pprint
from osbot_utils.utils.Env import env_value, env_vars, env_vars_list, load_dotenv, unload_dotenv, platform_darwin
from osbot_utils.utils.Files import file_not_exists, file_exists, file_create, file_delete
from osbot_utils.utils.Lists import list_contains_list

from osbot_utils.testing.Temp_File import Temp_File
from osbot_utils.utils.Env import env_value, env_vars, env_vars_list, load_dotenv, unload_dotenv, platform_darwin, \
find_dotenv_file
from osbot_utils.utils.Files import file_not_exists, file_exists, file_create, file_delete, file_full_path, \
parent_folder, current_temp_folder, path_combine
from osbot_utils.utils.Lists import list_contains_list


class test_Env(TestCase):
Expand Down Expand Up @@ -50,6 +56,45 @@ def test_env_vars_list(self):
assert env_vars_list() == sorted(set(env_vars()))
assert list_contains_list(env_vars_list(), ['PATH', 'HOME', 'PWD']) is True

def test_find_dotenv_file(self):
assert find_dotenv_file() == file_full_path(test_Env.temp_env_file) # we should find the temp .env that was added to the current test folder
assert find_dotenv_file(parent_folder(path='.', use_full_path=True)) is None # there should be no .env paths anywere in the current parent path folders
with Temp_Folder() as folder_a:
with Temp_Folder(parent_folder=folder_a.full_path) as folder_b:
with Temp_Folder(parent_folder=folder_b.full_path) as folder_c:
env_file__in_folder_a = file_create(path_combine(folder_a.full_path, '.env'), 'TEMP__ENV_VAR_A=1')
env_file__in_folder_b = file_create(path_combine(folder_b.full_path, '.env'), 'TEMP__ENV_VAR_B=1')
env_file__in_folder_c = file_create(path_combine(folder_c.full_path, '.env'), 'TEMP__ENV_VAR_C=1')

assert parent_folder(folder_c.full_path) == folder_b.full_path
assert parent_folder(folder_b.full_path) == folder_a.full_path
assert parent_folder(folder_a.full_path) == current_temp_folder()

assert parent_folder(env_file__in_folder_a) == folder_a.full_path
assert parent_folder(env_file__in_folder_b) == folder_b.full_path
assert parent_folder(env_file__in_folder_c) == folder_c.full_path

assert find_dotenv_file(folder_c.full_path) == env_file__in_folder_c
assert find_dotenv_file(folder_b.full_path) == env_file__in_folder_b
assert find_dotenv_file(folder_a.full_path) == env_file__in_folder_a

file_delete(env_file__in_folder_c)
assert find_dotenv_file(folder_c.full_path) == env_file__in_folder_b
assert find_dotenv_file(folder_b.full_path) == env_file__in_folder_b
assert find_dotenv_file(folder_a.full_path) == env_file__in_folder_a

file_delete(env_file__in_folder_b)
assert find_dotenv_file(folder_c.full_path) == env_file__in_folder_a
assert find_dotenv_file(folder_b.full_path) == env_file__in_folder_a
assert find_dotenv_file(folder_a.full_path) == env_file__in_folder_a

file_delete(env_file__in_folder_a)
assert find_dotenv_file(folder_c.full_path) is None
assert find_dotenv_file(folder_b.full_path) is None
assert find_dotenv_file(folder_a.full_path) is None



def test_load_dotenv(self):
env_file_data = """
AN_VAR_1=41
Expand Down

0 comments on commit b29a395

Please sign in to comment.