source: sasview/sasview/sasview.py @ 4e080980

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.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 4e080980 was 4e080980, checked in by Paul Kienzle <pkienzle@…>, 9 years ago

include traceback in sasview errors so we can debug more easily

  • Property mode set to 100644
File size: 6.2 KB
RevLine 
[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################################################################################
11import os
12import sys
13import logging
[4e080980]14import traceback
15
[f76bf17]16logging.basicConfig(level=logging.INFO,
17                    format='%(asctime)s %(levelname)s %(message)s',
18                    filename=os.path.join(os.path.expanduser("~"),
19                                          'sasview.log'))
20logging.captureWarnings(True)
21
22class 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
37stderr_logger = logging.getLogger('STDERR')
38sl = StreamToLogger(stderr_logger, logging.ERROR)
39sys.stderr = sl
40
41# Log the python version
42logging.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.
49WX_ENV_VAR = "SASVIEW_WX_VERSION"
50if 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])
58else:
59    logging.info("You have not set the %s environment variable, so using default version of wxPython." % WX_ENV_VAR)
60
61import wx
62try:
63    logging.info("Wx version: %s" % wx.__version__)
64except:
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.
69if 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")
76from sas.guiframe import gui_manager
77from sas.guiframe.gui_style import GUIFRAME
78from welcome_panel import WelcomePanel
79# For py2exe, import config here
80import local_config
81PLUGIN_MODEL_DIR = 'plugin_models'
82APP_NAME = 'SasView'
83
84class SasViewApp(gui_manager.ViewApp):
85    """
86    """
87 
88
89class 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
157def 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
176if __name__ == "__main__":
177    run()
178
Note: See TracBrowser for help on using the repository browser.