source: sasmodels/sasmodels/custom/__init__.py @ 2a0c7a6

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 2a0c7a6 was 2a0c7a6, checked in by Paul Kienzle <pkienzle@…>, 5 years ago

allow ~ expansion in model names

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