Skip to content

Commit

Permalink
Merge pull request #27 from HDE/master
Browse files Browse the repository at this point in the history
Release v0.1.5
  • Loading branch information
yxd-hde authored Dec 14, 2017
2 parents 22cefd0 + 2519478 commit 4f0a3e2
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 23 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
30 changes: 21 additions & 9 deletions lambda_local/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()

Expand Down
29 changes: 29 additions & 0 deletions lambda_local/environment_variables.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 4 additions & 0 deletions lambda_local/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions tests/environment_variables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"STR_KEY": "foo",
"INT_KEY": 100,
"BOOL_KEY": false
}
15 changes: 15 additions & 0 deletions tests/test_environment_variables.py
Original file line number Diff line number Diff line change
@@ -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'

0 comments on commit 4f0a3e2

Please sign in to comment.