source: sasview/src/sas/sasgui/__init__.py @ 67d8b1b

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalcmagnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 67d8b1b was c6bdb3b, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

more fiddling with configs

  • Property mode set to 100644
File size: 2.4 KB
RevLine 
[3aa2f3c]1import sys
[efe730d]2import os
[3aa2f3c]3from os.path import exists, expanduser, dirname, realpath, join as joinpath
4
[914ba0a]5
[3aa2f3c]6def dirn(path, n):
7    path = realpath(path)
8    for _ in range(n):
9        path = dirname(path)
10    return path
[efe730d]11
12# Set up config directories
13def make_user_folder():
[3aa2f3c]14    path = joinpath(expanduser("~"),'.sasview')
15    if not exists(path):
[efe730d]16        os.mkdir(path)
17    return path
18
[3aa2f3c]19
[efe730d]20def find_app_folder():
[3aa2f3c]21    # We are starting out with the following info:
22    #     __file__ = .../sas/sasgui/__init__.pyc
23    # Check if the sister path .../sas/sasview exists, and use it as the
24    # app directory.  This will only be the case if the app is not frozen.
25    path = joinpath(dirn(__file__, 2), 'sasview')
26    if exists(path):
[efe730d]27        return path
28
[3aa2f3c]29    # If we are running frozen, then root is a parent directory
30    if sys.platform == 'darwin':
31        # Here is the path to the file on the mac:
32        #     .../Sasview.app/Contents/Resources/lib/python2.7/site-packages.zip/sas/sasgui/__init__.pyc
33        # We want the path to the Resources directory.
34        path = dirn(__file__, 6)
35    elif os.name == 'nt':
36        # Here is the path to the file on windows:
37        #     ../Sasview/library.zip/sas/sasgui/__init__.pyc
38        # We want the path to the Sasview directory.
39        path = dirn(__file__, 4)
40    else:
41        raise RuntimeError("Couldn't find the app directory")
[efe730d]42    return path
43
44USER_FOLDER = make_user_folder()
45APP_FOLDER = find_app_folder()
46
47
48def get_app_dir():
49    return APP_FOLDER
50
51def get_user_dir():
52    return USER_FOLDER
53
[914ba0a]54def get_custom_config_path():
55    dirname = os.path.join(get_user_dir(), 'config')
56    # If the directory doesn't exist, create it
57    if not os.path.exists(dirname):
58        os.makedirs(dirname)
59    path = os.path.join(dirname, "custom_config.py")
60    return path
61
[efe730d]62_config_cache = None
63def get_local_config():
64    global _config_cache
65    if not _config_cache:
66        _config_cache = _load_config()
67    return _config_cache
68
69def _load_config():
70    import os
71    import sys
72    import logging
[c6bdb3b]73    from sasmodels.custom import load_module_from_path
[efe730d]74
[914ba0a]75    logger = logging.getLogger(__name__)
[efe730d]76    dirname = get_app_dir()
77    filename = 'local_config.py'
78    path = os.path.join(dirname, filename)
[c6bdb3b]79    try:
80        module = load_module_from_path('sas.sasgui.local_config', path)
81        logger.info("GuiManager loaded %s", path)
82        return module
83    except Exception as exc:
84        logger.critical("Error loading %s: %s", path, exc)
85        sys.exit()
Note: See TracBrowser for help on using the repository browser.