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