source: sasview/sasview/sasview.py @ cde6e52

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 cde6e52 was cde6e52, checked in by lewis, 5 years ago

Attempt to fix mac build

  • Property mode set to 100644
File size: 6.6 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
71
72try:
73    logging.info("Wx version: %s" % wx.__version__)
74except:
75    logging.error("Wx version: error reading version")
76
77import wxcruft
78wxcruft.call_later_fix()
79#wxcruft.trace_new_id()
80
81#Always use private .matplotlib setup to avoid conflicts with other
82#uses of matplotlib
83#Have to check if .sasview exists first
84sasdir = os.path.join(os.path.expanduser("~"),'.sasview')
85if not os.path.exists(sasdir):
86    os.mkdir(sasdir)
87mplconfigdir = os.path.join(os.path.expanduser("~"),'.sasview','.matplotlib')
88if not os.path.exists(mplconfigdir):
89    os.mkdir(mplconfigdir)
90os.environ['MPLCONFIGDIR'] = mplconfigdir
91reload(sys)
92sys.setdefaultencoding("iso-8859-1")
93from sas.sasgui.guiframe import gui_manager
94from sas.sasgui.guiframe.gui_style import GUIFRAME
95from welcome_panel import WelcomePanel
96# For py2exe, import config here
97import local_config
98PLUGIN_MODEL_DIR = 'plugin_models'
99APP_NAME = 'SasView'
100
101from matplotlib import backend_bases
102backend_bases.FigureCanvasBase.filetypes.pop('pgf', None)
103
104class SasView():
105    """
106    Main class for running the SasView application
107    """
108    def __init__(self):
109        """
110        """
111        #from gui_manager import ViewApp
112        self.gui = gui_manager.SasViewApp(0)
113        # Set the application manager for the GUI
114        self.gui.set_manager(self)
115        # Add perspectives to the basic application
116        # Additional perspectives can still be loaded
117        # dynamically
118        # Note: py2exe can't find dynamically loaded
119        # modules. We load the fitting module here
120        # to ensure a complete Windows executable build.
121
122        # Fitting perspective
123        try:
124            import sas.sasgui.perspectives.fitting as module
125            fitting_plug = module.Plugin()
126            self.gui.add_perspective(fitting_plug)
127        except Exception:
128            logging.error("%s: could not find Fitting plug-in module"% APP_NAME)
129            logging.error(traceback.format_exc())
130
131        # P(r) perspective
132        try:
133            import sas.sasgui.perspectives.pr as module
134            pr_plug = module.Plugin()
135            self.gui.add_perspective(pr_plug)
136        except:
137            logging.error("%s: could not find P(r) plug-in module"% APP_NAME)
138            logging.error(traceback.format_exc())
139
140        #Invariant perspective
141        try:
142            import sas.sasgui.perspectives.invariant as module
143            invariant_plug = module.Plugin()
144            self.gui.add_perspective(invariant_plug)
145        except Exception as e :
146            logging.error("%s: could not find Invariant plug-in module"% \
147                          APP_NAME)
148            logging.error(traceback.format_exc())
149
150        #Calculator perspective
151        try:
152            import sas.sasgui.perspectives.calculator as module
153            calculator_plug = module.Plugin()
154            self.gui.add_perspective(calculator_plug)
155        except:
156            logging.error("%s: could not find Calculator plug-in module"% \
157                                                        APP_NAME)
158            logging.error(traceback.format_exc())
159
160
161        # Add welcome page
162        self.gui.set_welcome_panel(WelcomePanel)
163
164        # Build the GUI
165        self.gui.build_gui()
166        # delete unused model folder
167        self.gui.clean_plugin_models(PLUGIN_MODEL_DIR)
168        # Start the main loop
169        self.gui.MainLoop()
170
171
172def run():
173    """
174    __main__ method for loading and running SasView
175    """
176    from multiprocessing import freeze_support
177    freeze_support()
178    if len(sys.argv) > 1:
179        ## Run sasview as an interactive python interpreter
180        #if sys.argv[1] == "-i":
181        #    sys.argv = ["ipython", "--pylab"]
182        #    from IPython import start_ipython
183        #    sys.exit(start_ipython())
184        thing_to_run = sys.argv[1]
185        sys.argv = sys.argv[1:]
186        import runpy
187        if os.path.exists(thing_to_run):
188            runpy.run_path(thing_to_run, run_name="__main__")
189        else:
190            runpy.run_module(thing_to_run, run_name="__main__")
191    else:
192        SasView()
193
194if __name__ == "__main__":
195    run()
Note: See TracBrowser for help on using the repository browser.