source: sasmodels/sasmodels/custom/__init__.py @ 0f48f1e

core_shell_microgelscostrafo411magnetic_modelticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 0f48f1e was 0f48f1e, checked in by Paul Kienzle <pkienzle@…>, 7 years ago

remove any associated .pyc file before loading a module from path; this may fix the config load problem in sasview

  • Property mode set to 100644
File size: 1.7 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"""
10from __future__ import division, print_function
11
12import sys
13import os
14from os.path import basename, splitext
15
16try:
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
25except 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        if path.endswith(".py") and os.path.exists(path) and os.path.exists(path+"c"):
34            # remove automatic pyc file before loading a py file
35            os.unlink(path+"c")
36        module = imp.load_source(fullname, os.path.expanduser(path))
37        return module
38
39def load_custom_kernel_module(path):
40    """load SAS kernel from *path* as *sasmodels.custom.modelname*"""
41    # Pull off the last .ext if it exists; there may be others
42    name = basename(splitext(path)[0])
43    # Placing the model in the 'sasmodels.custom' name space.
44    kernel_module = load_module_from_path('sasmodels.custom.'+name,
45                                          os.path.expanduser(path))
46    return kernel_module
Note: See TracBrowser for help on using the repository browser.