[415fb82] | 1 | """ |
---|
| 2 | Base module for loading and running the main SasView application. |
---|
| 3 | """ |
---|
[f76bf17] | 4 | ################################################################################ |
---|
| 5 | #This software was developed by the University of Tennessee as part of the |
---|
| 6 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
[415fb82] | 7 | #project funded by the US National Science Foundation. |
---|
[f76bf17] | 8 | # |
---|
| 9 | #See the license text in license.txt |
---|
| 10 | # |
---|
| 11 | #copyright 2009, University of Tennessee |
---|
| 12 | ################################################################################ |
---|
| 13 | import os |
---|
| 14 | import sys |
---|
| 15 | import logging |
---|
[4e080980] | 16 | import traceback |
---|
| 17 | |
---|
[efe730d] | 18 | log_file = os.path.join(os.path.expanduser("~"), 'sasview.log') |
---|
[f76bf17] | 19 | logging.basicConfig(level=logging.INFO, |
---|
| 20 | format='%(asctime)s %(levelname)s %(message)s', |
---|
[efe730d] | 21 | filename=log_file, |
---|
| 22 | ) |
---|
[f76bf17] | 23 | logging.captureWarnings(True) |
---|
| 24 | |
---|
| 25 | class StreamToLogger(object): |
---|
| 26 | """ |
---|
| 27 | File-like stream object that redirects writes to a logger instance. |
---|
| 28 | """ |
---|
| 29 | def __init__(self, logger, log_level=logging.INFO): |
---|
| 30 | self.logger = logger |
---|
| 31 | self.log_level = log_level |
---|
| 32 | self.linebuf = '' |
---|
[415fb82] | 33 | |
---|
[f76bf17] | 34 | def write(self, buf): |
---|
[415fb82] | 35 | """ |
---|
| 36 | Main logging method |
---|
| 37 | """ |
---|
[f76bf17] | 38 | # Write the message to stdout so we can see it when running interactively |
---|
| 39 | sys.stdout.write(buf) |
---|
| 40 | for line in buf.rstrip().splitlines(): |
---|
| 41 | self.logger.log(self.log_level, line.rstrip()) |
---|
| 42 | |
---|
| 43 | stderr_logger = logging.getLogger('STDERR') |
---|
| 44 | sl = StreamToLogger(stderr_logger, logging.ERROR) |
---|
| 45 | sys.stderr = sl |
---|
| 46 | |
---|
[415fb82] | 47 | # Log the start of the session |
---|
| 48 | logging.info(" --- SasView session started ---") |
---|
| 49 | |
---|
[f76bf17] | 50 | # Log the python version |
---|
| 51 | logging.info("Python: %s" % sys.version) |
---|
| 52 | |
---|
| 53 | # Allow the dynamic selection of wxPython via an environment variable, when devs |
---|
| 54 | # who have multiple versions of the module installed want to pick between them. |
---|
| 55 | # This variable does not have to be set of course, and through normal usage will |
---|
| 56 | # probably not be, but this can make things a little easier when upgrading to a |
---|
| 57 | # new version of wx. |
---|
| 58 | WX_ENV_VAR = "SASVIEW_WX_VERSION" |
---|
| 59 | if WX_ENV_VAR in os.environ: |
---|
[415fb82] | 60 | logging.info("You have set the %s environment variable to %s." % \ |
---|
| 61 | (WX_ENV_VAR, os.environ[WX_ENV_VAR])) |
---|
[f76bf17] | 62 | import wxversion |
---|
| 63 | if wxversion.checkInstalled(os.environ[WX_ENV_VAR]): |
---|
| 64 | logging.info("Version %s of wxPython is installed, so using that version." % os.environ[WX_ENV_VAR]) |
---|
| 65 | wxversion.select(os.environ[WX_ENV_VAR]) |
---|
| 66 | else: |
---|
| 67 | logging.error("Version %s of wxPython is not installed, so using default version." % os.environ[WX_ENV_VAR]) |
---|
| 68 | else: |
---|
| 69 | logging.info("You have not set the %s environment variable, so using default version of wxPython." % WX_ENV_VAR) |
---|
| 70 | |
---|
| 71 | import wx |
---|
[6f3fea2] | 72 | |
---|
[f76bf17] | 73 | try: |
---|
| 74 | logging.info("Wx version: %s" % wx.__version__) |
---|
| 75 | except: |
---|
| 76 | logging.error("Wx version: error reading version") |
---|
[415fb82] | 77 | |
---|
[efe730d] | 78 | from . import wxcruft |
---|
[6f16e25] | 79 | wxcruft.call_later_fix() |
---|
[6f3fea2] | 80 | #wxcruft.trace_new_id() |
---|
[558d64e] | 81 | #Always use private .matplotlib setup to avoid conflicts with other |
---|
| 82 | #uses of matplotlib |
---|
[efe730d] | 83 | |
---|
| 84 | import sas.sasgui |
---|
| 85 | mplconfigdir = os.path.join(sas.sasgui.get_user_dir(), '.matplotlib') |
---|
[558d64e] | 86 | if not os.path.exists(mplconfigdir): |
---|
| 87 | os.mkdir(mplconfigdir) |
---|
| 88 | os.environ['MPLCONFIGDIR'] = mplconfigdir |
---|
| 89 | reload(sys) |
---|
| 90 | sys.setdefaultencoding("iso-8859-1") |
---|
[efe730d] | 91 | |
---|
| 92 | |
---|
[d85c194] | 93 | from sas.sasgui.guiframe import gui_manager |
---|
[efe730d] | 94 | from .welcome_panel import WelcomePanel |
---|
[f76bf17] | 95 | PLUGIN_MODEL_DIR = 'plugin_models' |
---|
| 96 | APP_NAME = 'SasView' |
---|
| 97 | |
---|
| 98 | class SasView(): |
---|
| 99 | """ |
---|
[415fb82] | 100 | Main class for running the SasView application |
---|
[f76bf17] | 101 | """ |
---|
| 102 | def __init__(self): |
---|
| 103 | """ |
---|
| 104 | """ |
---|
| 105 | #from gui_manager import ViewApp |
---|
[78f75d02] | 106 | self.gui = gui_manager.SasViewApp(0) |
---|
[f76bf17] | 107 | # Set the application manager for the GUI |
---|
| 108 | self.gui.set_manager(self) |
---|
| 109 | # Add perspectives to the basic application |
---|
| 110 | # Additional perspectives can still be loaded |
---|
| 111 | # dynamically |
---|
| 112 | # Note: py2exe can't find dynamically loaded |
---|
| 113 | # modules. We load the fitting module here |
---|
| 114 | # to ensure a complete Windows executable build. |
---|
| 115 | |
---|
| 116 | # Fitting perspective |
---|
| 117 | try: |
---|
[d85c194] | 118 | import sas.sasgui.perspectives.fitting as module |
---|
[f76bf17] | 119 | fitting_plug = module.Plugin() |
---|
| 120 | self.gui.add_perspective(fitting_plug) |
---|
[415fb82] | 121 | except Exception: |
---|
[4e080980] | 122 | logging.error("%s: could not find Fitting plug-in module"% APP_NAME) |
---|
| 123 | logging.error(traceback.format_exc()) |
---|
[f76bf17] | 124 | |
---|
| 125 | # P(r) perspective |
---|
| 126 | try: |
---|
[d85c194] | 127 | import sas.sasgui.perspectives.pr as module |
---|
[f21d496] | 128 | pr_plug = module.Plugin() |
---|
[f76bf17] | 129 | self.gui.add_perspective(pr_plug) |
---|
| 130 | except: |
---|
| 131 | logging.error("%s: could not find P(r) plug-in module"% APP_NAME) |
---|
[4e080980] | 132 | logging.error(traceback.format_exc()) |
---|
| 133 | |
---|
[f76bf17] | 134 | #Invariant perspective |
---|
| 135 | try: |
---|
[d85c194] | 136 | import sas.sasgui.perspectives.invariant as module |
---|
[f21d496] | 137 | invariant_plug = module.Plugin() |
---|
[f76bf17] | 138 | self.gui.add_perspective(invariant_plug) |
---|
[b699768] | 139 | except Exception as e : |
---|
[f76bf17] | 140 | logging.error("%s: could not find Invariant plug-in module"% \ |
---|
| 141 | APP_NAME) |
---|
[4e080980] | 142 | logging.error(traceback.format_exc()) |
---|
| 143 | |
---|
[f76bf17] | 144 | #Calculator perspective |
---|
| 145 | try: |
---|
[d85c194] | 146 | import sas.sasgui.perspectives.calculator as module |
---|
[f21d496] | 147 | calculator_plug = module.Plugin() |
---|
[f76bf17] | 148 | self.gui.add_perspective(calculator_plug) |
---|
| 149 | except: |
---|
| 150 | logging.error("%s: could not find Calculator plug-in module"% \ |
---|
| 151 | APP_NAME) |
---|
[4e080980] | 152 | logging.error(traceback.format_exc()) |
---|
| 153 | |
---|
[415fb82] | 154 | |
---|
[f76bf17] | 155 | # Add welcome page |
---|
| 156 | self.gui.set_welcome_panel(WelcomePanel) |
---|
[415fb82] | 157 | |
---|
[f76bf17] | 158 | # Build the GUI |
---|
| 159 | self.gui.build_gui() |
---|
[415fb82] | 160 | # delete unused model folder |
---|
[f76bf17] | 161 | self.gui.clean_plugin_models(PLUGIN_MODEL_DIR) |
---|
| 162 | # Start the main loop |
---|
| 163 | self.gui.MainLoop() |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | def run(): |
---|
[415fb82] | 167 | """ |
---|
| 168 | __main__ method for loading and running SasView |
---|
| 169 | """ |
---|
[f76bf17] | 170 | from multiprocessing import freeze_support |
---|
| 171 | freeze_support() |
---|
| 172 | if len(sys.argv) > 1: |
---|
| 173 | ## Run sasview as an interactive python interpreter |
---|
[efe730d] | 174 | if sys.argv[1] == "-i": |
---|
| 175 | sys.argv = ["ipython", "--pylab"] |
---|
| 176 | from IPython import start_ipython |
---|
| 177 | sys.exit(start_ipython()) |
---|
[f76bf17] | 178 | thing_to_run = sys.argv[1] |
---|
| 179 | sys.argv = sys.argv[1:] |
---|
| 180 | import runpy |
---|
| 181 | if os.path.exists(thing_to_run): |
---|
| 182 | runpy.run_path(thing_to_run, run_name="__main__") |
---|
| 183 | else: |
---|
| 184 | runpy.run_module(thing_to_run, run_name="__main__") |
---|
| 185 | else: |
---|
| 186 | SasView() |
---|
| 187 | |
---|
| 188 | if __name__ == "__main__": |
---|
| 189 | run() |
---|
| 190 | |
---|