def main(): set_stdout_encoding() parser = argparse.ArgumentParser( description="Compare JSON representation of two concrete files") parser.add_argument('--include-uuids', action='store_true', help="Include UUIDs in JSON output") parser.add_argument('--include-timestamps', action='store_true', help="Include timestamps in JSON output") parser.add_argument('-l', '--loglevel', '--log-level', help='Logging verbosity level threshold (to stderr)', default='info') parser.add_argument('file_one') parser.add_argument('file_two') concrete.version.add_argparse_argument(parser) args = parser.parse_args() logging.basicConfig(format='%(asctime)-15s %(levelname)s: %(message)s', level=args.loglevel.upper()) tmp_path = tempfile.mkdtemp() json_one_filename = os.path.join(tmp_path, os.path.basename(args.file_one)) json_two_filename = os.path.join(tmp_path, os.path.basename(args.file_two)) # Prevent filename collision if json_one_filename == json_two_filename: json_two_filename += ".1" json_comm_one = communication_file_to_json( args.file_one, remove_timestamps=not args.include_timestamps, remove_uuids=not args.include_uuids) json_comm_two = communication_file_to_json( args.file_two, remove_timestamps=not args.include_timestamps, remove_uuids=not args.include_uuids) codecs.open(json_one_filename, "w", encoding="utf-8").write(json_comm_one) codecs.open(json_two_filename, "w", encoding="utf-8").write(json_comm_two) diff_command = "diff --unified %s %s" % ( json_one_filename, json_two_filename) diff_output = subprocess.Popen( diff_command, shell=True, stdout=subprocess.PIPE).stdout.read() # Clean up temporary files os.remove(json_one_filename) os.remove(json_two_filename) os.rmdir(tmp_path) print(diff_output)
def main(): # Make stdout output UTF-8, preventing "'ascii' codec can't encode" errors sys.stdout = codecs.getwriter('utf8')(sys.stdout) parser = argparse.ArgumentParser(description="Pretty Print a Concrete Communication file") parser.add_argument('communication_file') parser.add_argument('json_file', nargs='?', default='STDOUT') args = parser.parse_args() if args.json_file == 'STDOUT': print communication_file_to_json(args.communication_file) else: f = codecs.open(args.json_file, "w", encoding="utf-8") f.write(communication_file_to_json(args.communication_file)) f.close()
def main(): parser = argparse.ArgumentParser( description="Compare JSON representation of two concrete files") parser.add_argument('--include-uuids', action='store_true', help="Include UUIDs in JSON output") parser.add_argument('--include-timestamps', action='store_true', help="Include timestamps in JSON output") parser.add_argument('file_one') parser.add_argument('file_two') concrete.version.add_argparse_argument(parser) args = parser.parse_args() tmp_path = tempfile.mkdtemp() json_one_filename = os.path.join(tmp_path, os.path.basename(args.file_one)) json_two_filename = os.path.join(tmp_path, os.path.basename(args.file_two)) # Prevent filename collision if json_one_filename == json_two_filename: json_two_filename += ".1" json_comm_one = communication_file_to_json( args.file_one, remove_timestamps=not args.include_timestamps, remove_uuids=not args.include_uuids) json_comm_two = communication_file_to_json( args.file_two, remove_timestamps=not args.include_timestamps, remove_uuids=not args.include_uuids) codecs.open(json_one_filename, "w", encoding="utf-8").write(json_comm_one) codecs.open(json_two_filename, "w", encoding="utf-8").write(json_comm_two) diff_command = "diff --unified %s %s" % (json_one_filename, json_two_filename) diff_output = subprocess.Popen(diff_command, shell=True, stdout=subprocess.PIPE).stdout.read() # Clean up temporary files os.remove(json_one_filename) os.remove(json_two_filename) os.rmdir(tmp_path) print diff_output
def main(): parser = argparse.ArgumentParser( description="Compare JSON representation of two concrete files") parser.add_argument('--include-uuids', action='store_true', help="Include UUIDs in JSON output") parser.add_argument('--include-timestamps', action='store_true', help="Include timestamps in JSON output") parser.add_argument('file_one') parser.add_argument('file_two') concrete.version.add_argparse_argument(parser) args = parser.parse_args() tmp_path = tempfile.mkdtemp() json_one_filename = os.path.join(tmp_path, os.path.basename(args.file_one)) json_two_filename = os.path.join(tmp_path, os.path.basename(args.file_two)) # Prevent filename collision if json_one_filename == json_two_filename: json_two_filename += ".1" json_comm_one = communication_file_to_json( args.file_one, remove_timestamps=not args.include_timestamps, remove_uuids=not args.include_uuids) json_comm_two = communication_file_to_json( args.file_two, remove_timestamps=not args.include_timestamps, remove_uuids=not args.include_uuids) codecs.open(json_one_filename, "w", encoding="utf-8").write(json_comm_one) codecs.open(json_two_filename, "w", encoding="utf-8").write(json_comm_two) diff_command = "diff --unified %s %s" % ( json_one_filename, json_two_filename) diff_output = subprocess.Popen( diff_command, shell=True, stdout=subprocess.PIPE).stdout.read() # Clean up temporary files os.remove(json_one_filename) os.remove(json_two_filename) os.rmdir(tmp_path) print diff_output
def main(): set_stdout_encoding() parser = argparse.ArgumentParser( description="Pretty Print a Concrete file") parser.add_argument('--concrete_type', default='communication', choices=['communication', 'tokenlattice'], help='Default: communication') parser.add_argument('--protocol', default='simple', choices=['simple', 'TJSONProtocol'], help='Default: simple') parser.add_argument('--remove-timestamps', action='store_true', help="Removes timestamps from JSON output") parser.add_argument('--remove-uuids', action='store_true', help="Removes UUIDs from JSON output") parser.add_argument('-l', '--loglevel', '--log-level', help='Logging verbosity level threshold (to stderr)', default='info') parser.add_argument('concrete_file', help='path to input concrete communication file') parser.add_argument('json_file', nargs='?', default='-', help='path to output json file') concrete.version.add_argparse_argument(parser) args = parser.parse_args() logging.basicConfig(format='%(asctime)-15s %(levelname)s: %(message)s', level=args.loglevel.upper()) if args.protocol == 'simple': if args.concrete_type == 'communication': json_communication = communication_file_to_json( args.concrete_file, remove_timestamps=args.remove_timestamps, remove_uuids=args.remove_uuids ) else: json_communication = tokenlattice_file_to_json( args.concrete_file ) else: if args.concrete_type == 'communication': comm = read_communication_from_file(args.concrete_file) json_communication = TSerialization.serialize( comm, TJSONProtocol.TJSONProtocolFactory()).decode('utf-8') else: raise NotImplementedError if args.json_file == '-': print(json_communication) else: with codecs.open(args.json_file, 'w', encoding='utf-8') as f: f.write(json_communication)
def main(): # Make stdout output UTF-8, preventing "'ascii' codec can't encode" errors sys.stdout = codecs.getwriter('utf8')(sys.stdout) parser = argparse.ArgumentParser( description="Pretty Print a Concrete file") parser.add_argument('--concrete_type', default='communication', choices=['communication', 'tokenlattice'], help='Default: communication') parser.add_argument('--protocol', default='simple', choices=['simple', 'TJSONProtocol'], help='Default: simple') parser.add_argument('--remove-timestamps', action='store_true', help="Removes timestamps from JSON output") parser.add_argument('--remove-uuids', action='store_true', help="Removes UUIDs from JSON output") parser.add_argument('concrete_file') parser.add_argument('json_file', nargs='?', default='STDOUT') concrete.version.add_argparse_argument(parser) args = parser.parse_args() if args.protocol == 'simple': if args.concrete_type == 'communication': json_communication = communication_file_to_json( args.concrete_file, remove_timestamps=args.remove_timestamps, remove_uuids=args.remove_uuids ) else: json_communication = tokenlattice_file_to_json(args.concrete_file) else: if args.concrete_type == 'communication': comm = read_communication_from_file(args.concrete_file) json_communication = TSerialization.serialize( comm, TJSONProtocol.TJSONProtocolFactory()) else: raise NotImplementedError if args.json_file == 'STDOUT': print json_communication else: f = codecs.open(args.json_file, "w", encoding="utf-8") f.write(json_communication) f.close()