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