source: sasview/src/sas/sasview/sasview.py @ 7c105e8

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalcmagnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 7c105e8 was 7c105e8, checked in by Paul Kienzle <pkienzle@…>, 7 years ago

make run.py work again

  • Property mode set to 100644
File size: 7.3 KB
Line 
1# -*- coding: utf-8 -*-
2"""
3Base module for loading and running the main SasView application.
4"""
5################################################################################
6# This software was developed by the University of Tennessee as part of the
7# Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
8# project funded by the US National Science Foundation.
9#
10# See the license text in license.txt
11#
12# copyright 2009, University of Tennessee
13################################################################################
14import os
15import os.path
16import sys
17import traceback
18import logging
19
20reload(sys)
21sys.setdefaultencoding("iso-8859-1")
22
23PLUGIN_MODEL_DIR = 'plugin_models'
24APP_NAME = 'SasView'
25
26class SasView():
27    """
28    Main class for running the SasView application
29    """
30    def __init__(self):
31        """
32        """
33        logger = logging.getLogger(__name__)
34
35        from sas.sasgui.guiframe.gui_manager import SasViewApp
36        self.gui = SasViewApp(0)
37        # Set the application manager for the GUI
38        self.gui.set_manager(self)
39        # Add perspectives to the basic application
40        # Additional perspectives can still be loaded
41        # dynamically
42        # Note: py2exe can't find dynamically loaded
43        # modules. We load the fitting module here
44        # to ensure a complete Windows executable build.
45
46        # Fitting perspective
47        try:
48            import sas.sasgui.perspectives.fitting as module
49            fitting_plug = module.Plugin()
50            self.gui.add_perspective(fitting_plug)
51        except Exception:
52            logger.error("%s: could not find Fitting plug-in module"% APP_NAME)
53            logger.error(traceback.format_exc())
54
55        # P(r) perspective
56        try:
57            import sas.sasgui.perspectives.pr as module
58            pr_plug = module.Plugin()
59            self.gui.add_perspective(pr_plug)
60        except:
61            logger.error("%s: could not find P(r) plug-in module"% APP_NAME)
62            logger.error(traceback.format_exc())
63
64        # Invariant perspective
65        try:
66            import sas.sasgui.perspectives.invariant as module
67            invariant_plug = module.Plugin()
68            self.gui.add_perspective(invariant_plug)
69        except Exception as e :
70            logger.error("%s: could not find Invariant plug-in module"% \
71                          APP_NAME)
72            logger.error(traceback.format_exc())
73
74        # Corfunc perspective
75        try:
76            import sas.sasgui.perspectives.corfunc as module
77            corfunc_plug = module.Plugin()
78            self.gui.add_perspective(corfunc_plug)
79        except:
80            logger.error("Unable to load corfunc module")
81
82        # Calculator perspective
83        try:
84            import sas.sasgui.perspectives.calculator as module
85            calculator_plug = module.Plugin()
86            self.gui.add_perspective(calculator_plug)
87        except:
88            logger.error("%s: could not find Calculator plug-in module"% \
89                                                        APP_NAME)
90            logger.error(traceback.format_exc())
91
92        # File converter tool
93        try:
94            import sas.sasgui.perspectives.file_converter as module
95            converter_plug = module.Plugin()
96            self.gui.add_perspective(converter_plug)
97        except:
98            logger.error("%s: could not find File Converter plug-in module"% \
99                                                        APP_NAME)
100            logger.error(traceback.format_exc())
101
102        # Add welcome page
103        from .welcome_panel import WelcomePanel
104        self.gui.set_welcome_panel(WelcomePanel)
105
106        # Build the GUI
107        self.gui.build_gui()
108        # delete unused model folder
109        self.gui.clean_plugin_models(PLUGIN_MODEL_DIR)
110        # Start the main loop
111        self.gui.MainLoop()
112
113
114def setup_logging():
115    from sas.logger_config import SetupLogger
116
117    logger = SetupLogger(__name__).config_production()
118    # Log the start of the session
119    logger.info(" --- SasView session started ---")
120    # Log the python version
121    logger.info("Python: %s" % sys.version)
122    return logger
123
124
125def setup_wx():
126    # Allow the dynamic selection of wxPython via an environment variable, when devs
127    # who have multiple versions of the module installed want to pick between them.
128    # This variable does not have to be set of course, and through normal usage will
129    # probably not be, but this can make things a little easier when upgrading to a
130    # new version of wx.
131    logger = logging.getLogger(__name__)
132    WX_ENV_VAR = "SASVIEW_WX_VERSION"
133    if WX_ENV_VAR in os.environ:
134        logger.info("You have set the %s environment variable to %s." % \
135                     (WX_ENV_VAR, os.environ[WX_ENV_VAR]))
136        import wxversion
137        if wxversion.checkInstalled(os.environ[WX_ENV_VAR]):
138            logger.info("Version %s of wxPython is installed, so using that version." % os.environ[WX_ENV_VAR])
139            wxversion.select(os.environ[WX_ENV_VAR])
140        else:
141            logger.error("Version %s of wxPython is not installed, so using default version." % os.environ[WX_ENV_VAR])
142    else:
143        logger.info("You have not set the %s environment variable, so using default version of wxPython." % WX_ENV_VAR)
144
145    import wx
146
147    try:
148        logger.info("Wx version: %s" % wx.__version__)
149    except:
150        logger.error("Wx version: error reading version")
151
152    from . import wxcruft
153    wxcruft.call_later_fix()
154    #wxcruft.trace_new_id()
155    #Always use private .matplotlib setup to avoid conflicts with other
156    #uses of matplotlib
157
158
159def setup_mpl():
160    import sas.sasgui
161    # Always use private .matplotlib setup to avoid conflicts with other
162    mplconfigdir = os.path.join(sas.sasgui.get_user_dir(), '.matplotlib')
163    if not os.path.exists(mplconfigdir):
164        os.mkdir(mplconfigdir)
165    os.environ['MPLCONFIGDIR'] = mplconfigdir
166    # Set backend to WXAgg; this overrides matplotlibrc, but shouldn't override
167    # mpl.use().  Note: Don't import matplotlib here since the script that
168    # we are running may not actually need it; also, putting as little on the
169    # path as we can
170    os.environ['MPLBACKEND'] = 'WXAgg'
171
172
173    from matplotlib import backend_bases
174    backend_bases.FigureCanvasBase.filetypes.pop('pgf', None)
175
176def run_gui():
177    """
178    __main__ method for loading and running SasView
179    """
180    from multiprocessing import freeze_support
181    freeze_support()
182    setup_logging()
183    setup_wx()
184    setup_mpl()
185    SasView()
186
187
188def run_cli():
189    setup_logging()
190    setup_mpl()
191    if len(sys.argv) == 1:
192        # Run sasview as an interactive python interpreter
193        sys.argv = ["ipython", "--pylab"]
194        from IPython import start_ipython
195        sys.exit(start_ipython())
196    else:
197        # Run sasview as a python script interpreter
198        ## Run sasview as an interactive python interpreter
199        # if sys.argv[1] == "-i":
200        #    sys.argv = ["ipython", "--pylab"]
201        #    from IPython import start_ipython
202        #    sys.exit(start_ipython())
203        thing_to_run = sys.argv[1]
204        sys.argv = sys.argv[1:]
205        import runpy
206        if os.path.exists(thing_to_run):
207            runpy.run_path(thing_to_run, run_name="__main__")
208        else:
209            runpy.run_module(thing_to_run, run_name="__main__")
210
211
212if __name__ == "__main__":
213    run_gui()
Note: See TracBrowser for help on using the repository browser.