Extending pyprotobuf

Developing a custom generator

Create a setup.py

Custom generators can be registered by creating a setup.py with the entry point pyprotobuf.generators. pyprotobuf will detect the generator and make it available as a format.

setup.py:

setup(
    # ...
    entry_points = '''
    [pyprotobuf.generators]
    custom = custom_generator
    '''
    # ...
 )

Create a generator module

Note

The CodeGenerator api is unstable.

custom_generator.py:

from pyprotobuf.codegenerator import CodeGenerator

class Generator(CodeGenerator):
    def generate_file(self, protonode, **kwargs):
        """ Custom ProtoNode generating logic

            :return: The compiled code
            :rtype: str
        """
        pass

__generator__ = Generator

The compiler AST

The compiler produces a tree in the form of:

  • RootNode * PackageNode

    • FileNode * MessageNode * ...

FileNodes without packages declarations are placed in an unnamed PackageNode under the root.