[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 |
---|
| 71 | try: |
---|
| 72 | logging.info("Wx version: %s" % wx.__version__) |
---|
| 73 | except: |
---|
| 74 | logging.error("Wx version: error reading version") |
---|
[415fb82] | 75 | |
---|
| 76 | # The below will make sure that sasview application uses the matplotlib font |
---|
| 77 | # bundled with sasview. |
---|
[f76bf17] | 78 | if hasattr(sys, 'frozen'): |
---|
| 79 | mplconfigdir = os.path.join(os.path.expanduser("~"), '.matplotlib') |
---|
| 80 | if not os.path.exists(mplconfigdir): |
---|
| 81 | os.mkdir(mplconfigdir) |
---|
| 82 | os.environ['MPLCONFIGDIR'] = mplconfigdir |
---|
| 83 | reload(sys) |
---|
| 84 | sys.setdefaultencoding("iso-8859-1") |
---|
| 85 | from sas.guiframe import gui_manager |
---|
| 86 | from sas.guiframe.gui_style import GUIFRAME |
---|
[5f0be1f] | 87 | from welcome_panel import WelcomePanel |
---|
[f76bf17] | 88 | # For py2exe, import config here |
---|
[5f0be1f] | 89 | import local_config |
---|
[f76bf17] | 90 | PLUGIN_MODEL_DIR = 'plugin_models' |
---|
| 91 | APP_NAME = 'SasView' |
---|
| 92 | |
---|
| 93 | class SasView(): |
---|
| 94 | """ |
---|
[415fb82] | 95 | Main class for running the SasView application |
---|
[f76bf17] | 96 | """ |
---|
| 97 | def __init__(self): |
---|
| 98 | """ |
---|
| 99 | """ |
---|
| 100 | #from gui_manager import ViewApp |
---|
[78f75d02] | 101 | self.gui = gui_manager.SasViewApp(0) |
---|
[f76bf17] | 102 | # Set the application manager for the GUI |
---|
| 103 | self.gui.set_manager(self) |
---|
| 104 | # Add perspectives to the basic application |
---|
| 105 | # Additional perspectives can still be loaded |
---|
| 106 | # dynamically |
---|
| 107 | # Note: py2exe can't find dynamically loaded |
---|
| 108 | # modules. We load the fitting module here |
---|
| 109 | # to ensure a complete Windows executable build. |
---|
| 110 | |
---|
| 111 | # Fitting perspective |
---|
| 112 | try: |
---|
| 113 | import sas.perspectives.fitting as module |
---|
| 114 | fitting_plug = module.Plugin() |
---|
| 115 | self.gui.add_perspective(fitting_plug) |
---|
[415fb82] | 116 | except Exception: |
---|
[4e080980] | 117 | logging.error("%s: could not find Fitting plug-in module"% APP_NAME) |
---|
| 118 | logging.error(traceback.format_exc()) |
---|
[f76bf17] | 119 | |
---|
| 120 | # P(r) perspective |
---|
| 121 | try: |
---|
[415fb82] | 122 | import sas.perspectives.pr as module |
---|
[f21d496] | 123 | pr_plug = module.Plugin() |
---|
[f76bf17] | 124 | self.gui.add_perspective(pr_plug) |
---|
| 125 | except: |
---|
| 126 | logging.error("%s: could not find P(r) plug-in module"% APP_NAME) |
---|
[4e080980] | 127 | logging.error(traceback.format_exc()) |
---|
| 128 | |
---|
[f76bf17] | 129 | #Invariant perspective |
---|
| 130 | try: |
---|
[415fb82] | 131 | import sas.perspectives.invariant as module |
---|
[f21d496] | 132 | invariant_plug = module.Plugin() |
---|
[f76bf17] | 133 | self.gui.add_perspective(invariant_plug) |
---|
| 134 | except: |
---|
| 135 | logging.error("%s: could not find Invariant plug-in module"% \ |
---|
| 136 | APP_NAME) |
---|
[4e080980] | 137 | logging.error(traceback.format_exc()) |
---|
| 138 | |
---|
[f76bf17] | 139 | #Calculator perspective |
---|
| 140 | try: |
---|
[415fb82] | 141 | import sas.perspectives.calculator as module |
---|
[f21d496] | 142 | calculator_plug = module.Plugin() |
---|
[f76bf17] | 143 | self.gui.add_perspective(calculator_plug) |
---|
| 144 | except: |
---|
| 145 | logging.error("%s: could not find Calculator plug-in module"% \ |
---|
| 146 | APP_NAME) |
---|
[4e080980] | 147 | logging.error(traceback.format_exc()) |
---|
| 148 | |
---|
[415fb82] | 149 | |
---|
[f76bf17] | 150 | # Add welcome page |
---|
| 151 | self.gui.set_welcome_panel(WelcomePanel) |
---|
[415fb82] | 152 | |
---|
[f76bf17] | 153 | # Build the GUI |
---|
| 154 | self.gui.build_gui() |
---|
[415fb82] | 155 | # delete unused model folder |
---|
[f76bf17] | 156 | self.gui.clean_plugin_models(PLUGIN_MODEL_DIR) |
---|
| 157 | # Start the main loop |
---|
| 158 | self.gui.MainLoop() |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | def run(): |
---|
[415fb82] | 162 | """ |
---|
| 163 | __main__ method for loading and running SasView |
---|
| 164 | """ |
---|
[f76bf17] | 165 | from multiprocessing import freeze_support |
---|
| 166 | freeze_support() |
---|
| 167 | if len(sys.argv) > 1: |
---|
| 168 | ## Run sasview as an interactive python interpreter |
---|
| 169 | #if sys.argv[1] == "-i": |
---|
| 170 | # sys.argv = ["ipython", "--pylab"] |
---|
| 171 | # from IPython import start_ipython |
---|
| 172 | # sys.exit(start_ipython()) |
---|
| 173 | thing_to_run = sys.argv[1] |
---|
| 174 | sys.argv = sys.argv[1:] |
---|
| 175 | import runpy |
---|
| 176 | if os.path.exists(thing_to_run): |
---|
| 177 | runpy.run_path(thing_to_run, run_name="__main__") |
---|
| 178 | else: |
---|
| 179 | runpy.run_module(thing_to_run, run_name="__main__") |
---|
| 180 | else: |
---|
| 181 | SasView() |
---|
| 182 | |
---|
| 183 | if __name__ == "__main__": |
---|
| 184 | run() |
---|
| 185 | |
---|