#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# =============================================================================
# Copyright Ostap developers
# =============================================================================
## @file ostap
#  
#     .oooooo.                .                        
#    d8P'  `Y8b             .o8                        
#   888      888  .oooo.o .o888oo  .oooo.   oo.ooooo.  
#   888      888 d88(  "8   888   `P  )88b   888' `88b 
#   888      888 `"Y88b.    888    .oP"888   888   888 
#   `88b    d88' o.  )88b   888 . d8(  888   888   888 
#    `Y8bood8P'  8""888P'   "888" `Y888""8o  888bod8P' 
#                                            888       
#                                           o888o      
#                                                    
#  Simple interactive PyRoot-based analysis environment to provide access
#  to zillions useful decorators for ROOT (and not only ROOT!) objects&classes  
# =============================================================================
"""
Simple interactive PyRoot-based analysis environment
to provide access to zillions useful decorators for ROOT
(and not only ROOT) objects&classes

``Python-based Interactive Environment for Smart and Friendly Physics Analysis''

The project has been designed with the kind help from Pere MATO and Andrey TSAREGORODTSEV. 

And it is based on the LoKi project: ``C++ ToolKit for Smart and Friendly Physics Analysis''
"""
# =============================================================================
## perform the actual start of ostap
import ostap.fixes.fixes 
from   ostap.core.run_ostap   import *
from   ostap.core.ostap_setup import imported
from   ostap.utils.basic      import with_ipython, typename 
import ostap.core.config      as     config  
# =============================================================================
# logging 
# =============================================================================
from ostap.logger.logger import getLogger    
logger = getLogger ( 'ostap' )

# =============================================================================
if with_ipython () :
    raise RuntimeError("Can't start Ostap from ipython!")

# ============================================================================= 
if config.batch :
    ## nothing to do 
    logger.debug  ('Bye, bye...')
    exit () 

# =============================================================================
## import imported symbols :-)
if imported : 
    globals().update ( imported )
    from ostap.logger.utils import map2table 
    ss = { k : typename  ( v ) for k,v in imported.items() }
    title  = "Imported symbols" 
    header = ( 'Symbol', 'Type' )
    logger.info ("%s:\n%s" % ( title , map2table ( ss , header = header , title = title , prefix = '# ' ) ) ) 
    del map2table, title , ss 
    
# =============================================================================
## start Ostap interactive shell 
# ============================================================================= 
if not config.arguments.simple : # ============================================
    # =========================================================================
    try : # ===================================================================
        # =====================================================================
        import IPython # ======================================================
        # =====================================================================
    except ImportError : # ====================================================
        # =====================================================================
        logger.error ( "Can't use IPython, switch to `simple' interactive console" )
        arguments.simple      = True
        arguments.embed       = False

# =============================================================================
if config.arguments.embed : ## the only one option up to Ostap v1r9
    # =========================================================================
    logger.info ('Start embedded interactive shell')
    
    import IPython
    IPython.embed ()
    # =========================================================================
elif config.arguments.simple : ## new from Ostap v1r10 
    # =========================================================================
    _vars = globals().copy()
    _vars.update( locals() )
    
    import readline
    import code
    
    logger.info ('Start simple interactive shell') 
    
    shell = code.InteractiveConsole( _vars )
    shell.interact()
    # ==========================================================================    
else :                         ## new from Ostap v1r10 
    # ==========================================================================
    _vars = globals().copy()
    _vars.update( locals() )
    
    logger.info ('Start interactive shell') 
    
    import IPython
    IPython.start_ipython ( argv = [] , user_ns = _vars )
    # =========================================================================
    
# =============================================================================
exit ()  ##  bye-bye 


# =============================================================================
##                                                                      The END 
# =============================================================================
