tools

_dict_to_dotenv

msdss_base_dotenv.tools._dict_to_dotenv(env_dict)[source]

Converts a dict to a .env formatted str.

Parameters

env_dict (dict) – Key value dictionary representing env variables.

Returns

Str representing a .env formatted file structure.

Return type

str

Author

Richard Wen <rrwen.dev@gmail.com>

Example

from msdss_base_dotenv.tools import _dict_to_dotenv

# Create default key value env
env = dict(USER='msdss', PASSWORD='msdss123')

# Convert to dotenv file str
env_str = _dict_to_dotenv(env)

# Display results
print('env: ' + str(env))
print('env_str: ' + env_str)
env: {'USER': 'msdss', 'PASSWORD': 'msdss123'}
env_str: USER=msdss
PASSWORD=msdss123

clear_env_file

msdss_base_dotenv.tools.clear_env_file(env_file='./.env', key_path=None)[source]

Deletes the encrypted environment file and its key if they exist.

Parameters
  • env_file (str) – Path of the encrypted environment file.

  • key_path (str) – Path of the key file used to unlock the encrypted file. If None, this defaults to the package’s directory.

Author

Richard Wen <rrwen.dev@gmail.com>

Example

from msdss_base_dotenv.tools import *

# Create default key value env
env = dict(USER='msdss', PASSWORD='msdss123')

# Save the key value env to an encrypted file
save_env_file(env)

# Remove the env file and its associated key
clear_env_file()

del_env_var

msdss_base_dotenv.tools.del_env_var(name, env_file='./.env', key_path=None)[source]

Deletes an environmental variable using a file from msdss_base_dotenv.tools.save_env_file().

Parameters
  • name (str) – The name of the environmental variable to be deleted.

  • env_file (str) – Path of the environment save file.

  • key_path (str) – Path of the key file used to unlock the save file. If None, this defaults to the package’s directory.

Author

Richard Wen <rrwen.dev@gmail.com>

Example

import os

from msdss_base_dotenv.tools import *

# Clear any existing env files
clear_env_file()

# Create key value env and save it
env = dict(USER='msdss', PASSWORD='msdss123')
save_env_file(env)

# Load the saved env
load_env_file()

# Remove the password var from the saved env
del_env_var('PASSWORD')

# Load the saved env after the removal
load_env_file()
loaded_env = dict(
    USER=os.environ['USER'],
    PASSWORD=os.getenv('PASSWORD', None)
)

# Display the results
print('env: ' + str(env))
print('loaded_env: ' + str(loaded_env))

# Clear env files
clear_env_file()
env: {'USER': 'msdss', 'PASSWORD': 'msdss123'}
loaded_env: {'USER': 'msdss', 'PASSWORD': None}

env_exists

msdss_base_dotenv.tools.env_exists(env_file='./.env', key_path=None)[source]

Checks if an environment exists using a saved environment file from msdss_base_dotenv.tools.save_env_file().

Parameters
  • env_file (str) – Path of the environment save file.

  • key_path (str) – Path of the key file used to unlock the save file. If None, this defaults to the package’s directory.

Returns

Whether or not env_file and key_path exist, which define whether the env exists.

Return type

bool

Author

Richard Wen <rrwen.dev@gmail.com>

Example

from msdss_base_dotenv.tools import *

# Clear any existing env files
clear_env_file()

# Check if env exists
exists_before = env_exists()

# Create key value env and save it
env = dict(USER='msdss', PASSWORD='msdss123')
save_env_file(env)

# Check if env exists
exists_after = env_exists()

# Display the results
print('exists_before: ' + str(exists_before))
print('exists_after: ' + str(exists_after))

# Clear env files
clear_env_file()
exists_before: False
exists_after: True

load_env_file

msdss_base_dotenv.tools.load_env_file(env_file='./.env', key_path=None, defaults={}, set_env=True, return_dict=False)[source]

