Skip to content

Commit

Permalink
msggen: adding example and fixes typo
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Apr 26, 2022
1 parent 6340bc7 commit d93a3ae
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
36 changes: 36 additions & 0 deletions contrib/msggen/examples/generator_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#! /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.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)
4 changes: 2 additions & 2 deletions contrib/msggen/msggen/gen/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class IGenerator(ABC):
"""
Change of responsibility handler that need to be
Chain of responsibility handler that need to be
implemented by all the generators.
"""

Expand All @@ -21,7 +21,7 @@ def generate(self, service: Service):

class GeneratorChain:
"""
Chain responsibility patter implementation to generalize
Chain responsibility pattern implementation to generalize
the generation method.
"""

Expand Down

0 comments on commit d93a3ae

Please sign in to comment.