1 | # Setup and find Custom config dir |
---|
2 | import sys |
---|
3 | import imp |
---|
4 | import os.path |
---|
5 | import logging |
---|
6 | import shutil |
---|
7 | |
---|
8 | from sas.sasgui.guiframe.CategoryInstaller import CategoryInstaller |
---|
9 | from sas.sasgui import get_user_dir, get_app_dir |
---|
10 | |
---|
11 | def 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 |
---|
21 | def 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 | |
---|
31 | def _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 | |
---|
61 | def _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 |
---|