def main(): # Read request protobuf message from stdin and parse data = sys.stdin.buffer.read() request = plugin.CodeGeneratorRequest() request.ParseFromString(data) # log("request options: %r"% request.parameter) # assume no spaces around options and key and vals do not contain '"' or ',' if request.parameter: options = { k: v for k, v in (opt.split('=') for opt in request.parameter.split(',')) } else: options = {*() } # https://twitter.com/raymondh/status/980864744549576704 # log("request options: %r"% options) # Create new response object to fill in and return to the caller response = plugin.CodeGeneratorResponse() # Generate cypher statements. We're only going to produce one file content = generate_code(request, options) f = response.file.add() f.name = 'proto.cql' f.content = '\n'.join(content) # Serialise and return response protobuf message to caller output = response.SerializeToString() sys.stdout.buffer.write(output)
def main(): # Read request message from stdin OPTS = Cmdoptions() if six.PY2: DATA = sys.stdin.read() else: DATA = sys.stdin.buffer.read() # open('test.dat', 'wb').write(DATA) # DATA = open('test.dat', 'rb').read() # Parse request REQUEST = plugin.CodeGeneratorRequest() REQUEST.ParseFromString(DATA) # Create response RESPONSE = plugin.CodeGeneratorResponse() # Generate code generate_code(OPTS, REQUEST, RESPONSE) # Serialise response message OUTPUT = RESPONSE.SerializeToString() # Write to stdout if six.PY2: sys.stdout.write(OUTPUT) else: sys.stdout.buffer.write(OUTPUT)
def main(): # type: () -> None # Read request message from stdin if six.PY3: data = sys.stdin.buffer.read() else: data = sys.stdin.read() # Parse request request = plugin_pb2.CodeGeneratorRequest() request.ParseFromString(data) # Create response response = plugin_pb2.CodeGeneratorResponse() # Generate mypy generate_mypy_stubs(Descriptors(request), response, "quiet" in request.parameter) # Serialise response message output = response.SerializeToString() # Write to stdout if six.PY3: sys.stdout.buffer.write(output) else: sys.stdout.write(output)
def plugin(output_descriptors): """Protoc plugin entry point. This defines protoc plugin and manages the stdin -> stdout flow. An api_proto_plugin is defined by the provided visitor. See http://www.expobrain.net/2015/09/13/create-a-plugin-for-google-protocol-buffer/ for further details on protoc plugin basics. Args: output_descriptors: a list of OutputDescriptors. """ request = plugin_pb2.CodeGeneratorRequest() request.ParseFromString(sys.stdin.buffer.read()) response = plugin_pb2.CodeGeneratorResponse() cprofile_enabled = os.getenv('CPROFILE_ENABLED') # We use request.file_to_generate rather than request.file_proto here since we # are invoked inside a Bazel aspect, each node in the DAG will be visited once # by the aspect and we only want to generate docs for the current node. for file_to_generate in request.file_to_generate: # Find the FileDescriptorProto for the file we actually are generating. file_proto = [ pf for pf in request.proto_file if pf.name == file_to_generate ][0] if cprofile_enabled: pr = cProfile.Profile() pr.enable() for od in output_descriptors: f = response.file.add() f.name = file_proto.name + od.output_suffix # Don't run API proto plugins on things like WKT types etc. if not file_proto.package.startswith('envoy.'): continue if request.HasField("parameter") and od.want_params: params = dict( param.split('=') for param in request.parameter.split(',')) xformed_proto = od.xform(file_proto, params) visitor_factory = od.visitor_factory(params) else: xformed_proto = od.xform(file_proto) visitor_factory = od.visitor_factory() f.content = traverse.traverse_file( xformed_proto, visitor_factory) if xformed_proto else '' if cprofile_enabled: pr.disable() stats_stream = io.StringIO() ps = pstats.Stats(pr, stream=stats_stream).sort_stats( os.getenv('CPROFILE_SORTBY', 'cumulative')) stats_file = response.file.add() stats_file.name = file_proto.name + '.profile' ps.print_stats() stats_file.content = stats_stream.getvalue() # Also include the original FileDescriptorProto as text proto, this is # useful when debugging. descriptor_file = response.file.add() descriptor_file.name = file_proto.name + ".descriptor.proto" descriptor_file.content = str(file_proto) sys.stdout.buffer.write(response.SerializeToString())
def Main(): # http://www.expobrain.net/2015/09/13/create-a-plugin-for-google-protocol-buffer/ request = plugin_pb2.CodeGeneratorRequest() request.ParseFromString(sys.stdin.read()) response = plugin_pb2.CodeGeneratorResponse() cprofile_enabled = os.getenv('CPROFILE_ENABLED') for proto_file in request.proto_file: f = response.file.add() f.name = proto_file.name + '.rst' if cprofile_enabled: pr = cProfile.Profile() pr.enable() # We don't actually generate any RST right now, we just string dump the # input proto file descriptor into the output file. f.content = GenerateRst(proto_file) if cprofile_enabled: pr.disable() stats_stream = StringIO.StringIO() ps = pstats.Stats(pr, stream=stats_stream).sort_stats( os.getenv('CPROFILE_SORTBY', 'cumulative')) stats_file = response.file.add() stats_file.name = proto_file.name + '.rst.profile' ps.print_stats() stats_file.content = stats_stream.getvalue() sys.stdout.write(response.SerializeToString())
def main_plugin(): '''Main function when invoked as a protoc plugin.''' import sys if sys.platform == "win32": import os, msvcrt # Set stdin and stdout to binary mode msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) data = sys.stdin.read() request = plugin_pb2.CodeGeneratorRequest.FromString(data) import shlex args = shlex.split(request.parameter) options, dummy = optparser.parse_args(args) Globals.verbose_options = options.verbose response = plugin_pb2.CodeGeneratorResponse() for filename in request.file_to_generate: for fdesc in request.proto_file: if fdesc.name == filename: results = process_file(filename, fdesc, options) f = response.file.add() f.name = results['headername'] f.content = results['headerdata'] f = response.file.add() f.name = results['sourcename'] f.content = results['sourcedata'] sys.stdout.write(response.SerializeToString())
def wrapper(): # type: () -> None # Read request message from stdin if six.PY3: data = sys.stdin.buffer.read() else: data = sys.stdin.read() # Parse request request = plugin_pb2.CodeGeneratorRequest() request.ParseFromString(data) # Create response response = plugin_pb2.CodeGeneratorResponse() func(request, response) # Serialise response message output = response.SerializeToString() # Write to stdout if six.PY3: sys.stdout.buffer.write(output) else: sys.stdout.write(output)
def entrypoint(): try: source = sys.stdin.buffer dest = sys.stdout.buffer except AttributeError: source = sys.stdin dest = sys.stdout # Read request message from stdin r = plugin_pb2.CodeGeneratorRequest.FromString(source.read()) output_filename = 'gapic.v1.yaml' if r.parameter: with open(r.parameter, 'r') as f: gapic_v2 = yaml.load(f) output_filename = re.sub(r'[._]v[\d]', '.v1', r.parameter) if '.v1' not in output_filename: output_filename = re.sub(r'\.ya?ml$', '.v1.yaml', output_filename) else: gapic_v2 = {} # Create a GAPIC v1 YAML. gapic_v1 = gapic_utils.reconstruct_gapic_yaml(gapic_v2, r) # Write to stdout F = plugin_pb2.CodeGeneratorResponse.File dest.write( plugin_pb2.CodeGeneratorResponse(file=[ F(name=output_filename, content=yaml.dump( gapic_v1, default_flow_style=False, )) ]).SerializeToString())
def code_generation() -> Iterator[Tuple[plugin_pb2.CodeGeneratorRequest, plugin_pb2.CodeGeneratorResponse], ]: if len(sys.argv) > 1 and sys.argv[1] in ("-V", "--version"): print("mypy-protobuf " + __version__) sys.exit(0) # Read request message from stdin data = sys.stdin.buffer.read() # Parse request request = plugin_pb2.CodeGeneratorRequest() request.ParseFromString(data) # Create response response = plugin_pb2.CodeGeneratorResponse() # Declare support for optional proto3 fields response.supported_features |= ( plugin_pb2.CodeGeneratorResponse.FEATURE_PROTO3_OPTIONAL) yield request, response # Serialise response message output = response.SerializeToString() # Write to stdout sys.stdout.buffer.write(output)
def main(): # logging.debug(sys.argv) # parser = optparse.OptionParser() # parser.add_option('--out', dest='outdir') # parser.add_option('--parameter', dest='parameter') # options = parser.parse_args() # outdir = '' # parameter = '' # logging.debug('options: %s', options) # logging.debug('outdir: %s', outdir) # logging.debug('param: %s', parameter) # Read request message from stdin input = sys.stdin.read() # Parse request request = plugin.CodeGeneratorRequest() request.ParseFromString(input) # Create response response = plugin.CodeGeneratorResponse() # Generate code generate_code(request, response) # Serialise response message output = response.SerializeToString() # Write to stdout sys.stdout.write(output)
def main_cli(): # The main function when running from the command line and debugging. Instead of receiving data from protoc this # will read in a binary file stored the previous time main_plugin() is ran. with open("debug_embbeded_proto.bin", 'rb') as file: data = file.read() request = plugin.CodeGeneratorRequest.FromString(data) # Create response response = plugin.CodeGeneratorResponse() # Generate code try: generate_code(request, response) except jinja2.UndefinedError as e: response.error = "Embedded Proto error - Template Undefined Error exception: " + str(e) except jinja2.TemplateRuntimeError as e: response.error = "Embedded Proto error - Template Runtime Error exception: " + str(e) except jinja2.TemplateAssertionError as e: response.error = "Embedded Proto error - TemplateAssertionError exception: " + str(e) except jinja2.TemplateSyntaxError as e: response.error = "Embedded Proto error - TemplateSyntaxError exception: " + str(e) except jinja2.TemplateError as e: response.error = "Embedded Proto error - TemplateError exception: " + str(e) except Exception as e: response.error = "Embedded Proto error - " + str(e) # For debugging purposes print the result to the console. for response_file in response.file: print(response_file.name) print(response_file.content)
def _generate(self, request): response = plugin.CodeGeneratorResponse() for proto_file in request.proto_file: name, content = self._make_file(proto_file) f = response.file.add() f.name = name f.content = content return response
def parse(): data = sys.stdin.read() request = plugin.CodeGeneratorRequest() request.ParseFromString(data) response = plugin.CodeGeneratorResponse() gen(request, response) output = response.SerializeToString() sys.stdout.write(output)
def main(): data = sys.stdin.buffer.read() request = plugin_pb2.CodeGeneratorRequest() request.ParseFromString(data) response = plugin_pb2.CodeGeneratorResponse() generate_service_metadata(request, response) output = response.SerializeToString() sys.stdout.buffer.write(output)
def main() -> None: # Load the request from stdin request = plugin.CodeGeneratorRequest.FromString(sys.stdin.buffer.read()) # Create a response response = plugin.CodeGeneratorResponse() process(request, response) # Serialize response and write to stdout sys.stdout.buffer.write(response.SerializeToString())
def main() -> int: """Protobuf compiler plugin entrypoint. Reads a CodeGeneratorRequest proto from stdin and writes a CodeGeneratorResponse to stdout. """ data = sys.stdin.buffer.read() request = plugin_pb2.CodeGeneratorRequest.FromString(data) response = plugin_pb2.CodeGeneratorResponse() process_proto_request(request, response) sys.stdout.buffer.write(response.SerializeToString()) return 0
def generate_reactive(request): params = AutoGen.parse_parameter(request.parameter) is_java = params["file_ext"] == "java" name_parser_factory.set_template_path(params["template_path"]) type_info_factory.set_template_path(params["template_path"]) template_env = get_template_env(params["template_path"]) _codegen_response = plugin_pb2.CodeGeneratorResponse() for proto_file in request.proto_file: package = AutoGen.extract_package(proto_file, is_java) plugin_name, plugin_dir = AutoGen.extract_plugin_name_and_dir( proto_file.name, package, is_java) if package.startswith( "google" ) or package == "mavsdk.options" or package.startswith( "com.google") or package == "options.mavsdk": continue docs = Docs.collect_docs(proto_file.source_code_info) enums = Enum.collect_enums(plugin_name, package, proto_file.enum_type, template_env, docs) structs = Struct.collect_structs(plugin_name, package, proto_file.message_type, template_env, docs) requests = Struct.collect_requests(package, proto_file.message_type, docs) responses = Struct.collect_responses(package, proto_file.message_type, docs) methods = Method.collect_methods(plugin_name, package, is_server(plugin_name), docs, proto_file.service[0].method, structs, requests, responses, template_env) out_file = File(plugin_name, package, template_env, docs, enums, structs, methods, has_result(structs), is_server(plugin_name)) # Fill response f = _codegen_response.file.add() f.name = f"{plugin_dir}/{plugin_name}.{params['file_ext']}" f.content = str(out_file) return _codegen_response
def run_plugin(input_raw_file, output_raw_file): request = plugin.CodeGeneratorRequest() request.ParseFromString(input_raw_file.read()) proto_py = to_dict_with_locations(request) response = plugin.CodeGeneratorResponse() file = response.file.add() file.name = 'request.json' file.content = json.dumps(proto_py) output_raw_file.write(response.SerializeToString())
def main() -> None: parser = argparse.ArgumentParser() parser.add_argument("--prefix", type=str, default="") data = sys.stdin.buffer.read() request = plugin_pb2.CodeGeneratorRequest() request.ParseFromString(data) args = parser.parse_args(request.parameter.split()) response = plugin_pb2.CodeGeneratorResponse() cython_files = ProtoFile.from_file_descriptor_protos( request.proto_file, set(request.file_to_generate), args.prefix) write_module(cython_files, response) sys.stdout.buffer.write(response.SerializeToString())
def generate_response(request): response = plugin_pb2.CodeGeneratorResponse() sources = [ FileSource.from_descriptor(file_descriptor) for file_descriptor in request.proto_file ] for pipeline in (py3_pipeline, typescript_pipeline): for path, source in pipeline.run(sources=sources).items(): f = response.file.add() f.name = path f.content = source return response
def Plugin(output_suffix, visitor): """Protoc plugin entry point. This defines protoc plugin and manages the stdin -> stdout flow. An api_proto_plugin is defined by the provided visitor. See http://www.expobrain.net/2015/09/13/create-a-plugin-for-google-protocol-buffer/ for further details on protoc plugin basics. Args: output_suffix: output files are generated alongside their corresponding input .proto, with this filename suffix. visitor: visitor.Visitor defining the business logic of the plugin. """ request = plugin_pb2.CodeGeneratorRequest() request.ParseFromString(sys.stdin.buffer.read()) response = plugin_pb2.CodeGeneratorResponse() cprofile_enabled = os.getenv('CPROFILE_ENABLED') # We use request.file_to_generate rather than request.file_proto here since we # are invoked inside a Bazel aspect, each node in the DAG will be visited once # by the aspect and we only want to generate docs for the current node. for file_to_generate in request.file_to_generate: # Find the FileDescriptorProto for the file we actually are generating. file_proto = [pf for pf in request.proto_file if pf.name == file_to_generate][0] f = response.file.add() f.name = file_proto.name + output_suffix if cprofile_enabled: pr = cProfile.Profile() pr.enable() # We don't actually generate any RST right now, we just string dump the # input proto file descriptor into the output file. f.content = traverse.TraverseFile(file_proto, visitor) if cprofile_enabled: pr.disable() stats_stream = io.StringIO() ps = pstats.Stats(pr, stream=stats_stream).sort_stats(os.getenv('CPROFILE_SORTBY', 'cumulative')) stats_file = response.file.add() stats_file.name = file_proto.name + output_suffix + '.profile' ps.print_stats() stats_file.content = stats_stream.getvalue() # Also include the original FileDescriptorProto as text proto, this is # useful when debugging. descriptor_file = response.file.add() descriptor_file.name = file_proto.name + ".descriptor.proto" descriptor_file.content = str(file_proto) sys.stdout.buffer.write(response.SerializeToString())
def generate(self): response = plugin.CodeGeneratorResponse() for f in self.request.file_to_generate: self.fd = self.get_fd(f) pbmod = os.path.splitext(self.fd.name)[0] + "_pb2" try: out_file = response.file.add() out_file.name = os.path.splitext(self.fd.name)[0] + "_nrpc.py" out_file.content = template.render(g=self, fd=self.fd, pbmod=pbmod) finally: self.fd = None return response
def main(): """Main generation method.""" request = plugin_pb2.CodeGeneratorRequest() request.ParseFromString(sys.stdin.read()) response = plugin_pb2.CodeGeneratorResponse() packages = {} to_generate = set(request.file_to_generate) for proto_file in request.proto_file: if proto_file.name not in to_generate: continue if not proto_file.package: sys.stderr.write( '%s: package definition required, but not found\n' % proto_file.name) sys.exit(1) write_proto_cc(response, proto_file) package = proto_file.package.lstrip('.') if package in packages: packages[package].append(proto_file) else: packages[package] = [proto_file] package_list = [{ 'name': k, 'children': v } for (k, v) in packages.iteritems()] # sys.stderr.write('%s' % package_list) for package in package_list: write_module_cc(response, package) write_setup_py(response, package_list) write_tests(response, package_list) write_manifest(response, package_list) create_namespaces(response, package_list) sys.stdout.write(response.SerializeToString())
def main(): # Uncomment to enable to debug logging. # logger.setLevel(logging.DEBUG) logger.debug("Python version info: %r" % sys.version_info) request = plugin_pb2.CodeGeneratorRequest() request.ParseFromString(sys.stdin.read()) response = plugin_pb2.CodeGeneratorResponse() # Dependent proto files are included in the plugin_pb2.CodeGeneratorRequest(). Only process the proto file which the # protoc compiler was invoked with. proto_file = _get_direct_proto_file_from_request(request) Protodoc(proto_file, response).generate_rsts() sys.stdout.write(response.SerializeToString())
def main() -> int: """Protobuf compiler plugin entrypoint. Reads a CodeGeneratorRequest proto from stdin and writes a CodeGeneratorResponse to stdout. """ data = sys.stdin.buffer.read() request = plugin_pb2.CodeGeneratorRequest.FromString(data) response = plugin_pb2.CodeGeneratorResponse() process_proto_request(request, response) # Declare that this plugin supports optional fields in proto3. response.supported_features |= ( # type: ignore[attr-defined] response.FEATURE_PROTO3_OPTIONAL) # type: ignore[attr-defined] sys.stdout.buffer.write(response.SerializeToString()) return 0
def main_cli(): # The main function when running from the command line and debugging. Instead of receiving data from protoc this # will read in a binary file stored the previous time main_plugin() is ran. with open("debug_embbeded_proto.bin", 'rb') as file: data = file.read() request = plugin.CodeGeneratorRequest.FromString(data) # Create response response = plugin.CodeGeneratorResponse() # Generate code generate_code(request, response) for response_file in response.file: print(response_file.name) print(response_file.content)
def main(data): # Parse request request = plugin.CodeGeneratorRequest() request.ParseFromString(data) java_package = resolve_java_package_name(request) gapic_config = gapic_utils.read_from_gapic_yaml(request.parameter) # Generate output response = plugin.CodeGeneratorResponse() generate_resource_name_types(response, gapic_config, java_package) # Serialise response message output = response.SerializeToString() return output
def main(data): # Parse request request = plugin.CodeGeneratorRequest() request.ParseFromString(data) java_packages = resolve_java_package_names(request) gapic_config = gapic_utils.read_from_gapic_yaml(request) # Generate output response = plugin.CodeGeneratorResponse() for java_package in java_packages: generate_resource_name_types(response, gapic_config, java_package) response.supported_features = plugin.CodeGeneratorResponse.FEATURE_PROTO3_OPTIONAL # Serialise response message output = response.SerializeToString() return output
def main() -> None: # Read request message from stdin data = sys.stdin.buffer.read() # Parse request request = plugin.CodeGeneratorRequest() request.ParseFromString(data) # Create response response = plugin.CodeGeneratorResponse() # Generate mypy generate_mypy_stubs(Descriptors(request), response) # Serialise response message output = response.SerializeToString() # Write to stdout sys.stdout.buffer.write(output)
def generate_code( request: plugin_pb2.CodeGeneratorRequest ) -> plugin_pb2.CodeGeneratorResponse: global _response if len(request.proto_file) == 0: return for fd in request.proto_file: _process_file1(fd) for fd in request.proto_file: _process_file2(fd) response = _response _response = plugin_pb2.CodeGeneratorResponse() _message_path_2_file_path.clear() _message_path_2_message_name_offset.clear() return response