# ----------------------------------------------------------------------------# SymForce - Copyright 2022, Skydio, Inc.# This source code is under the Apache 2.0 license found in the LICENSE file.# ----------------------------------------------------------------------------importrefrompathlibimportPathfromsymforceimporttypingasTfromsymforce.codegenimportCodegenfromsymforce.codegenimportCppConfigfromsymforce.codegenimporttemplate_utilfromsymforce.valuesimportgenerated_key_selectionfromsymforce.values.valuesimportValues
[docs]defgenerate_values_keys(values:Values,output_dir:T.Openable,config:CppConfig,namespace:str="sym",generated_file_name:str="keys.h",excluded_keys:T.Optional[T.Set[generated_key_selection.GeneratedKey]]=None,skip_directory_nesting:bool=False,)->None:""" Generate C++ variables to easily create ``sym::Key``s from the python key names Args: values: Will generate an entry for each (recursive) key in the values output_dir: Directory in which to output the generated header config: The CppConfig to use namespace: Namepace for the generated header generated_file_name: Filename of the generated header excluded_keys: Set of disallowed generated keys (for instance, if that key is used elsewhere) skip_directory_nesting: Generate the output file directly into output_dir instead of adding the usual directory structure inside output_dir """ifnotisinstance(output_dir,Path):output_dir=Path(output_dir)items=values.items_recursive()items=list({re.sub(r"(\[[0-9]+\])+$","",key):valueforkey,valueinitems}.items())keys=[item[0]foriteminitems]generated_keys=generated_key_selection.pick_generated_keys_for_variable_names(keys,excluded_keys)vars_to_generate=[(key,generated_keys[key],value)forkey,valueinitems]ifskip_directory_nesting:cpp_function_dir=output_direlse:cpp_function_dir=output_dir/"cpp"/"symforce"/namespacetemplate_util.render_template(template_path="keys.h.jinja",data=dict(Codegen.common_data(),namespace=namespace,vars=vars_to_generate),config=config.render_template_config,template_dir=config.template_dir(),output_path=cpp_function_dir/generated_file_name,)