source: sasview/src/sas/_config.py @ 104613f

Last change on this file since 104613f was 93b34cc, checked in by Paul Kienzle <pkienzle@…>, 6 years ago

add SAS_OPENCL default to the correct path

  • Property mode set to 100644
File size: 4.0 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    #logger.info("custom config path %s", path)
89    if not os.path.isfile(path):
90        try:
91            # if the custom config file does not exist, copy the default from
92            # the app dir
93            shutil.copyfile(os.path.join(app_dir, "custom_config.py"), path)
94        except Exception:
95            logger.error("Could not copy default custom config.")
96
97    custom_config = load_custom_config(path)
98
99    #Adding SAS_OPENCL if it doesn't exist in the config file
100    # - to support backcompability
101    if not hasattr(custom_config, "SAS_OPENCL"):
102        custom_config.SAS_OPENCL = None
103        try:
104            open(path, "a+").write("SAS_OPENCL = \"None\"\n")
105        except Exception:
106            logger.error("Could not update custom config with SAS_OPENCL.")
107
108    return custom_config
109
110def load_custom_config(path):
111    if os.path.exists(path):
112        try:
113            module = load_module_from_path('sas.custom_config', path)
114            logger.info("GuiManager loaded %s", path)
115            return module
116        except Exception as exc:
117            logger.error("Error loading %s: %s", path, exc)
118
119    from sas.sasview import custom_config
120    logger.info("GuiManager custom_config defaults to sas.sasview.custom_config")
121    return custom_config
Note: See TracBrowser for help on using the repository browser.