Skip to content

Commit 4f0a3e2

Browse files
authored
Merge pull request #27 from HDE/master
Release v0.1.5
2 parents 22cefd0 + 2519478 commit 4f0a3e2

File tree

7 files changed

+94
-23
lines changed

7 files changed

+94
-23
lines changed

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,30 @@ Run `python-lambda-local -h` to see the help.
2828
```
2929
usage: python-lambda-local [-h] [-l LIBRARY_PATH] [-f HANDLER_FUNCTION]
3030
[-t TIMEOUT] [-a ARN_STRING] [-v VERSION_NAME]
31+
[--version]
3132
FILE EVENT
3233
3334
Run AWS Lambda function written in Python on local machine.
3435
3536
positional arguments:
36-
FILE Lambda function file name
37-
EVENT Event data file name.
37+
FILE lambda function file name
38+
EVENT event data file name
3839
3940
optional arguments:
4041
-h, --help show this help message and exit
4142
-l LIBRARY_PATH, --library LIBRARY_PATH
42-
Path of 3rd party libraries.
43+
path of 3rd party libraries
4344
-f HANDLER_FUNCTION, --function HANDLER_FUNCTION
44-
Lambda function handler name. Default: "handler".
45+
lambda function handler name, default: "handler"
4546
-t TIMEOUT, --timeout TIMEOUT
46-
Seconds until lambda function timeout. Default: 3
47+
seconds until lambda function timeout, default: 3
4748
-a ARN_STRING, --arn-string ARN_STRING
48-
arn string for function
49+
ARN string for lambda function
4950
-v VERSION_NAME, --version-name VERSION_NAME
50-
function version name
51+
lambda function version name
52+
-e ENVIRONMENT_VARIABLES, --environment-variables ENVIRONMENT_VARIABLES
53+
path to flat json file with environment variables
54+
--version print the version of python-lambda-local and exit
5155
```
5256

5357
### Prepare development directory

README.rst

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,28 @@ Run ``python-lambda-local -h`` to see the help.
3333

3434
usage: python-lambda-local [-h] [-l LIBRARY_PATH] [-f HANDLER_FUNCTION]
3535
[-t TIMEOUT] [-a ARN_STRING] [-v VERSION_NAME]
36+
[--version]
3637
FILE EVENT
3738

3839
Run AWS Lambda function written in Python on local machine.
3940

4041
positional arguments:
41-
FILE Lambda function file name
42-
EVENT Event data file name.
42+
FILE lambda function file name
43+
EVENT event data file name
4344

4445
optional arguments:
4546
-h, --help show this help message and exit
4647
-l LIBRARY_PATH, --library LIBRARY_PATH
47-
Path of 3rd party libraries.
48+
path of 3rd party libraries
4849
-f HANDLER_FUNCTION, --function HANDLER_FUNCTION
49-
Lambda function handler name. Default: "handler".
50+
lambda function handler name, default: "handler"
5051
-t TIMEOUT, --timeout TIMEOUT
51-
Seconds until lambda function timeout. Default: 3
52+
seconds until lambda function timeout, default: 3
5253
-a ARN_STRING, --arn-string ARN_STRING
53-
arn string for function
54+
ARN string for lambda function
5455
-v VERSION_NAME, --version-name VERSION_NAME
55-
function version name
56+
lambda function version name
57+
--version print the version of python-lambda-local and exit
5658

5759
Prepare development directory
5860
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

lambda_local/__init__.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@
99
import argparse
1010
import sys
1111
from multiprocessing import Process
12+
import pkg_resources
1213

1314
from .main import run
1415

1516

17+
__version__ = pkg_resources.require("python-lambda-local")[0].version
18+
19+
1620
def main():
1721
args = parse_args()
1822

@@ -27,23 +31,31 @@ def parse_args():
2731
parser = argparse.ArgumentParser(description="Run AWS Lambda function" +
2832
" written in Python on local machine.")
2933
parser.add_argument("file", metavar="FILE", type=str,
30-
help="Lambda function file name")
34+
help="lambda function file name")
3135
parser.add_argument("event", metavar="EVENT", type=str,
32-
help="Event data file name.")
36+
help="event data file name")
3337
parser.add_argument("-l", "--library", metavar="LIBRARY_PATH",
34-
type=str, help="Path of 3rd party libraries.")
38+
type=str, help="path of 3rd party libraries")
3539
parser.add_argument("-f", "--function", metavar="HANDLER_FUNCTION",
3640
type=str, default="handler",
37-
help="Lambda function handler name. \
38-
Default: \"handler\".")
41+
help="lambda function handler name, \
42+
default: \"handler\"")
3943
parser.add_argument("-t", "--timeout", metavar="TIMEOUT", type=int,
4044
default=3,
41-
help="Seconds until lambda function timeout. \
42-
Default: 3")
45+
help="seconds until lambda function timeout, \
46+
default: 3")
4347
parser.add_argument("-a", "--arn-string", metavar="ARN_STRING", type=str,
44-
default="", help="arn string for function")
48+
default="", help="ARN string for lambda function")
4549
parser.add_argument("-v", "--version-name", metavar="VERSION_NAME",
46-
type=str, default="", help="function version name")
50+
type=str, default="",
51+
help="lambda function version name")
52+
parser.add_argument("-e", "--environment-variables",
53+
metavar="ENVIRONMENT_VARIABLES", type=str,
54+
help="path to flat json file with environment variables")
55+
56+
parser.add_argument("--version", action="version",
57+
version="%(prog)s " + __version__,
58+
help="print the version of python-lambda-local and exit")
4759

4860
return parser.parse_args()
4961

lambda_local/environment_variables.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import json
2+
import os
3+
4+
5+
def set_environment_variables(json_file_path):
6+
"""
7+
Read and set environment variables from a flat json file.
8+
9+
Bear in mind that env vars set this way and later on read using
10+
`os.getenv` function will be strings since after all env vars are just
11+
that - plain strings.
12+
13+
Json file example:
14+
```
15+
{
16+
"FOO": "bar",
17+
"BAZ": true
18+
}
19+
```
20+
21+
:param json_file_path: path to flat json file
22+
:type json_file_path: str
23+
"""
24+
if json_file_path:
25+
with open(json_file_path) as json_file:
26+
env_vars = json.loads(json_file.read())
27+
28+
for env_name, env_value in env_vars.items():
29+
os.environ[str(env_name)] = str(env_value)

lambda_local/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from . import event
1717
from . import context
18+
from .environment_variables import set_environment_variables
1819
from .timeout import time_limit
1920
from .timeout import TimeoutException
2021

@@ -31,6 +32,9 @@
3132

3233

3334
def run(args):
35+
# set env vars if path to json file was given
36+
set_environment_variables(args.environment_variables)
37+
3438
e = event.read_event(args.event)
3539
c = context.Context(args.timeout, args.arn_string, args.version_name)
3640
if args.library is not None:

tests/environment_variables.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"STR_KEY": "foo",
3+
"INT_KEY": 100,
4+
"BOOL_KEY": false
5+
}

tests/test_environment_variables.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import os
2+
3+
from lambda_local.environment_variables import set_environment_variables
4+
5+
6+
def test_set_environment_variables():
7+
os.getenv('STR_KEY') is None
8+
os.getenv('INT_KEY') is None
9+
os.getenv('BOOL_KEY') is None
10+
11+
set_environment_variables('tests/environment_variables.json')
12+
13+
assert os.getenv('STR_KEY') == 'foo'
14+
assert os.getenv('INT_KEY') == '100'
15+
assert os.getenv('BOOL_KEY') == 'False'

0 commit comments

Comments
 (0)