source: sasview/src/sas/sasgui/guiframe/customdir.py @ d66dbcc

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 d66dbcc was 914ba0a, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

merge with master

  • Property mode set to 100644
File size: 1.8 KB
Line 
1# Setup and find Custom config dir
2import os.path
3import logging
4import shutil
5import imp
6
7from sas.sasgui import get_custom_config_path
8
9logger = logging.getLogger(__name__)
10
11_config_cache = None
12def setup_custom_config():
13    """
14    Setup the custom config dir and cat file
15    """
16    global _config_cache
17    if not _config_cache:
18        _config_cache = _setup_custom_config()
19    return _config_cache
20
21
22def _setup_custom_config():
23    path = get_custom_config_path()
24    try:
25        if not os.path.isfile(path):
26            # if the custom config file does not exist, copy the default from
27            # the app dir
28            shutil.copyfile(os.path.join(get_app_dir(), "custom_config.py"),
29                            path)
30        #Adding SAS_OPENCL if it doesn't exist in the config file
31        # - to support backcompability
32        if not "SAS_OPENCL" in open(path).read():
33            open(config_file, "a+").write("SAS_OPENCL = \"None\"\n")
34    except Exception:
35        #import traceback; logging.error(traceback.format_exc())
36        logger.error("Could not copy default custom config.")
37
38    custom_config = _load_config(path)
39    return custom_config
40
41def _load_config(path):
42    if os.path.exists(path):
43        try:
44            fObj = None
45            fObj, config_path, descr = imp.find_module('custom_config', [os.path.dirname(path)])
46            custom_config = imp.load_module('custom_config', fObj, config_path, descr)
47            logger.info("GuiManager loaded %s" % config_path)
48            return custom_config
49        except Exception:
50            logger.error("Error loading %s: %s" % (path, sys.exc_value))
51        finally:
52            if fObj is not None:
53                fObj.close()
54    from sas.sasview import custom_config
55    logging.info("GuiManager custom_config defaults to sas.sasview.custom_config")
56    return custom_config
Note: See TracBrowser for help on using the repository browser.