|
12 | 12 | # License for the specific language governing permissions and limitations |
13 | 13 | # under the License. |
14 | 14 |
|
| 15 | +import os |
15 | 16 |
|
16 | | -class Function(object): |
| 17 | +from fdk import constants |
17 | 18 |
|
18 | | - def __init__(self, func_module_path, entrypoint="handler"): |
19 | | - self.spec, self.mod = self.import_from_source(func_module_path) |
| 19 | + |
| 20 | +def get_delayed_module_init_class(): |
| 21 | + if constants.is_py37(): |
| 22 | + return Python37DelayedImport |
| 23 | + else: |
| 24 | + return Python35plusDelayedImport |
| 25 | + |
| 26 | + |
| 27 | +class PythonDelayedImportAbstraction(object): |
| 28 | + |
| 29 | + def __init__(self, func_module_path): |
| 30 | + self._mod_path = func_module_path |
20 | 31 | self._executed = False |
21 | | - self._entrypoint = entrypoint |
22 | 32 |
|
23 | | - @staticmethod |
24 | | - def import_from_source(func_module_path): |
| 33 | + @property |
| 34 | + def executed(self): |
| 35 | + return self._executed |
| 36 | + |
| 37 | + @executed.setter |
| 38 | + def executed(self, exec_flag): |
| 39 | + self._executed = exec_flag |
| 40 | + |
| 41 | + def get_module(self): |
| 42 | + raise Exception("Not implemented") |
| 43 | + |
| 44 | + |
| 45 | +class Python35plusDelayedImport(PythonDelayedImportAbstraction): |
| 46 | + |
| 47 | + def __init__(self, func_module_path): |
| 48 | + self._func_module = None |
| 49 | + super(Python35plusDelayedImport, self).__init__(func_module_path) |
| 50 | + |
| 51 | + def get_module(self): |
| 52 | + if not self.executed: |
| 53 | + import imp |
| 54 | + fname, ext = os.path.splitext( |
| 55 | + os.path.basename(self._mod_path)) |
| 56 | + self._func_module = imp.load_source(fname, self._mod_path) |
| 57 | + self.executed = True |
| 58 | + |
| 59 | + return self._func_module |
| 60 | + |
| 61 | + |
| 62 | +class Python37DelayedImport(PythonDelayedImportAbstraction): |
| 63 | + |
| 64 | + def import_from_source(self): |
25 | 65 | from importlib import util |
26 | 66 | func_module_spec = util.spec_from_file_location( |
27 | | - "func", func_module_path |
| 67 | + "func", self._mod_path |
28 | 68 | ) |
29 | 69 | func_module = util.module_from_spec(func_module_spec) |
30 | | - return func_module_spec, func_module |
| 70 | + self._func_module_spec = func_module_spec |
| 71 | + self._func_module = func_module |
| 72 | + |
| 73 | + def get_module(self): |
| 74 | + if not self.executed: |
| 75 | + self.import_from_source() |
| 76 | + self._func_module_spec.loader.exec_module( |
| 77 | + self._func_module) |
| 78 | + self.executed = True |
| 79 | + |
| 80 | + return self._func_module |
31 | 81 |
|
32 | | - def handler(self): |
33 | | - if self._executed is False: |
34 | | - self.spec.loader.exec_module(self.mod) |
35 | | - self._executed = True |
36 | 82 |
|
37 | | - return getattr(self.mod, self._entrypoint) |
| 83 | +class Function(object): |
| 84 | + |
| 85 | + def __init__(self, func_module_path, entrypoint="handler"): |
| 86 | + dm = get_delayed_module_init_class() |
| 87 | + self._delayed_module_class = dm(func_module_path) |
| 88 | + self._entrypoint = entrypoint |
| 89 | + |
| 90 | + def handler(self): |
| 91 | + mod = self._delayed_module_class.get_module() |
| 92 | + return getattr(mod, self._entrypoint) |
0 commit comments