[7fb59b2] | 1 | # -*- coding: utf-8 -*- |
---|
[415fb82] | 2 | """ |
---|
| 3 | Base module for loading and running the main SasView application. |
---|
| 4 | """ |
---|
[f76bf17] | 5 | ################################################################################ |
---|
[f36e01f] | 6 | # This software was developed by the University of Tennessee as part of the |
---|
| 7 | # Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 8 | # project funded by the US National Science Foundation. |
---|
[f76bf17] | 9 | # |
---|
[f36e01f] | 10 | # See the license text in license.txt |
---|
[f76bf17] | 11 | # |
---|
[f36e01f] | 12 | # copyright 2009, University of Tennessee |
---|
[f76bf17] | 13 | ################################################################################ |
---|
| 14 | import os |
---|
[5af6f58] | 15 | import os.path |
---|
[f76bf17] | 16 | import sys |
---|
[4e080980] | 17 | import traceback |
---|
[7c105e8] | 18 | import logging |
---|
[4e080980] | 19 | |
---|
[558d64e] | 20 | reload(sys) |
---|
| 21 | sys.setdefaultencoding("iso-8859-1") |
---|
[f36e01f] | 22 | |
---|
[b963b20] | 23 | import sas |
---|
| 24 | |
---|
[f76bf17] | 25 | APP_NAME = 'SasView' |
---|
[1693141] | 26 | PLUGIN_MODEL_DIR = 'plugin_models' |
---|
[f76bf17] | 27 | |
---|
[1693141] | 28 | class SasView(object): |
---|
[f76bf17] | 29 | """ |
---|
[415fb82] | 30 | Main class for running the SasView application |
---|
[f76bf17] | 31 | """ |
---|
| 32 | def __init__(self): |
---|
| 33 | """ |
---|
| 34 | """ |
---|
[7c105e8] | 35 | logger = logging.getLogger(__name__) |
---|
| 36 | |
---|
[899e084] | 37 | from sas.sasgui.guiframe.gui_manager import SasViewApp |
---|
| 38 | self.gui = SasViewApp(0) |
---|
[f76bf17] | 39 | # Set the application manager for the GUI |
---|
| 40 | self.gui.set_manager(self) |
---|
| 41 | # Add perspectives to the basic application |
---|
| 42 | # Additional perspectives can still be loaded |
---|
| 43 | # dynamically |
---|
| 44 | # Note: py2exe can't find dynamically loaded |
---|
| 45 | # modules. We load the fitting module here |
---|
| 46 | # to ensure a complete Windows executable build. |
---|
| 47 | |
---|
[65f3930] | 48 | # Rebuild .sasview/categories.json. This triggers a load of sasmodels |
---|
| 49 | # and all the plugins. |
---|
| 50 | try: |
---|
| 51 | from sas.sascalc.fit.models import ModelManager |
---|
| 52 | from sas.sasgui.guiframe.CategoryInstaller import CategoryInstaller |
---|
| 53 | model_list = ModelManager().cat_model_list() |
---|
| 54 | CategoryInstaller.check_install(model_list=model_list) |
---|
| 55 | except Exception: |
---|
| 56 | logger.error("%s: could not load SasView models") |
---|
| 57 | logger.error(traceback.format_exc()) |
---|
| 58 | |
---|
[f76bf17] | 59 | # Fitting perspective |
---|
| 60 | try: |
---|
[77d92cd] | 61 | import sas.sasgui.perspectives.fitting as module |
---|
[f76bf17] | 62 | fitting_plug = module.Plugin() |
---|
| 63 | self.gui.add_perspective(fitting_plug) |
---|
[415fb82] | 64 | except Exception: |
---|
[b39d32d5] | 65 | logger.error("%s: could not find Fitting plug-in module", APP_NAME) |
---|
[64ca561] | 66 | logger.error(traceback.format_exc()) |
---|
[f76bf17] | 67 | |
---|
| 68 | # P(r) perspective |
---|
| 69 | try: |
---|
[d85c194] | 70 | import sas.sasgui.perspectives.pr as module |
---|
[f21d496] | 71 | pr_plug = module.Plugin() |
---|
[f76bf17] | 72 | self.gui.add_perspective(pr_plug) |
---|
[b39d32d5] | 73 | except Exception: |
---|
| 74 | logger.error("%s: could not find P(r) plug-in module", APP_NAME) |
---|
[64ca561] | 75 | logger.error(traceback.format_exc()) |
---|
[4e080980] | 76 | |
---|
[f36e01f] | 77 | # Invariant perspective |
---|
[f76bf17] | 78 | try: |
---|
[d85c194] | 79 | import sas.sasgui.perspectives.invariant as module |
---|
[f21d496] | 80 | invariant_plug = module.Plugin() |
---|
[f76bf17] | 81 | self.gui.add_perspective(invariant_plug) |
---|
[b39d32d5] | 82 | except Exception: |
---|
| 83 | logger.error("%s: could not find Invariant plug-in module", |
---|
| 84 | APP_NAME) |
---|
[64ca561] | 85 | logger.error(traceback.format_exc()) |
---|
[4e080980] | 86 | |
---|
[c23f303] | 87 | # Corfunc perspective |
---|
| 88 | try: |
---|
| 89 | import sas.sasgui.perspectives.corfunc as module |
---|
| 90 | corfunc_plug = module.Plugin() |
---|
| 91 | self.gui.add_perspective(corfunc_plug) |
---|
[b39d32d5] | 92 | except Exception: |
---|
[64ca561] | 93 | logger.error("Unable to load corfunc module") |
---|
[c23f303] | 94 | |
---|
[f36e01f] | 95 | # Calculator perspective |
---|
[f76bf17] | 96 | try: |
---|
[d85c194] | 97 | import sas.sasgui.perspectives.calculator as module |
---|
[f21d496] | 98 | calculator_plug = module.Plugin() |
---|
[f76bf17] | 99 | self.gui.add_perspective(calculator_plug) |
---|
[b39d32d5] | 100 | except Exception: |
---|
| 101 | logger.error("%s: could not find Calculator plug-in module", |
---|
| 102 | APP_NAME) |
---|
[64ca561] | 103 | logger.error(traceback.format_exc()) |
---|
[4e080980] | 104 | |
---|
[77d92cd] | 105 | # File converter tool |
---|
| 106 | try: |
---|
| 107 | import sas.sasgui.perspectives.file_converter as module |
---|
| 108 | converter_plug = module.Plugin() |
---|
| 109 | self.gui.add_perspective(converter_plug) |
---|
[b39d32d5] | 110 | except Exception: |
---|
| 111 | logger.error("%s: could not find File Converter plug-in module", |
---|
| 112 | APP_NAME) |
---|
[64ca561] | 113 | logger.error(traceback.format_exc()) |
---|
[77d92cd] | 114 | |
---|
[f76bf17] | 115 | # Add welcome page |
---|
[899e084] | 116 | from .welcome_panel import WelcomePanel |
---|
[f76bf17] | 117 | self.gui.set_welcome_panel(WelcomePanel) |
---|
[415fb82] | 118 | |
---|
[f76bf17] | 119 | # Build the GUI |
---|
| 120 | self.gui.build_gui() |
---|
[415fb82] | 121 | # delete unused model folder |
---|
[f76bf17] | 122 | self.gui.clean_plugin_models(PLUGIN_MODEL_DIR) |
---|
| 123 | # Start the main loop |
---|
| 124 | self.gui.MainLoop() |
---|
| 125 | |
---|
| 126 | |
---|
[899e084] | 127 | def setup_logging(): |
---|
[7c105e8] | 128 | from sas.logger_config import SetupLogger |
---|
[899e084] | 129 | |
---|
[7c105e8] | 130 | logger = SetupLogger(__name__).config_production() |
---|
[899e084] | 131 | # Log the start of the session |
---|
[7c105e8] | 132 | logger.info(" --- SasView session started ---") |
---|
[899e084] | 133 | # Log the python version |
---|
[7c105e8] | 134 | logger.info("Python: %s" % sys.version) |
---|
| 135 | return logger |
---|
[899e084] | 136 | |
---|
| 137 | |
---|
| 138 | def setup_wx(): |
---|
| 139 | # Allow the dynamic selection of wxPython via an environment variable, when devs |
---|
| 140 | # who have multiple versions of the module installed want to pick between them. |
---|
| 141 | # This variable does not have to be set of course, and through normal usage will |
---|
| 142 | # probably not be, but this can make things a little easier when upgrading to a |
---|
| 143 | # new version of wx. |
---|
[7c105e8] | 144 | logger = logging.getLogger(__name__) |
---|
[899e084] | 145 | WX_ENV_VAR = "SASVIEW_WX_VERSION" |
---|
| 146 | if WX_ENV_VAR in os.environ: |
---|
[b39d32d5] | 147 | logger.info("You have set the %s environment variable to %s.", |
---|
| 148 | WX_ENV_VAR, os.environ[WX_ENV_VAR]) |
---|
[899e084] | 149 | import wxversion |
---|
| 150 | if wxversion.checkInstalled(os.environ[WX_ENV_VAR]): |
---|
[b39d32d5] | 151 | logger.info("Version %s of wxPython is installed, so using that version.", |
---|
| 152 | os.environ[WX_ENV_VAR]) |
---|
[899e084] | 153 | wxversion.select(os.environ[WX_ENV_VAR]) |
---|
| 154 | else: |
---|
[b39d32d5] | 155 | logger.error("Version %s of wxPython is not installed, so using default version.", |
---|
| 156 | os.environ[WX_ENV_VAR]) |
---|
[899e084] | 157 | else: |
---|
[b39d32d5] | 158 | logger.info("You have not set the %s environment variable, so using default version of wxPython.", |
---|
| 159 | WX_ENV_VAR) |
---|
[899e084] | 160 | |
---|
| 161 | import wx |
---|
| 162 | |
---|
| 163 | try: |
---|
[b39d32d5] | 164 | logger.info("Wx version: %s", wx.__version__) |
---|
| 165 | except AttributeError: |
---|
[914ba0a] | 166 | logger.error("Wx version: error reading version") |
---|
[899e084] | 167 | |
---|
| 168 | from . import wxcruft |
---|
| 169 | wxcruft.call_later_fix() |
---|
| 170 | #wxcruft.trace_new_id() |
---|
| 171 | #Always use private .matplotlib setup to avoid conflicts with other |
---|
| 172 | #uses of matplotlib |
---|
| 173 | |
---|
| 174 | |
---|
[b963b20] | 175 | def setup_mpl(backend=None): |
---|
[914ba0a] | 176 | # Always use private .matplotlib setup to avoid conflicts with other |
---|
[b963b20] | 177 | mplconfigdir = os.path.join(sas.get_user_dir(), '.matplotlib') |
---|
[899e084] | 178 | if not os.path.exists(mplconfigdir): |
---|
| 179 | os.mkdir(mplconfigdir) |
---|
| 180 | os.environ['MPLCONFIGDIR'] = mplconfigdir |
---|
| 181 | # Set backend to WXAgg; this overrides matplotlibrc, but shouldn't override |
---|
| 182 | # mpl.use(). Note: Don't import matplotlib here since the script that |
---|
| 183 | # we are running may not actually need it; also, putting as little on the |
---|
| 184 | # path as we can |
---|
[1693141] | 185 | os.environ['MPLBACKEND'] = backend |
---|
[899e084] | 186 | |
---|
[1693141] | 187 | # TODO: ... so much for not importing matplotlib unless we need it... |
---|
[914ba0a] | 188 | from matplotlib import backend_bases |
---|
| 189 | backend_bases.FigureCanvasBase.filetypes.pop('pgf', None) |
---|
| 190 | |
---|
[1693141] | 191 | def setup_sasmodels(): |
---|
| 192 | """ |
---|
| 193 | Prepare sasmodels for running within sasview. |
---|
| 194 | """ |
---|
| 195 | # Set SAS_MODELPATH so sasmodels can find our custom models |
---|
[b963b20] | 196 | plugin_dir = os.path.join(sas.get_user_dir(), PLUGIN_MODEL_DIR) |
---|
[1693141] | 197 | os.environ['SAS_MODELPATH'] = plugin_dir |
---|
[b963b20] | 198 | #Initiliaze enviromental variable with custom setting but only if variable not set |
---|
| 199 | SAS_OPENCL = sas.get_custom_config().SAS_OPENCL |
---|
| 200 | if SAS_OPENCL and "SAS_OPENCL" not in os.environ: |
---|
| 201 | os.environ["SAS_OPENCL"] = SAS_OPENCL |
---|
[60a7820] | 202 | |
---|
[899e084] | 203 | def run_gui(): |
---|
[415fb82] | 204 | """ |
---|
| 205 | __main__ method for loading and running SasView |
---|
| 206 | """ |
---|
[f76bf17] | 207 | from multiprocessing import freeze_support |
---|
| 208 | freeze_support() |
---|
[899e084] | 209 | setup_logging() |
---|
[b963b20] | 210 | setup_mpl(backend='WXAgg') |
---|
[1693141] | 211 | setup_sasmodels() |
---|
| 212 | setup_wx() |
---|
[899e084] | 213 | SasView() |
---|
| 214 | |
---|
| 215 | |
---|
| 216 | def run_cli(): |
---|
[1693141] | 217 | from multiprocessing import freeze_support |
---|
| 218 | freeze_support() |
---|
[7c105e8] | 219 | setup_logging() |
---|
[b963b20] | 220 | # Use default matplotlib backend on mac/linux, but wx on windows. |
---|
| 221 | # The problem on mac is that the wx backend requires pythonw. On windows |
---|
| 222 | # we are sure to wx since it is the shipped with the app. |
---|
| 223 | setup_mpl(backend='WXAgg' if os.name == 'nt' else None) |
---|
[1693141] | 224 | setup_sasmodels() |
---|
[899e084] | 225 | if len(sys.argv) == 1: |
---|
| 226 | # Run sasview as an interactive python interpreter |
---|
[4b26bc14] | 227 | try: |
---|
| 228 | from IPython import start_ipython |
---|
| 229 | sys.argv = ["ipython", "--pylab"] |
---|
| 230 | sys.exit(start_ipython()) |
---|
| 231 | except ImportError: |
---|
| 232 | import code |
---|
[b39d32d5] | 233 | code.interact(local={'exit': sys.exit}) |
---|
[899e084] | 234 | else: |
---|
| 235 | # Run sasview as a python script interpreter |
---|
[f76bf17] | 236 | ## Run sasview as an interactive python interpreter |
---|
[f36e01f] | 237 | # if sys.argv[1] == "-i": |
---|
[f76bf17] | 238 | # sys.argv = ["ipython", "--pylab"] |
---|
| 239 | # from IPython import start_ipython |
---|
| 240 | # sys.exit(start_ipython()) |
---|
| 241 | thing_to_run = sys.argv[1] |
---|
| 242 | sys.argv = sys.argv[1:] |
---|
| 243 | import runpy |
---|
| 244 | if os.path.exists(thing_to_run): |
---|
| 245 | runpy.run_path(thing_to_run, run_name="__main__") |
---|
| 246 | else: |
---|
| 247 | runpy.run_module(thing_to_run, run_name="__main__") |
---|
| 248 | |
---|
[f36e01f] | 249 | |
---|
[f76bf17] | 250 | if __name__ == "__main__": |
---|
[899e084] | 251 | run_gui() |
---|