1 | """ |
---|
2 | Base 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 | ################################################################################ |
---|
13 | import os |
---|
14 | import sys |
---|
15 | import logging |
---|
16 | import traceback |
---|
17 | |
---|
18 | logging.basicConfig(level=logging.INFO, |
---|
19 | format='%(asctime)s %(levelname)s %(message)s', |
---|
20 | filename=os.path.join(os.path.expanduser("~"), |
---|
21 | 'sasview.log')) |
---|
22 | logging.captureWarnings(True) |
---|
23 | |
---|
24 | class 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 | |
---|
42 | stderr_logger = logging.getLogger('STDERR') |
---|
43 | sl = StreamToLogger(stderr_logger, logging.ERROR) |
---|
44 | sys.stderr = sl |
---|
45 | |
---|
46 | # Log the start of the session |
---|
47 | logging.info(" --- SasView session started ---") |
---|
48 | |
---|
49 | # Log the python version |
---|
50 | logging.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. |
---|
57 | WX_ENV_VAR = "SASVIEW_WX_VERSION" |
---|
58 | if 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]) |
---|
67 | else: |
---|
68 | logging.info("You have not set the %s environment variable, so using default version of wxPython." % WX_ENV_VAR) |
---|
69 | |
---|
70 | import wx |
---|
71 | |
---|
72 | try: |
---|
73 | logging.info("Wx version: %s" % wx.__version__) |
---|
74 | except: |
---|
75 | logging.error("Wx version: error reading version") |
---|
76 | |
---|
77 | import wxcruft |
---|
78 | wxcruft.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 |
---|
84 | sasdir = os.path.join(os.path.expanduser("~"),'.sasview') |
---|
85 | if not os.path.exists(sasdir): |
---|
86 | os.mkdir(sasdir) |
---|
87 | mplconfigdir = os.path.join(os.path.expanduser("~"),'.sasview','.matplotlib') |
---|
88 | if not os.path.exists(mplconfigdir): |
---|
89 | os.mkdir(mplconfigdir) |
---|
90 | os.environ['MPLCONFIGDIR'] = mplconfigdir |
---|
91 | reload(sys) |
---|
92 | sys.setdefaultencoding("iso-8859-1") |
---|
93 | from sas.sasgui.guiframe import gui_manager |
---|
94 | from sas.sasgui.guiframe.gui_style import GUIFRAME |
---|
95 | from welcome_panel import WelcomePanel |
---|
96 | # For py2exe, import config here |
---|
97 | import local_config |
---|
98 | PLUGIN_MODEL_DIR = 'plugin_models' |
---|
99 | APP_NAME = 'SasView' |
---|
100 | |
---|
101 | class SasView(): |
---|
102 | """ |
---|
103 | Main class for running the SasView application |
---|
104 | """ |
---|
105 | def __init__(self): |
---|
106 | """ |
---|
107 | """ |
---|
108 | #from gui_manager import ViewApp |
---|
109 | self.gui = gui_manager.SasViewApp(0) |
---|
110 | # Set the application manager for the GUI |
---|
111 | self.gui.set_manager(self) |
---|
112 | # Add perspectives to the basic application |
---|
113 | # Additional perspectives can still be loaded |
---|
114 | # dynamically |
---|
115 | # Note: py2exe can't find dynamically loaded |
---|
116 | # modules. We load the fitting module here |
---|
117 | # to ensure a complete Windows executable build. |
---|
118 | |
---|
119 | # Fitting perspective |
---|
120 | try: |
---|
121 | import sas.sasgui.perspectives.fitting as module |
---|
122 | fitting_plug = module.Plugin() |
---|
123 | self.gui.add_perspective(fitting_plug) |
---|
124 | except Exception: |
---|
125 | logging.error("%s: could not find Fitting plug-in module"% APP_NAME) |
---|
126 | logging.error(traceback.format_exc()) |
---|
127 | |
---|
128 | # P(r) perspective |
---|
129 | try: |
---|
130 | import sas.sasgui.perspectives.pr as module |
---|
131 | pr_plug = module.Plugin() |
---|
132 | self.gui.add_perspective(pr_plug) |
---|
133 | except: |
---|
134 | logging.error("%s: could not find P(r) plug-in module"% APP_NAME) |
---|
135 | logging.error(traceback.format_exc()) |
---|
136 | |
---|
137 | #Invariant perspective |
---|
138 | try: |
---|
139 | import sas.sasgui.perspectives.invariant as module |
---|
140 | invariant_plug = module.Plugin() |
---|
141 | self.gui.add_perspective(invariant_plug) |
---|
142 | except Exception as e : |
---|
143 | logging.error("%s: could not find Invariant plug-in module"% \ |
---|
144 | APP_NAME) |
---|
145 | logging.error(traceback.format_exc()) |
---|
146 | |
---|
147 | #Calculator perspective |
---|
148 | try: |
---|
149 | import sas.sasgui.perspectives.calculator as module |
---|
150 | calculator_plug = module.Plugin() |
---|
151 | self.gui.add_perspective(calculator_plug) |
---|
152 | except: |
---|
153 | logging.error("%s: could not find Calculator plug-in module"% \ |
---|
154 | APP_NAME) |
---|
155 | logging.error(traceback.format_exc()) |
---|
156 | |
---|
157 | |
---|
158 | # Add welcome page |
---|
159 | self.gui.set_welcome_panel(WelcomePanel) |
---|
160 | |
---|
161 | # Build the GUI |
---|
162 | self.gui.build_gui() |
---|
163 | # delete unused model folder |
---|
164 | self.gui.clean_plugin_models(PLUGIN_MODEL_DIR) |
---|
165 | # Start the main loop |
---|
166 | self.gui.MainLoop() |
---|
167 | |
---|
168 | |
---|
169 | def run(): |
---|
170 | """ |
---|
171 | __main__ method for loading and running SasView |
---|
172 | """ |
---|
173 | from multiprocessing import freeze_support |
---|
174 | freeze_support() |
---|
175 | if len(sys.argv) > 1: |
---|
176 | ## Run sasview as an interactive python interpreter |
---|
177 | #if sys.argv[1] == "-i": |
---|
178 | # sys.argv = ["ipython", "--pylab"] |
---|
179 | # from IPython import start_ipython |
---|
180 | # sys.exit(start_ipython()) |
---|
181 | thing_to_run = sys.argv[1] |
---|
182 | sys.argv = sys.argv[1:] |
---|
183 | import runpy |
---|
184 | if os.path.exists(thing_to_run): |
---|
185 | runpy.run_path(thing_to_run, run_name="__main__") |
---|
186 | else: |
---|
187 | runpy.run_module(thing_to_run, run_name="__main__") |
---|
188 | else: |
---|
189 | SasView() |
---|
190 | |
---|
191 | if __name__ == "__main__": |
---|
192 | run() |
---|
193 | |
---|