flattools

Pure Python Flatbuffer Schema Compiler


Keywords
flatbuffers, python, thriftpy
License
MIT
Install
pip install flattools==0.6

Documentation

Flattools

Python tools to deal with flatbuffers:

  • flatc.py

    Flatbuffer compiler in python

  • thrift2fbs.py.

    Convert thrift to flatbuffers where possible

  • pp.py

    gdb pretty printer for flatbuffers

Code Demo: thrift2fbs.py

struct Person {
    1: string name,
    2: string address,
    3: i16 age
}

struct Email {
    1: string subject = 'Subject',
    2: string content,
    3: Person sender,
    4: required Person recver,
}

enum Operation {
  ADD = 1,
  SUBTRACT = 2,
  MULTIPLY = 3,
  DIVIDE = 4
}

Generates

table Person {
   name : string;
   address : string;
   age : short;
}

table Email {
   subject : string;
   content : string;
   sender : Person;
   recver : Person;
}

enum Operation : int {
  MULTIPLY = 3,
  ADD = 1,
  SUBTRACT = 2,
  DIVIDE = 4,
}

Code Demo: flatc.py

There are several templates in tests/fbs_template*

Running

$ flatc.py kvstore.fbs --yaml

Generates something like

# automatically generated by the FlatBuffers compiler, do not modify
attributes:
    obj_id:
        optional: True
        type: long
    age:
        optional: True
        type: long
    ...

This uses templated code generation using jinja2.

Installation

RPM based systems

$ sudo yum install python-ply python-jinja2

Ubuntu/Debian

$ sudo apt install python-ply python-jinja2