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