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

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

move sasview to src/sas/sasview and refactor bundled apps for easier debugging

  • Property mode set to 100644
File size: 2.5 KB
Line 
1# Setup and find Custom config dir
2import sys
3import imp
4import os.path
5import logging
6import shutil
7
8from sas.sasgui.guiframe.CategoryInstaller import CategoryInstaller
9from sas.sasgui import get_user_dir, get_app_dir
10
11def get_custom_config_path():
12    dirname = os.path.join(get_user_dir(), 'config')
13    # If the directory doesn't exist, create it
14    if not os.path.exists(dirname):
15        os.makedirs(dirname)
16    path = os.path.join(dirname, "custom_config.py")
17    return path
18
19
20_config_cache = None
21def setup_custom_config():
22    """
23    Setup the custom config dir and cat file
24    """
25    global _config_cache
26    if not _config_cache:
27        _config_cache = _setup_custom_config()
28    return _config_cache
29
30
31def _setup_custom_config():
32    path = get_custom_config_path()
33    if not os.path.isfile(path):
34        # if the custom config file does not exist, copy the default from
35        # the app dir
36        try:
37            shutil.copyfile(os.path.join(get_app_dir(), "custom_config.py"),
38                            path)
39        except Exception:
40            #import traceback; logging.error(traceback.format_exc())
41            logging.error("Could not copy default custom config.")
42
43    custom_config = _load_config(path)
44
45    cat_file = CategoryInstaller.get_user_file()
46    # If the user category file doesn't exist copy the default to
47    # the user directory
48    if not os.path.isfile(cat_file):
49        try:
50            default_cat_file = CategoryInstaller.get_default_file()
51            if os.path.isfile(default_cat_file):
52                shutil.copyfile(default_cat_file, cat_file)
53            else:
54                logging.error("Unable to find/copy default cat file")
55        except Exception:
56            logging.error("Unable to copy default cat file to the user dir.")
57
58    return custom_config
59
60
61def _load_config(path):
62    if os.path.exists(path):
63        try:
64            fObj = None
65            fObj, config_path, descr = imp.find_module('custom_config', [os.path.dirname(path)])
66            custom_config = imp.load_module('custom_config', fObj, config_path, descr)
67            logging.info("GuiManager loaded %s" % config_path)
68            return custom_config
69        except Exception:
70            logging.error("Error loading %s: %s" % (path, sys.exc_value))
71        finally:
72            if fObj is not None:
73                fObj.close()
74    from sas.sasview import custom_config
75    logging.info("GuiManager custom_config defaults to sas.sasview.custom_config")
76    return custom_config
Note: See TracBrowser for help on using the repository browser.