def main(): options = parse_option() initialize_log_handler(options.log_level) logger = logbook.Logger("pingparsing cli") logger.level = options.log_level pingparsing.set_log_level(options.log_level) output = {} if is_use_stdin(): from concurrent import futures pingparsing.set_log_level(options.log_level) max_workers = (multiprocessing.cpu_count() * 2 if options.max_workers is None else options.max_workers) count, deadline = get_ping_param(options) logger.debug("max-workers={}, count={}, deadline={}".format( max_workers, count, deadline)) try: with futures.ProcessPoolExecutor(max_workers) as executor: future_list = [] for dest_or_file in options.destination_or_file: logger.debug("start {}".format(dest_or_file)) future_list.append( executor.submit( parse_ping, logger, dest_or_file, options.interface, count, deadline, options.icmp_reply, )) for future in futures.as_completed(future_list): key, ping_data = future.result() output[key] = ping_data finally: logger.debug("shutdown ProcessPoolExecutor") executor.shutdown() else: ping_result_text = sys.stdin.read() ping_parser = pingparsing.PingParsing() stats = ping_parser.parse(ping_result_text) output = stats.as_dict() if options.icmp_reply: output["icmp_reply"] = stats.icmp_reply_list if options.indent <= 0: result = json.dumps(output) else: result = json.dumps(output, indent=options.indent) print_result(result) return 0
def test_exception(self, value, expected): with pytest.raises(expected): set_log_level(value)
def test_smoke(self, value): set_log_level(value)
def main(): options = parse_option() initialize_log_handler(options.log_level) logger = logbook.Logger("pingparsing cli") logger.level = options.log_level pingparsing.set_log_level(options.log_level) output = {} if is_use_stdin(): from concurrent import futures pingparsing.set_log_level(options.log_level) max_workers = (multiprocessing.cpu_count() * 2 if options.max_workers is None else options.max_workers) # count, deadline = get_ping_param(options) # logger.debug("max-workers={}, count={}, deadline={}".format( # max_workers, count, deadline)) try: with futures.ProcessPoolExecutor(max_workers) as executor: future_list = [] for dest_or_file in options.destination_or_file: logger.debug("start {}".format(dest_or_file)) if os.path.isfile(dest_or_file): with open(dest_or_file, "r") as ins: for line in ins: future_list.append( executor.submit( parse_ping, logger, line.strip('\r\n').strip('\n'), options.interface)) else: future_list.append( executor.submit(parse_ping, logger, dest_or_file, options.interface)) for future in futures.as_completed(future_list): key, ping_data = future.result() output[key] = ping_data finally: logger.debug("shutdown ProcessPoolExecutor") executor.shutdown() else: ping_result_text = sys.stdin.read() ping_parser = pingparsing.PingParsing() ping_parser.parse(ping_result_text) output = ping_parser.as_dict() f = open(options.output, 'w') if options.indent <= 0: print(json.dumps(output)) f.write(json.dumps(output)) else: print(json.dumps(output, indent=options.indent)) f.write(json.dumps(output, indent=options.indent)) f.close() return 0