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