示例#1
0
def main():
    """Main generation method."""
    request = plugin_pb2.CodeGeneratorRequest()
    request.ParseFromString(sys.stdin.read())

    response = plugin_pb2.CodeGeneratorResponse()

    parents = set()

    generateFiles = set(request.file_to_generate)
    files = []
    for fileObject in request.proto_file:
        if fileObject.name not in generateFiles:
            continue
        if not fileObject.package:
            sys.stderr.write(
                '%s: package definition required, but not found\n' %
                fileObject.name)
            sys.exit(1)

        name = fileObject.name.split('.')[0]
        files.append({
            'name': name,
            'package': fileObject.package.lstrip('.'),
            'messages': fileObject.message_type
        })

        path = fileObject.package.lstrip('.').split('.')[:-1]
        for i in range(len(path)):
            filePathParts = path[:i + 1]
            package = '.'.join(filePathParts)
            filePath = os.path.join(*filePathParts)
            if package not in parents:
                initPy = response.file.add()
                initPy.name = os.path.join('src', filePath, '__init__.py')
                initPy.content = """
import pkg_resources
pkg_resources.declare_namespace('%s')
""" % package
                parents.add(package)

        # Write the C file.
        writeCFile(response, name, fileObject)

    writeSetupPy(response, files, parents)
    writeTests(response, files)
    writeManifest(response, files)

    sys.stdout.write(response.SerializeToString())
def main():
    plugin_require_bin = sys.stdin.read()
    code_gen_req = plugin_pb2.CodeGeneratorRequest()
    code_gen_req.ParseFromString(plugin_require_bin)

    env = Env()
    for proto_file in code_gen_req.proto_file:
        code_gen_file(proto_file, env, proto_file.name
                      in code_gen_req.file_to_generate)

    code_generated = plugin_pb2.CodeGeneratorResponse()
    for k in _files:
        file_desc = code_generated.file.add()
        file_desc.name = k
        file_desc.content = _files[k]

    sys.stdout.write(code_generated.SerializeToString())
示例#3
0
def main():

    cg_req = plugin_pb2.CodeGeneratorRequest()
    cg_req.ParseFromString(sys.stdin.read())

    #print >> sys.stderr, cg_req

    files_to_generate = set(cg_req.file_to_generate)

    cg_resp = plugin_pb2.CodeGeneratorResponse()

    for proto_file in cg_req.proto_file:

        if proto_file.name not in files_to_generate:
            continue

        # base name of input proto file
        name = proto_file.name.split('.')[0]

        # package namespace components
        package = proto_file.package.split('.') if proto_file.package else []

        # expected location of generated cpp header
        cpp_header = package + ['%s.pb.h' % name]
        cpp_header = os.path.join(*cpp_header)

        # path template for generated Message / Enum sources
        msg_path = '/'.join([name, '%s.cpp'])

        generated_names = []

        enum_queue = [(i.name, i) for i in proto_file.enum_type]
        msg_queue = [(i.name, i) for i in proto_file.message_type]

        # Enumerate messages
        while msg_queue:

            msg_full_name, msg_type = msg_queue.pop(0)

            ofile = cg_resp.file.add()
            ofile.name = msg_path % msg_full_name

            #print >> sys.stderr, ofile.name

            ofile.content = '\n'.join(
                generate_message(msg_full_name, msg_type, cpp_header, package))

            generated_names.append(msg_full_name)

            for nested_msg in msg_type.nested_type:
                msg_queue.append(
                    ('%s_%s' % (msg_full_name, nested_msg.name), nested_msg))

            for nested_enum in msg_type.enum_type:
                enum_queue.append(
                    ('%s_%s' % (msg_full_name, nested_enum.name), nested_enum))

        # Enumerate enumerations
        while enum_queue:

            enum_full_name, enum_type = enum_queue.pop(0)

            ofile = cg_resp.file.add()
            ofile.name = msg_path % enum_full_name

            #print >> sys.stderr, ofile.name

            ofile.content = '\n'.join(
                generate_enum(enum_full_name, enum_type, cpp_header, package))

            generated_names.append(enum_full_name)

        # Generate top-level module
        ofile = cg_resp.file.add()
        ofile.name = '_%s.cpp' % name

        ofile.content = '\n'.join(
            generate_module(name, generated_names, package))

    sys.stdout.write(cg_resp.SerializeToString())
示例#4
0
def PluginRequestFromString(data):
    request_proto = plugin_pb2.CodeGeneratorRequest()
    request_proto.ParseFromString(data)
    return PluginRequest(request_proto)