diff --git a/README.md b/README.md index 844245a..c314974 100644 --- a/README.md +++ b/README.md @@ -28,26 +28,30 @@ Run `python-lambda-local -h` to see the help. ``` usage: python-lambda-local [-h] [-l LIBRARY_PATH] [-f HANDLER_FUNCTION] [-t TIMEOUT] [-a ARN_STRING] [-v VERSION_NAME] + [--version] FILE EVENT Run AWS Lambda function written in Python on local machine. positional arguments: - FILE Lambda function file name - EVENT Event data file name. + FILE lambda function file name + EVENT event data file name optional arguments: -h, --help show this help message and exit -l LIBRARY_PATH, --library LIBRARY_PATH - Path of 3rd party libraries. + path of 3rd party libraries -f HANDLER_FUNCTION, --function HANDLER_FUNCTION - Lambda function handler name. Default: "handler". + lambda function handler name, default: "handler" -t TIMEOUT, --timeout TIMEOUT - Seconds until lambda function timeout. Default: 3 + seconds until lambda function timeout, default: 3 -a ARN_STRING, --arn-string ARN_STRING - arn string for function + ARN string for lambda function -v VERSION_NAME, --version-name VERSION_NAME - function version name + lambda function version name + -e ENVIRONMENT_VARIABLES, --environment-variables ENVIRONMENT_VARIABLES + path to flat json file with environment variables + --version print the version of python-lambda-local and exit ``` ### Prepare development directory diff --git a/README.rst b/README.rst index 1b46963..deb21a0 100644 --- a/README.rst +++ b/README.rst @@ -33,26 +33,28 @@ Run ``python-lambda-local -h`` to see the help. usage: python-lambda-local [-h] [-l LIBRARY_PATH] [-f HANDLER_FUNCTION] [-t TIMEOUT] [-a ARN_STRING] [-v VERSION_NAME] + [--version] FILE EVENT Run AWS Lambda function written in Python on local machine. positional arguments: - FILE Lambda function file name - EVENT Event data file name. + FILE lambda function file name + EVENT event data file name optional arguments: -h, --help show this help message and exit -l LIBRARY_PATH, --library LIBRARY_PATH - Path of 3rd party libraries. + path of 3rd party libraries -f HANDLER_FUNCTION, --function HANDLER_FUNCTION - Lambda function handler name. Default: "handler". + lambda function handler name, default: "handler" -t TIMEOUT, --timeout TIMEOUT - Seconds until lambda function timeout. Default: 3 + seconds until lambda function timeout, default: 3 -a ARN_STRING, --arn-string ARN_STRING - arn string for function + ARN string for lambda function -v VERSION_NAME, --version-name VERSION_NAME - function version name + lambda function version name + --version print the version of python-lambda-local and exit Prepare development directory ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/lambda_local/__init__.py b/lambda_local/__init__.py index 9dfd854..f56a061 100644 --- a/lambda_local/__init__.py +++ b/lambda_local/__init__.py @@ -9,10 +9,14 @@ import argparse import sys from multiprocessing import Process +import pkg_resources from .main import run +__version__ = pkg_resources.require("python-lambda-local")[0].version + + def main(): args = parse_args() @@ -27,23 +31,31 @@ def parse_args(): parser = argparse.ArgumentParser(description="Run AWS Lambda function" + " written in Python on local machine.") parser.add_argument("file", metavar="FILE", type=str, - help="Lambda function file name") + help="lambda function file name") parser.add_argument("event", metavar="EVENT", type=str, - help="Event data file name.") + help="event data file name") parser.add_argument("-l", "--library", metavar="LIBRARY_PATH", - type=str, help="Path of 3rd party libraries.") + type=str, help="path of 3rd party libraries") parser.add_argument("-f", "--function", metavar="HANDLER_FUNCTION", type=str, default="handler", - help="Lambda function handler name. \ -Default: \"handler\".") + help="lambda function handler name, \ +default: \"handler\"") parser.add_argument("-t", "--timeout", metavar="TIMEOUT", type=int, default=3, - help="Seconds until lambda function timeout. \ -Default: 3") + help="seconds until lambda function timeout, \ +default: 3") parser.add_argument("-a", "--arn-string", metavar="ARN_STRING", type=str, - default="", help="arn string for function") + default="", help="ARN string for lambda function") parser.add_argument("-v", "--version-name", metavar="VERSION_NAME", - type=str, default="", help="function version name") + type=str, default="", + help="lambda function version name") + parser.add_argument("-e", "--environment-variables", + metavar="ENVIRONMENT_VARIABLES", type=str, + help="path to flat json file with environment variables") + + parser.add_argument("--version", action="version", + version="%(prog)s " + __version__, + help="print the version of python-lambda-local and exit") return parser.parse_args() diff --git a/lambda_local/environment_variables.py b/lambda_local/environment_variables.py new file mode 100644 index 0000000..b518309 --- /dev/null +++ b/lambda_local/environment_variables.py @@ -0,0 +1,29 @@ +import json +import os + + +def set_environment_variables(json_file_path): + """ + Read and set environment variables from a flat json file. + + Bear in mind that env vars set this way and later on read using + `os.getenv` function will be strings since after all env vars are just + that - plain strings. + + Json file example: + ``` + { + "FOO": "bar", + "BAZ": true + } + ``` + + :param json_file_path: path to flat json file + :type json_file_path: str + """ + if json_file_path: + with open(json_file_path) as json_file: + env_vars = json.loads(json_file.read()) + + for env_name, env_value in env_vars.items(): + os.environ[str(env_name)] = str(env_value) diff --git a/lambda_local/main.py b/lambda_local/main.py index 3a46f94..95d4358 100644 --- a/lambda_local/main.py +++ b/lambda_local/main.py @@ -15,6 +15,7 @@ from . import event from . import context +from .environment_variables import set_environment_variables from .timeout import time_limit from .timeout import TimeoutException @@ -31,6 +32,9 @@ def run(args): + # set env vars if path to json file was given + set_environment_variables(args.environment_variables) + e = event.read_event(args.event) c = context.Context(args.timeout, args.arn_string, args.version_name) if args.library is not None: diff --git a/tests/environment_variables.json b/tests/environment_variables.json new file mode 100644 index 0000000..b9b81db --- /dev/null +++ b/tests/environment_variables.json @@ -0,0 +1,5 @@ +{ + "STR_KEY": "foo", + "INT_KEY": 100, + "BOOL_KEY": false +} diff --git a/tests/test_environment_variables.py b/tests/test_environment_variables.py new file mode 100644 index 0000000..6582779 --- /dev/null +++ b/tests/test_environment_variables.py @@ -0,0 +1,15 @@ +import os + +from lambda_local.environment_variables import set_environment_variables + + +def test_set_environment_variables(): + os.getenv('STR_KEY') is None + os.getenv('INT_KEY') is None + os.getenv('BOOL_KEY') is None + + set_environment_variables('tests/environment_variables.json') + + assert os.getenv('STR_KEY') == 'foo' + assert os.getenv('INT_KEY') == '100' + assert os.getenv('BOOL_KEY') == 'False'