[cd0a808] | 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 | """ |
---|
[3c852d4] | 10 | from __future__ import division, print_function |
---|
[cd0a808] | 11 | |
---|
[3c852d4] | 12 | import sys |
---|
[cd0a808] | 13 | import os |
---|
| 14 | from os.path import basename, splitext |
---|
| 15 | |
---|
| 16 | try: |
---|
| 17 | # Python 3.5 and up |
---|
[7ae2b7f] | 18 | from importlib.util import spec_from_file_location, module_from_spec # type: ignore |
---|
[cd0a808] | 19 | def load_module_from_path(fullname, path): |
---|
[40a87fa] | 20 | """load module from *path* as *fullname*""" |
---|
[2a0c7a6] | 21 | spec = spec_from_file_location(fullname, os.path.expanduser(path)) |
---|
[cd0a808] | 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): |
---|
[40a87fa] | 29 | """load module from *path* as *fullname*""" |
---|
[3c852d4] | 30 | # Clear out old definitions, if any |
---|
| 31 | if fullname in sys.modules: |
---|
| 32 | del sys.modules[fullname] |
---|
[2a0c7a6] | 33 | module = imp.load_source(fullname, os.path.expanduser(path)) |
---|
[4bfbca2] | 34 | #os.unlink(path+"c") # remove the automatic pyc file |
---|
[cd0a808] | 35 | return module |
---|
| 36 | |
---|
| 37 | def load_custom_kernel_module(path): |
---|
[40a87fa] | 38 | """load SAS kernel from *path* as *sasmodels.custom.modelname*""" |
---|
[cd0a808] | 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. |
---|
[2a0c7a6] | 42 | kernel_module = load_module_from_path('sasmodels.custom.'+name, |
---|
| 43 | os.path.expanduser(path)) |
---|
[cd0a808] | 44 | return kernel_module |
---|