def main(): parser = ArgumentParser( formatter_class=ArgumentDefaultsHelpFormatter, description="Interface with a Concrete Annotator service") parser.add_argument('host', help="Hostname of annotator service to which to" " connect.") parser.add_argument('port', type=int, help="Port of annotator service to which to connect.") parser.add_argument('--input', default='-', help="Input source to use. '-' for stdin; otherwise" " takes a path to a file.") parser.add_argument('--output', default='-', help="Output source to use. '-' for stdout; otherwise" " takes a path to a file.") concrete.version.add_argparse_argument(parser) args = parser.parse_args() # Won't work on Windows... but that use case is very unlikely input_path = '/dev/fd/0' if (args.input) == '-' else args.input output_path = '/dev/fd/1' if (args.output) == '-' else args.output reader = CommunicationReader(input_path) with AnnotatorClientWrapper(args.host, args.port) as client: with CommunicationWriter(output_path) as writer: for (comm, _) in reader: writer.write(client.annotate(comm))
def test_CommunicationWriter_gz_fixed_point_unicode(output_file): input_file = 'tests/testdata/les-deux-chandeliers.concrete' comm = read_communication_from_file(input_file) with CommunicationWriter(output_file, gzip=True) as writer: writer.write(comm) with open(input_file, 'rb') as expected_f: expected_data = expected_f.read() with gzip.open(output_file, 'rb') as actual_f: actual_data = actual_f.read() assert expected_data == actual_data
def test_CommunicationWriter_fixed_point_ctx_mgr(output_file): input_file = 'tests/testdata/simple_1.concrete' comm = read_communication_from_file(input_file) with CommunicationWriter(output_file) as writer: writer.write(comm) with open(input_file, 'rb') as expected_f: expected_data = expected_f.read() with open(output_file, 'rb') as actual_f: actual_data = actual_f.read() assert expected_data == actual_data
def test_CommunicationWriter_gz_fixed_point(output_file): input_file = 'tests/testdata/simple_1.concrete' comm = read_communication_from_file(input_file) writer = CommunicationWriter(gzip=True) try: writer.open(output_file) writer.write(comm) finally: writer.close() with open(input_file, 'rb') as expected_f: expected_data = expected_f.read() with gzip.open(output_file, 'rb') as actual_f: actual_data = actual_f.read() assert expected_data == actual_data
def main(): set_stdout_encoding() parser = ArgumentParser( formatter_class=ArgumentDefaultsHelpFormatter, description= "Interface with a Concrete AnnotateCommunicationService server. " "Supports either THttp/TJSONProtocol (using the '--uri' flag) " "or TSocket/TCompactProtocol (using '--host'/'--port')") parser.add_argument( '--host', default='localhost', help="Hostname of TSocket/TCompactProtocol AnnotateCommunicationService" ) parser.add_argument( '-p', '--port', type=int, default=9090, help="Port of TSocket/TCompactProtocol AnnotateCommunicationService") parser.add_argument( '--uri', '--url', help="URI of THttpServer/TJSONProtocol AnnotateCommunicationService") parser.add_argument('-l', '--loglevel', '--log-level', help='Logging verbosity level threshold (to stderr)', default='info') parser.add_argument('--input', default='-', help="Input source to use. '-' for stdin; otherwise" " takes a path to a file.") parser.add_argument('--output', default='-', help="Output source to use. '-' for stdout; otherwise" " takes a path to a file.") concrete.version.add_argparse_argument(parser) args = parser.parse_args() logging.basicConfig(format='%(asctime)-15s %(levelname)s: %(message)s', level=args.loglevel.upper()) # Won't work on Windows if args.input == '-': reader_kwargs = dict(filetype=FileType.STREAM) input_path = '/dev/fd/0' else: reader_kwargs = dict() input_path = args.input output_path = '/dev/fd/1' if args.output == '-' else args.output reader = CommunicationReader(input_path, **reader_kwargs) if args.uri: try: with HTTPAnnotateCommunicationClientWrapper(args.uri) as client: with CommunicationWriter(output_path) as writer: for (comm, _) in reader: writer.write(client.annotate(comm)) except TProtocolException as ex: logging.error(ex) logging.error( "Successfully connected to the URI '{}' using HTTP, but the URI does not " "appear to be an AnnotateCommunicationService endpoint that uses the " "Thrift THttp transport and TJSONProtocol encoding".format( args.uri)) else: try: with AnnotateCommunicationClientWrapper(args.host, args.port) as client: with CommunicationWriter(output_path) as writer: for (comm, _) in reader: writer.write(client.annotate(comm)) except TTransportException: pass