source: sasview/sasview/sasview.py @ 77d92cd

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 77d92cd was 77d92cd, checked in by lewis, 8 years ago

Add backbone for file converter

  • Property mode set to 100644
File size: 7.0 KB
RevLine 
[415fb82]1"""
2Base 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################################################################################
13import os
14import sys
15import logging
[4e080980]16import traceback
17
[f76bf17]18logging.basicConfig(level=logging.INFO,
19                    format='%(asctime)s %(levelname)s %(message)s',
20                    filename=os.path.join(os.path.expanduser("~"),
21                                          'sasview.log'))
22logging.captureWarnings(True)
23
24class 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
42stderr_logger = logging.getLogger('STDERR')
43sl = StreamToLogger(stderr_logger, logging.ERROR)
44sys.stderr = sl
45
[415fb82]46# Log the start of the session
47logging.info(" --- SasView session started ---")
48
[f76bf17]49# Log the python version
50logging.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.
57WX_ENV_VAR = "SASVIEW_WX_VERSION"
58if 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])
67else:
68    logging.info("You have not set the %s environment variable, so using default version of wxPython." % WX_ENV_VAR)
69
70import wx
[6f3fea2]71
[f76bf17]72try:
73    logging.info("Wx version: %s" % wx.__version__)
74except:
75    logging.error("Wx version: error reading version")
[415fb82]76
[85130cb]77import wxcruft
[6f16e25]78wxcruft.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]84sasdir = os.path.join(os.path.expanduser("~"),'.sasview')
85if not os.path.exists(sasdir):
86    os.mkdir(sasdir)
[558d64e]87mplconfigdir = os.path.join(os.path.expanduser("~"),'.sasview','.matplotlib')
88if not os.path.exists(mplconfigdir):
89    os.mkdir(mplconfigdir)
90os.environ['MPLCONFIGDIR'] = mplconfigdir
91reload(sys)
92sys.setdefaultencoding("iso-8859-1")
[d85c194]93from sas.sasgui.guiframe import gui_manager
94from sas.sasgui.guiframe.gui_style import GUIFRAME
[5f0be1f]95from welcome_panel import WelcomePanel
[f76bf17]96# For py2exe, import config here
[5f0be1f]97import local_config
[f76bf17]98PLUGIN_MODEL_DIR = 'plugin_models'
99APP_NAME = 'SasView'
100
101class SasView():
102    """
[415fb82]103    Main class for running the SasView application
[f76bf17]104    """
105    def __init__(self):
106        """
107        """
108        #from gui_manager import ViewApp
[78f75d02]109        self.gui = gui_manager.SasViewApp(0)
[f76bf17]110        # Set the application manager for the GUI
111        self.gui.set_manager(self)
112        # Add perspectives to the basic application
113        # Additional perspectives can still be loaded
114        # dynamically
115        # Note: py2exe can't find dynamically loaded
116        # modules. We load the fitting module here
117        # to ensure a complete Windows executable build.
118
119        # Fitting perspective
120        try:
[77d92cd]121            import sas.sasgui.perspectives.fitting as module
[f76bf17]122            fitting_plug = module.Plugin()
123            self.gui.add_perspective(fitting_plug)
[415fb82]124        except Exception:
[4e080980]125            logging.error("%s: could not find Fitting plug-in module"% APP_NAME)
126            logging.error(traceback.format_exc())
[f76bf17]127
128        # P(r) perspective
129        try:
[d85c194]130            import sas.sasgui.perspectives.pr as module
[f21d496]131            pr_plug = module.Plugin()
[f76bf17]132            self.gui.add_perspective(pr_plug)
133        except:
134            logging.error("%s: could not find P(r) plug-in module"% APP_NAME)
[4e080980]135            logging.error(traceback.format_exc())
136
[f76bf17]137        #Invariant perspective
138        try:
[d85c194]139            import sas.sasgui.perspectives.invariant as module
[f21d496]140            invariant_plug = module.Plugin()
[f76bf17]141            self.gui.add_perspective(invariant_plug)
[b699768]142        except Exception as e :
[f76bf17]143            logging.error("%s: could not find Invariant plug-in module"% \
144                          APP_NAME)
[4e080980]145            logging.error(traceback.format_exc())
146
[77d92cd]147        #Calculator perspective
[f76bf17]148        try:
[d85c194]149            import sas.sasgui.perspectives.calculator as module
[f21d496]150            calculator_plug = module.Plugin()
[f76bf17]151            self.gui.add_perspective(calculator_plug)
152        except:
153            logging.error("%s: could not find Calculator plug-in module"% \
154                                                        APP_NAME)
[4e080980]155            logging.error(traceback.format_exc())
156
[77d92cd]157        # File converter tool
158        try:
159            import sas.sasgui.perspectives.file_converter as module
160            converter_plug = module.Plugin()
161            self.gui.add_perspective(converter_plug)
162        except:
163            logging.error("%s: could not find File Converter plug-in module"% \
164                                                        APP_NAME)
165            logging.error(traceback.format_exc())
166
[415fb82]167
[f76bf17]168        # Add welcome page
169        self.gui.set_welcome_panel(WelcomePanel)
[415fb82]170
[f76bf17]171        # Build the GUI
172        self.gui.build_gui()
[415fb82]173        # delete unused model folder
[f76bf17]174        self.gui.clean_plugin_models(PLUGIN_MODEL_DIR)
175        # Start the main loop
176        self.gui.MainLoop()
177
178
179def run():
[415fb82]180    """
181    __main__ method for loading and running SasView
182    """
[f76bf17]183    from multiprocessing import freeze_support
184    freeze_support()
185    if len(sys.argv) > 1:
186        ## Run sasview as an interactive python interpreter
187        #if sys.argv[1] == "-i":
188        #    sys.argv = ["ipython", "--pylab"]
189        #    from IPython import start_ipython
190        #    sys.exit(start_ipython())
191        thing_to_run = sys.argv[1]
192        sys.argv = sys.argv[1:]
193        import runpy
194        if os.path.exists(thing_to_run):
195            runpy.run_path(thing_to_run, run_name="__main__")
196        else:
197            runpy.run_module(thing_to_run, run_name="__main__")
198    else:
199        SasView()
200
201if __name__ == "__main__":
202    run()
Note: See TracBrowser for help on using the repository browser.