Loads a saved environment file from msdss_base_dotenv.tools.save_env_file().

Parameters
  • env_file (str) – Path of the environment save file.

  • key_path (str) – Path of the key file used to unlock the save file. If None, this defaults to the package’s directory.

  • defaults (dict) – Key and value pairs representing default environment values to be loaded. These will replace ones in env if they do not exist or are unset.

  • set_env (bool) – Whether to set the os.environ with the variables in the env_file or not.

  • return_dict (bool) – Whether to return a dictionary of the env variables or not.

Returns

Dictionary of the decrypted key value environment from env_file if return_dict is True.

Return type

dict

Author

Richard Wen <rrwen.dev@gmail.com>

Example

import os

from msdss_base_dotenv.tools import *

# Clear any existing env files
clear_env_file()

# Create key value env and save it with defaults
env = dict(USER='msdss', PASSWORD='msdss123')
defaults = dict(DATABASE='postgres', PASSWORD='already-set')
save_env_file(env, defaults=defaults)

# Load the saved env file
load_env_file()
loaded_env = dict(
    USER=os.environ['USER'],
    PASSWORD=os.environ['PASSWORD'],
    DATABASE=os.environ['DATABASE']
)

# Display the results
print('env: ' + str(env))
print('defaults: ' + str(defaults))
print('loaded_env: ' + str(loaded_env))

# Clear env files
clear_env_file()
env: {'USER': 'msdss', 'PASSWORD': 'msdss123'}
defaults: {'DATABASE': 'postgres', 'PASSWORD': 'already-set'}
loaded_env: {'USER': 'msdss', 'PASSWORD': 'msdss123', 'DATABASE': 'postgres'}

save_env_file

msdss_base_dotenv.tools.save_env_file(env, env_file='./.env', key_path=None, defaults={})[source]

Saves a login file with the connection details.

Parameters
  • env (dict) – Key and value pairs representing environment values to be saved.

  • env_file (str) – Path of the encrypted environment save file. Read/write/execute permissions will be set only for the owner of this file.

  • key_path (str) – Path of the key file used to unlock the save file. If None, this defaults to the package’s directory.

  • defaults (dict) – Key and value pairs representing default environment values to be saved. These will replace ones in env if they do not exist or are unset.

Author

Richard Wen <rrwen.dev@gmail.com>

Example

from msdss_base_dotenv.tools import *

# Clear any existing env files
clear_env_file()

# Create default key value env
env = dict(USER='msdss', PASSWORD='msdss123')

# Save the key value env to an encrypted file
save_env_file(env)

# Clear env files
clear_env_file()

set_env_var

msdss_base_dotenv.tools.set_env_var(name, value, env_file='./.env', key_path=None)[source]

Sets an environmental variable using a file from msdss_base_dotenv.tools.save_env_file().

Parameters
  • name (str) – The name of the environmental variable to be set.

  • value (str) – The value of the environmental variable to be set.

  • env_file (str) – Path of the environment save file.

  • key_path (str) – Path of the key file used to unlock the save file. If None, this defaults to the package’s directory.

Author

Richard Wen <rrwen.dev@gmail.com>

Example

import os

from msdss_base_dotenv.tools import *

# Clear any existing env files
clear_env_file()

# Create key value env and save it
env = dict(USER='msdss')
save_env_file(env)

# Add/set a password var to the saved env
set_env_var('USER', 'msdssnew')
set_env_var('PASSWORD', 'msdss123')

# Load the saved env after the addition
load_env_file()
loaded_env = dict(
    USER=os.environ['USER'],
    PASSWORD=os.environ['PASSWORD']
)

# Display the results
print('env: ' + str(env))
print('loaded_env: ' + str(loaded_env))

# Clear env files
clear_env_file()
env: {'USER': 'msdss'}
loaded_env: {'USER': 'msdssnew', 'PASSWORD': 'msdss123'}