source: sasview/src/sas/_config.py @ 47be5a1

magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249
Last change on this file since 47be5a1 was 47be5a1, checked in by Paul Kienzle <pkienzle@…>, 5 years ago

redo handling of cust config failures

  • Property mode set to 100644
File size: 3.9 KB
Line 
1# Setup and find Custom config dir
2from __future__ import print_function
3
4import sys
5import os
6from os.path import exists, expanduser, dirname, realpath, join as joinpath
7import logging
8import shutil
9
10from sasmodels.custom import load_module_from_path
11
12logger = logging.getLogger(__name__)
13
14def dirn(path, n):
15    """
16    Return the directory n up from the current path
17    """
18    path = realpath(path)
19    for _ in range(n):
20        path = dirname(path)
21    return path
22
23def find_app_dir():
24    """
25    Locate the parent directory of the sasview resources.  For the normal
26    application this will be the directory containing sasview.py.  For the
27    frozen application this will be the path where the resources are installed.
28    """
29    # We are starting out with the following info:
30    #     __file__ = .../sas/__init__.pyc
31    # Check if the path .../sas/sasview exists, and use it as the
32    # app directory.  This will only be the case if the app is not frozen.
33    path = joinpath(dirname(__file__), 'sasview')
34    if exists(path):
35        return path
36
37    # If we are running frozen, then root is a parent directory
38    if sys.platform == 'darwin':
39        # Here is the path to the file on the mac:
40        #     .../Sasview.app/Contents/Resources/lib/python2.7/site-packages.zip/sas/__init__.pyc
41        # We want the path to the Resources directory.
42        path = dirn(__file__, 5)
43    elif os.name == 'nt':
44        # Here is the path to the file on windows:
45        #     ../Sasview/library.zip/sas/__init__.pyc
46        # We want the path to the Sasview directory.
47        path = dirn(__file__, 3)
48    else:
49        raise RuntimeError("Couldn't find the app directory")
50    return path
51
52def make_user_dir():
53    """
54    Create the user directory ~/.sasview if it doesn't already exist.
55    """
56    path = joinpath(expanduser("~"),'.sasview')
57    if not exists(path):
58        os.mkdir(path)
59    return path
60
61def load_local_config(app_dir):
62    logger = logging.getLogger(__name__)
63    filename = 'local_config.py'
64    path = os.path.join(app_dir, filename)
65    try:
66        module = load_module_from_path('sas.local_config', path)
67        logger.info("GuiManager loaded %s", path)
68        return module
69    except Exception as exc:
70        logger.critical("Error loading %s: %s", path, exc)
71        sys.exit()
72
73def make_custom_config_path(user_dir):
74    """
75    The location of the cusstom config file.
76
77    Returns ~/.sasview/config/custom_config.py
78    """
79    dirname = os.path.join(user_dir, 'config')
80    # If the directory doesn't exist, create it
81    if not os.path.exists(dirname):
82        os.makedirs(dirname)
83    path = os.path.join(dirname, "custom_config.py")
84    return path
85
86def setup_custom_config(app_dir, user_dir):
87    path = make_custom_config_path(user_dir)
88    if not os.path.isfile(path):
89        try:
90            # if the custom config file does not exist, copy the default from
91            # the app dir
92            shutil.copyfile(os.path.join(app_dir, "custom_config.py"), path)
93        except Exception:
94            logger.error("Could not copy default custom config.")
95
96    custom_config = load_custom_config(path)
97
98    #Adding SAS_OPENCL if it doesn't exist in the config file
99    # - to support backcompability
100    if not hasattr(custom_config, "SAS_OPENCL"):
101        custom_config.SAS_OPENCL = None
102        try:
103            open(config_file, "a+").write("SAS_OPENCL = \"None\"\n")
104        except Exception:
105            logger.error("Could not update custom config with SAS_OPENCL.")
106
107    return custom_config
108
109def load_custom_config(path):
110    if os.path.exists(path):
111        try:
112            module = load_module_from_path('sas.custom_config', path)
113            logger.info("GuiManager loaded %s", path)
114            return module
115        except Exception as exc:
116            logger.error("Error loading %s: %s", path, exc)
117
118    from sas.sasview import custom_config
119    logger.info("GuiManager custom_config defaults to sas.sasview.custom_config")
120    return custom_config
Note: See TracBrowser for help on using the repository browser.