1 | """ |
---|
2 | Custom Models |
---|
3 | ------------- |
---|
4 | |
---|
5 | This is a place holder for the custom models namespace. When models are |
---|
6 | loaded from a file by :func:`generate.load_kernel_module` they are loaded |
---|
7 | as if they exist in *sasmodels.custom*. This package needs to exist for this |
---|
8 | to occur without error. |
---|
9 | """ |
---|
10 | from __future__ import division, print_function |
---|
11 | |
---|
12 | import sys |
---|
13 | import os |
---|
14 | from os.path import basename, splitext |
---|
15 | |
---|
16 | try: |
---|
17 | # Python 3.5 and up |
---|
18 | from importlib.util import spec_from_file_location, module_from_spec # type: ignore |
---|
19 | def load_module_from_path(fullname, path): |
---|
20 | """load module from *path* as *fullname*""" |
---|
21 | spec = spec_from_file_location(fullname, os.path.expanduser(path)) |
---|
22 | module = module_from_spec(spec) |
---|
23 | spec.loader.exec_module(module) |
---|
24 | return module |
---|
25 | except ImportError: |
---|
26 | # CRUFT: python 2 |
---|
27 | import imp |
---|
28 | def load_module_from_path(fullname, path): |
---|
29 | """load module from *path* as *fullname*""" |
---|
30 | # Clear out old definitions, if any |
---|
31 | if fullname in sys.modules: |
---|
32 | del sys.modules[fullname] |
---|
33 | module = imp.load_source(fullname, os.path.expanduser(path)) |
---|
34 | #os.unlink(path+"c") # remove the automatic pyc file |
---|
35 | return module |
---|
36 | |
---|
37 | def load_custom_kernel_module(path): |
---|
38 | """load SAS kernel from *path* as *sasmodels.custom.modelname*""" |
---|
39 | # Pull off the last .ext if it exists; there may be others |
---|
40 | name = basename(splitext(path)[0]) |
---|
41 | # Placing the model in the 'sasmodels.custom' name space. |
---|
42 | kernel_module = load_module_from_path('sasmodels.custom.'+name, |
---|
43 | os.path.expanduser(path)) |
---|
44 | return kernel_module |
---|