[b963b20] | 1 | # Setup and find Custom config dir |
---|
| 2 | from __future__ import print_function |
---|
| 3 | |
---|
| 4 | import sys |
---|
| 5 | import os |
---|
| 6 | from os.path import exists, expanduser, dirname, realpath, join as joinpath |
---|
| 7 | import logging |
---|
| 8 | import shutil |
---|
| 9 | |
---|
| 10 | from sasmodels.custom import load_module_from_path |
---|
| 11 | |
---|
| 12 | logger = logging.getLogger(__name__) |
---|
| 13 | |
---|
| 14 | def dirn(path, n): |
---|
| 15 | """ |
---|
| 16 | Return the directory n up from the current path |
---|
| 17 | """ |
---|
| 18 | path = realpath(path) |
---|
| 19 | for _ in range(n): |
---|
| 20 | path = dirname(path) |
---|
| 21 | return path |
---|
| 22 | |
---|
| 23 | def find_app_dir(): |
---|
| 24 | """ |
---|
| 25 | Locate the parent directory of the sasview resources. For the normal |
---|
| 26 | application this will be the directory containing sasview.py. For the |
---|
| 27 | frozen application this will be the path where the resources are installed. |
---|
| 28 | """ |
---|
| 29 | # We are starting out with the following info: |
---|
| 30 | # __file__ = .../sas/__init__.pyc |
---|
| 31 | # Check if the path .../sas/sasview exists, and use it as the |
---|
| 32 | # app directory. This will only be the case if the app is not frozen. |
---|
| 33 | path = joinpath(dirname(__file__), 'sasview') |
---|
| 34 | if exists(path): |
---|
| 35 | return path |
---|
| 36 | |
---|
| 37 | # If we are running frozen, then root is a parent directory |
---|
| 38 | if sys.platform == 'darwin': |
---|
| 39 | # Here is the path to the file on the mac: |
---|
| 40 | # .../Sasview.app/Contents/Resources/lib/python2.7/site-packages.zip/sas/__init__.pyc |
---|
| 41 | # We want the path to the Resources directory. |
---|
| 42 | path = dirn(__file__, 5) |
---|
| 43 | elif os.name == 'nt': |
---|
| 44 | # Here is the path to the file on windows: |
---|
| 45 | # ../Sasview/library.zip/sas/__init__.pyc |
---|
| 46 | # We want the path to the Sasview directory. |
---|
| 47 | path = dirn(__file__, 3) |
---|
| 48 | else: |
---|
| 49 | raise RuntimeError("Couldn't find the app directory") |
---|
| 50 | return path |
---|
| 51 | |
---|
| 52 | def make_user_dir(): |
---|
| 53 | """ |
---|
| 54 | Create the user directory ~/.sasview if it doesn't already exist. |
---|
| 55 | """ |
---|
| 56 | path = joinpath(expanduser("~"),'.sasview') |
---|
| 57 | if not exists(path): |
---|
| 58 | os.mkdir(path) |
---|
| 59 | return path |
---|
| 60 | |
---|
| 61 | def load_local_config(app_dir): |
---|
| 62 | logger = logging.getLogger(__name__) |
---|
| 63 | filename = 'local_config.py' |
---|
| 64 | path = os.path.join(app_dir, filename) |
---|
| 65 | try: |
---|
| 66 | module = load_module_from_path('sas.local_config', path) |
---|
[88e1f57] | 67 | #logger.info("GuiManager loaded %s", path) |
---|
[b963b20] | 68 | return module |
---|
| 69 | except Exception as exc: |
---|
[88e1f57] | 70 | #logger.critical("Error loading %s: %s", path, exc) |
---|
[b963b20] | 71 | sys.exit() |
---|
| 72 | |
---|
| 73 | def make_custom_config_path(user_dir): |
---|
| 74 | """ |
---|
| 75 | The location of the cusstom config file. |
---|
| 76 | |
---|
| 77 | Returns ~/.sasview/config/custom_config.py |
---|
| 78 | """ |
---|
| 79 | dirname = os.path.join(user_dir, 'config') |
---|
| 80 | # If the directory doesn't exist, create it |
---|
| 81 | if not os.path.exists(dirname): |
---|
| 82 | os.makedirs(dirname) |
---|
| 83 | path = os.path.join(dirname, "custom_config.py") |
---|
| 84 | return path |
---|
| 85 | |
---|
| 86 | def setup_custom_config(app_dir, user_dir): |
---|
| 87 | path = make_custom_config_path(user_dir) |
---|
| 88 | if not os.path.isfile(path): |
---|
| 89 | try: |
---|
| 90 | # if the custom config file does not exist, copy the default from |
---|
| 91 | # the app dir |
---|
| 92 | shutil.copyfile(os.path.join(app_dir, "custom_config.py"), path) |
---|
| 93 | except Exception: |
---|
| 94 | logger.error("Could not copy default custom config.") |
---|
| 95 | |
---|
| 96 | #Adding SAS_OPENCL if it doesn't exist in the config file |
---|
| 97 | # - to support backcompability |
---|
| 98 | if not "SAS_OPENCL" in open(path).read(): |
---|
| 99 | try: |
---|
| 100 | open(config_file, "a+").write("SAS_OPENCL = \"None\"\n") |
---|
| 101 | except Exception: |
---|
| 102 | logger.error("Could not update custom config with SAS_OPENCL.") |
---|
| 103 | |
---|
| 104 | custom_config = load_custom_config(path) |
---|
| 105 | return custom_config |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | def load_custom_config(path): |
---|
| 109 | if os.path.exists(path): |
---|
| 110 | try: |
---|
| 111 | module = load_module_from_path('sas.custom_config', path) |
---|
| 112 | logger.info("GuiManager loaded %s", path) |
---|
| 113 | return module |
---|
| 114 | except Exception as exc: |
---|
| 115 | logger.error("Error loading %s: %s", path, exc) |
---|
| 116 | |
---|
| 117 | from sas.sasview import custom_config |
---|
| 118 | logger.info("GuiManager custom_config defaults to sas.sasview.custom_config") |
---|
| 119 | return custom_config |
---|