Changeset d3b0c77 in sasview for src/sas/sasview/sasview.py


Ignore:
Timestamp:
Sep 23, 2017 4:24:27 PM (7 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
b963b20, fca1f50
Parents:
9706d88 (diff), dba8557 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'ticket-887-reorg' into ticket-853-fit-gui-to-calc

File:
1 moved

Legend:

Unmodified
Added
Removed
  • src/sas/sasview/sasview.py

    r69363c7 rd3b0c77  
    1616import sys 
    1717import traceback 
    18  
    19 from sas.sasview.logger_config import SetupLogger 
    20  
    21 logger = SetupLogger(__name__).config_production() 
    22  
    23  
    24 # Log the start of the session 
    25 logger.info(" --- SasView session started ---") 
    26 # Log the python version 
    27 logger.info("Python: %s" % sys.version) 
    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. 
    34 WX_ENV_VAR = "SASVIEW_WX_VERSION" 
    35 if WX_ENV_VAR in os.environ: 
    36     logger.info("You have set the %s environment variable to %s." % \ 
    37                  (WX_ENV_VAR, os.environ[WX_ENV_VAR])) 
    38     import wxversion 
    39     if wxversion.checkInstalled(os.environ[WX_ENV_VAR]): 
    40         logger.info("Version %s of wxPython is installed, so using that version." % os.environ[WX_ENV_VAR]) 
    41         wxversion.select(os.environ[WX_ENV_VAR]) 
    42     else: 
    43         logger.error("Version %s of wxPython is not installed, so using default version." % os.environ[WX_ENV_VAR]) 
    44 else: 
    45     logger.info("You have not set the %s environment variable, so using default version of wxPython." % WX_ENV_VAR) 
    46  
    47 import wx 
    48  
    49 try: 
    50     logger.info("Wx version: %s" % wx.__version__) 
    51 except: 
    52     logger.error("Wx version: error reading version") 
    53  
    54 import wxcruft 
    55 wxcruft.call_later_fix() 
    56 # wxcruft.trace_new_id() 
    57  
    58 # Always use private .matplotlib setup to avoid conflicts with other 
    59 # uses of matplotlib 
    60 # Have to check if .sasview exists first 
    61 sasdir = os.path.join(os.path.expanduser("~"),'.sasview') 
    62 if not os.path.exists(sasdir): 
    63     os.mkdir(sasdir) 
    64 mplconfigdir = os.path.join(os.path.expanduser("~"),'.sasview','.matplotlib') 
    65 if not os.path.exists(mplconfigdir): 
    66     os.mkdir(mplconfigdir) 
    67 os.environ['MPLCONFIGDIR'] = mplconfigdir 
     18import logging 
     19 
    6820reload(sys) 
    6921sys.setdefaultencoding("iso-8859-1") 
    70 from sas.sasgui.guiframe import gui_manager 
    71 from sas.sasgui.guiframe.gui_style import GUIFRAME 
    72 from welcome_panel import WelcomePanel 
    73  
     22 
     23APP_NAME = 'SasView' 
    7424PLUGIN_MODEL_DIR = 'plugin_models' 
    75 APP_NAME = 'SasView' 
    76  
    77 # Set SAS_MODELPATH so sasmodels can find our custom models 
    78 os.environ['SAS_MODELPATH'] = os.path.join(sasdir, PLUGIN_MODEL_DIR) 
    79  
    80 from matplotlib import backend_bases 
    81 backend_bases.FigureCanvasBase.filetypes.pop('pgf', None) 
    82  
    83 class SasView(): 
     25 
     26class SasView(object): 
    8427    """ 
    8528    Main class for running the SasView application 
     
    8831        """ 
    8932        """ 
    90         # from gui_manager import ViewApp 
    91         self.gui = gui_manager.SasViewApp(0) 
     33        logger = logging.getLogger(__name__) 
     34 
     35        from sas.sasgui.guiframe.gui_manager import SasViewApp 
     36        self.gui = SasViewApp(0) 
    9237        # Set the application manager for the GUI 
    9338        self.gui.set_manager(self) 
     
    11661            self.gui.add_perspective(fitting_plug) 
    11762        except Exception: 
    118             logger.error("%s: could not find Fitting plug-in module"% APP_NAME) 
     63            logger.error("%s: could not find Fitting plug-in module", APP_NAME) 
    11964            logger.error(traceback.format_exc()) 
    12065 
     
    12469            pr_plug = module.Plugin() 
    12570            self.gui.add_perspective(pr_plug) 
    126         except: 
    127             logger.error("%s: could not find P(r) plug-in module"% APP_NAME) 
     71        except Exception: 
     72            logger.error("%s: could not find P(r) plug-in module", APP_NAME) 
    12873            logger.error(traceback.format_exc()) 
    12974 
     
    13378            invariant_plug = module.Plugin() 
    13479            self.gui.add_perspective(invariant_plug) 
    135         except Exception as e : 
    136             logger.error("%s: could not find Invariant plug-in module"% \ 
    137                           APP_NAME) 
     80        except Exception: 
     81            logger.error("%s: could not find Invariant plug-in module", 
     82                         APP_NAME) 
    13883            logger.error(traceback.format_exc()) 
    13984 
     
    14388            corfunc_plug = module.Plugin() 
    14489            self.gui.add_perspective(corfunc_plug) 
    145         except: 
     90        except Exception: 
    14691            logger.error("Unable to load corfunc module") 
    14792 
     
    15196            calculator_plug = module.Plugin() 
    15297            self.gui.add_perspective(calculator_plug) 
    153         except: 
    154             logger.error("%s: could not find Calculator plug-in module"% \ 
    155                                                         APP_NAME) 
     98        except Exception: 
     99            logger.error("%s: could not find Calculator plug-in module", 
     100                         APP_NAME) 
    156101            logger.error(traceback.format_exc()) 
    157102 
     
    161106            converter_plug = module.Plugin() 
    162107            self.gui.add_perspective(converter_plug) 
    163         except: 
    164             logger.error("%s: could not find File Converter plug-in module"% \ 
    165                                                         APP_NAME) 
     108        except Exception: 
     109            logger.error("%s: could not find File Converter plug-in module", 
     110                         APP_NAME) 
    166111            logger.error(traceback.format_exc()) 
    167112 
    168113        # Add welcome page 
     114        from .welcome_panel import WelcomePanel 
    169115        self.gui.set_welcome_panel(WelcomePanel) 
    170116 
     
    177123 
    178124 
    179 def run(): 
     125def 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 
     136def 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 
     173def 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 
     190def 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 
     202def run_gui(): 
    180203    """ 
    181204    __main__ method for loading and running SasView 
     
    183206    from multiprocessing import freeze_support 
    184207    freeze_support() 
    185     if len(sys.argv) > 1: 
     208    setup_logging() 
     209    setup_mpl() 
     210    setup_sasmodels() 
     211    setup_wx() 
     212    SasView() 
     213 
     214 
     215def 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 
    186232        ## Run sasview as an interactive python interpreter 
    187233        # if sys.argv[1] == "-i": 
     
    196242        else: 
    197243            runpy.run_module(thing_to_run, run_name="__main__") 
    198     else: 
    199         SasView() 
    200244 
    201245 
    202246if __name__ == "__main__": 
    203     run() 
     247    run_gui() 
Note: See TracChangeset for help on using the changeset viewer.