def run(flags, output=sys.stdout):
    try :
        if flags.structs:
            # which protocol to use
            if flags.protocol == 'binary':
                protocol = TBinaryProtocol
            elif flags.protocol == 'compact':
                protocol = TCompactProtocol
            elif flags.protocol == 'json':
                protocol = TJSONProtocol
            else:
                output.write('Unknown protocol: %s' % flags.protocol)
                output.write('Valid options for --protocol are: %s' % VALID_PROTOCOLS)
                sys.exit(1)

            thrift_file = ThriftStructFile(
                protocol,
                file_name=flags.file,
                read_values=not flags.skip_values,
                padding=flags.padding,
                debug=flags.debug
            )
        else:
            thrift_file = ThriftMessageFile(
                file_name=flags.file,
                finagle_thrift=flags.finagle_thrift,
                read_values=not flags.skip_values,
                padding=flags.padding,
                debug=flags.debug
            )
    except ThriftFile.Error as ex:
        output.write(ex.message)
        sys.exit(1)

    pp = pprint.PrettyPrinter(indent=4)
    holes = []
    total_msg_read = 0
    try:
        for msg, hole in thrift_file:
            output.write(pp.pformat(msg.as_dict) if flags.pretty else msg)
            output.write('\n')
            if hole:
                holes.append(hole)
            total_msg_read += 1
            if 0 < flags.max_messages <= total_msg_read:
                break
    except __HOLE__:
        pass

    what = 'structs' if flags.structs else 'msgs'
    if holes:
        output.write('Read %s: %d\nHoles: %d\n' % (what, total_msg_read, len(holes)))
        if flags.show_holes:
            for idx, hole in enumerate(holes, start=1):
                output.write('#%d: start=%d, size=%d' % (idx, hole[0], hole[1]))
    else:
        output.write('Read %s: %d\nNo bytes skipped' % (what, total_msg_read))
dataset/ETHPy150Open pinterest/thrift-tools/thrift_tools/file_reader.py/run