source: sasview/sasview/sasview.py @ 517f3d4

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 517f3d4 was 517f3d4, checked in by Doucet, Mathieu <doucetm@…>, 9 years ago

Write stderr to sasview.log

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