def test_apply_filter_exception(self): with self.assertRaises(InvalidFilterException) as exp: apply_filter('unknown', ['args']) message = str(exp.exception) self.assertEqual("'unknown' is an invalid filter. Available filters: " + ', '.join(sorted(available_filters)), message)
def test_apply_filter_exception(self): with self.assertRaises(InvalidFilterException) as exp: apply_filter('unknown', ['args']) message = str(exp.exception) self.assertEqual( "'unknown' is an invalid filter. Available filters: " + ', '.join(sorted(available_filters)), message)
def mode_json(args, debug=False): import json from coalib.coala_main import run_coala from coalib.misc.DictUtilities import inverse_dicts from coalib.misc.Exceptions import get_exitcode from coalib.output.Logging import configure_json_logging from coalib.output.JSONEncoder import create_json_encoder if args.log_json: log_stream = configure_json_logging() JSONEncoder = create_json_encoder(use_relpath=args.relpath) results = [] if args.show_bears: try: from coalib.parsing.FilterHelper import apply_filter local_bears, global_bears = apply_filter('language', args.filter_by_language) bears = inverse_dicts(local_bears, global_bears) for bear, _ in sorted(bears.items(), key=lambda bear_tuple: bear_tuple[0].name): results.append(bear) except BaseException as exception: # pylint: disable=broad-except return get_exitcode(exception) else: results, exitcode, _ = run_coala(args=args, debug=debug) retval = {'bears': results} if args.show_bears else {'results': results} if args.log_json: retval['logs'] = [ json.loads(line) for line in log_stream.getvalue().splitlines() ] if args.output: filename = str(args.output[0]) with open(filename, 'w+') as fp: json.dump(retval, fp, cls=JSONEncoder, sort_keys=True, indent=2, separators=(',', ': ')) else: print( json.dumps(retval, cls=JSONEncoder, sort_keys=True, indent=2, separators=(',', ': '))) return 0 if args.show_bears else exitcode
def main(debug=False): configure_logging() args = None # to have args variable in except block when parse_args fails try: # Note: We parse the args here once to check whether to show bears or # not. args = default_arg_parser().parse_args() if args.debug: req_ipdb = PipRequirement('ipdb') if not req_ipdb.is_installed(): logging.error( '--debug flag requires ipdb. ' 'You can install it with:\n%s', ' '.join(req_ipdb.install_command())) sys.exit(13) if debug or args.debug: args.log_level = 'DEBUG' # Defer imports so if e.g. --help is called they won't be run from coalib.coala_modes import (mode_format, mode_json, mode_non_interactive, mode_normal) from coalib.output.ConsoleInteraction import ( show_bears, show_language_bears_capabilities) console_printer = ConsolePrinter(print_colored=not args.no_color) configure_logging(not args.no_color) if args.show_bears: from coalib.settings.ConfigurationGathering import get_all_bears kwargs = {} if args.bears: kwargs['bear_globs'] = args.bears filtered_bears = get_all_bears(**kwargs) if args.filter_by_language: logging.warning("'--filter-by-language ...' is deprecated. " "Use '--filter-by language ...' instead.") if args.filter_by is None: args.filter_by = [] args.filter_by.append(['language'] + args.filter_by_language) if args.filter_by: # Each iteration of the following loop applies # filters one by one provided as arguments try: filtered_bears = apply_filters(args.filter_by, filtered_bears) except (InvalidFilterException, NotImplementedError) as ex: # If filter is not available or is unusable console_printer.print(ex) return 2 local_bears, global_bears = filtered_bears show_bears(local_bears, global_bears, args.show_description or args.show_details, args.show_details, console_printer, args) return 0 elif args.show_capabilities: from coalib.collecting.Collectors import ( filter_capabilities_by_languages) local_bears, _ = apply_filter('language', args.show_capabilities) capabilities = filter_capabilities_by_languages( local_bears, args.show_capabilities) show_language_bears_capabilities(capabilities, console_printer) return 0 if args.json: return mode_json(args, debug=debug) except BaseException as exception: # pylint: disable=broad-except if not isinstance(exception, SystemExit): if args and args.debug: import ipdb with ipdb.launch_ipdb_on_exception(): raise if debug: raise return get_exitcode(exception) if args.format: return mode_format(args, debug=debug) if args.non_interactive: return mode_non_interactive(console_printer, args, debug=debug) return mode_normal(console_printer, None, args, debug=debug)
def test_apply_filter(self): apply_filter_result = apply_filter('language', ['c', 'java']) self.assertIsNotNone(apply_filter_result)
def main(debug=False): configure_logging() args = None # to have args variable in except block when parse_args fails try: # Note: We parse the args here once to check whether to show bears or # not. args = default_arg_parser().parse_args() if args.debug: req_ipdb = PipRequirement('ipdb') if not req_ipdb.is_installed(): logging.error('--debug flag requires ipdb. ' 'You can install it with:\n%s', ' '.join(req_ipdb.install_command())) sys.exit(13) if debug or args.debug: args.log_level = 'DEBUG' # Defer imports so if e.g. --help is called they won't be run from coalib.coala_modes import ( mode_format, mode_json, mode_non_interactive, mode_normal) from coalib.output.ConsoleInteraction import ( show_bears, show_language_bears_capabilities) console_printer = ConsolePrinter(print_colored=not args.no_color) configure_logging(not args.no_color) if args.show_bears: from coalib.settings.ConfigurationGathering import get_all_bears kwargs = {} if args.bears: kwargs['bear_globs'] = args.bears filtered_bears = get_all_bears(**kwargs) if args.filter_by_language: logging.warning( "'--filter-by-language ...' is deprecated. " "Use '--filter-by language ...' instead.") if args.filter_by is None: args.filter_by = [] args.filter_by.append(['language'] + args.filter_by_language) if args.filter_by: # Each iteration of the following loop applies # filters one by one provided as arguments try: args.filter_by = filter_vector_to_dict(args.filter_by) filtered_bears = apply_filters( args.filter_by, filtered_bears) except (InvalidFilterException, NotImplementedError) as ex: # If filter is not available or is unusable console_printer.print(ex) return 2 local_bears, global_bears = filtered_bears show_bears(local_bears, global_bears, args.show_description or args.show_details, args.show_details, console_printer, args) return 0 elif args.show_capabilities: from coalib.collecting.Collectors import ( filter_capabilities_by_languages) local_bears, _ = apply_filter('language', args.show_capabilities) capabilities = filter_capabilities_by_languages( local_bears, args.show_capabilities) show_language_bears_capabilities(capabilities, console_printer) return 0 if args.json: return mode_json(args, debug=debug) except BaseException as exception: # pylint: disable=broad-except if not isinstance(exception, SystemExit): if args and args.debug: import ipdb with ipdb.launch_ipdb_on_exception(): raise if debug: raise return get_exitcode(exception) if args.format: return mode_format(args, debug=debug) if args.non_interactive: return mode_non_interactive(console_printer, args, debug=debug) return mode_normal(console_printer, None, args, debug=debug)