1 | |
---|
2 | ################################################################################ |
---|
3 | #This software was developed by the University of Tennessee as part of the |
---|
4 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
5 | #project funded by the US National Science Foundation. |
---|
6 | # |
---|
7 | #See the license text in license.txt |
---|
8 | # |
---|
9 | #copyright 2009, University of Tennessee |
---|
10 | ################################################################################ |
---|
11 | import os |
---|
12 | import sys |
---|
13 | import logging |
---|
14 | from shutil import copy |
---|
15 | logging.basicConfig(level=logging.INFO, |
---|
16 | format='%(asctime)s %(levelname)s %(message)s', |
---|
17 | filename=os.path.join(os.path.expanduser("~"), |
---|
18 | 'sasview.log')) |
---|
19 | |
---|
20 | class StreamToLogger(object): |
---|
21 | """ |
---|
22 | File-like stream object that redirects writes to a logger instance. |
---|
23 | """ |
---|
24 | def __init__(self, logger, log_level=logging.INFO): |
---|
25 | self.logger = logger |
---|
26 | self.log_level = log_level |
---|
27 | self.linebuf = '' |
---|
28 | |
---|
29 | def write(self, buf): |
---|
30 | # Write the message to stdout so we can see it when running interactively |
---|
31 | sys.stdout.write(buf) |
---|
32 | for line in buf.rstrip().splitlines(): |
---|
33 | self.logger.log(self.log_level, line.rstrip()) |
---|
34 | |
---|
35 | stderr_logger = logging.getLogger('STDERR') |
---|
36 | sl = StreamToLogger(stderr_logger, logging.ERROR) |
---|
37 | sys.stderr = sl |
---|
38 | |
---|
39 | # Log the python version |
---|
40 | logging.info("Python: %s" % sys.version) |
---|
41 | |
---|
42 | # Allow the dynamic selection of wxPython via an environment variable, when devs |
---|
43 | # who have multiple versions of the module installed want to pick between them. |
---|
44 | # This variable does not have to be set of course, and through normal usage will |
---|
45 | # probably not be, but this can make things a little easier when upgrading to a |
---|
46 | # new version of wx. |
---|
47 | WX_ENV_VAR = "SASVIEW_WX_VERSION" |
---|
48 | if WX_ENV_VAR in os.environ: |
---|
49 | logging.info("You have set the %s environment variable to %s." % (WX_ENV_VAR, os.environ[WX_ENV_VAR])) |
---|
50 | import wxversion |
---|
51 | if wxversion.checkInstalled(os.environ[WX_ENV_VAR]): |
---|
52 | logging.info("Version %s of wxPython is installed, so using that version." % os.environ[WX_ENV_VAR]) |
---|
53 | wxversion.select(os.environ[WX_ENV_VAR]) |
---|
54 | else: |
---|
55 | logging.error("Version %s of wxPython is not installed, so using default version." % os.environ[WX_ENV_VAR]) |
---|
56 | else: |
---|
57 | logging.info("You have not set the %s environment variable, so using default version of wxPython." % WX_ENV_VAR) |
---|
58 | |
---|
59 | import wx |
---|
60 | try: |
---|
61 | logging.info("Wx version: %s" % wx.__version__) |
---|
62 | except: |
---|
63 | logging.error("Wx version: error reading version") |
---|
64 | |
---|
65 | # The below will make sure that sasview application uses the matplotlib font |
---|
66 | # bundled with sasview. |
---|
67 | if hasattr(sys, 'frozen'): |
---|
68 | mplconfigdir = os.path.join(os.path.expanduser("~"), '.matplotlib') |
---|
69 | if not os.path.exists(mplconfigdir): |
---|
70 | os.mkdir(mplconfigdir) |
---|
71 | os.environ['MPLCONFIGDIR'] = mplconfigdir |
---|
72 | reload(sys) |
---|
73 | sys.setdefaultencoding("iso-8859-1") |
---|
74 | from sas.guiframe import gui_manager |
---|
75 | from sas.guiframe.gui_style import GUIFRAME |
---|
76 | from welcome_panel import WelcomePanel |
---|
77 | # For py2exe, import config here |
---|
78 | import local_config |
---|
79 | PLUGIN_MODEL_DIR = 'plugin_models' |
---|
80 | APP_NAME = 'SasView' |
---|
81 | |
---|
82 | class SasViewApp(gui_manager.ViewApp): |
---|
83 | """ |
---|
84 | """ |
---|
85 | |
---|
86 | |
---|
87 | class SasView(): |
---|
88 | """ |
---|
89 | """ |
---|
90 | def __init__(self): |
---|
91 | """ |
---|
92 | """ |
---|
93 | #from gui_manager import ViewApp |
---|
94 | self.gui = SasViewApp(0) |
---|
95 | # Set the application manager for the GUI |
---|
96 | self.gui.set_manager(self) |
---|
97 | # Add perspectives to the basic application |
---|
98 | # Additional perspectives can still be loaded |
---|
99 | # dynamically |
---|
100 | # Note: py2exe can't find dynamically loaded |
---|
101 | # modules. We load the fitting module here |
---|
102 | # to ensure a complete Windows executable build. |
---|
103 | |
---|
104 | # Fitting perspective |
---|
105 | try: |
---|
106 | import sas.perspectives.fitting as module |
---|
107 | fitting_plug = module.Plugin() |
---|
108 | self.gui.add_perspective(fitting_plug) |
---|
109 | except Exception as inst: |
---|
110 | logging.error("Fitting problems: " + str(inst)) |
---|
111 | logging.error("%s: could not find Fitting plug-in module"% APP_NAME) |
---|
112 | logging.error(sys.exc_value) |
---|
113 | |
---|
114 | # P(r) perspective |
---|
115 | try: |
---|
116 | import sas.perspectives.pr as module |
---|
117 | pr_plug = module.Plugin(standalone=False) |
---|
118 | self.gui.add_perspective(pr_plug) |
---|
119 | except: |
---|
120 | logging.error("%s: could not find P(r) plug-in module"% APP_NAME) |
---|
121 | logging.error(sys.exc_value) |
---|
122 | |
---|
123 | #Invariant perspective |
---|
124 | try: |
---|
125 | import sas.perspectives.invariant as module |
---|
126 | invariant_plug = module.Plugin(standalone=False) |
---|
127 | self.gui.add_perspective(invariant_plug) |
---|
128 | except: |
---|
129 | raise |
---|
130 | logging.error("%s: could not find Invariant plug-in module"% \ |
---|
131 | APP_NAME) |
---|
132 | logging.error(sys.exc_value) |
---|
133 | |
---|
134 | #Calculator perspective |
---|
135 | try: |
---|
136 | import sas.perspectives.calculator as module |
---|
137 | calculator_plug = module.Plugin(standalone=False) |
---|
138 | self.gui.add_perspective(calculator_plug) |
---|
139 | except: |
---|
140 | logging.error("%s: could not find Calculator plug-in module"% \ |
---|
141 | APP_NAME) |
---|
142 | logging.error(sys.exc_value) |
---|
143 | |
---|
144 | |
---|
145 | # Add welcome page |
---|
146 | self.gui.set_welcome_panel(WelcomePanel) |
---|
147 | |
---|
148 | # Build the GUI |
---|
149 | self.gui.build_gui() |
---|
150 | # delete unused model folder |
---|
151 | self.gui.clean_plugin_models(PLUGIN_MODEL_DIR) |
---|
152 | # Start the main loop |
---|
153 | self.gui.MainLoop() |
---|
154 | |
---|
155 | |
---|
156 | def run(): |
---|
157 | from multiprocessing import freeze_support |
---|
158 | freeze_support() |
---|
159 | if len(sys.argv) > 1: |
---|
160 | ## Run sasview as an interactive python interpreter |
---|
161 | #if sys.argv[1] == "-i": |
---|
162 | # sys.argv = ["ipython", "--pylab"] |
---|
163 | # from IPython import start_ipython |
---|
164 | # sys.exit(start_ipython()) |
---|
165 | thing_to_run = sys.argv[1] |
---|
166 | sys.argv = sys.argv[1:] |
---|
167 | import runpy |
---|
168 | if os.path.exists(thing_to_run): |
---|
169 | runpy.run_path(thing_to_run, run_name="__main__") |
---|
170 | else: |
---|
171 | runpy.run_module(thing_to_run, run_name="__main__") |
---|
172 | else: |
---|
173 | SasView() |
---|
174 | |
---|
175 | if __name__ == "__main__": |
---|
176 | run() |
---|
177 | |
---|