source: sasview/sasview/sasview.py @ f76bf17

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 f76bf17 was f76bf17, checked in by krzywon, 9 years ago

A fix ticket for #324 - logging now captures and records warnings in the
cosole log. Removed a redundant separator in the Fitting menu that was
there from when the fitting engines were still available. Changed the
logging type of a successful data load from a warning to info.

  • Property mode set to 100644
File size: 6.2 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'))
19logging.captureWarnings(True)
20
21class StreamToLogger(object):
22    """
23        File-like stream object that redirects writes to a logger instance.
24    """
25    def __init__(self, logger, log_level=logging.INFO):
26        self.logger = logger
27        self.log_level = log_level
28        self.linebuf = ''
29 
30    def write(self, buf):
31        # Write the message to stdout so we can see it when running interactively
32        sys.stdout.write(buf)
33        for line in buf.rstrip().splitlines():
34            self.logger.log(self.log_level, line.rstrip())
35
36stderr_logger = logging.getLogger('STDERR')
37sl = StreamToLogger(stderr_logger, logging.ERROR)
38sys.stderr = sl
39
40# Log the python version
41logging.info("Python: %s" % sys.version)
42
43# Allow the dynamic selection of wxPython via an environment variable, when devs
44# who have multiple versions of the module installed want to pick between them.
45# This variable does not have to be set of course, and through normal usage will
46# probably not be, but this can make things a little easier when upgrading to a
47# new version of wx.
48WX_ENV_VAR = "SASVIEW_WX_VERSION"
49if WX_ENV_VAR in os.environ:
50    logging.info("You have set the %s environment variable to %s." % (WX_ENV_VAR, os.environ[WX_ENV_VAR]))
51    import wxversion
52    if wxversion.checkInstalled(os.environ[WX_ENV_VAR]):
53        logging.info("Version %s of wxPython is installed, so using that version." % os.environ[WX_ENV_VAR])
54        wxversion.select(os.environ[WX_ENV_VAR])
55    else:
56        logging.error("Version %s of wxPython is not installed, so using default version." % os.environ[WX_ENV_VAR])
57else:
58    logging.info("You have not set the %s environment variable, so using default version of wxPython." % WX_ENV_VAR)
59
60import wx
61try:
62    logging.info("Wx version: %s" % wx.__version__)
63except:
64    logging.error("Wx version: error reading version")
65   
66# The below will make sure that sasview application uses the matplotlib font
67# bundled with sasview.
68if hasattr(sys, 'frozen'):
69    mplconfigdir = os.path.join(os.path.expanduser("~"), '.matplotlib')
70    if not os.path.exists(mplconfigdir):
71        os.mkdir(mplconfigdir)
72    os.environ['MPLCONFIGDIR'] = mplconfigdir
73    reload(sys)
74    sys.setdefaultencoding("iso-8859-1")
75from sas.guiframe import gui_manager
76from sas.guiframe.gui_style import GUIFRAME
77from welcome_panel import WelcomePanel
78# For py2exe, import config here
79import local_config
80PLUGIN_MODEL_DIR = 'plugin_models'
81APP_NAME = 'SasView'
82
83class SasViewApp(gui_manager.ViewApp):
84    """
85    """
86 
87
88class SasView():
89    """
90    """
91    def __init__(self):
92        """
93        """
94        #from gui_manager import ViewApp
95        self.gui = SasViewApp(0) 
96        # Set the application manager for the GUI
97        self.gui.set_manager(self)
98        # Add perspectives to the basic application
99        # Additional perspectives can still be loaded
100        # dynamically
101        # Note: py2exe can't find dynamically loaded
102        # modules. We load the fitting module here
103        # to ensure a complete Windows executable build.
104
105        # Fitting perspective
106        try:
107            import sas.perspectives.fitting as module   
108            fitting_plug = module.Plugin()
109            self.gui.add_perspective(fitting_plug)
110        except Exception as inst:
111            logging.error("Fitting problems: " + str(inst))
112            logging.error("%s: could not find Fitting plug-in module"% APP_NAME) 
113            logging.error(sys.exc_value)
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)
122            logging.error(sys.exc_value) 
123       
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)
133            logging.error(sys.exc_value) 
134       
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)
143            logging.error(sys.exc_value) 
144       
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.