[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 | |
---|
[0225a3f] | 20 | try: |
---|
| 21 | reload(sys) |
---|
| 22 | sys.setdefaultencoding("iso-8859-1") |
---|
| 23 | except NameError: |
---|
| 24 | # On python 3 sys.setdefaultencoding does nothing, so pass. |
---|
| 25 | # We know we are in python 3 at this point since reload is no longer in |
---|
| 26 | # builtins, but instead has been moved to importlib, hence the NameError. |
---|
| 27 | pass |
---|
[f36e01f] | 28 | |
---|
[b963b20] | 29 | import sas |
---|
| 30 | |
---|
[f76bf17] | 31 | APP_NAME = 'SasView' |
---|
[1693141] | 32 | PLUGIN_MODEL_DIR = 'plugin_models' |
---|
[f76bf17] | 33 | |
---|
[1693141] | 34 | class SasView(object): |
---|
[f76bf17] | 35 | """ |
---|
[415fb82] | 36 | Main class for running the SasView application |
---|
[f76bf17] | 37 | """ |
---|
| 38 | def __init__(self): |
---|
| 39 | """ |
---|
| 40 | """ |
---|
[7c105e8] | 41 | logger = logging.getLogger(__name__) |
---|
| 42 | |
---|
[899e084] | 43 | from sas.sasgui.guiframe.gui_manager import SasViewApp |
---|
| 44 | self.gui = SasViewApp(0) |
---|
[f76bf17] | 45 | # Set the application manager for the GUI |
---|
| 46 | self.gui.set_manager(self) |
---|
| 47 | # Add perspectives to the basic application |
---|
| 48 | # Additional perspectives can still be loaded |
---|
| 49 | # dynamically |
---|
| 50 | # Note: py2exe can't find dynamically loaded |
---|
| 51 | # modules. We load the fitting module here |
---|
| 52 | # to ensure a complete Windows executable build. |
---|
| 53 | |
---|
[65f3930] | 54 | # Rebuild .sasview/categories.json. This triggers a load of sasmodels |
---|
| 55 | # and all the plugins. |
---|
| 56 | try: |
---|
| 57 | from sas.sascalc.fit.models import ModelManager |
---|
| 58 | from sas.sasgui.guiframe.CategoryInstaller import CategoryInstaller |
---|
| 59 | model_list = ModelManager().cat_model_list() |
---|
| 60 | CategoryInstaller.check_install(model_list=model_list) |
---|
| 61 | except Exception: |
---|
| 62 | logger.error("%s: could not load SasView models") |
---|
| 63 | logger.error(traceback.format_exc()) |
---|
| 64 | |
---|
[f76bf17] | 65 | # Fitting perspective |
---|
| 66 | try: |
---|
[77d92cd] | 67 | import sas.sasgui.perspectives.fitting as module |
---|
[f76bf17] | 68 | fitting_plug = module.Plugin() |
---|
| 69 | self.gui.add_perspective(fitting_plug) |
---|
[415fb82] | 70 | except Exception: |
---|
[b39d32d5] | 71 | logger.error("%s: could not find Fitting plug-in module", APP_NAME) |
---|
[64ca561] | 72 | logger.error(traceback.format_exc()) |
---|
[f76bf17] | 73 | |
---|
| 74 | # P(r) perspective |
---|
| 75 | try: |
---|
[d85c194] | 76 | import sas.sasgui.perspectives.pr as module |
---|
[f21d496] | 77 | pr_plug = module.Plugin() |
---|
[f76bf17] | 78 | self.gui.add_perspective(pr_plug) |
---|
[b39d32d5] | 79 | except Exception: |
---|
| 80 | logger.error("%s: could not find P(r) plug-in module", APP_NAME) |
---|
[64ca561] | 81 | logger.error(traceback.format_exc()) |
---|
[4e080980] | 82 | |
---|
[f36e01f] | 83 | # Invariant perspective |
---|
[f76bf17] | 84 | try: |
---|
[d85c194] | 85 | import sas.sasgui.perspectives.invariant as module |
---|
[f21d496] | 86 | invariant_plug = module.Plugin() |
---|
[f76bf17] | 87 | self.gui.add_perspective(invariant_plug) |
---|
[b39d32d5] | 88 | except Exception: |
---|
| 89 | logger.error("%s: could not find Invariant plug-in module", |
---|
| 90 | APP_NAME) |
---|
[64ca561] | 91 | logger.error(traceback.format_exc()) |
---|
[4e080980] | 92 | |
---|
[c23f303] | 93 | # Corfunc perspective |
---|
| 94 | try: |
---|
| 95 | import sas.sasgui.perspectives.corfunc as module |
---|
| 96 | corfunc_plug = module.Plugin() |
---|
| 97 | self.gui.add_perspective(corfunc_plug) |
---|
[b39d32d5] | 98 | except Exception: |
---|
[64ca561] | 99 | logger.error("Unable to load corfunc module") |
---|
[c23f303] | 100 | |
---|
[f36e01f] | 101 | # Calculator perspective |
---|
[f76bf17] | 102 | try: |
---|
[d85c194] | 103 | import sas.sasgui.perspectives.calculator as module |
---|
[f21d496] | 104 | calculator_plug = module.Plugin() |
---|
[f76bf17] | 105 | self.gui.add_perspective(calculator_plug) |
---|
[b39d32d5] | 106 | except Exception: |
---|
| 107 | logger.error("%s: could not find Calculator plug-in module", |
---|
| 108 | APP_NAME) |
---|
[64ca561] | 109 | logger.error(traceback.format_exc()) |
---|
[4e080980] | 110 | |
---|
[77d92cd] | 111 | # File converter tool |
---|
| 112 | try: |
---|
| 113 | import sas.sasgui.perspectives.file_converter as module |
---|
| 114 | converter_plug = module.Plugin() |
---|
| 115 | self.gui.add_perspective(converter_plug) |
---|
[b39d32d5] | 116 | except Exception: |
---|
| 117 | logger.error("%s: could not find File Converter plug-in module", |
---|
| 118 | APP_NAME) |
---|
[64ca561] | 119 | logger.error(traceback.format_exc()) |
---|
[77d92cd] | 120 | |
---|
[f76bf17] | 121 | # Add welcome page |
---|
[899e084] | 122 | from .welcome_panel import WelcomePanel |
---|
[f76bf17] | 123 | self.gui.set_welcome_panel(WelcomePanel) |
---|
[415fb82] | 124 | |
---|
[f76bf17] | 125 | # Build the GUI |
---|
| 126 | self.gui.build_gui() |
---|
[415fb82] | 127 | # delete unused model folder |
---|
[f76bf17] | 128 | self.gui.clean_plugin_models(PLUGIN_MODEL_DIR) |
---|
| 129 | # Start the main loop |
---|
| 130 | self.gui.MainLoop() |
---|
| 131 | |
---|
| 132 | |
---|
[899e084] | 133 | def setup_logging(): |
---|
[7c105e8] | 134 | from sas.logger_config import SetupLogger |
---|
[899e084] | 135 | |
---|
[7c105e8] | 136 | logger = SetupLogger(__name__).config_production() |
---|
[899e084] | 137 | # Log the start of the session |
---|
[7c105e8] | 138 | logger.info(" --- SasView session started ---") |
---|
[899e084] | 139 | # Log the python version |
---|
[7c105e8] | 140 | logger.info("Python: %s" % sys.version) |
---|
| 141 | return logger |
---|
[899e084] | 142 | |
---|
| 143 | |
---|
| 144 | def setup_wx(): |
---|
| 145 | # Allow the dynamic selection of wxPython via an environment variable, when devs |
---|
| 146 | # who have multiple versions of the module installed want to pick between them. |
---|
| 147 | # This variable does not have to be set of course, and through normal usage will |
---|
| 148 | # probably not be, but this can make things a little easier when upgrading to a |
---|
| 149 | # new version of wx. |
---|
[7c105e8] | 150 | logger = logging.getLogger(__name__) |
---|
[899e084] | 151 | WX_ENV_VAR = "SASVIEW_WX_VERSION" |
---|
| 152 | if WX_ENV_VAR in os.environ: |
---|
[b39d32d5] | 153 | logger.info("You have set the %s environment variable to %s.", |
---|
| 154 | WX_ENV_VAR, os.environ[WX_ENV_VAR]) |
---|
[899e084] | 155 | import wxversion |
---|
| 156 | if wxversion.checkInstalled(os.environ[WX_ENV_VAR]): |
---|
[b39d32d5] | 157 | logger.info("Version %s of wxPython is installed, so using that version.", |
---|
| 158 | os.environ[WX_ENV_VAR]) |
---|
[899e084] | 159 | wxversion.select(os.environ[WX_ENV_VAR]) |
---|
| 160 | else: |
---|
[b39d32d5] | 161 | logger.error("Version %s of wxPython is not installed, so using default version.", |
---|
| 162 | os.environ[WX_ENV_VAR]) |
---|
[899e084] | 163 | else: |
---|
[b39d32d5] | 164 | logger.info("You have not set the %s environment variable, so using default version of wxPython.", |
---|
| 165 | WX_ENV_VAR) |
---|
[899e084] | 166 | |
---|
| 167 | import wx |
---|
| 168 | |
---|
| 169 | try: |
---|
[b39d32d5] | 170 | logger.info("Wx version: %s", wx.__version__) |
---|
| 171 | except AttributeError: |
---|
[914ba0a] | 172 | logger.error("Wx version: error reading version") |
---|
[899e084] | 173 | |
---|
| 174 | from . import wxcruft |
---|
| 175 | wxcruft.call_later_fix() |
---|
| 176 | #wxcruft.trace_new_id() |
---|
| 177 | #Always use private .matplotlib setup to avoid conflicts with other |
---|
| 178 | #uses of matplotlib |
---|
| 179 | |
---|
| 180 | |
---|
[b963b20] | 181 | def setup_mpl(backend=None): |
---|
[914ba0a] | 182 | # Always use private .matplotlib setup to avoid conflicts with other |
---|
[b963b20] | 183 | mplconfigdir = os.path.join(sas.get_user_dir(), '.matplotlib') |
---|
[899e084] | 184 | if not os.path.exists(mplconfigdir): |
---|
| 185 | os.mkdir(mplconfigdir) |
---|
| 186 | os.environ['MPLCONFIGDIR'] = mplconfigdir |
---|
| 187 | # Set backend to WXAgg; this overrides matplotlibrc, but shouldn't override |
---|
| 188 | # mpl.use(). Note: Don't import matplotlib here since the script that |
---|
| 189 | # we are running may not actually need it; also, putting as little on the |
---|
| 190 | # path as we can |
---|
[b277220] | 191 | if backend: |
---|
| 192 | os.environ['MPLBACKEND'] = backend |
---|
[899e084] | 193 | |
---|
[1693141] | 194 | # TODO: ... so much for not importing matplotlib unless we need it... |
---|
[914ba0a] | 195 | from matplotlib import backend_bases |
---|
| 196 | backend_bases.FigureCanvasBase.filetypes.pop('pgf', None) |
---|
| 197 | |
---|
[0a5d1a8] | 198 | def check_sasmodels_compiler(): |
---|
[1693141] | 199 | """ |
---|
[d2647feb] | 200 | Checking c compiler for sasmodels and raises xcode command line |
---|
| 201 | tools for installation |
---|
[1693141] | 202 | """ |
---|
[47439ce] | 203 | import wx |
---|
[c82cf89] | 204 | import subprocess |
---|
[49f9f9e] | 205 | #Generic message box created becuase standard MessageBox is not moveable |
---|
[47439ce] | 206 | class GenericMessageBox(wx.Dialog): |
---|
| 207 | def __init__(self, parent, text, title = ''): |
---|
| 208 | wx.Dialog.__init__(self, parent, -1, title = title, |
---|
| 209 | size = (360,140), pos=(20,20), |
---|
| 210 | style = wx.DEFAULT_DIALOG_STYLE ) |
---|
[360eff3] | 211 | panel = wx.Panel(self, -1) |
---|
| 212 | top_row_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
[47439ce] | 213 | |
---|
[360eff3] | 214 | error_bitmap = wx.ArtProvider.GetBitmap( |
---|
| 215 | wx.ART_ERROR, wx.ART_MESSAGE_BOX |
---|
| 216 | ) |
---|
| 217 | error_bitmap_ctrl = wx.StaticBitmap(panel, -1) |
---|
| 218 | error_bitmap_ctrl.SetBitmap(error_bitmap) |
---|
| 219 | label = wx.StaticText(panel, -1, text) |
---|
| 220 | top_row_sizer.Add(error_bitmap_ctrl, flag=wx.ALL, border=10) |
---|
| 221 | top_row_sizer.Add(label, flag=wx.ALIGN_CENTER_VERTICAL) |
---|
| 222 | |
---|
| 223 | #Create the OK button in the bottom row. |
---|
[08707cc] | 224 | ok_button = wx.Button(panel, wx.ID_OK ) |
---|
[360eff3] | 225 | ok_button.SetFocus() |
---|
| 226 | ok_button.SetDefault() |
---|
| 227 | |
---|
| 228 | sizer = wx.BoxSizer(wx.VERTICAL) |
---|
| 229 | sizer.Add(top_row_sizer) |
---|
| 230 | sizer.Add(ok_button, flag=wx.ALIGN_CENTER | wx.ALL, border=5) |
---|
| 231 | panel.SetSizer(sizer) |
---|
| 232 | |
---|
[c82cf89] | 233 | logger = logging.getLogger(__name__) |
---|
| 234 | try: |
---|
[86d6abd] | 235 | subprocess.check_output(["cc","--version"], stderr=subprocess.STDOUT) |
---|
[360eff3] | 236 | except subprocess.CalledProcessError as exc: |
---|
| 237 | app = wx.App() |
---|
| 238 | dlg = GenericMessageBox(parent=None, |
---|
[2bd0e4a] | 239 | text='No compiler installed. Please follow instruction for\n' |
---|
[aadfd693] | 240 | 'command line developers tools installation and restart SasView\n\n' |
---|
[2bd0e4a] | 241 | 'SasView is terminating now\n', |
---|
[360eff3] | 242 | title = 'Compiler Info') |
---|
[e810d8f] | 243 | dlg.ShowModal() |
---|
[47439ce] | 244 | |
---|
[86d6abd] | 245 | logger.error("No compiler installed. %s\n"%(exc)) |
---|
[d2647feb] | 246 | logger.error(traceback.format_exc()) |
---|
[86d6abd] | 247 | |
---|
| 248 | raise RuntimeError("Terminating sasview") |
---|
[0a5d1a8] | 249 | |
---|
| 250 | def setup_sasmodels(): |
---|
| 251 | """ |
---|
| 252 | Prepare sasmodels for running within sasview. |
---|
| 253 | """ |
---|
[1693141] | 254 | # Set SAS_MODELPATH so sasmodels can find our custom models |
---|
[b963b20] | 255 | plugin_dir = os.path.join(sas.get_user_dir(), PLUGIN_MODEL_DIR) |
---|
[1693141] | 256 | os.environ['SAS_MODELPATH'] = plugin_dir |
---|
[20fa5fe] | 257 | #Initialize environment variable with custom setting but only if variable not set |
---|
[b963b20] | 258 | SAS_OPENCL = sas.get_custom_config().SAS_OPENCL |
---|
| 259 | if SAS_OPENCL and "SAS_OPENCL" not in os.environ: |
---|
| 260 | os.environ["SAS_OPENCL"] = SAS_OPENCL |
---|
[60a7820] | 261 | |
---|
[899e084] | 262 | def run_gui(): |
---|
[415fb82] | 263 | """ |
---|
| 264 | __main__ method for loading and running SasView |
---|
| 265 | """ |
---|
[f76bf17] | 266 | from multiprocessing import freeze_support |
---|
| 267 | freeze_support() |
---|
[899e084] | 268 | setup_logging() |
---|
[b963b20] | 269 | setup_mpl(backend='WXAgg') |
---|
[1693141] | 270 | setup_sasmodels() |
---|
| 271 | setup_wx() |
---|
[d2647feb] | 272 | if sys.platform == "darwin": |
---|
| 273 | check_sasmodels_compiler() |
---|
[899e084] | 274 | SasView() |
---|
| 275 | |
---|
| 276 | |
---|
| 277 | def run_cli(): |
---|
[1693141] | 278 | from multiprocessing import freeze_support |
---|
| 279 | freeze_support() |
---|
[7c105e8] | 280 | setup_logging() |
---|
[b963b20] | 281 | # Use default matplotlib backend on mac/linux, but wx on windows. |
---|
| 282 | # The problem on mac is that the wx backend requires pythonw. On windows |
---|
| 283 | # we are sure to wx since it is the shipped with the app. |
---|
| 284 | setup_mpl(backend='WXAgg' if os.name == 'nt' else None) |
---|
[1693141] | 285 | setup_sasmodels() |
---|
[aaa801e] | 286 | if len(sys.argv) == 1 or sys.argv[1] == '-i': |
---|
[899e084] | 287 | # Run sasview as an interactive python interpreter |
---|
[4b26bc14] | 288 | try: |
---|
| 289 | from IPython import start_ipython |
---|
| 290 | sys.argv = ["ipython", "--pylab"] |
---|
| 291 | sys.exit(start_ipython()) |
---|
| 292 | except ImportError: |
---|
| 293 | import code |
---|
[b39d32d5] | 294 | code.interact(local={'exit': sys.exit}) |
---|
[0225a3f] | 295 | elif sys.argv[1] == '-c': |
---|
| 296 | exec(sys.argv[2]) |
---|
[899e084] | 297 | else: |
---|
[f76bf17] | 298 | thing_to_run = sys.argv[1] |
---|
| 299 | sys.argv = sys.argv[1:] |
---|
| 300 | import runpy |
---|
| 301 | if os.path.exists(thing_to_run): |
---|
| 302 | runpy.run_path(thing_to_run, run_name="__main__") |
---|
| 303 | else: |
---|
| 304 | runpy.run_module(thing_to_run, run_name="__main__") |
---|
| 305 | |
---|
[f36e01f] | 306 | |
---|
[f76bf17] | 307 | if __name__ == "__main__": |
---|
[899e084] | 308 | run_gui() |
---|