From a8b36218060b22b8e8c7295b7453fdb8b9a1f8da Mon Sep 17 00:00:00 2001 From: Tanaya Mankad Date: Sat, 11 Nov 2023 19:55:26 -0700 Subject: [PATCH] Generate support headers and normalize their names. --- lattice/cpp/generate_support_headers.py | 16 ++++++++++++ lattice/cpp/templates/enum-info.h.txt | 16 ++++++++++++ lattice/cpp/templates/load-object.h.txt | 33 +++++++++++++++++++++++++ lattice/cpp_entries.py | 2 +- lattice/header_entries.py | 14 +++++------ lattice/lattice.py | 2 ++ lattice/util.py | 3 +++ 7 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 lattice/cpp/generate_support_headers.py create mode 100644 lattice/cpp/templates/enum-info.h.txt create mode 100644 lattice/cpp/templates/load-object.h.txt diff --git a/lattice/cpp/generate_support_headers.py b/lattice/cpp/generate_support_headers.py new file mode 100644 index 0000000..82c9c9b --- /dev/null +++ b/lattice/cpp/generate_support_headers.py @@ -0,0 +1,16 @@ +from jinja2 import Template +import os +import sys +from lattice.file_io import dump +from lattice.util import snake_style +from pathlib import Path + +# file_loader = FileSystemLoader(os.path.join(os.path.dirname(__file__), 'generation_templates')) +# env = Environment(loader=file_loader) + +def generate_support_headers(namespace_name: str, output_directory: Path): + for template in Path(__file__).with_name("templates").iterdir(): + enum_info = Template(template.read_text()) + generated_file_name = "-".join(snake_style(template.stem).split("_")) + dump(enum_info.render(namespace=namespace_name), Path(output_directory) / generated_file_name) + diff --git a/lattice/cpp/templates/enum-info.h.txt b/lattice/cpp/templates/enum-info.h.txt new file mode 100644 index 0000000..847f988 --- /dev/null +++ b/lattice/cpp/templates/enum-info.h.txt @@ -0,0 +1,16 @@ +#ifndef TYPEINFO_205_H_ +#define TYPEINFO_205_H_ + +#include + +namespace {{namespace}} { + + struct enum_info + { + std::string_view enumerant_name; + std::string_view display_text; + std::string_view description; + }; +} + +#endif // TYPEINFO_205_H_ \ No newline at end of file diff --git a/lattice/cpp/templates/load-object.h.txt b/lattice/cpp/templates/load-object.h.txt new file mode 100644 index 0000000..e88df43 --- /dev/null +++ b/lattice/cpp/templates/load-object.h.txt @@ -0,0 +1,33 @@ +#ifndef LOADOBJECT_205_H_ +#define LOADOBJECT_205_H_ + +#include +#include + +namespace {{namespace}} { + + template + void json_get(nlohmann::json j, + Courierr::Courierr& logger, + const char *subnode, + T& object, + bool& object_is_set, + bool required = false) + { + try + { + object = j.at(subnode).get(); + object_is_set = true; + } + catch (nlohmann::json::out_of_range & ex) + { + object_is_set = false; + if (required) + { + logger.warning(ex.what()); + } + } + } +} + +#endif \ No newline at end of file diff --git a/lattice/cpp_entries.py b/lattice/cpp_entries.py index 715e3c8..0422832 100644 --- a/lattice/cpp_entries.py +++ b/lattice/cpp_entries.py @@ -394,4 +394,4 @@ def _get_items_to_serialize(self, header_tree): # ............................................................................................. def _add_included_headers(self, main_header): self._preamble.clear() - self._preamble.append(f'#include <{snake_style(main_header)}.h>\n#include \n') + self._preamble.append(f'#include <{snake_style(main_header)}.h>\n#include \n') diff --git a/lattice/header_entries.py b/lattice/header_entries.py index 9c54637..8628cc4 100644 --- a/lattice/header_entries.py +++ b/lattice/header_entries.py @@ -1,7 +1,7 @@ import os import re from .file_io import load, get_base_stem -from .util import snake_style +from .util import snake_style, bigladder_filename_style from typing import Optional import pathlib @@ -600,7 +600,7 @@ def translate(self, input_file_path, top_namespace: str, forward_declarations_pa 'Name') HeaderTranslator.modified_insertion_sort(self._namespace.child_entries) # PerformanceMapBase object needs sibling grid/lookup vars to be created, so parse last - self._add_performance_overloads() + #self._add_performance_overloads() # Final passes through dictionary in order to add elements related to serialization for base_level_tag in ( @@ -657,21 +657,21 @@ def _add_included_headers(self, ref_list): if ref_list: includes = '' for r in ref_list: - includes += f'#include <{snake_style(r)}.h>' + includes += f'#include <{bigladder_filename_style(r)}.h>' includes += '\n' self._preamble.append(includes) - self._preamble.append('#include \n#include \n#include \n#include \n') + self._preamble.append('#include \n#include \n#include \n#include \n') # ............................................................................................. def _add_member_headers(self, data_element): if 'unique_ptr' in data_element.type: m = re.search(r'\<(.*)\>', data_element.type) if m: - include = f'#include <{snake_style(m.group(1))}.h>\n' + include = f'#include <{bigladder_filename_style(m.group(1))}.h>\n' if include not in self._preamble: self._preamble.append(include) if data_element.superclass: - include = f'#include <{snake_style(data_element.superclass)}.h>\n' + include = f'#include <{bigladder_filename_style(data_element.superclass)}.h>\n' if include not in self._preamble: self._preamble.append(include) @@ -680,7 +680,7 @@ def _add_function_overrides(self, parent_node, base_class_name): '''Get base class virtual functions to be overridden.''' base_class = os.path.join(os.path.dirname(__file__), 'src', - f'{snake_style(base_class_name)}.h') + f'{bigladder_filename_style(base_class_name)}.h') try: with open(base_class) as b: for line in b: diff --git a/lattice/lattice.py b/lattice/lattice.py index 88712a1..24c1a4c 100644 --- a/lattice/lattice.py +++ b/lattice/lattice.py @@ -14,6 +14,7 @@ from .docs import HugoWeb, DocumentFile from .header_entries import HeaderTranslator from .cpp_entries import CPPTranslator +from lattice.cpp.generate_support_headers import generate_support_headers class SchemaFile: # pylint:disable=R0902 """Parse the components of a schema file.""" @@ -355,3 +356,4 @@ def generate_cpp_headers(self): dump(str(h), schema.cpp_header_path) c.translate(self.root_directory.name, h) dump(str(c), schema.cpp_source_path) + generate_support_headers(self.root_directory.name, schema.cpp_header_path.parent) \ No newline at end of file diff --git a/lattice/util.py b/lattice/util.py index eeaddef..f5222de 100644 --- a/lattice/util.py +++ b/lattice/util.py @@ -4,3 +4,6 @@ def snake_style(s): #return ''.join(['_'+c.lower() if c.isupper() else c for c in s]).lstrip('_') a = re.compile('((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))') return a.sub(r'_\1', s).lower() + +def bigladder_filename_style(s): + return "-".join(snake_style(s).split("_")) \ No newline at end of file