#!/usr/bin/python3 # -*- coding: utf-8 -*- from jinja2 import Environment, FileSystemLoader from MsgData import MsgData import os from pathlib import Path # \brief Class that usees the loaded MsgData and outputs converts to Protobuf class MsgProtobuf(MsgData): # \brief Initializer def __init__(self): super().__init__() # \brief Write the loaded MsgData to a C++ header message to protobuf utility class # \param[in] device_name name of device (e.g. Denali, Leahi) # \param[in] output_dir directory where the output header file will be written # \param[in] namespace namespace to use in the header file, if namespace is blank then no namespace will be added def write_msg_proto_header(self, device_name, output_dir=None, namespace=None): env = Environment(loader = FileSystemLoader(f'{Path(__file__).parent.absolute()}/templates')) template = env.get_template('MsgDefs_h.jinja') render = template.render(msg_data = self.data, device_name = device_name, cpp_namespace = namespace) header_path = Path(output_dir).joinpath(f"{device_name}MsgProto.h") with open({header_path}, mode='w', encoding='utf-8', newline='\n') as out_file: out_file.write(render) out_file.close() print(f"Wrote C++ Protobuf utiltity header file {header_path}") # \brief Write the loaded MsgData to a protobuf file # \param[in] filename name of the outputted protobuf file # \param[in] output_dir directory where the outputted file will be written def write_proto(self, filename, namespace=None, output_dir=None): env = Environment(loader = FileSystemLoader(f'{Path(__file__).parent.absolute()}/templates')) template = env.get_template('MsgDefs_proto.jinja') render = template.render({ 'msg_proto': self, 'proto_namespace': namespace }) proto_path = Path(output_dir).joinpath(filename) with open(proto_path, mode='w', encoding='utf-8', newline='\n') as out_file: out_file.write(render) out_file.close() print(f"Wrote Protobuf message defintion file {proto_path}")