Package: checkenv
checkenvA modern best-practice is tostore your application's configuration in environmental variables.  This allows you to keep all config data outside of your repository, and store it in a standard, system-agnostic location.  Modern build/deploy/development tools make it easier to manage these variables per-host, but they're still often undocumented, and can lead to bugs when missing.This module lets you define all the environmental variables your application relies on in anenv.jsonfile.  It then provides a method to check for these variables at application launch, and print a help screen if any are missing.Inspired from the popular npm package,checkenv.UsageFirst, define a JSON file calledenv.jsonin your project root (see below for the specific structure). Next, install the library usingpipconnected to thePyPiindex:pipinstallcheckenvThen, add the following line to the top of your project's entry file:fromcheckenvimportcheckcheck()By default,checkenvwill print a pretty error message and callsys.exit()if any required variables are missing. It will also print an error message if optional variables are missing, but will not exit the process.You can specify a filename other thanenv.jsonby setting the optional parameterfilename.  The library will attempt to load this file from the root path of your project.  You can also specify an absolute file path.If you would like to handle errors yourself,checktakes an optionalraise_exceptionargument which causes it to raise exceptions instead of exiting the process.fromcheckenvimportchecktry:check(raise_exception=True)exceptExceptionase:# do something with the error 'e' because the process will not exitAn exception can be one of three classes of Exceptions:checkenv.exceptions.CheckEnvException- thrown if any mandatory environment variables are missing; containsmissingandoptionalproperties that contain a list of environment variable namesjsonschema.exceptions import ValidationError- thrown if the input JSON files is invalidIOError- thrown if the input JSON file cannot be foundYou can also silence any output tostdoutby setting the optional parameterno_output=True.  It is recommended to use this in conjunction withraise_exception=Trueand handling the error yourself; otherwise, your application can fail silently because you do not realize that something is wrong with your environment variables.ConfigurationYour JSON file should define the environmental variables as keys, and either a boolean (required) as the value, or a configuration object with any of the options below.JSON{"ENVIRONMENT":{"description":"This defines the current environment"},"PORT":{"description":"This is the port the API server will run on","default":3000},"PYTHON_PATH":true,"DEBUG":{"required":false,"description":"If set, enables additional debug messages"}}Object Propertiesrequired- Defines whether or not this variable is required. By default, all variables are required, so you must explicitly set them to optional by setting this tofalse.description- Describes the variable and how it should be used. Useful for new developers setting up the project, and is printed in the error output if present.default- Defines the default value to use if variable is unset. Implicitly setsrequiredtofalseregardless of any specified value.Change Log1.2.0Added ability forcheck()to throw exceptions instead of killing the running process withraise_exception=TrueAdded ability to silence all output tostdoutwithno_output=TrueUpdated documentation with thefilenameparameter feature that allows you to specify an input JSON file with a different name thanenv.jsonUpdated README.md with usage instructions for these new featuresIncreased code coverage to 95%+1.1.0Expanded supported Python interpreter versions -checkenvnow supports Python versions 2.7, 3.5, 3.6, 3.7, and 3.8.Refactored the code with classes - although this does not add additional functionality, the code is cleaner, easier to understand, and better documented for future improvementsAdded tests withpytestandtoxEvery pushed branch undergoes automated testing with CircleCIStarted tracking code coverage percentage and currently hovering around ~50% (to be improved)1.0.0Initial release