diff options
Diffstat (limited to '')
-rw-r--r-- | src/aristaproto/plugin/compiler.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/aristaproto/plugin/compiler.py b/src/aristaproto/plugin/compiler.py new file mode 100644 index 0000000..4bbcc48 --- /dev/null +++ b/src/aristaproto/plugin/compiler.py @@ -0,0 +1,50 @@ +import os.path + + +try: + # aristaproto[compiler] specific dependencies + import black + import isort.api + import jinja2 +except ImportError as err: + print( + "\033[31m" + f"Unable to import `{err.name}` from aristaproto plugin! " + "Please ensure that you've installed aristaproto as " + '`pip install "aristaproto[compiler]"` so that compiler dependencies ' + "are included." + "\033[0m" + ) + raise SystemExit(1) + +from .models import OutputTemplate + + +def outputfile_compiler(output_file: OutputTemplate) -> str: + templates_folder = os.path.abspath( + os.path.join(os.path.dirname(__file__), "..", "templates") + ) + + env = jinja2.Environment( + trim_blocks=True, + lstrip_blocks=True, + loader=jinja2.FileSystemLoader(templates_folder), + ) + template = env.get_template("template.py.j2") + + code = template.render(output_file=output_file) + code = isort.api.sort_code_string( + code=code, + show_diff=False, + py_version=37, + profile="black", + combine_as_imports=True, + lines_after_imports=2, + quiet=True, + force_grid_wrap=2, + known_third_party=["grpclib", "aristaproto"], + ) + return black.format_str( + src_contents=code, + mode=black.Mode(), + ) |