[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 |
---|
[899e084] | 19 | |
---|
| 20 | reload(sys) |
---|
| 21 | sys.setdefaultencoding("iso-8859-1") |
---|
| 22 | |
---|
[f76bf17] | 23 | PLUGIN_MODEL_DIR = 'plugin_models' |
---|
| 24 | APP_NAME = 'SasView' |
---|
| 25 | |
---|
| 26 | class SasView(): |
---|
| 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 | |
---|
| 46 | # Fitting perspective |
---|
| 47 | try: |
---|
[899e084] | 48 | import sas.sasgui.perspectives.fitting as module |
---|
[f76bf17] | 49 | fitting_plug = module.Plugin() |
---|
| 50 | self.gui.add_perspective(fitting_plug) |
---|
[415fb82] | 51 | except Exception: |
---|
[64ca561] | 52 | logger.error("%s: could not find Fitting plug-in module"% APP_NAME) |
---|
| 53 | logger.error(traceback.format_exc()) |
---|
[f76bf17] | 54 | |
---|
| 55 | # P(r) perspective |
---|
| 56 | try: |
---|
[d85c194] | 57 | import sas.sasgui.perspectives.pr as module |
---|
[f21d496] | 58 | pr_plug = module.Plugin() |
---|
[f76bf17] | 59 | self.gui.add_perspective(pr_plug) |
---|
| 60 | except: |
---|
[64ca561] | 61 | logger.error("%s: could not find P(r) plug-in module"% APP_NAME) |
---|
| 62 | logger.error(traceback.format_exc()) |
---|
[4e080980] | 63 | |
---|
[f36e01f] | 64 | # Invariant perspective |
---|
[f76bf17] | 65 | try: |
---|
[d85c194] | 66 | import sas.sasgui.perspectives.invariant as module |
---|
[f21d496] | 67 | invariant_plug = module.Plugin() |
---|
[f76bf17] | 68 | self.gui.add_perspective(invariant_plug) |
---|
[b699768] | 69 | except Exception as e : |
---|
[64ca561] | 70 | logger.error("%s: could not find Invariant plug-in module"% \ |
---|
[f76bf17] | 71 | APP_NAME) |
---|
[64ca561] | 72 | logger.error(traceback.format_exc()) |
---|
[4e080980] | 73 | |
---|
[c23f303] | 74 | # Corfunc perspective |
---|
| 75 | try: |
---|
| 76 | import sas.sasgui.perspectives.corfunc as module |
---|
| 77 | corfunc_plug = module.Plugin() |
---|
| 78 | self.gui.add_perspective(corfunc_plug) |
---|
| 79 | except: |
---|
[64ca561] | 80 | logger.error("Unable to load corfunc module") |
---|
[4e080980] | 81 | |
---|
[f36e01f] | 82 | # Calculator perspective |
---|
[f76bf17] | 83 | try: |
---|
[d85c194] | 84 | import sas.sasgui.perspectives.calculator as module |
---|
[f21d496] | 85 | calculator_plug = module.Plugin() |
---|
[f76bf17] | 86 | self.gui.add_perspective(calculator_plug) |
---|
| 87 | except: |
---|
[64ca561] | 88 | logger.error("%s: could not find Calculator plug-in module"% \ |
---|
[f76bf17] | 89 | APP_NAME) |
---|
[64ca561] | 90 | logger.error(traceback.format_exc()) |
---|
[4e080980] | 91 | |
---|
[77d92cd] | 92 | # File converter tool |
---|
| 93 | try: |
---|
| 94 | import sas.sasgui.perspectives.file_converter as module |
---|
| 95 | converter_plug = module.Plugin() |
---|
| 96 | self.gui.add_perspective(converter_plug) |
---|
| 97 | except: |
---|
[64ca561] | 98 | logger.error("%s: could not find File Converter plug-in module"% \ |
---|
[77d92cd] | 99 | APP_NAME) |
---|
[64ca561] | 100 | logger.error(traceback.format_exc()) |
---|
[415fb82] | 101 | |
---|
[f76bf17] | 102 | # Add welcome page |
---|
[899e084] | 103 | from .welcome_panel import WelcomePanel |
---|
[f76bf17] | 104 | self.gui.set_welcome_panel(WelcomePanel) |
---|
[415fb82] | 105 | |
---|
[f76bf17] | 106 | # Build the GUI |
---|
| 107 | self.gui.build_gui() |
---|
[415fb82] | 108 | # delete unused model folder |
---|
[f76bf17] | 109 | self.gui.clean_plugin_models(PLUGIN_MODEL_DIR) |
---|
| 110 | # Start the main loop |
---|
| 111 | self.gui.MainLoop() |
---|
| 112 | |
---|
| 113 | |
---|
[899e084] | 114 | def setup_logging(): |
---|
[7c105e8] | 115 | from sas.logger_config import SetupLogger |
---|
[899e084] | 116 | |
---|
[7c105e8] | 117 | logger = SetupLogger(__name__).config_production() |
---|
[899e084] | 118 | # Log the start of the session |
---|
[7c105e8] | 119 | logger.info(" --- SasView session started ---") |
---|
[899e084] | 120 | # Log the python version |
---|
[7c105e8] | 121 | logger.info("Python: %s" % sys.version) |
---|
| 122 | return logger |
---|
[899e084] | 123 | |
---|
| 124 | |
---|
| 125 | def setup_wx(): |
---|
| 126 | # Allow the dynamic selection of wxPython via an environment variable, when devs |
---|
| 127 | # who have multiple versions of the module installed want to pick between them. |
---|
| 128 | # This variable does not have to be set of course, and through normal usage will |
---|
| 129 | # probably not be, but this can make things a little easier when upgrading to a |
---|
| 130 | # new version of wx. |
---|
[7c105e8] | 131 | logger = logging.getLogger(__name__) |
---|
[899e084] | 132 | WX_ENV_VAR = "SASVIEW_WX_VERSION" |
---|
| 133 | if WX_ENV_VAR in os.environ: |
---|
[914ba0a] | 134 | logger.info("You have set the %s environment variable to %s." % \ |
---|
[899e084] | 135 | (WX_ENV_VAR, os.environ[WX_ENV_VAR])) |
---|
| 136 | import wxversion |
---|
| 137 | if wxversion.checkInstalled(os.environ[WX_ENV_VAR]): |
---|
[914ba0a] | 138 | logger.info("Version %s of wxPython is installed, so using that version." % os.environ[WX_ENV_VAR]) |
---|
[899e084] | 139 | wxversion.select(os.environ[WX_ENV_VAR]) |
---|
| 140 | else: |
---|
[914ba0a] | 141 | logger.error("Version %s of wxPython is not installed, so using default version." % os.environ[WX_ENV_VAR]) |
---|
[899e084] | 142 | else: |
---|
[914ba0a] | 143 | logger.info("You have not set the %s environment variable, so using default version of wxPython." % WX_ENV_VAR) |
---|
[899e084] | 144 | |
---|
| 145 | import wx |
---|
| 146 | |
---|
| 147 | try: |
---|
[914ba0a] | 148 | logger.info("Wx version: %s" % wx.__version__) |
---|
[899e084] | 149 | except: |
---|
[914ba0a] | 150 | logger.error("Wx version: error reading version") |
---|
[899e084] | 151 | |
---|
| 152 | from . import wxcruft |
---|
| 153 | wxcruft.call_later_fix() |
---|
| 154 | #wxcruft.trace_new_id() |
---|
| 155 | #Always use private .matplotlib setup to avoid conflicts with other |
---|
| 156 | #uses of matplotlib |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | def setup_mpl(): |
---|
| 160 | import sas.sasgui |
---|
[914ba0a] | 161 | # Always use private .matplotlib setup to avoid conflicts with other |
---|
[899e084] | 162 | mplconfigdir = os.path.join(sas.sasgui.get_user_dir(), '.matplotlib') |
---|
| 163 | if not os.path.exists(mplconfigdir): |
---|
| 164 | os.mkdir(mplconfigdir) |
---|
| 165 | os.environ['MPLCONFIGDIR'] = mplconfigdir |
---|
| 166 | # Set backend to WXAgg; this overrides matplotlibrc, but shouldn't override |
---|
| 167 | # mpl.use(). Note: Don't import matplotlib here since the script that |
---|
| 168 | # we are running may not actually need it; also, putting as little on the |
---|
| 169 | # path as we can |
---|
| 170 | os.environ['MPLBACKEND'] = 'WXAgg' |
---|
| 171 | |
---|
| 172 | |
---|
[914ba0a] | 173 | from matplotlib import backend_bases |
---|
| 174 | backend_bases.FigureCanvasBase.filetypes.pop('pgf', None) |
---|
| 175 | |
---|
[899e084] | 176 | def run_gui(): |
---|
[415fb82] | 177 | """ |
---|
| 178 | __main__ method for loading and running SasView |
---|
| 179 | """ |
---|
[f76bf17] | 180 | from multiprocessing import freeze_support |
---|
| 181 | freeze_support() |
---|
[899e084] | 182 | setup_logging() |
---|
| 183 | setup_wx() |
---|
| 184 | setup_mpl() |
---|
| 185 | SasView() |
---|
| 186 | |
---|
| 187 | |
---|
| 188 | def run_cli(): |
---|
[7c105e8] | 189 | setup_logging() |
---|
[899e084] | 190 | setup_mpl() |
---|
| 191 | if len(sys.argv) == 1: |
---|
| 192 | # Run sasview as an interactive python interpreter |
---|
| 193 | sys.argv = ["ipython", "--pylab"] |
---|
| 194 | from IPython import start_ipython |
---|
| 195 | sys.exit(start_ipython()) |
---|
| 196 | else: |
---|
| 197 | # Run sasview as a python script interpreter |
---|
[f76bf17] | 198 | ## Run sasview as an interactive python interpreter |
---|
[f36e01f] | 199 | # if sys.argv[1] == "-i": |
---|
[f76bf17] | 200 | # sys.argv = ["ipython", "--pylab"] |
---|
| 201 | # from IPython import start_ipython |
---|
| 202 | # sys.exit(start_ipython()) |
---|
| 203 | thing_to_run = sys.argv[1] |
---|
| 204 | sys.argv = sys.argv[1:] |
---|
| 205 | import runpy |
---|
| 206 | if os.path.exists(thing_to_run): |
---|
| 207 | runpy.run_path(thing_to_run, run_name="__main__") |
---|
| 208 | else: |
---|
| 209 | runpy.run_module(thing_to_run, run_name="__main__") |
---|
| 210 | |
---|
[f36e01f] | 211 | |
---|
[f76bf17] | 212 | if __name__ == "__main__": |
---|
[899e084] | 213 | run_gui() |
---|