def format_exceptions(debug=False, pdb=False, profile=False, profile_filename=None, start_time=None): if debug: set_log_level('DEBUG') try: if profile: import cProfile profile = cProfile.Profile() profile.enable() yield if profile: profile.disable() if profile_filename: dump_path = os.path.expanduser(profile_filename) print('\nSaving cProfile output to: {}'.format(dump_path)) profile.dump_stats(dump_path) else: print('\n\n----PROFILE OUTPUT----\n\n') stats = pstats.Stats(profile).sort_stats('cumulative') stats.print_stats(20) # Print first 20 lines except Exception as e: if debug: traceback.print_exc() if pdb: import ipdb ipdb.post_mortem(e.__traceback__) else: stack = traceback.extract_tb(e.__traceback__) # Get last stack trace entry still in Calliope last = [i for i in stack if 'calliope' in i[0]][-1] if debug: err_string = '\nError in {}, {}:{}'.format( last[2], last[0], last[1]) else: err_string = '\nError in {}:'.format(last[2]) click.secho(err_string, fg='red') click.secho(str(e), fg='red') if start_time: print_end_time(start_time, msg='aborted due to an error') sys.exit(1)
def format_exceptions( debug=False, pdb=False, profile=False, profile_filename=None, start_time=None): if debug: set_log_level('DEBUG') try: if profile: import cProfile profile = cProfile.Profile() profile.enable() yield if profile: profile.disable() if profile_filename: dump_path = os.path.expanduser(profile_filename) print('\nSaving cProfile output to: {}'.format(dump_path)) profile.dump_stats(dump_path) else: print('\n\n----PROFILE OUTPUT----\n\n') stats = pstats.Stats(profile).sort_stats('cumulative') stats.print_stats(20) # Print first 20 lines except Exception as e: if debug: traceback.print_exc() if pdb: import ipdb ipdb.post_mortem(e.__traceback__) else: stack = traceback.extract_tb(e.__traceback__) # Get last stack trace entry still in Calliope last = [i for i in stack if 'calliope' in i[0]][-1] if debug: err_string = '\nError in {}, {}:{}'.format(last[2], last[0], last[1]) else: err_string = '\nError in {}:'.format(last[2]) click.secho(err_string, fg='red') click.secho(str(e), fg='red') if start_time: print_end_time(start_time, msg='aborted due to an error') sys.exit(1)
def set_quietness_level(quiet): if quiet: set_log_level('WARNING') else: set_log_level('SOLVER')