Skip to main content

A easy why to mock your production environment variables during development in your project

Project description

Varenv

current :green_heart: license python

A simple way to mock your environment variables during development.

Just add'em to the varenv.conf.json and have fun =). The varenv package will not overwrite any already existente environment variables.

This library was designed to be inbuilt in pojects that, when going to production, will consume environment variable, very commonly used in application that'll run in docker containers. So, they need something to mock these variable under development but that won't get in the way in production.


Basic Usage

Create a file called varenv.conf.json at your project's root path like this:

{
  "SRPC_SERVER": "127.0.0.1",
  "SRPC_SERVER_PORT": "2727",
  "ANY_OTHER_VARIABLE_I_DESIRE": 567865
}

It can also be YAML file called varenv.conf.yml or varenv.conf.yaml. The equivalent to the above file being:

SRPC_SERVER: 127.0.0.1
SRPC_SERVER_PORT: '2727'
ANY_OTHER_VARIABLE_I_DESIRE: 567865

now use it like this in your program:

import varenv.varenv as varenv

my_server_ip = varenv.get_env("SRPC_SERVER")
my_server_port = varenv.get_env("SRPC_SERVER_PORT")

# after a while, for some reason, something chaged your enviroment variables values
# then refresh it
varenv.refresh()
new_server_port = varenv.get_env("SRPC_SERVER_PORT")

If you want to change the location of the virenv.conf.json file, you can define a environment variable called VARENV_CONF_FILE_PATH to any path you desire.

You can do that in a variaty of ways, here is two exemples:

by python:

import os
os.environ['VARENV_CONF_FILE_PATH'] = '/folder/my_path/virenv.conf.json'
import varenv

by your .bashrc file:

VARENV_CONF_FILE_PATH=/folder/my_path/virenv.conf.json

by bash when calling your python program:

VARENV_CONF_FILE_PATH=/folder/my_path/virenv.conf.json python3 my_program.py

Using Variales with Alias

In the version 1.0.6 a new feature was added thinking about the following user case: considering the existense of 3 clusters environments in a company's cloud system, let's say: development, production and homolog; and a CI/CD server like GitLab's* one have a look is also being used. It may be needed to use the same variable with different values across all these clusters, this can lead to a problem when configuring that on GitLab's enviroment variables.

Exemple: Supposing the need to define a database password. That's gonna have different values on the development, production and homolog clusters enviroments, also it's gotta a value for the developer to use in his personal machine. To approach this question, define the varenv.conf.yml file like this:

DATA_BASE_ENDPOINT: localhost
DATA_BASE_PORT: '5432'
DATA_BASE_PASSWORD:
    alias:
        - DATA_BASE_PASSWORD_PROD
        - DATA_BASE_PASSWORD_HOMOLOG
        - DATA_BASE_PASSWORD_DEV
    value: p@ssword_f0r_the_developer
ANY_OTHER_VARIABLE_I_DESIRE: 567865

In the python code call the variable DATA_BASE_PASSWORD like this:

import varenv.varenv as varenv


data_base_endpoint = varenv.get_env("DATA_BASE_ENDPOINT")
data_base_port = varenv.get_env("DATA_BASE_PORT")
data_base_password = varenv.get_env("DATA_BASE_PASSWORD")

# NOTE: the value defined within the code is gonna be the dict's key that defines the alias in the config file.

In the CI/CD server define the variables: DATA_BASE_PASSWORD_PROD, DATA_BASE_PASSWORD_HOMOLOG and DATA_BASE_PASSWORD_DEV, passing each one to it's respective cluster.


How does it work?

When the Varenv get started and the variable with an alias is found, the module will search for the presence of every value in the alias array in the enviroment, stoping at the first match case found.

Note: The variables are gonna be searched at the same order as presented in the alias array in the file, and be resented to the programmer as in the object key. So if, for whatever reason, in the above exemple DATA_BASE_PASSWORD_PROD and DATA_BASE_PASSWORD_DEV get defined in the same enviroment, only the value of the first one will be taken into consideration and will be available in code as DATA_BASE_PASSWORD.



Author's Note

create by me, davincif, this project was first though to fulfill the needs of a another professional project I've made. But it sounds so potentially useful the the community that I decided to open this package here freely distributed.

I actively use this project since I created it back in 2018, and it happens to be pretty useful. Wich is surprising given that I thought it would be a one project thing. Thus I decided keep maintaining it, basically by solving bugs and adding new features required by other projects.

So let me know if you want to help out, or if you need any formal concentiment to use this software, despite the fact that it's already free and open by terms of a very permissive license as zlib.


See also
  • Dependency Manager project: A simple way of managing pip dependencies, separating in dev and prod, and tracking them.
  • SimplestRPC project: A simple RPC for python - study project.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

varenv-1.0.6.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

varenv-1.0.6-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file varenv-1.0.6.tar.gz.

File metadata

  • Download URL: varenv-1.0.6.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.8.0

File hashes

Hashes for varenv-1.0.6.tar.gz
Algorithm Hash digest
SHA256 a0d64ceef206fff6e73e1a92451bd98b0ff1839cb4c29bf4efe90e20db380633
MD5 1953ed0e2856038e9eb533cb5d862c81
BLAKE2b-256 5bdc986e0d09bd46090c13110c5c02f0b155100541e41bdd5ef8b6b15c96ecd8

See more details on using hashes here.

File details

Details for the file varenv-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: varenv-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.8.0

File hashes

Hashes for varenv-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 f6db9a43ae83219e01caa15742e62e89e2bcaf8a69f52c2b24244a730145228b
MD5 dc8c66521e54c856179f9c8c5f9de44d
BLAKE2b-256 7ff93f36f459ffa0de413bd6a567061340811c7da3893a20926e33f8601ab860

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page