source: sasview/sasview/sasview.py @ bbb8a56

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

Fixed a path issue with the SVWelcome.png file.

  • Property mode set to 100644
File size: 6.4 KB
Line 
1"""
2Base module for loading and running the main SasView application.
3"""
4################################################################################
5#This software was developed by the University of Tennessee as part of the
6#Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
7#project funded by the US National Science Foundation.
8#
9#See the license text in license.txt
10#
11#copyright 2009, University of Tennessee
12################################################################################
13import os
14import sys
15import logging
16import traceback
17
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 = ''
32
33    def write(self, buf):
34        """
35        Main logging method
36        """
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
46# Log the start of the session
47logging.info(" --- SasView session started ---")
48
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:
59    logging.info("You have set the %s environment variable to %s." % \
60                 (WX_ENV_VAR, os.environ[WX_ENV_VAR]))
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
71try:
72    logging.info("Wx version: %s" % wx.__version__)
73except:
74    logging.error("Wx version: error reading version")
75
76# The below will make sure that sasview application uses the matplotlib font
77# bundled with sasview.
78if hasattr(sys, 'frozen'):
79    mplconfigdir = os.path.join(os.path.expanduser("~"), '.matplotlib')
80    if not os.path.exists(mplconfigdir):
81        os.mkdir(mplconfigdir)
82    os.environ['MPLCONFIGDIR'] = mplconfigdir
83    reload(sys)
84    sys.setdefaultencoding("iso-8859-1")
85from sas.guiframe import gui_manager
86from sas.guiframe.gui_style import GUIFRAME
87from welcome_panel import WelcomePanel
88# For py2exe, import config here
89import local_config
90PLUGIN_MODEL_DIR = 'plugin_models'
91APP_NAME = 'SasView'
92
93class SasViewApp(gui_manager.ViewApp):
94    """
95    """
96 
97
98class SasView():
99    """
100    Main class for running the SasView application
101    """
102    def __init__(self):
103        """
104        """
105        #from gui_manager import ViewApp
106        self.gui = SasViewApp(0)
107        # Set the application manager for the GUI
108        self.gui.set_manager(self)
109        # Add perspectives to the basic application
110        # Additional perspectives can still be loaded
111        # dynamically
112        # Note: py2exe can't find dynamically loaded
113        # modules. We load the fitting module here
114        # to ensure a complete Windows executable build.
115
116        # Fitting perspective
117        try:
118            import sas.perspectives.fitting as module   
119            fitting_plug = module.Plugin()
120            self.gui.add_perspective(fitting_plug)
121        except Exception:
122            logging.error("%s: could not find Fitting plug-in module"% APP_NAME)
123            logging.error(traceback.format_exc())
124
125        # P(r) perspective
126        try:
127            import sas.perspectives.pr as module
128            pr_plug = module.Plugin(standalone=False)
129            self.gui.add_perspective(pr_plug)
130        except:
131            logging.error("%s: could not find P(r) plug-in module"% APP_NAME)
132            logging.error(traceback.format_exc())
133
134        #Invariant perspective
135        try:
136            import sas.perspectives.invariant as module
137            invariant_plug = module.Plugin(standalone=False)
138            self.gui.add_perspective(invariant_plug)
139        except:
140            logging.error("%s: could not find Invariant plug-in module"% \
141                          APP_NAME)
142            logging.error(traceback.format_exc())
143
144        #Calculator perspective   
145        try:
146            import sas.perspectives.calculator as module
147            calculator_plug = module.Plugin(standalone=False)
148            self.gui.add_perspective(calculator_plug)
149        except:
150            logging.error("%s: could not find Calculator plug-in module"% \
151                                                        APP_NAME)
152            logging.error(traceback.format_exc())
153
154
155        # Add welcome page
156        self.gui.set_welcome_panel(WelcomePanel)
157
158        # Build the GUI
159        self.gui.build_gui()
160        # delete unused model folder
161        self.gui.clean_plugin_models(PLUGIN_MODEL_DIR)
162        # Start the main loop
163        self.gui.MainLoop()
164
165
166def run():
167    """
168    __main__ method for loading and running SasView
169    """
170    from multiprocessing import freeze_support
171    freeze_support()
172    if len(sys.argv) > 1:
173        ## Run sasview as an interactive python interpreter
174        #if sys.argv[1] == "-i":
175        #    sys.argv = ["ipython", "--pylab"]
176        #    from IPython import start_ipython
177        #    sys.exit(start_ipython())
178        thing_to_run = sys.argv[1]
179        sys.argv = sys.argv[1:]
180        import runpy
181        if os.path.exists(thing_to_run):
182            runpy.run_path(thing_to_run, run_name="__main__")
183        else:
184            runpy.run_module(thing_to_run, run_name="__main__")
185    else:
186        SasView()
187
188if __name__ == "__main__":
189    run()
190
Note: See TracBrowser for help on using the repository browser.