source: sasview/sasview/sasview.py @ 69363c7

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 69363c7 was 69363c7, checked in by Paul Kienzle <pkienzle@…>, 7 years ago

Merge branch 'master' into ticket-853-fit-gui-to-calc

  • Property mode set to 100644
File size: 7.1 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
18
19from sas.sasview.logger_config import SetupLogger
20
21logger = SetupLogger(__name__).config_production()
22
23
24# Log the start of the session
25logger.info(" --- SasView session started ---")
26# Log the python version
27logger.info("Python: %s" % sys.version)
28
29# Allow the dynamic selection of wxPython via an environment variable, when devs
30# who have multiple versions of the module installed want to pick between them.
31# This variable does not have to be set of course, and through normal usage will
32# probably not be, but this can make things a little easier when upgrading to a
33# new version of wx.
34WX_ENV_VAR = "SASVIEW_WX_VERSION"
35if WX_ENV_VAR in os.environ:
36    logger.info("You have set the %s environment variable to %s." % \
37                 (WX_ENV_VAR, os.environ[WX_ENV_VAR]))
38    import wxversion
39    if wxversion.checkInstalled(os.environ[WX_ENV_VAR]):
40        logger.info("Version %s of wxPython is installed, so using that version." % os.environ[WX_ENV_VAR])
41        wxversion.select(os.environ[WX_ENV_VAR])
42    else:
43        logger.error("Version %s of wxPython is not installed, so using default version." % os.environ[WX_ENV_VAR])
44else:
45    logger.info("You have not set the %s environment variable, so using default version of wxPython." % WX_ENV_VAR)
46
47import wx
48
49try:
50    logger.info("Wx version: %s" % wx.__version__)
51except:
52    logger.error("Wx version: error reading version")
53
54import wxcruft
55wxcruft.call_later_fix()
56# wxcruft.trace_new_id()
57
58# Always use private .matplotlib setup to avoid conflicts with other
59# uses of matplotlib
60# Have to check if .sasview exists first
61sasdir = os.path.join(os.path.expanduser("~"),'.sasview')
62if not os.path.exists(sasdir):
63    os.mkdir(sasdir)
64mplconfigdir = os.path.join(os.path.expanduser("~"),'.sasview','.matplotlib')
65if not os.path.exists(mplconfigdir):
66    os.mkdir(mplconfigdir)
67os.environ['MPLCONFIGDIR'] = mplconfigdir
68reload(sys)
69sys.setdefaultencoding("iso-8859-1")
70from sas.sasgui.guiframe import gui_manager
71from sas.sasgui.guiframe.gui_style import GUIFRAME
72from welcome_panel import WelcomePanel
73
74PLUGIN_MODEL_DIR = 'plugin_models'
75APP_NAME = 'SasView'
76
77# Set SAS_MODELPATH so sasmodels can find our custom models
78os.environ['SAS_MODELPATH'] = os.path.join(sasdir, PLUGIN_MODEL_DIR)
79
80from matplotlib import backend_bases
81backend_bases.FigureCanvasBase.filetypes.pop('pgf', None)
82
83class SasView():
84    """
85    Main class for running the SasView application
86    """
87    def __init__(self):
88        """
89        """
90        # from gui_manager import ViewApp
91        self.gui = gui_manager.SasViewApp(0)
92        # Set the application manager for the GUI
93        self.gui.set_manager(self)
94        # Add perspectives to the basic application
95        # Additional perspectives can still be loaded
96        # dynamically
97        # Note: py2exe can't find dynamically loaded
98        # modules. We load the fitting module here
99        # to ensure a complete Windows executable build.
100
101        # Rebuild .sasview/categories.json.  This triggers a load of sasmodels
102        # and all the plugins.
103        try:
104            from sas.sascalc.fit.models import ModelManager
105            from sas.sasgui.guiframe.CategoryInstaller import CategoryInstaller
106            model_list = ModelManager().cat_model_list()
107            CategoryInstaller.check_install(model_list=model_list)
108        except Exception:
109            logger.error("%s: could not load SasView models")
110            logger.error(traceback.format_exc())
111
112        # Fitting perspective
113        try:
114            import sas.sasgui.perspectives.fitting as module
115            fitting_plug = module.Plugin()
116            self.gui.add_perspective(fitting_plug)
117        except Exception:
118            logger.error("%s: could not find Fitting plug-in module"% APP_NAME)
119            logger.error(traceback.format_exc())
120
121        # P(r) perspective
122        try:
123            import sas.sasgui.perspectives.pr as module
124            pr_plug = module.Plugin()
125            self.gui.add_perspective(pr_plug)
126        except:
127            logger.error("%s: could not find P(r) plug-in module"% APP_NAME)
128            logger.error(traceback.format_exc())
129
130        # Invariant perspective
131        try:
132            import sas.sasgui.perspectives.invariant as module
133            invariant_plug = module.Plugin()
134            self.gui.add_perspective(invariant_plug)
135        except Exception as e :
136            logger.error("%s: could not find Invariant plug-in module"% \
137                          APP_NAME)
138            logger.error(traceback.format_exc())
139
140        # Corfunc perspective
141        try:
142            import sas.sasgui.perspectives.corfunc as module
143            corfunc_plug = module.Plugin()
144            self.gui.add_perspective(corfunc_plug)
145        except:
146            logger.error("Unable to load corfunc module")
147
148        # Calculator perspective
149        try:
150            import sas.sasgui.perspectives.calculator as module
151            calculator_plug = module.Plugin()
152            self.gui.add_perspective(calculator_plug)
153        except:
154            logger.error("%s: could not find Calculator plug-in module"% \
155                                                        APP_NAME)
156            logger.error(traceback.format_exc())
157
158        # File converter tool
159        try:
160            import sas.sasgui.perspectives.file_converter as module
161            converter_plug = module.Plugin()
162            self.gui.add_perspective(converter_plug)
163        except:
164            logger.error("%s: could not find File Converter plug-in module"% \
165                                                        APP_NAME)
166            logger.error(traceback.format_exc())
167
168        # Add welcome page
169        self.gui.set_welcome_panel(WelcomePanel)
170
171        # Build the GUI
172        self.gui.build_gui()
173        # delete unused model folder
174        self.gui.clean_plugin_models(PLUGIN_MODEL_DIR)
175        # Start the main loop
176        self.gui.MainLoop()
177
178
179def run():
180    """
181    __main__ method for loading and running SasView
182    """
183    from multiprocessing import freeze_support
184    freeze_support()
185    if len(sys.argv) > 1:
186        ## Run sasview as an interactive python interpreter
187        # if sys.argv[1] == "-i":
188        #    sys.argv = ["ipython", "--pylab"]
189        #    from IPython import start_ipython
190        #    sys.exit(start_ipython())
191        thing_to_run = sys.argv[1]
192        sys.argv = sys.argv[1:]
193        import runpy
194        if os.path.exists(thing_to_run):
195            runpy.run_path(thing_to_run, run_name="__main__")
196        else:
197            runpy.run_module(thing_to_run, run_name="__main__")
198    else:
199        SasView()
200
201
202if __name__ == "__main__":
203    run()
Note: See TracBrowser for help on using the repository browser.