1 | # -*- coding: utf-8 -*- |
---|
2 | """ |
---|
3 | Base 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 | ################################################################################ |
---|
14 | import os |
---|
15 | import os.path |
---|
16 | import sys |
---|
17 | import traceback |
---|
18 | import logging |
---|
19 | |
---|
20 | reload(sys) |
---|
21 | sys.setdefaultencoding("iso-8859-1") |
---|
22 | |
---|
23 | APP_NAME = 'SasView' |
---|
24 | PLUGIN_MODEL_DIR = 'plugin_models' |
---|
25 | |
---|
26 | class SasView(object): |
---|
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 | # Rebuild .sasview/categories.json. This triggers a load of sasmodels |
---|
47 | # and all the plugins. |
---|
48 | try: |
---|
49 | from sas.sascalc.fit.models import ModelManager |
---|
50 | from sas.sasgui.guiframe.CategoryInstaller import CategoryInstaller |
---|
51 | model_list = ModelManager().cat_model_list() |
---|
52 | CategoryInstaller.check_install(model_list=model_list) |
---|
53 | except Exception: |
---|
54 | logger.error("%s: could not load SasView models") |
---|
55 | logger.error(traceback.format_exc()) |
---|
56 | |
---|
57 | # Fitting perspective |
---|
58 | try: |
---|
59 | import sas.sasgui.perspectives.fitting as module |
---|
60 | fitting_plug = module.Plugin() |
---|
61 | self.gui.add_perspective(fitting_plug) |
---|
62 | except Exception: |
---|
63 | logger.error("%s: could not find Fitting plug-in module", APP_NAME) |
---|
64 | logger.error(traceback.format_exc()) |
---|
65 | |
---|
66 | # P(r) perspective |
---|
67 | try: |
---|
68 | import sas.sasgui.perspectives.pr as module |
---|
69 | pr_plug = module.Plugin() |
---|
70 | self.gui.add_perspective(pr_plug) |
---|
71 | except Exception: |
---|
72 | logger.error("%s: could not find P(r) plug-in module", APP_NAME) |
---|
73 | logger.error(traceback.format_exc()) |
---|
74 | |
---|
75 | # Invariant perspective |
---|
76 | try: |
---|
77 | import sas.sasgui.perspectives.invariant as module |
---|
78 | invariant_plug = module.Plugin() |
---|
79 | self.gui.add_perspective(invariant_plug) |
---|
80 | except Exception: |
---|
81 | logger.error("%s: could not find Invariant plug-in module", |
---|
82 | APP_NAME) |
---|
83 | logger.error(traceback.format_exc()) |
---|
84 | |
---|
85 | # Corfunc perspective |
---|
86 | try: |
---|
87 | import sas.sasgui.perspectives.corfunc as module |
---|
88 | corfunc_plug = module.Plugin() |
---|
89 | self.gui.add_perspective(corfunc_plug) |
---|
90 | except Exception: |
---|
91 | logger.error("Unable to load corfunc module") |
---|
92 | |
---|
93 | # Calculator perspective |
---|
94 | try: |
---|
95 | import sas.sasgui.perspectives.calculator as module |
---|
96 | calculator_plug = module.Plugin() |
---|
97 | self.gui.add_perspective(calculator_plug) |
---|
98 | except Exception: |
---|
99 | logger.error("%s: could not find Calculator plug-in module", |
---|
100 | APP_NAME) |
---|
101 | logger.error(traceback.format_exc()) |
---|
102 | |
---|
103 | # File converter tool |
---|
104 | try: |
---|
105 | import sas.sasgui.perspectives.file_converter as module |
---|
106 | converter_plug = module.Plugin() |
---|
107 | self.gui.add_perspective(converter_plug) |
---|
108 | except Exception: |
---|
109 | logger.error("%s: could not find File Converter plug-in module", |
---|
110 | APP_NAME) |
---|
111 | logger.error(traceback.format_exc()) |
---|
112 | |
---|
113 | # Add welcome page |
---|
114 | from .welcome_panel import WelcomePanel |
---|
115 | self.gui.set_welcome_panel(WelcomePanel) |
---|
116 | |
---|
117 | # Build the GUI |
---|
118 | self.gui.build_gui() |
---|
119 | # delete unused model folder |
---|
120 | self.gui.clean_plugin_models(PLUGIN_MODEL_DIR) |
---|
121 | # Start the main loop |
---|
122 | self.gui.MainLoop() |
---|
123 | |
---|
124 | |
---|
125 | def setup_logging(): |
---|
126 | from sas.logger_config import SetupLogger |
---|
127 | |
---|
128 | logger = SetupLogger(__name__).config_production() |
---|
129 | # Log the start of the session |
---|
130 | logger.info(" --- SasView session started ---") |
---|
131 | # Log the python version |
---|
132 | logger.info("Python: %s" % sys.version) |
---|
133 | return logger |
---|
134 | |
---|
135 | |
---|
136 | def setup_wx(): |
---|
137 | # Allow the dynamic selection of wxPython via an environment variable, when devs |
---|
138 | # who have multiple versions of the module installed want to pick between them. |
---|
139 | # This variable does not have to be set of course, and through normal usage will |
---|
140 | # probably not be, but this can make things a little easier when upgrading to a |
---|
141 | # new version of wx. |
---|
142 | logger = logging.getLogger(__name__) |
---|
143 | WX_ENV_VAR = "SASVIEW_WX_VERSION" |
---|
144 | if WX_ENV_VAR in os.environ: |
---|
145 | logger.info("You have set the %s environment variable to %s.", |
---|
146 | WX_ENV_VAR, os.environ[WX_ENV_VAR]) |
---|
147 | import wxversion |
---|
148 | if wxversion.checkInstalled(os.environ[WX_ENV_VAR]): |
---|
149 | logger.info("Version %s of wxPython is installed, so using that version.", |
---|
150 | os.environ[WX_ENV_VAR]) |
---|
151 | wxversion.select(os.environ[WX_ENV_VAR]) |
---|
152 | else: |
---|
153 | logger.error("Version %s of wxPython is not installed, so using default version.", |
---|
154 | os.environ[WX_ENV_VAR]) |
---|
155 | else: |
---|
156 | logger.info("You have not set the %s environment variable, so using default version of wxPython.", |
---|
157 | WX_ENV_VAR) |
---|
158 | |
---|
159 | import wx |
---|
160 | |
---|
161 | try: |
---|
162 | logger.info("Wx version: %s", wx.__version__) |
---|
163 | except AttributeError: |
---|
164 | logger.error("Wx version: error reading version") |
---|
165 | |
---|
166 | from . import wxcruft |
---|
167 | wxcruft.call_later_fix() |
---|
168 | #wxcruft.trace_new_id() |
---|
169 | #Always use private .matplotlib setup to avoid conflicts with other |
---|
170 | #uses of matplotlib |
---|
171 | |
---|
172 | |
---|
173 | def setup_mpl(backend='WXAgg'): |
---|
174 | import sas.sasgui |
---|
175 | # Always use private .matplotlib setup to avoid conflicts with other |
---|
176 | mplconfigdir = os.path.join(sas.sasgui.get_user_dir(), '.matplotlib') |
---|
177 | if not os.path.exists(mplconfigdir): |
---|
178 | os.mkdir(mplconfigdir) |
---|
179 | os.environ['MPLCONFIGDIR'] = mplconfigdir |
---|
180 | # Set backend to WXAgg; this overrides matplotlibrc, but shouldn't override |
---|
181 | # mpl.use(). Note: Don't import matplotlib here since the script that |
---|
182 | # we are running may not actually need it; also, putting as little on the |
---|
183 | # path as we can |
---|
184 | os.environ['MPLBACKEND'] = backend |
---|
185 | |
---|
186 | # TODO: ... so much for not importing matplotlib unless we need it... |
---|
187 | from matplotlib import backend_bases |
---|
188 | backend_bases.FigureCanvasBase.filetypes.pop('pgf', None) |
---|
189 | |
---|
190 | def setup_sasmodels(): |
---|
191 | """ |
---|
192 | Prepare sasmodels for running within sasview. |
---|
193 | """ |
---|
194 | import sas.sasgui |
---|
195 | # Set SAS_MODELPATH so sasmodels can find our custom models |
---|
196 | plugin_dir = os.path.join(sas.sasgui.get_user_dir(), PLUGIN_MODEL_DIR) |
---|
197 | os.environ['SAS_MODELPATH'] = plugin_dir |
---|
198 | # TODO: SAS_OPENCL flag belongs in setup_sasmodels |
---|
199 | # this will require restructuring of the config management so that it |
---|
200 | # can occur outside of sasgui. |
---|
201 | |
---|
202 | def run_gui(): |
---|
203 | """ |
---|
204 | __main__ method for loading and running SasView |
---|
205 | """ |
---|
206 | from multiprocessing import freeze_support |
---|
207 | freeze_support() |
---|
208 | setup_logging() |
---|
209 | setup_mpl() |
---|
210 | setup_sasmodels() |
---|
211 | setup_wx() |
---|
212 | SasView() |
---|
213 | |
---|
214 | |
---|
215 | def run_cli(): |
---|
216 | from multiprocessing import freeze_support |
---|
217 | freeze_support() |
---|
218 | setup_logging() |
---|
219 | setup_mpl(backend='Agg') |
---|
220 | setup_sasmodels() |
---|
221 | if len(sys.argv) == 1: |
---|
222 | # Run sasview as an interactive python interpreter |
---|
223 | try: |
---|
224 | from IPython import start_ipython |
---|
225 | sys.argv = ["ipython", "--pylab"] |
---|
226 | sys.exit(start_ipython()) |
---|
227 | except ImportError: |
---|
228 | import code |
---|
229 | code.interact(local={'exit': sys.exit}) |
---|
230 | else: |
---|
231 | # Run sasview as a python script interpreter |
---|
232 | ## Run sasview as an interactive python interpreter |
---|
233 | # if sys.argv[1] == "-i": |
---|
234 | # sys.argv = ["ipython", "--pylab"] |
---|
235 | # from IPython import start_ipython |
---|
236 | # sys.exit(start_ipython()) |
---|
237 | thing_to_run = sys.argv[1] |
---|
238 | sys.argv = sys.argv[1:] |
---|
239 | import runpy |
---|
240 | if os.path.exists(thing_to_run): |
---|
241 | runpy.run_path(thing_to_run, run_name="__main__") |
---|
242 | else: |
---|
243 | runpy.run_module(thing_to_run, run_name="__main__") |
---|
244 | |
---|
245 | |
---|
246 | if __name__ == "__main__": |
---|
247 | run_gui() |
---|