From b7bc6992978245e5c66f1fe69914247f41628fea Mon Sep 17 00:00:00 2001 From: Gareth Date: Wed, 19 Apr 2023 02:20:13 +0000 Subject: [PATCH] [SymForce-External] Add update_template_data to CodegenConfig Add update_template_data to CodegenConfig This review adds one small method to `CodegenConfig`, with the intention of making it easier to support additional languages: - `update_template_data`, which allows `CodegenConfig` subclasses to pass additional fields or functions to the templating engine. Closes symforce-org/symforce#186 GitOrigin-RevId: 222d3132e0a98723454bcd4390f2215457a0e7cb --- symforce/codegen/codegen.py | 3 ++- symforce/codegen/codegen_config.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/symforce/codegen/codegen.py b/symforce/codegen/codegen.py index 3ed8e2dde..e922a5d8a 100644 --- a/symforce/codegen/codegen.py +++ b/symforce/codegen/codegen.py @@ -538,8 +538,9 @@ def generate_function( self.namespace = namespace template_data = dict(self.common_data(), spec=self) - template_dir = self.config.template_dir() + self.config.update_template_data(data=template_data) + template_dir = self.config.template_dir() backend_name = self.config.backend_name() if skip_directory_nesting: out_function_dir = output_dir diff --git a/symforce/codegen/codegen_config.py b/symforce/codegen/codegen_config.py index 64b416281..f7494e520 100644 --- a/symforce/codegen/codegen_config.py +++ b/symforce/codegen/codegen_config.py @@ -136,3 +136,13 @@ def format_eigen_lcm_accessor(key: str, i: int) -> str: Format accessor for eigen_lcm types. """ pass + + def update_template_data(self, data: T.Dict[str, T.Any]) -> None: + """ + Derived classes may override this to customize the "template data" dict. This dict + is passed to jinja at code-generation time. + + Args: + data: Dict passed by Codegen. The function should modify this in-place. + """ + pass