def main(): """ Do the full hutch-python launch sequence. Parses the user's cli arguments and distributes them as needed to the setup functions. """ # Parse the user's arguments args = parser.parse_args() # Set up logging first if args.cfg is None: log_dir = None else: log_dir = os.path.join(os.path.dirname(args.cfg), 'logs') setup_logging(dir_logs=log_dir) # Debug mode next if args.debug: debug_mode(True) # Do the first log message, now that logging is ready logger.debug('cli starting with args %s', args) # Options that mean skipping the python environment if args.create: hutch = args.create envs_dir = CONDA_BASE / 'envs' if envs_dir.exists(): # Pick most recent pcds release in our common env base = str(CONDA_BASE) path_obj = sorted(envs_dir.glob('pcds-*'))[-1] env = path_obj.name else: # Fallback: pick current env base = str(Path(os.environ['CONDA_EXE']).parent.parent) env = os.environ['CONDA_DEFAULT_ENV'] logger.info(('Creating hutch-python dir for hutch %s using' ' base=%s env=%s'), hutch, base, env) cookiecutter(str(DIR_MODULE / 'cookiecutter'), no_input=True, extra_context=dict(base=base, env=env, hutch=hutch)) return # Now other flags if args.sim: set_daq_sim(True) # Save whether we are an interactive session or a script session opts_cache['script'] = args.script # Load objects based on the configuration file objs = load(cfg=args.cfg, args=args) script = opts_cache.get('script') if script is None: # Finally start the interactive session start_ipython(argv=['--quick'], user_ns=objs, config=configure_ipython_session()) else: # Instead of setting up ipython, run the script with objs with open(script) as fn: code = compile(fn.read(), script, 'exec') exec(code, objs, objs)
def setup_cli_env(): """ Parse the user's arguments and gather the session's objects. Inlcudes objects defined by `load_conf.load` as well as debug methods from :mod:`log_setup`. Returns ------- objs: ``dict`` Mapping of object name ``str`` to object """ # Parse the user's arguments args = parser.parse_args() # Make sure the hutch's directory is in the path sys.path.insert(0, os.getcwd()) # Set up logging first if args.cfg is None: log_dir = None else: log_dir = os.path.join(os.path.dirname(args.cfg), 'logs') setup_logging(dir_logs=log_dir) # Debug mode next if args.debug: debug_mode(True) # Options that mean skipping the python environment if args.create: hutch = args.create envs_dir = CONDA_BASE / 'envs' if envs_dir.exists(): # Pick most recent pcds release in our common env base = str(CONDA_BASE) path_obj = sorted(envs_dir.glob('pcds-*'))[-1] env = path_obj.name else: # Fallback: pick current env base = str(Path(os.environ['CONDA_EXE']).parent.parent) env = os.environ['CONDA_DEFAULT_ENV'] logger.info(('Creating hutch-python dir for hutch %s using' ' base=%s env=%s'), hutch, base, env) cookiecutter(str(DIR_MODULE / 'cookiecutter'), no_input=True, extra_context=dict(base=base, env=env, hutch=hutch)) return {} # Now other flags if args.sim: set_daq_sim(True) # Save whether we are an interactive session or a script session opts_cache['script'] = args.script # Load objects based on the configuration file objs = load(cfg=args.cfg) # Add cli debug tools objs['_debug_console_level'] = set_console_level objs['_debug_mode'] = debug_mode objs['_debug_context'] = debug_context objs['_debug_wrapper'] = debug_wrapper return objs
def main(): """ Do the full hutch-python launch sequence. Parses the user's cli arguments and distributes them as needed to the setup functions. """ # Parse the user's arguments args = parser.parse_args() # Set up logging first if args.cfg is None: log_dir = None else: log_dir = os.path.join(os.path.dirname(args.cfg), 'logs') setup_logging(dir_logs=log_dir) # Debug mode next if args.debug: debug_mode(True) # Do the first log message, now that logging is ready logger.debug('cli starting with args %s', args) # Options that mean skipping the python environment if args.create: hutch = args.create envs_dir = CONDA_BASE / 'envs' if envs_dir.exists(): # Pick most recent pcds release in our common env base = str(CONDA_BASE) path_obj = sorted(envs_dir.glob('pcds-*'))[-1] env = path_obj.name else: # Fallback: pick current env base = str(Path(os.environ['CONDA_EXE']).parent.parent) env = os.environ['CONDA_DEFAULT_ENV'] logger.info(('Creating hutch-python dir for hutch %s using' ' base=%s env=%s'), hutch, base, env) cookiecutter(str(DIR_MODULE / 'cookiecutter'), no_input=True, extra_context=dict(base=base, env=env, hutch=hutch)) return # Now other flags if args.sim: set_daq_sim(True) # Save whether we are an interactive session or a script session opts_cache['script'] = args.script # Load objects based on the configuration file objs = load(cfg=args.cfg, args=args) # Add cli debug tools objs['debug_console_level'] = set_console_level objs['debug_mode'] = debug_mode objs['debug_context'] = debug_context objs['debug_wrapper'] = debug_wrapper script = opts_cache.get('script') if script is None: ipy_config = Config() # Important Utilities ipy_config.InteractiveShellApp.extensions = [ 'hutch_python.ipython_log', 'hutch_python.bug' ] # Matplotlib setup if we have a screen if os.getenv('DISPLAY'): ipy_config.InteractiveShellApp.matplotlib = 'qt5' else: logger.warning('No DISPLAY environment variable detected. ' 'Methods that create graphics will not ' 'function properly.') # Finally start the interactive session start_ipython(argv=['--quick'], user_ns=objs, config=ipy_config) else: # Instead of setting up ipython, run the script with objs with open(script) as fn: code = compile(fn.read(), script, 'exec') exec(code, objs, objs)