Changeset 899e084 in sasview for src/sas/sasview/sasview.py


Ignore:
Timestamp:
Jun 27, 2016 7:36:41 AM (5 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:
80c2c85
Parents:
efe730d
Message:

build console app as well as gui

File:
1 edited

Legend:

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

    refe730d r899e084  
    1616import traceback 
    1717 
    18 log_file = os.path.join(os.path.expanduser("~"), 'sasview.log') 
    19 logging.basicConfig(level=logging.INFO, 
    20                     format='%(asctime)s %(levelname)s %(message)s', 
    21                     filename=log_file, 
    22 ) 
    23 logging.captureWarnings(True) 
     18 
     19reload(sys) 
     20sys.setdefaultencoding("iso-8859-1") 
     21 
    2422 
    2523class StreamToLogger(object): 
     
    4139            self.logger.log(self.log_level, line.rstrip()) 
    4240 
    43 stderr_logger = logging.getLogger('STDERR') 
    44 sl = StreamToLogger(stderr_logger, logging.ERROR) 
    45 sys.stderr = sl 
    46  
    47 # Log the start of the session 
    48 logging.info(" --- SasView session started ---") 
    49  
    50 # Log the python version 
    51 logging.info("Python: %s" % sys.version) 
    52  
    53 # Allow the dynamic selection of wxPython via an environment variable, when devs 
    54 # who have multiple versions of the module installed want to pick between them. 
    55 # This variable does not have to be set of course, and through normal usage will 
    56 # probably not be, but this can make things a little easier when upgrading to a 
    57 # new version of wx. 
    58 WX_ENV_VAR = "SASVIEW_WX_VERSION" 
    59 if WX_ENV_VAR in os.environ: 
    60     logging.info("You have set the %s environment variable to %s." % \ 
    61                  (WX_ENV_VAR, os.environ[WX_ENV_VAR])) 
    62     import wxversion 
    63     if wxversion.checkInstalled(os.environ[WX_ENV_VAR]): 
    64         logging.info("Version %s of wxPython is installed, so using that version." % os.environ[WX_ENV_VAR]) 
    65         wxversion.select(os.environ[WX_ENV_VAR]) 
    66     else: 
    67         logging.error("Version %s of wxPython is not installed, so using default version." % os.environ[WX_ENV_VAR]) 
    68 else: 
    69     logging.info("You have not set the %s environment variable, so using default version of wxPython." % WX_ENV_VAR) 
    70  
    71 import wx 
    72  
    73 try: 
    74     logging.info("Wx version: %s" % wx.__version__) 
    75 except: 
    76     logging.error("Wx version: error reading version") 
    77  
    78 from . import wxcruft 
    79 wxcruft.call_later_fix() 
    80 #wxcruft.trace_new_id() 
    81 #Always use private .matplotlib setup to avoid conflicts with other 
    82 #uses of matplotlib 
    83  
    84 import sas.sasgui 
    85 mplconfigdir = os.path.join(sas.sasgui.get_user_dir(), '.matplotlib') 
    86 if not os.path.exists(mplconfigdir): 
    87     os.mkdir(mplconfigdir) 
    88 os.environ['MPLCONFIGDIR'] = mplconfigdir 
    89 reload(sys) 
    90 sys.setdefaultencoding("iso-8859-1") 
    91  
    92  
    93 from sas.sasgui.guiframe import gui_manager 
    94 from .welcome_panel import WelcomePanel 
    9541PLUGIN_MODEL_DIR = 'plugin_models' 
    9642APP_NAME = 'SasView' 
     
    10349        """ 
    10450        """ 
    105         #from gui_manager import ViewApp 
    106         self.gui = gui_manager.SasViewApp(0) 
     51        from sas.sasgui.guiframe.gui_manager import SasViewApp 
     52        self.gui = SasViewApp(0) 
    10753        # Set the application manager for the GUI 
    10854        self.gui.set_manager(self) 
     
    11662        # Fitting perspective 
    11763        try: 
    118             import sas.sasgui.perspectives.fitting as module     
     64            import sas.sasgui.perspectives.fitting as module 
    11965            fitting_plug = module.Plugin() 
    12066            self.gui.add_perspective(fitting_plug) 
     
    14288            logging.error(traceback.format_exc()) 
    14389 
    144         #Calculator perspective    
     90        #Calculator perspective 
    14591        try: 
    14692            import sas.sasgui.perspectives.calculator as module 
     
    154100 
    155101        # Add welcome page 
     102        from .welcome_panel import WelcomePanel 
    156103        self.gui.set_welcome_panel(WelcomePanel) 
    157104 
     
    164111 
    165112 
    166 def run(): 
     113def setup_logging(): 
     114    log_file = os.path.join(os.path.expanduser("~"), 'sasview.log') 
     115    logging.basicConfig(level=logging.INFO, 
     116                        format='%(asctime)s %(levelname)s %(message)s', 
     117                        filename=log_file, 
     118                        ) 
     119    logging.captureWarnings(True) 
     120 
     121    stderr_logger = logging.getLogger('STDERR') 
     122    sl = StreamToLogger(stderr_logger, logging.ERROR) 
     123    sys.stderr = sl 
     124 
     125    # Log the start of the session 
     126    logging.info(" --- SasView session started ---") 
     127 
     128    # Log the python version 
     129    logging.info("Python: %s" % sys.version) 
     130 
     131 
     132def setup_wx(): 
     133    # Allow the dynamic selection of wxPython via an environment variable, when devs 
     134    # who have multiple versions of the module installed want to pick between them. 
     135    # This variable does not have to be set of course, and through normal usage will 
     136    # probably not be, but this can make things a little easier when upgrading to a 
     137    # new version of wx. 
     138    WX_ENV_VAR = "SASVIEW_WX_VERSION" 
     139    if WX_ENV_VAR in os.environ: 
     140        logging.info("You have set the %s environment variable to %s." % \ 
     141                     (WX_ENV_VAR, os.environ[WX_ENV_VAR])) 
     142        import wxversion 
     143        if wxversion.checkInstalled(os.environ[WX_ENV_VAR]): 
     144            logging.info("Version %s of wxPython is installed, so using that version." % os.environ[WX_ENV_VAR]) 
     145            wxversion.select(os.environ[WX_ENV_VAR]) 
     146        else: 
     147            logging.error("Version %s of wxPython is not installed, so using default version." % os.environ[WX_ENV_VAR]) 
     148    else: 
     149        logging.info("You have not set the %s environment variable, so using default version of wxPython." % WX_ENV_VAR) 
     150 
     151    import wx 
     152 
     153    try: 
     154        logging.info("Wx version: %s" % wx.__version__) 
     155    except: 
     156        logging.error("Wx version: error reading version") 
     157 
     158    from . import wxcruft 
     159    wxcruft.call_later_fix() 
     160    #wxcruft.trace_new_id() 
     161    #Always use private .matplotlib setup to avoid conflicts with other 
     162    #uses of matplotlib 
     163 
     164 
     165def setup_mpl(): 
     166    import sas.sasgui 
     167    mplconfigdir = os.path.join(sas.sasgui.get_user_dir(), '.matplotlib') 
     168    if not os.path.exists(mplconfigdir): 
     169        os.mkdir(mplconfigdir) 
     170    os.environ['MPLCONFIGDIR'] = mplconfigdir 
     171    # Set backend to WXAgg; this overrides matplotlibrc, but shouldn't override 
     172    # mpl.use().  Note: Don't import matplotlib here since the script that 
     173    # we are running may not actually need it; also, putting as little on the 
     174    # path as we can 
     175    os.environ['MPLBACKEND'] = 'WXAgg' 
     176 
     177 
     178def run_gui(): 
    167179    """ 
    168180    __main__ method for loading and running SasView 
     
    170182    from multiprocessing import freeze_support 
    171183    freeze_support() 
    172     if len(sys.argv) > 1: 
    173         ## Run sasview as an interactive python interpreter 
    174         if sys.argv[1] == "-i": 
    175             sys.argv = ["ipython", "--pylab"] 
    176             from IPython import start_ipython 
    177             sys.exit(start_ipython()) 
     184    setup_logging() 
     185    setup_wx() 
     186    setup_mpl() 
     187    SasView() 
     188 
     189 
     190def run_cli(): 
     191    setup_mpl() 
     192    if len(sys.argv) == 1: 
     193        # Run sasview as an interactive python interpreter 
     194        sys.argv = ["ipython", "--pylab"] 
     195        from IPython import start_ipython 
     196        sys.exit(start_ipython()) 
     197    else: 
     198        # Run sasview as a python script interpreter 
    178199        thing_to_run = sys.argv[1] 
    179200        sys.argv = sys.argv[1:] 
     
    183204        else: 
    184205            runpy.run_module(thing_to_run, run_name="__main__") 
    185     else: 
    186         SasView() 
    187206 
    188207if __name__ == "__main__": 
    189     run() 
    190  
     208    run_gui() 
     209 
Note: See TracChangeset for help on using the changeset viewer.