1 | import sys |
---|
2 | import os |
---|
3 | from os.path import exists, expanduser, dirname, realpath, join as joinpath |
---|
4 | |
---|
5 | |
---|
6 | def dirn(path, n): |
---|
7 | path = realpath(path) |
---|
8 | for _ in range(n): |
---|
9 | path = dirname(path) |
---|
10 | return path |
---|
11 | |
---|
12 | # Set up config directories |
---|
13 | def make_user_folder(): |
---|
14 | path = joinpath(expanduser("~"),'.sasview') |
---|
15 | if not exists(path): |
---|
16 | os.mkdir(path) |
---|
17 | return path |
---|
18 | |
---|
19 | |
---|
20 | def find_app_folder(): |
---|
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): |
---|
27 | return path |
---|
28 | |
---|
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") |
---|
42 | return path |
---|
43 | |
---|
44 | USER_FOLDER = make_user_folder() |
---|
45 | APP_FOLDER = find_app_folder() |
---|
46 | |
---|
47 | |
---|
48 | def get_app_dir(): |
---|
49 | if APP_FOLDER is None: |
---|
50 | raise RuntimeError("Need to initialize sas.sasgui.USER_FOLDER") |
---|
51 | return APP_FOLDER |
---|
52 | |
---|
53 | def get_user_dir(): |
---|
54 | if USER_FOLDER is None: |
---|
55 | raise RuntimeError("Need to initialize sas.sasgui.USER_FOLDER") |
---|
56 | return USER_FOLDER |
---|
57 | |
---|
58 | def get_custom_config_path(): |
---|
59 | dirname = os.path.join(get_user_dir(), 'config') |
---|
60 | # If the directory doesn't exist, create it |
---|
61 | if not os.path.exists(dirname): |
---|
62 | os.makedirs(dirname) |
---|
63 | path = os.path.join(dirname, "custom_config.py") |
---|
64 | return path |
---|
65 | |
---|
66 | _config_cache = None |
---|
67 | def get_local_config(): |
---|
68 | global _config_cache |
---|
69 | if not _config_cache: |
---|
70 | _config_cache = _load_config() |
---|
71 | return _config_cache |
---|
72 | |
---|
73 | def _load_config(): |
---|
74 | import os |
---|
75 | import sys |
---|
76 | import imp |
---|
77 | import logging |
---|
78 | |
---|
79 | logger = logging.getLogger(__name__) |
---|
80 | dirname = get_app_dir() |
---|
81 | filename = 'local_config.py' |
---|
82 | path = os.path.join(dirname, filename) |
---|
83 | if os.path.exists(path): |
---|
84 | try: |
---|
85 | fObj = None |
---|
86 | fObj, config_path, descr = imp.find_module('local_config', [APP_FOLDER]) |
---|
87 | config = imp.load_module('local_config', fObj, config_path, descr) |
---|
88 | logger.info("GuiManager loaded %s" % config_path) |
---|
89 | return config |
---|
90 | except Exception: |
---|
91 | import traceback; logger.error(traceback.format_exc()) |
---|
92 | logger.error("Error loading %s: %s" % (path, sys.exc_value)) |
---|
93 | finally: |
---|
94 | if fObj is not None: |
---|
95 | fObj.close() |
---|
96 | from sas.sasgui.guiframe import config |
---|
97 | logger.info("GuiManager config defaults to sas.sasgui.guiframe") |
---|
98 | return config |
---|
99 | |
---|