Skip to content

Commit

Permalink
Handle / in lambda handlers (#79)
Browse files Browse the repository at this point in the history
* handle / in lambda handlers and replace them with .

* handle names with `/`

* try to fix lint?

* add more tests and move function to its own file

* fix lint

* format again

* format tests
  • Loading branch information
Czechh authored Aug 26, 2020
1 parent 11bb3f2 commit a381b69
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
5 changes: 4 additions & 1 deletion datadog_lambda/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import os
from datadog_lambda.wrapper import datadog_lambda_wrapper
from datadog_lambda.module_name import modify_module_name


class HandlerError(Exception):
Expand All @@ -23,6 +24,8 @@ class HandlerError(Exception):
if len(parts) != 2:
raise HandlerError("Value %s for DD_LAMBDA_HANDLER has invalid format." % path)


(mod_name, handler_name) = parts
handler_module = import_module(mod_name)
modified_mod_name = modify_module_name(mod_name)
handler_module = import_module(modified_mod_name)
handler = datadog_lambda_wrapper(getattr(handler_module, handler_name))
4 changes: 4 additions & 0 deletions datadog_lambda/module_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def modify_module_name(module_name):
"""Returns a valid modified module to get imported
"""
return ".".join(module_name.split("/"))
21 changes: 21 additions & 0 deletions tests/test_module_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import sys
import os
import unittest

try:
from unittest.mock import patch, call, MagicMock
except ImportError:
from mock import patch, call, MagicMock

from datadog_lambda.module_name import modify_module_name


class TestModifyModuleName(unittest.TestCase):
def test_modify_module_name(self):
self.assertEqual(
modify_module_name("lambda/handler/name.bar"), "lambda.handler.name.bar"
)
self.assertEqual(
modify_module_name("lambda.handler/name.biz"), "lambda.handler.name.biz"
)
self.assertEqual(modify_module_name("foo.handler"), "foo.handler")

0 comments on commit a381b69

Please sign in to comment.