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
RevLine 
[7fb59b2]1# -*- coding: utf-8 -*-
[415fb82]2"""
3Base module for loading and running the main SasView application.
4"""
[f76bf17]5################################################################################
[f36e01f]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.
[f76bf17]9#
[f36e01f]10# See the license text in license.txt
[f76bf17]11#
[f36e01f]12# copyright 2009, University of Tennessee
[f76bf17]13################################################################################
14import os
[5af6f58]15import os.path
[f76bf17]16import sys
[4e080980]17import traceback
18
[63c7d581]19from sas.sasview.logger_config import SetupLogger
[38beeab]20
[a5a74e9]21logger = SetupLogger(__name__).config_production()
[f76bf17]22
[f36e01f]23
[415fb82]24# Log the start of the session
[64ca561]25logger.info(" --- SasView session started ---")
[f76bf17]26# Log the python version
[64ca561]27logger.info("Python: %s" % sys.version)
[f76bf17]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:
[64ca561]36    logger.info("You have set the %s environment variable to %s." % \
[415fb82]37                 (WX_ENV_VAR, os.environ[WX_ENV_VAR]))
[f76bf17]38    import wxversion
39    if wxversion.checkInstalled(os.environ[WX_ENV_VAR]):
[64ca561]40        logger.info("Version %s of wxPython is installed, so using that version." % os.environ[WX_ENV_VAR])
[f76bf17]41        wxversion.select(os.environ[WX_ENV_VAR])
42    else:
[64ca561]43        logger.error("Version %s of wxPython is not installed, so using default version." % os.environ[WX_ENV_VAR])
[f76bf17]44else:
[64ca561]45    logger.info("You have not set the %s environment variable, so using default version of wxPython." % WX_ENV_VAR)
[f76bf17]46
47import wx
[6f3fea2]48
[f76bf17]49try:
[64ca561]50    logger.info("Wx version: %s" % wx.__version__)
[f76bf17]51except:
[64ca561]52    logger.error("Wx version: error reading version")
[415fb82]53
[85130cb]54import wxcruft
[6f16e25]55wxcruft.call_later_fix()
[f36e01f]56# wxcruft.trace_new_id()
[6f3fea2]57
[f36e01f]58# Always use private .matplotlib setup to avoid conflicts with other
59# uses of matplotlib
60# Have to check if .sasview exists first
[1be5202]61sasdir = os.path.join(os.path.expanduser("~"),'.sasview')
62if not os.path.exists(sasdir):
63    os.mkdir(sasdir)
[558d64e]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")
[d85c194]70from sas.sasgui.guiframe import gui_manager
71from sas.sasgui.guiframe.gui_style import GUIFRAME
[5f0be1f]72from welcome_panel import WelcomePanel
[f36e01f]73
[f76bf17]74PLUGIN_MODEL_DIR = 'plugin_models'
75APP_NAME = 'SasView'
76
[3b0f8cc]77# Set SAS_MODELPATH so sasmodels can find our custom models
78os.environ['SAS_MODELPATH'] = os.path.join(sasdir, PLUGIN_MODEL_DIR)
79
[e68c9bf]80from matplotlib import backend_bases
[cde6e52]81backend_bases.FigureCanvasBase.filetypes.pop('pgf', None)
[e68c9bf]82
[f76bf17]83class SasView():
84    """
[415fb82]85    Main class for running the SasView application
[f76bf17]86    """
87    def __init__(self):
88        """
89        """
[f36e01f]90        # from gui_manager import ViewApp
[78f75d02]91        self.gui = gui_manager.SasViewApp(0)
[f76bf17]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
[65f3930]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
[f76bf17]112        # Fitting perspective
113        try:
[77d92cd]114            import sas.sasgui.perspectives.fitting as module
[f76bf17]115            fitting_plug = module.Plugin()
116            self.gui.add_perspective(fitting_plug)
[415fb82]117        except Exception:
[64ca561]118            logger.error("%s: could not find Fitting plug-in module"% APP_NAME)
119            logger.error(traceback.format_exc())
[f76bf17]120
121        # P(r) perspective
122        try:
[d85c194]123            import sas.sasgui.perspectives.pr as module
[f21d496]124            pr_plug = module.Plugin()
[f76bf17]125            self.gui.add_perspective(pr_plug)
126        except:
[64ca561]127            logger.error("%s: could not find P(r) plug-in module"% APP_NAME)
128            logger.error(traceback.format_exc())
[4e080980]129
[f36e01f]130        # Invariant perspective
[f76bf17]131        try:
[d85c194]132            import sas.sasgui.perspectives.invariant as module
[f21d496]133            invariant_plug = module.Plugin()
[f76bf17]134            self.gui.add_perspective(invariant_plug)
[b699768]135        except Exception as e :
[64ca561]136            logger.error("%s: could not find Invariant plug-in module"% \
[f76bf17]137                          APP_NAME)
[64ca561]138            logger.error(traceback.format_exc())
[4e080980]139
[c23f303]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:
[64ca561]146            logger.error("Unable to load corfunc module")
[c23f303]147
[f36e01f]148        # Calculator perspective
[f76bf17]149        try:
[d85c194]150            import sas.sasgui.perspectives.calculator as module
[f21d496]151            calculator_plug = module.Plugin()
[f76bf17]152            self.gui.add_perspective(calculator_plug)
153        except:
[64ca561]154            logger.error("%s: could not find Calculator plug-in module"% \
[f76bf17]155                                                        APP_NAME)
[64ca561]156            logger.error(traceback.format_exc())
[4e080980]157
[77d92cd]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:
[64ca561]164            logger.error("%s: could not find File Converter plug-in module"% \
[77d92cd]165                                                        APP_NAME)
[64ca561]166            logger.error(traceback.format_exc())
[77d92cd]167
[f76bf17]168        # Add welcome page
169        self.gui.set_welcome_panel(WelcomePanel)
[415fb82]170
[f76bf17]171        # Build the GUI
172        self.gui.build_gui()
[415fb82]173        # delete unused model folder
[f76bf17]174        self.gui.clean_plugin_models(PLUGIN_MODEL_DIR)
175        # Start the main loop
176        self.gui.MainLoop()
177
178
179def run():
[415fb82]180    """
181    __main__ method for loading and running SasView
182    """
[f76bf17]183    from multiprocessing import freeze_support
184    freeze_support()
185    if len(sys.argv) > 1:
186        ## Run sasview as an interactive python interpreter
[f36e01f]187        # if sys.argv[1] == "-i":
[f76bf17]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
[f36e01f]201
[f76bf17]202if __name__ == "__main__":
203    run()
Note: See TracBrowser for help on using the repository browser.