mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-21 16:14:23 +01:00
msggen: adding example and fixes typo
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
This commit is contained in:
committed by
Christian Decker
parent
4e902fbd88
commit
2ab2061cc9
37
contrib/msggen/examples/generator_example.py
Normal file
37
contrib/msggen/examples/generator_example.py
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
#! /usr/bin/python3
|
||||||
|
"""
|
||||||
|
Example of usage msggen module.
|
||||||
|
|
||||||
|
This example introduces a fake generator to understand how the
|
||||||
|
package works, If you would like to see a real generator example
|
||||||
|
try to see the Rust generator in the `msggen/gen/rust.py`
|
||||||
|
|
||||||
|
author: https://github.com/vincenzopalazzo
|
||||||
|
"""
|
||||||
|
from msggen.gen.generator import GeneratorChain, IGenerator
|
||||||
|
from msggen import Service
|
||||||
|
from msggen.utils import load_jsonrpc_service
|
||||||
|
|
||||||
|
|
||||||
|
class MonkylangGen(IGenerator):
|
||||||
|
"""This is the custom generator that implements a monkylang generator
|
||||||
|
that uses the interface handler IGenerator."""
|
||||||
|
|
||||||
|
def generate(self, service: Service):
|
||||||
|
self.write('println("Monky")')
|
||||||
|
|
||||||
|
|
||||||
|
def register_monkylang_gen(generator_chain: GeneratorChain):
|
||||||
|
"""Helper function to register the custom generator, and
|
||||||
|
load the correct path of the json schema."""
|
||||||
|
file = '<your_path_of_result>'
|
||||||
|
dest = open(file, 'w')
|
||||||
|
generator_chain.add_generator(MonkylangGen(dest))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
schema_dir = '<path_of_json_schema_dir>'
|
||||||
|
service = load_jsonrpc_service(schema_dir=schema_dir)
|
||||||
|
generator_chain = GeneratorChain()
|
||||||
|
register_monkylang_gen(generator_chain)
|
||||||
|
generator_chain.generate(service)
|
||||||
@@ -5,7 +5,7 @@ from msggen.gen.generator import GeneratorChain
|
|||||||
from msggen.utils import repo_root, load_jsonrpc_service
|
from msggen.utils import repo_root, load_jsonrpc_service
|
||||||
|
|
||||||
|
|
||||||
def gengrpc(generator_chain: GeneratorChain, meta):
|
def add_handler_gen_grpc(generator_chain: GeneratorChain, meta):
|
||||||
"""Load all mapped RPC methods, wrap them in a Service, and split them into messages.
|
"""Load all mapped RPC methods, wrap them in a Service, and split them into messages.
|
||||||
"""
|
"""
|
||||||
fname = repo_root() / "cln-grpc" / "proto" / "node.proto"
|
fname = repo_root() / "cln-grpc" / "proto" / "node.proto"
|
||||||
@@ -22,7 +22,7 @@ def gengrpc(generator_chain: GeneratorChain, meta):
|
|||||||
generator_chain.add_generator(GrpcServerGenerator(dest))
|
generator_chain.add_generator(GrpcServerGenerator(dest))
|
||||||
|
|
||||||
|
|
||||||
def genrustjsonrpc(generator_chain: GeneratorChain):
|
def add_handler_gen_rust_jsonrpc(generator_chain: GeneratorChain):
|
||||||
fname = repo_root() / "cln-rpc" / "src" / "model.rs"
|
fname = repo_root() / "cln-rpc" / "src" / "model.rs"
|
||||||
dest = open(fname, "w")
|
dest = open(fname, "w")
|
||||||
generator_chain.add_generator(RustGenerator(dest))
|
generator_chain.add_generator(RustGenerator(dest))
|
||||||
@@ -43,8 +43,8 @@ def run():
|
|||||||
meta = load_msggen_meta()
|
meta = load_msggen_meta()
|
||||||
generator_chain = GeneratorChain()
|
generator_chain = GeneratorChain()
|
||||||
|
|
||||||
gengrpc(generator_chain, meta)
|
add_handler_gen_grpc(generator_chain, meta)
|
||||||
genrustjsonrpc(generator_chain)
|
add_handler_gen_rust_jsonrpc(generator_chain)
|
||||||
|
|
||||||
generator_chain.generate(service)
|
generator_chain.generate(service)
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ from msggen.model import Service
|
|||||||
|
|
||||||
class IGenerator(ABC):
|
class IGenerator(ABC):
|
||||||
"""
|
"""
|
||||||
Change of responsibility handler that need to be
|
Chain of responsibility handler that need to be
|
||||||
implemented by all the generators.
|
implemented by all the generators.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@@ -21,7 +21,7 @@ class IGenerator(ABC):
|
|||||||
|
|
||||||
class GeneratorChain:
|
class GeneratorChain:
|
||||||
"""
|
"""
|
||||||
Chain responsibility patter implementation to generalize
|
Chain responsibility pattern implementation to generalize
|
||||||
the generation method.
|
the generation method.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user