source: sasview/sasview/sasview.py @ 3b0f8cc

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.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 3b0f8cc was 3b0f8cc, checked in by lewis, 7 years ago

Set SAS_MODELPATH env variable so sasmodels can find custom models

  • Property mode set to 100644
File size: 6.6 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        # Fitting perspective
102        try:
103            import sas.sasgui.perspectives.fitting as module
104            fitting_plug = module.Plugin()
105            self.gui.add_perspective(fitting_plug)
106        except Exception:
107            logger.error("%s: could not find Fitting plug-in module"% APP_NAME)
108            logger.error(traceback.format_exc())
109
110        # P(r) perspective
111        try:
112            import sas.sasgui.perspectives.pr as module
113            pr_plug = module.Plugin()
114            self.gui.add_perspective(pr_plug)
115        except:
116            logger.error("%s: could not find P(r) plug-in module"% APP_NAME)
117            logger.error(traceback.format_exc())
118
119        # Invariant perspective
120        try:
121            import sas.sasgui.perspectives.invariant as module
122            invariant_plug = module.Plugin()
123            self.gui.add_perspective(invariant_plug)
124        except Exception as e :
125            logger.error("%s: could not find Invariant plug-in module"% \
126                          APP_NAME)
127            logger.error(traceback.format_exc())
128
129        # Corfunc perspective
130        try:
131            import sas.sasgui.perspectives.corfunc as module
132            corfunc_plug = module.Plugin()
133            self.gui.add_perspective(corfunc_plug)
134        except:
135            logger.error("Unable to load corfunc module")
136
137        # Calculator perspective
138        try:
139            import sas.sasgui.perspectives.calculator as module
140            calculator_plug = module.Plugin()
141            self.gui.add_perspective(calculator_plug)
142        except:
143            logger.error("%s: could not find Calculator plug-in module"% \
144                                                        APP_NAME)
145            logger.error(traceback.format_exc())
146
147        # File converter tool
148        try:
149            import sas.sasgui.perspectives.file_converter as module
150            converter_plug = module.Plugin()
151            self.gui.add_perspective(converter_plug)
152        except:
153            logger.error("%s: could not find File Converter plug-in module"% \
154                                                        APP_NAME)
155            logger.error(traceback.format_exc())
156
157        # Add welcome page
158        self.gui.set_welcome_panel(WelcomePanel)
159
160        # Build the GUI
161        self.gui.build_gui()
162        # delete unused model folder
163        self.gui.clean_plugin_models(PLUGIN_MODEL_DIR)
164        # Start the main loop
165        self.gui.MainLoop()
166
167
168def run():
169    """
170    __main__ method for loading and running SasView
171    """
172    from multiprocessing import freeze_support
173    freeze_support()
174    if len(sys.argv) > 1:
175        ## Run sasview as an interactive python interpreter
176        # if sys.argv[1] == "-i":
177        #    sys.argv = ["ipython", "--pylab"]
178        #    from IPython import start_ipython
179        #    sys.exit(start_ipython())
180        thing_to_run = sys.argv[1]
181        sys.argv = sys.argv[1:]
182        import runpy
183        if os.path.exists(thing_to_run):
184            runpy.run_path(thing_to_run, run_name="__main__")
185        else:
186            runpy.run_module(thing_to_run, run_name="__main__")
187    else:
188        SasView()
189
190
191if __name__ == "__main__":
192    run()
Note: See TracBrowser for help on using the repository browser.