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

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 5a405bd was c6bdb3b, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

more fiddling with configs

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