diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0862dfe..b911010 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -52,6 +52,12 @@ repos: rev: 23.3.0 hooks: - id: black + args: ["--skip-string-normalization"] + + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.4.0 + hooks: + - id: double-quote-string-fixer - repo: local hooks: diff --git a/example_python/generate_parameter_module_example/custom_validation.py b/example_python/generate_parameter_module_example/custom_validation.py index 5e3ecd2..03eba7c 100644 --- a/example_python/generate_parameter_module_example/custom_validation.py +++ b/example_python/generate_parameter_module_example/custom_validation.py @@ -29,8 +29,8 @@ def no_args_validator(param): - return "" + return '' def validate_double_array_custom_func(param, arg1, arg2): - return "" + return '' diff --git a/example_python/generate_parameter_module_example/minimal_publisher.py b/example_python/generate_parameter_module_example/minimal_publisher.py index b2fb283..7eaf59c 100644 --- a/example_python/generate_parameter_module_example/minimal_publisher.py +++ b/example_python/generate_parameter_module_example/minimal_publisher.py @@ -27,17 +27,17 @@ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. -import rclpy -import rclpy.node - from generate_parameter_module_example.admittance_parameters import ( admittance_controller, ) +import rclpy +import rclpy.node + class MinimalParam(rclpy.node.Node): def __init__(self): - super().__init__("admittance_controller") + super().__init__('admittance_controller') self.timer = self.create_timer(1, self.timer_callback) self.param_listener = admittance_controller.ParamListener(self) @@ -71,5 +71,5 @@ def main(args=None): rclpy.spin(node) -if __name__ == "__main__": +if __name__ == '__main__': main() diff --git a/example_python/setup.py b/example_python/setup.py index 206412e..1ec5231 100644 --- a/example_python/setup.py +++ b/example_python/setup.py @@ -1,38 +1,39 @@ # -*- coding: utf-8 -*- import sys + from setuptools import setup -package_name = "generate_parameter_module_example" +package_name = 'generate_parameter_module_example' -if len(sys.argv) >= 2 and sys.argv[1] != "clean": +if len(sys.argv) >= 2 and sys.argv[1] != 'clean': from generate_parameter_library_py.setup_helper import generate_parameter_module # set module_name and yaml file - module_name = "admittance_parameters" - yaml_file = "generate_parameter_module_example/parameters.yaml" - validation_module = "generate_parameter_module_example.custom_validation" + module_name = 'admittance_parameters' + yaml_file = 'generate_parameter_module_example/parameters.yaml' + validation_module = 'generate_parameter_module_example.custom_validation' generate_parameter_module( module_name, yaml_file, validation_module=validation_module ) setup( name=package_name, - version="0.3.6", + version='0.3.6', packages=[package_name], data_files=[ - ("share/ament_index/resource_index/packages", ["resource/" + package_name]), - ("share/" + package_name, ["package.xml"]), + ('share/ament_index/resource_index/packages', ['resource/' + package_name]), + ('share/' + package_name, ['package.xml']), ], - install_requires=["setuptools"], + install_requires=['setuptools'], zip_safe=True, - maintainer="Paul Gesel", - maintainer_email="paulgesel@gmail.com", - description="Example usage of generate_parameter_library for a python module", - license="BSD-3-Clause", - tests_require=["pytest"], + maintainer='Paul Gesel', + maintainer_email='paulgesel@gmail.com', + description='Example usage of generate_parameter_library for a python module', + license='BSD-3-Clause', + tests_require=['pytest'], entry_points={ - "console_scripts": [ - "test_node = generate_parameter_module_example.minimal_publisher:main" + 'console_scripts': [ + 'test_node = generate_parameter_module_example.minimal_publisher:main' ], }, ) diff --git a/example_python/test/test_copyright.py b/example_python/test/test_copyright.py index 4a13c1e..858d8ea 100644 --- a/example_python/test/test_copyright.py +++ b/example_python/test/test_copyright.py @@ -19,10 +19,10 @@ # Remove the `skip` decorator once the source file(s) have a copyright header @pytest.mark.skip( - reason="No copyright header has been placed in the generated source file." + reason='No copyright header has been placed in the generated source file.' ) @pytest.mark.copyright @pytest.mark.linter def test_copyright(): - rc = main(argv=[".", "test"]) - assert rc == 0, "Found errors" + rc = main(argv=['.', 'test']) + assert rc == 0, 'Found errors' diff --git a/example_python/test/test_flake8.py b/example_python/test/test_flake8.py index 420810b..743f4f4 100644 --- a/example_python/test/test_flake8.py +++ b/example_python/test/test_flake8.py @@ -21,6 +21,6 @@ @pytest.mark.linter def test_flake8(): rc, errors = main_with_errors(argv=[]) - assert rc == 0, "Found %d code style errors / warnings:\n" % len( + assert rc == 0, 'Found %d code style errors / warnings:\n' % len( errors - ) + "\n".join(errors) + ) + '\n'.join(errors) diff --git a/example_python/test/test_pep257.py b/example_python/test/test_pep257.py index bd99ae8..1756914 100644 --- a/example_python/test/test_pep257.py +++ b/example_python/test/test_pep257.py @@ -20,5 +20,5 @@ @pytest.mark.linter @pytest.mark.pep257 def test_pep257(): - rc = main(argv=[".", "test"]) - assert rc == 0, "Found code style errors / warnings" + rc = main(argv=['.', 'test']) + assert rc == 0, 'Found code style errors / warnings' diff --git a/generate_parameter_library_py/generate_parameter_library_py/cpp_convertions.py b/generate_parameter_library_py/generate_parameter_library_py/cpp_convertions.py index f98e239..01cf619 100644 --- a/generate_parameter_library_py/generate_parameter_library_py/cpp_convertions.py +++ b/generate_parameter_library_py/generate_parameter_library_py/cpp_convertions.py @@ -11,51 +11,51 @@ class CPPConverstions: def __init__(self): self.defined_type_to_lang_type = { - "none": lambda defined_type, templates: None, - "bool": lambda defined_type, templates: "bool", - "double": lambda defined_type, templates: "double", - "int": lambda defined_type, templates: "int64_t", - "string": lambda defined_type, templates: "std::string", - "bool_array": lambda defined_type, templates: "std::vector", - "double_array": lambda defined_type, templates: "std::vector", - "int_array": lambda defined_type, templates: "std::vector", - "string_array": lambda defined_type, templates: "std::vector", - "double_array_fixed": lambda defined_type, templates: f"rsl::StaticVector<{templates[0]}, {templates[1]}>", - "int_array_fixed": lambda defined_type, templates: f"rsl::StaticVector<{templates[0]}, {templates[1]}>", - "string_array_fixed": lambda defined_type, templates: f"rsl::StaticVector<{templates[0]}, {templates[1]}>", - "string_fixed": lambda defined_type, templates: f"rsl::StaticString<{templates[1]}>", + 'none': lambda defined_type, templates: None, + 'bool': lambda defined_type, templates: 'bool', + 'double': lambda defined_type, templates: 'double', + 'int': lambda defined_type, templates: 'int64_t', + 'string': lambda defined_type, templates: 'std::string', + 'bool_array': lambda defined_type, templates: 'std::vector', + 'double_array': lambda defined_type, templates: 'std::vector', + 'int_array': lambda defined_type, templates: 'std::vector', + 'string_array': lambda defined_type, templates: 'std::vector', + 'double_array_fixed': lambda defined_type, templates: f'rsl::StaticVector<{templates[0]}, {templates[1]}>', + 'int_array_fixed': lambda defined_type, templates: f'rsl::StaticVector<{templates[0]}, {templates[1]}>', + 'string_array_fixed': lambda defined_type, templates: f'rsl::StaticVector<{templates[0]}, {templates[1]}>', + 'string_fixed': lambda defined_type, templates: f'rsl::StaticString<{templates[1]}>', } self.yaml_type_to_as_function = { - "none": None, - "string_array": "as_string_array()", - "double_array": "as_double_array()", - "int_array": "as_integer_array()", - "bool_array": "as_bool_array()", - "string": "as_string()", - "double": "as_double()", - "int": "as_int()", - "bool": "as_bool()", - "bool_array_fixed": "as_bool_array()", - "double_array_fixed": "as_double_array()", - "int_array_fixed": "as_integer_array()", - "string_array_fixed": "as_string_array()", - "string_fixed": "as_string()", + 'none': None, + 'string_array': 'as_string_array()', + 'double_array': 'as_double_array()', + 'int_array': 'as_integer_array()', + 'bool_array': 'as_bool_array()', + 'string': 'as_string()', + 'double': 'as_double()', + 'int': 'as_int()', + 'bool': 'as_bool()', + 'bool_array_fixed': 'as_bool_array()', + 'double_array_fixed': 'as_double_array()', + 'int_array_fixed': 'as_integer_array()', + 'string_array_fixed': 'as_string_array()', + 'string_fixed': 'as_string()', } self.lang_str_value_func = { - "none": self.no_code, - "bool": self.bool_to_str, - "double": self.float_to_str, - "int": self.int_to_str, - "string": self.str_to_str, - "bool_array": self.bool_array_to_str, - "double_array": self.float_array_to_str, - "int_array": self.int_array_to_str, - "string_array": self.str_array_to_str, - "bool_array_fixed": self.bool_array_fixed_to_str, - "double_array_fixed": self.float_array_fixed_to_str, - "int_array_fixed": self.int_array_fixed_to_str, - "string_array_fixed": self.str_array_fixed_to_str, - "string_fixed": self.str_fixed_to_str, + 'none': self.no_code, + 'bool': self.bool_to_str, + 'double': self.float_to_str, + 'int': self.int_to_str, + 'string': self.str_to_str, + 'bool_array': self.bool_array_to_str, + 'double_array': self.float_array_to_str, + 'int_array': self.int_array_to_str, + 'string_array': self.str_array_to_str, + 'bool_array_fixed': self.bool_array_fixed_to_str, + 'double_array_fixed': self.float_array_fixed_to_str, + 'int_array_fixed': self.int_array_fixed_to_str, + 'string_array_fixed': self.str_array_fixed_to_str, + 'string_fixed': self.str_fixed_to_str, } self.python_val_to_str_func = { "": self.bool_to_str, @@ -64,137 +64,137 @@ def __init__(self): "": self.str_to_str, } self.python_val_to_yaml_type = { - "": "bool", - "": "double", - "": "int", - "": "str", + "": 'bool', + "": 'double', + "": 'int', + "": 'str', } self.python_list_to_yaml_type = { - "": "bool_array", - "": "double_array", - "": "integer_array", - "": "string_array", + "": 'bool_array', + "": 'double_array', + "": 'integer_array', + "": 'string_array', } - self.open_bracket = "{" - self.close_bracket = "}" + self.open_bracket = '{' + self.close_bracket = '}' @typechecked def get_func_signature(self, function_name: str, base_type: str) -> str: - if function_name[-2:] == "<>": + if function_name[-2:] == '<>': function_base_name = function_name[:-2] template_type = base_type - function_name = function_base_name + f"<{template_type}>" + function_name = function_base_name + f'<{template_type}>' return function_name @typechecked def initialization_fail_validation(self, param_name: str) -> str: return ( - f"throw rclcpp::exceptions::InvalidParameterValueException" + f'throw rclcpp::exceptions::InvalidParameterValueException' f'(fmt::format("Invalid value set during initialization for ' f"parameter '{param_name}': {{}}\", validation_result.error()));" ) @typechecked def initialization_pass_validation(self, param_name: str) -> str: - return "" + return '' @typechecked def update_parameter_fail_validation(self) -> str: - return "return rsl::to_parameter_result_msg(validation_result);" + return 'return rsl::to_parameter_result_msg(validation_result);' @typechecked def update_parameter_pass_validation(self) -> str: - return "" + return '' @typechecked def no_code(self, s: Optional[str]): - return "" + return '' # value to c++ string conversion functions @typechecked def bool_to_str(self, cond: Optional[bool]): if cond is None: - return "" - return "true" if cond else "false" + return '' + return 'true' if cond else 'false' @typechecked def float_to_str(self, num: Optional[float]): if num is None: - return "" + return '' str_num = str(num) - if str_num == "nan": - str_num = "std::numeric_limits::quiet_NaN()" - elif str_num == "inf": - str_num = "std::numeric_limits::infinity()" - elif str_num == "-inf": - str_num = "-std::numeric_limits::infinity()" + if str_num == 'nan': + str_num = 'std::numeric_limits::quiet_NaN()' + elif str_num == 'inf': + str_num = 'std::numeric_limits::infinity()' + elif str_num == '-inf': + str_num = '-std::numeric_limits::infinity()' else: - if len(str_num.split(".")) == 1 and not str_num.__contains__("e"): - str_num += ".0" + if len(str_num.split('.')) == 1 and not str_num.__contains__('e'): + str_num += '.0' return str_num @typechecked def int_to_str(self, num: Optional[int]): if num is None: - return "" + return '' return str(num) @typechecked def str_to_str(self, s: Optional[str]): if s is None: - return "" + return '' return f'"{s}"' @typechecked def bool_array_to_str(self, values: Optional[list]): if values is None: - return "" - return "{" + ", ".join(self.bool_to_str(x) for x in values) + "}" + return '' + return '{' + ', '.join(self.bool_to_str(x) for x in values) + '}' @typechecked def float_array_to_str(self, values: Optional[list]): if values is None: - return "" - return "{" + ", ".join(self.float_to_str(x) for x in values) + "}" + return '' + return '{' + ', '.join(self.float_to_str(x) for x in values) + '}' @typechecked def int_array_to_str(self, values: Optional[list]): if values is None: - return "" - return "{" + ", ".join(self.int_to_str(x) for x in values) + "}" + return '' + return '{' + ', '.join(self.int_to_str(x) for x in values) + '}' @typechecked def str_array_to_str(self, s: Optional[list]): if s is None: - return "" - return "{" + ", ".join(self.str_to_str(x) for x in s) + "}" + return '' + return '{' + ', '.join(self.str_to_str(x) for x in s) + '}' @typechecked def str_array_fixed_to_str(self, s: Optional[list]): - raise compile_error("not implemented") + raise compile_error('not implemented') @typechecked def str_fixed_to_str(self, s: Optional[str]): if s is None: - return "" - return "{%s}" % self.str_to_str(s) + return '' + return '{%s}' % self.str_to_str(s) @typechecked def float_array_fixed_to_str(self, values: Optional[list]): if values is None: - return "" - return "{{" + ", ".join(self.float_to_str(x) for x in values) + "}}" + return '' + return '{{' + ', '.join(self.float_to_str(x) for x in values) + '}}' @typechecked def int_array_fixed_to_str(self, values: Optional[list]): if values is None: - return "" - return "{{" + ", ".join(self.int_to_str(x) for x in values) + "}}" + return '' + return '{{' + ', '.join(self.int_to_str(x) for x in values) + '}}' @typechecked def bool_array_fixed_to_str(self, values: Optional[list]): if values is None: - return "" - return "{{" + ", ".join(self.bool_to_str(x) for x in values) + "}}" + return '' + return '{{' + ', '.join(self.bool_to_str(x) for x in values) + '}}' diff --git a/generate_parameter_library_py/generate_parameter_library_py/generate_cpp_header.py b/generate_parameter_library_py/generate_parameter_library_py/generate_cpp_header.py index 67e4a7a..b96237d 100755 --- a/generate_parameter_library_py/generate_parameter_library_py/generate_cpp_header.py +++ b/generate_parameter_library_py/generate_parameter_library_py/generate_cpp_header.py @@ -36,8 +36,8 @@ from generate_parameter_library_py.parse_yaml import GenerateCode -def run(output_file, yaml_file, validate_header=""): - gen_param_struct = GenerateCode("cpp") +def run(output_file, yaml_file, validate_header=''): + gen_param_struct = GenerateCode('cpp') output_dir = os.path.dirname(output_file) if not os.path.isdir(output_dir): os.makedirs(output_dir) @@ -45,15 +45,15 @@ def run(output_file, yaml_file, validate_header=""): gen_param_struct.parse(yaml_file, validate_header) code = str(gen_param_struct) - with open(output_file, "w") as f: + with open(output_file, 'w') as f: f.write(code) def parse_args(): parser = argparse.ArgumentParser() - parser.add_argument("output_cpp_header_file") - parser.add_argument("input_yaml_file") - parser.add_argument("validate_header", nargs="?", default="") + parser.add_argument('output_cpp_header_file') + parser.add_argument('input_yaml_file') + parser.add_argument('validate_header', nargs='?', default='') return parser.parse_args() @@ -66,5 +66,5 @@ def main(): run(output_file, yaml_file, validate_header) -if __name__ == "__main__": +if __name__ == '__main__': sys.exit(main()) diff --git a/generate_parameter_library_py/generate_parameter_library_py/generate_markdown.py b/generate_parameter_library_py/generate_parameter_library_py/generate_markdown.py index 4b9dbf3..34dd1f2 100644 --- a/generate_parameter_library_py/generate_parameter_library_py/generate_markdown.py +++ b/generate_parameter_library_py/generate_parameter_library_py/generate_markdown.py @@ -46,21 +46,21 @@ class ParameterValidationMarkdown: @typechecked def __init__(self, validation: ValidationFunction): self.sentence_conventions = { - "bounds": "parameter must be within bounds VALUES", - "lt": "less than VALUES", - "gt": "greater than VALUES", - "lt_eq": "less than or equal to VALUES", - "gt_eq": "greater than or equal to VALUES", - "one_of": "one of the specified values: VALUES", - "fixed_size": "length must be equal to VALUES", - "size_gt": "length is greater than VALUES", - "size_lt": "length is less than VALUES", - "not_empty": "parameter is not empty", - "unique": "contains no duplicates", - "subset_of": "every element is one of the list VALUES", - "element_bounds": "each element of array must be within bounds VALUES", - "lower_element_bounds": "each element of array must be greater than or equal to VALUES", - "upper_element_bounds": "each element of array must be less than or equal to VALUES", + 'bounds': 'parameter must be within bounds VALUES', + 'lt': 'less than VALUES', + 'gt': 'greater than VALUES', + 'lt_eq': 'less than or equal to VALUES', + 'gt_eq': 'greater than or equal to VALUES', + 'one_of': 'one of the specified values: VALUES', + 'fixed_size': 'length must be equal to VALUES', + 'size_gt': 'length is greater than VALUES', + 'size_lt': 'length is less than VALUES', + 'not_empty': 'parameter is not empty', + 'unique': 'contains no duplicates', + 'subset_of': 'every element is one of the list VALUES', + 'element_bounds': 'each element of array must be within bounds VALUES', + 'lower_element_bounds': 'each element of array must be greater than or equal to VALUES', + 'upper_element_bounds': 'each element of array must be less than or equal to VALUES', } self.validation = validation @@ -69,17 +69,17 @@ def get_validation_type(self, function_base_name): if function_base_name in self.sentence_conventions: return self.sentence_conventions[function_base_name] else: - return "Custom validator: " + str(function_base_name) + return 'Custom validator: ' + str(function_base_name) def __str__(self): arguments = self.validation.arguments validation = self.get_validation_type(self.validation.function_base_name) - if validation.__contains__("VALUES"): - validation = validation.replace("VALUES", str(arguments[0])) + if validation.__contains__('VALUES'): + validation = validation.replace('VALUES', str(arguments[0])) elif arguments: - validation += ": " + str(arguments[0]) + validation += ': ' + str(arguments[0]) - return " - " + validation + return ' - ' + validation class ParameterDetailMarkdown: @@ -92,17 +92,17 @@ def __init__(self, declare_parameters: DeclareParameter): ] def __str__(self): - constraints = "\n".join(str(val) for val in self.param_validations) + constraints = '\n'.join(str(val) for val in self.param_validations) data = { - "name": self.declare_parameters.code_gen_variable.name, - "type": self.declare_parameters.code_gen_variable.defined_type, - "default_value": self.declare_parameters.code_gen_variable.lang_str_value, - "constraints": constraints, - "description": self.declare_parameters.parameter_description, + 'name': self.declare_parameters.code_gen_variable.name, + 'type': self.declare_parameters.code_gen_variable.defined_type, + 'default_value': self.declare_parameters.code_gen_variable.lang_str_value, + 'constraints': constraints, + 'description': self.declare_parameters.parameter_description, } - j2_template = Template(GenerateCode.templates["parameter_detail"]) + j2_template = Template(GenerateCode.templates['parameter_detail']) code = j2_template.render(data, trim_blocks=True) return code @@ -117,16 +117,16 @@ def __init__(self, gen_param_struct: GenerateCode): ] def __str__(self): - j2_template = Template(GenerateCode.templates["default_config"]) + j2_template = Template(GenerateCode.templates['default_config']) - tmp = "\n".join( - param.parameter_name + ": " + str(param.code_gen_variable.lang_str_value) + tmp = '\n'.join( + param.parameter_name + ': ' + str(param.code_gen_variable.lang_str_value) for param in self.gen_param_struct.declare_parameters ) data = { - "namespace": self.gen_param_struct.namespace, - "default_param_values": tmp, + 'namespace': self.gen_param_struct.namespace, + 'default_param_values': tmp, } code = j2_template.render(data, trim_blocks=True) @@ -144,16 +144,16 @@ def __init__(self, gen_param_struct: GenerateCode): ] def __str__(self): - words = self.gen_param_struct.namespace.split("_") - title = " ".join(word.capitalize() for word in words) + words = self.gen_param_struct.namespace.split('_') + title = ' '.join(word.capitalize() for word in words) data = { - "title": title, - "default_config": str(self.default_config), - "parameter_details": "\n".join(str(val) for val in self.param_details), + 'title': title, + 'default_config': str(self.default_config), + 'parameter_details': '\n'.join(str(val) for val in self.param_details), } - j2_template = Template(GenerateCode.templates["documentation"]) + j2_template = Template(GenerateCode.templates['documentation']) code = j2_template.render(data, trim_blocks=True) return code @@ -166,25 +166,25 @@ def run(yaml_file, output_file, language): if output_dir and not os.path.isdir(output_dir): os.makedirs(output_dir) - gen_param_struct.parse(yaml_file, "") + gen_param_struct.parse(yaml_file, '') auto_doc = AutoDocumentation(gen_param_struct) docs = str(auto_doc) - with open(output_file, "w") as f: + with open(output_file, 'w') as f: f.write(docs) pass def main(): parser = argparse.ArgumentParser() - parser.add_argument("--output_markdown_file") - parser.add_argument("--input_yaml_file") - parser.add_argument("--language", default="markdown") + parser.add_argument('--output_markdown_file') + parser.add_argument('--input_yaml_file') + parser.add_argument('--language', default='markdown') args = parser.parse_args() run(args.input_yaml_file, args.output_markdown_file, args.language) print(args) -if __name__ == "__main__": +if __name__ == '__main__': sys.exit(main()) diff --git a/generate_parameter_library_py/generate_parameter_library_py/generate_python_module.py b/generate_parameter_library_py/generate_parameter_library_py/generate_python_module.py index a9e3176..6193bd9 100755 --- a/generate_parameter_library_py/generate_parameter_library_py/generate_python_module.py +++ b/generate_parameter_library_py/generate_parameter_library_py/generate_python_module.py @@ -36,8 +36,8 @@ from generate_parameter_library_py.parse_yaml import GenerateCode -def run(output_file, yaml_file, validation_module=""): - gen_param_struct = GenerateCode("python") +def run(output_file, yaml_file, validation_module=''): + gen_param_struct = GenerateCode('python') output_dir = os.path.dirname(output_file) if not os.path.isdir(output_dir): os.makedirs(output_dir) @@ -45,15 +45,15 @@ def run(output_file, yaml_file, validation_module=""): gen_param_struct.parse(yaml_file, validation_module) code = str(gen_param_struct) - with open(output_file, "w") as f: + with open(output_file, 'w') as f: f.write(code) def parse_args(): parser = argparse.ArgumentParser() - parser.add_argument("output_python_module_file") - parser.add_argument("input_yaml_file") - parser.add_argument("validate_file", nargs="?", default="") + parser.add_argument('output_python_module_file') + parser.add_argument('input_yaml_file') + parser.add_argument('validate_file', nargs='?', default='') return parser.parse_args() @@ -66,5 +66,5 @@ def main(): run(output_file, yaml_file, validate_file) -if __name__ == "__main__": +if __name__ == '__main__': sys.exit(main()) diff --git a/generate_parameter_library_py/generate_parameter_library_py/parse_yaml.py b/generate_parameter_library_py/generate_parameter_library_py/parse_yaml.py index 4ee7f75..ad92c79 100644 --- a/generate_parameter_library_py/generate_parameter_library_py/parse_yaml.py +++ b/generate_parameter_library_py/generate_parameter_library_py/parse_yaml.py @@ -53,73 +53,73 @@ def __str__(self): # helper functions @typechecked def compile_error(msg: str): - return YAMLSyntaxError("\nERROR: " + msg) + return YAMLSyntaxError('\nERROR: ' + msg) @typechecked def array_type(defined_type: str): - return defined_type.__contains__("array") + return defined_type.__contains__('array') @typechecked def is_mapped_parameter(param_name: str): - return param_name.__contains__("__map_") + return param_name.__contains__('__map_') @typechecked def fixed_type_size(yaml_type: str): - tmp = yaml_type.split("_") + tmp = yaml_type.split('_') if len(tmp) < 3: return None - if tmp[-2] != "fixed" or not tmp[-1].isdigit(): + if tmp[-2] != 'fixed' or not tmp[-1].isdigit(): return None return int(tmp[-1]) @typechecked def pascal_case(string: str): - words = string.split("_") - return "".join(w.title() for w in words) + words = string.split('_') + return ''.join(w.title() for w in words) @typechecked def int_to_integer_str(value: str): - return value.replace("int", "integer") + return value.replace('int', 'integer') def get_dynamic_parameter_field(yaml_parameter_name: str): - tmp = yaml_parameter_name.split(".") + tmp = yaml_parameter_name.split('.') parameter_field = tmp[-1] return parameter_field def get_dynamic_mapped_parameter(yaml_parameter_name: str): - tmp = yaml_parameter_name.split(".") + tmp = yaml_parameter_name.split('.') tmp2 = tmp[-2] - mapped_param = tmp2.replace("__map_", "") + mapped_param = tmp2.replace('__map_', '') return mapped_param def get_dynamic_struct_name(yaml_parameter_name: str): - tmp = yaml_parameter_name.split(".") + tmp = yaml_parameter_name.split('.') struct_name = tmp[:-2] - return ".".join(struct_name) + return '.'.join(struct_name) def get_dynamic_parameter_name(yaml_parameter_name: str): struct_name = get_dynamic_struct_name(yaml_parameter_name) parameter_field = get_dynamic_parameter_field(yaml_parameter_name) parameter_name = [struct_name, parameter_field] - parameter_name = ".".join(parameter_name) + parameter_name = '.'.join(parameter_name) return parameter_name def get_dynamic_parameter_map(yaml_parameter_name: str): - tmp = yaml_parameter_name.split(".") + tmp = yaml_parameter_name.split('.') parameter_map = tmp[:-2] mapped_param = get_dynamic_mapped_parameter(yaml_parameter_name) - parameter_map.append(mapped_param + "_map") - parameter_map = ".".join(parameter_map) + parameter_map.append(mapped_param + '_map') + parameter_map = '.'.join(parameter_map) return parameter_map @@ -130,13 +130,13 @@ def is_fixed_type(yaml_type: str): @typechecked def get_fixed_base_type(yaml_type: str): - tmp = yaml_type.split("_") - return "_".join(tmp[: -(min(2, len(tmp) - 1))]) + tmp = yaml_type.split('_') + return '_'.join(tmp[: -(min(2, len(tmp) - 1))]) @typechecked def get_fixed_type(yaml_type: str): - return get_fixed_base_type(yaml_type) + "_fixed" + return get_fixed_base_type(yaml_type) + '_fixed' class CodeGenVariableBase: @@ -149,17 +149,17 @@ def __init__( defined_type: str, default_value: Any, ): - if language == "cpp": + if language == 'cpp': self.conversation = CPPConverstions() - elif language == "rst" or language == "markdown": + elif language == 'rst' or language == 'markdown': # cpp is used here because it the desired style of the markdown, # e.g. "false" for C++ instead of "False" for Python self.conversation = CPPConverstions() - elif language == "python": + elif language == 'python': self.conversation = PythonConvertions() else: raise compile_error( - "Invalid language, only c++ and python are currently supported." + 'Invalid language, only c++ and python are currently supported.' ) self.name = name @@ -170,16 +170,16 @@ def __init__( self.array_type = array_type(self.defined_type) if self.defined_type not in self.conversation.defined_type_to_lang_type: - allowed = ", ".join( + allowed = ', '.join( key for key in self.conversation.defined_type_to_lang_type ) raise compile_error( - f"Invalid parameter type `{defined_type}` for parameter {param_name}. Allowed types are: " + f'Invalid parameter type `{defined_type}` for parameter {param_name}. Allowed types are: ' + allowed ) func = self.conversation.defined_type_to_lang_type[self.defined_type] self.lang_type = func(self.defined_type, template) - tmp = defined_type.split("_") + tmp = defined_type.split('_') self.defined_base_type = tmp[0] func = self.conversation.defined_type_to_lang_type[self.defined_base_type] self.lang_base_type = func(self.defined_base_type, template) @@ -188,12 +188,12 @@ def __init__( self.lang_str_value = func(default_value) except TypeError: raise compile_error( - f"Parameter {param_name} has incorrect type. Expected: {defined_type}, got: {self.get_yaml_type_from_python(default_value)}" + f'Parameter {param_name} has incorrect type. Expected: {defined_type}, got: {self.get_yaml_type_from_python(default_value)}' ) def parameter_as_function_str(self): if self.defined_type not in self.conversation.yaml_type_to_as_function: - raise compile_error("invalid yaml type: %s" % type(self.defined_type)) + raise compile_error('invalid yaml type: %s' % type(self.defined_type)) return self.conversation.yaml_type_to_as_function[self.defined_type] def get_python_val_to_str_func(self, arg): @@ -223,7 +223,7 @@ def get_parameter_type(self): class CodeGenFixedVariable(CodeGenVariableBase): def process_type(self, defined_type): size = fixed_type_size(defined_type) - tmp = defined_type.split("_") + tmp = defined_type.split('_') yaml_base_type = tmp[0] func = self.conversation.defined_type_to_lang_type[yaml_base_type] lang_base_type = func(yaml_base_type, None) @@ -243,12 +243,12 @@ def __init__(self, code_gen_variable: CodeGenVariableBase): def __str__(self): value = self.code_gen_variable.lang_str_value data = { - "type": self.code_gen_variable.lang_type, - "name": self.code_gen_variable.name, - "value": value, + 'type': self.code_gen_variable.lang_type, + 'name': self.code_gen_variable.name, + 'value': value, } - j2_template = Template(GenerateCode.templates["declare_variable"]) + j2_template = Template(GenerateCode.templates['declare_variable']) code = j2_template.render(data, trim_blocks=True) return code @@ -259,7 +259,7 @@ def __init__(self, struct_name: str, fields: List[VariableDeclaration]): self.struct_name = struct_name self.fields = fields self.sub_structs = [] - self.struct_instance = "" + self.struct_instance = '' @typechecked def add_field(self, field: VariableDeclaration): @@ -269,38 +269,38 @@ def add_sub_struct(self, sub_struct): self.sub_structs.append(sub_struct) def field_content(self): - content = "".join(str(x) for x in self.fields) + content = ''.join(str(x) for x in self.fields) return str(content) def sub_struct_content(self): - content = "".join(str(x) for x in self.sub_structs) + content = ''.join(str(x) for x in self.sub_structs) return str(content) def __str__(self): - sub_struct_str = "".join(str(x) for x in self.sub_structs) - field_str = "".join(str(x) for x in self.fields) - if field_str == "" and sub_struct_str == "": - return "" + sub_struct_str = ''.join(str(x) for x in self.sub_structs) + field_str = ''.join(str(x) for x in self.fields) + if field_str == '' and sub_struct_str == '': + return '' if is_mapped_parameter(self.struct_name): map_val_type = pascal_case(self.struct_name) - map_name = self.struct_name.replace("__map_", "") + "_map" - map_name = map_name.replace(".", "_") + map_name = self.struct_name.replace('__map_', '') + '_map' + map_name = map_name.replace('.', '_') else: - map_val_type = "" - map_name = "" + map_val_type = '' + map_name = '' self.struct_instance = self.struct_name data = { - "struct_name": pascal_case(self.struct_name), - "struct_instance": self.struct_instance, - "struct_fields": str(field_str), - "sub_structs": str(sub_struct_str), - "map_value_type": map_val_type, - "map_name": map_name, + 'struct_name': pascal_case(self.struct_name), + 'struct_instance': self.struct_instance, + 'struct_fields': str(field_str), + 'sub_structs': str(sub_struct_str), + 'map_value_type': map_val_type, + 'map_name': map_name, } - j2_template = Template(GenerateCode.templates["declare_struct"]) + j2_template = Template(GenerateCode.templates['declare_struct']) code = j2_template.render(data, trim_blocks=True) return code @@ -315,7 +315,7 @@ def __init__( ): self.code_gen_variable = code_gen_variable self.function_name = function_name - self.function_base_name = function_name.replace("<>", "") + self.function_base_name = function_name.replace('<>', '') if arguments is not None: self.arguments = arguments @@ -329,20 +329,20 @@ def __str__(self): open_bracket = self.code_gen_variable.conversation.open_bracket close_bracket = self.code_gen_variable.conversation.close_bracket - code = function_name + "(param" + code = function_name + '(param' for arg in self.arguments: if isinstance(arg, list): - code += ", " + open_bracket + code += ', ' + open_bracket for a in arg[:-1]: val_func = self.code_gen_variable.get_python_val_to_str_func(a) - code += val_func(a) + ", " + code += val_func(a) + ', ' val_func = self.code_gen_variable.get_python_val_to_str_func(arg[-1]) code += val_func(arg[-1]) code += close_bracket else: val_func = self.code_gen_variable.get_python_val_to_str_func(arg) - code += ", " + val_func(arg) - code += ")" + code += ', ' + val_func(arg) + code += ')' return code @@ -361,12 +361,12 @@ def __init__( def __str__(self): data = { - "validation_function": str(self.validation_function), - "valid_effect": self.valid_effect, - "invalid_effect": self.invalid_effect, + 'validation_function': str(self.validation_function), + 'valid_effect': self.valid_effect, + 'invalid_effect': self.invalid_effect, } - j2_template = Template(GenerateCode.templates["parameter_validation"]) + j2_template = Template(GenerateCode.templates['parameter_validation']) code = j2_template.render(data, trim_blocks=True) return code @@ -385,37 +385,37 @@ def add_parameter_validation(self, parameter_validation: ParameterValidation): class UpdateParameter(UpdateParameterBase): def __str__(self): - parameter_validations_str = "".join(str(x) for x in self.parameter_validations) + parameter_validations_str = ''.join(str(x) for x in self.parameter_validations) data = { - "parameter_name": self.parameter_name, - "parameter_validations": str(parameter_validations_str), - "parameter_as_function": self.parameter_as_function, + 'parameter_name': self.parameter_name, + 'parameter_validations': str(parameter_validations_str), + 'parameter_as_function': self.parameter_as_function, } - j2_template = Template(GenerateCode.templates["update_parameter"]) + j2_template = Template(GenerateCode.templates['update_parameter']) code = j2_template.render(data, trim_blocks=True) return code class UpdateRuntimeParameter(UpdateParameterBase): def __str__(self): - parameter_validations_str = "".join(str(x) for x in self.parameter_validations) + parameter_validations_str = ''.join(str(x) for x in self.parameter_validations) mapped_param = get_dynamic_mapped_parameter(self.parameter_name) parameter_map = get_dynamic_parameter_map(self.parameter_name) struct_name = get_dynamic_struct_name(self.parameter_name) parameter_field = get_dynamic_parameter_field(self.parameter_name) data = { - "mapped_param": mapped_param, - "parameter_map": parameter_map, - "struct_name": struct_name, - "parameter_field": parameter_field, - "parameter_validations": str(parameter_validations_str), - "parameter_as_function": self.parameter_as_function, + 'mapped_param': mapped_param, + 'parameter_map': parameter_map, + 'struct_name': struct_name, + 'parameter_field': parameter_field, + 'parameter_validations': str(parameter_validations_str), + 'parameter_as_function': self.parameter_as_function, } - j2_template = Template(GenerateCode.templates["update_runtime_parameter"]) + j2_template = Template(GenerateCode.templates['update_runtime_parameter']) code = j2_template.render(data, trim_blocks=True) return code @@ -426,8 +426,8 @@ def __init__(self, parameter_name: str): self.parameter_name = parameter_name def __str__(self): - data = {"parameter_name": self.parameter_name} - j2_template = Template(GenerateCode.templates["set_stack_params"]) + data = {'parameter_name': self.parameter_name} + j2_template = Template(GenerateCode.templates['set_stack_params']) code = j2_template.render(data, trim_blocks=True) return code @@ -446,30 +446,30 @@ def add_parameter_validation(self, parameter_validation: ParameterValidation): class SetParameter(SetParameterBase): def __str__(self): - parameter_validations_str = "".join(str(x) for x in self.parameter_validations) + parameter_validations_str = ''.join(str(x) for x in self.parameter_validations) data = { - "parameter_name": self.parameter_name, - "parameter_validations": str(parameter_validations_str), - "parameter_as_function": self.parameter_as_function, + 'parameter_name': self.parameter_name, + 'parameter_validations': str(parameter_validations_str), + 'parameter_as_function': self.parameter_as_function, } - j2_template = Template(GenerateCode.templates["set_parameter"]) + j2_template = Template(GenerateCode.templates['set_parameter']) code = j2_template.render(data, trim_blocks=True) return code class SetRuntimeParameter(SetParameterBase): def __str__(self): - parameter_validations_str = "".join(str(x) for x in self.parameter_validations) + parameter_validations_str = ''.join(str(x) for x in self.parameter_validations) parameter_field = get_dynamic_parameter_field(self.parameter_name) data = { - "parameter_field": parameter_field, - "parameter_validations": str(parameter_validations_str), - "parameter_as_function": self.parameter_as_function, + 'parameter_field': parameter_field, + 'parameter_validations': str(parameter_validations_str), + 'parameter_as_function': self.parameter_as_function, } - j2_template = Template(GenerateCode.templates["set_runtime_parameter"]) + j2_template = Template(GenerateCode.templates['set_runtime_parameter']) code = j2_template.render(data, trim_blocks=True) return code @@ -493,21 +493,21 @@ def __init__( class DeclareParameter(DeclareParameterBase): def __str__(self): if len(self.code_gen_variable.lang_str_value) == 0: - self.parameter_value = "" + self.parameter_value = '' else: self.parameter_value = self.parameter_name bool_to_str = self.code_gen_variable.conversation.bool_to_str parameter_validations = self.parameter_validations data = { - "parameter_name": self.parameter_name, - "parameter_value": self.parameter_value, - "parameter_type": self.code_gen_variable.get_parameter_type(), - "parameter_description": self.parameter_description, - "parameter_read_only": bool_to_str(self.parameter_read_only), - "parameter_validations": parameter_validations, + 'parameter_name': self.parameter_name, + 'parameter_value': self.parameter_value, + 'parameter_type': self.code_gen_variable.get_parameter_type(), + 'parameter_description': self.parameter_description, + 'parameter_read_only': bool_to_str(self.parameter_read_only), + 'parameter_validations': parameter_validations, } - j2_template = Template(GenerateCode.templates["declare_parameter"]) + j2_template = Template(GenerateCode.templates['declare_parameter']) code = j2_template.render(data, trim_blocks=True) return code @@ -527,7 +527,7 @@ def __init__( parameter_validations, ) self.set_runtime_parameter = None - self.param_struct_instance = "updated_params" + self.param_struct_instance = 'updated_params' @typechecked def add_set_runtime_parameter(self, set_runtime_parameter: SetRuntimeParameter): @@ -536,13 +536,13 @@ def add_set_runtime_parameter(self, set_runtime_parameter: SetRuntimeParameter): def __str__(self): if self.set_runtime_parameter is None: raise AssertionError( - "add_set_runtime_parameter was not called before str()" + 'add_set_runtime_parameter was not called before str()' ) if self.code_gen_variable.default_value is None: - default_value = "" + default_value = '' else: - default_value = "non-empty" + default_value = 'non-empty' bool_to_str = self.code_gen_variable.conversation.bool_to_str parameter_field = get_dynamic_parameter_field(self.parameter_name) @@ -551,22 +551,22 @@ def __str__(self): struct_name = get_dynamic_struct_name(self.parameter_name) data = { - "struct_name": struct_name, - "parameter_type": self.code_gen_variable.get_parameter_type(), - "parameter_description": self.parameter_description, - "parameter_read_only": bool_to_str(self.parameter_read_only), - "parameter_as_function": self.code_gen_variable.parameter_as_function_str(), - "mapped_param": mapped_param, - "mapped_param_underscore": mapped_param.replace(".", "_"), - "set_runtime_parameter": self.set_runtime_parameter, - "parameter_map": parameter_map, - "param_struct_instance": self.param_struct_instance, - "parameter_field": parameter_field, - "default_value": default_value, - "parameter_validations": self.parameter_validations, + 'struct_name': struct_name, + 'parameter_type': self.code_gen_variable.get_parameter_type(), + 'parameter_description': self.parameter_description, + 'parameter_read_only': bool_to_str(self.parameter_read_only), + 'parameter_as_function': self.code_gen_variable.parameter_as_function_str(), + 'mapped_param': mapped_param, + 'mapped_param_underscore': mapped_param.replace('.', '_'), + 'set_runtime_parameter': self.set_runtime_parameter, + 'parameter_map': parameter_map, + 'param_struct_instance': self.param_struct_instance, + 'parameter_field': parameter_field, + 'default_value': default_value, + 'parameter_validations': self.parameter_validations, } - j2_template = Template(GenerateCode.templates["declare_runtime_parameter"]) + j2_template = Template(GenerateCode.templates['declare_runtime_parameter']) code = j2_template.render(data, trim_blocks=True) return code @@ -591,30 +591,30 @@ def __str__(self): ) data = { - "parameter_map": parameter_map, - "mapped_param": mapped_param, - "dynamic_declare_parameter": str(self.dynamic_declare_parameter), - "struct_name": struct_name, - "parameter_field": parameter_field, + 'parameter_map': parameter_map, + 'mapped_param': mapped_param, + 'dynamic_declare_parameter': str(self.dynamic_declare_parameter), + 'struct_name': struct_name, + 'parameter_field': parameter_field, } - j2_template = Template(GenerateCode.templates["remove_runtime_parameter"]) + j2_template = Template(GenerateCode.templates['remove_runtime_parameter']) code = j2_template.render(data, trim_blocks=True) return code def get_all_templates(language: str): template_lang_path = os.path.join( - os.path.dirname(__file__), "jinja_templates", language + os.path.dirname(__file__), 'jinja_templates', language ) - if language == "markdown": + if language == 'markdown': template_markdown_path = os.path.join( - os.path.dirname(__file__), "jinja_templates", "markdown" + os.path.dirname(__file__), 'jinja_templates', 'markdown' ) template_paths = [template_lang_path, template_markdown_path] - elif language == "rst": + elif language == 'rst': template_rst_path = os.path.join( - os.path.dirname(__file__), "jinja_templates", "rst" + os.path.dirname(__file__), 'jinja_templates', 'rst' ) template_paths = [template_lang_path, template_rst_path] else: @@ -635,16 +635,16 @@ def get_all_templates(language: str): def preprocess_inputs(language, name, value, nested_name_list): # define parameter name - param_name = "".join(x + "." for x in nested_name_list[1:]) + name + param_name = ''.join(x + '.' for x in nested_name_list[1:]) + name # required attributes try: - defined_type = value["type"] + defined_type = value['type'] except KeyError as e: - raise compile_error("No type defined for parameter %s" % param_name) + raise compile_error('No type defined for parameter %s' % param_name) # check for invalid syntax - valid_keys = {"default_value", "description", "read_only", "validation", "type"} + valid_keys = {'default_value', 'description', 'read_only', 'validation', 'type'} invalid_keys = value.keys() - valid_keys if len(invalid_keys) > 0: raise compile_error( @@ -653,7 +653,7 @@ def preprocess_inputs(language, name, value, nested_name_list): ) # optional attributes - default_value = value.get("default_value", None) + default_value = value.get('default_value', None) if not is_fixed_type(defined_type): code_gen_variable = CodeGenVariable( language, name, param_name, defined_type, default_value @@ -663,12 +663,12 @@ def preprocess_inputs(language, name, value, nested_name_list): language, name, param_name, defined_type, default_value ) - description = value.get("description", "") - read_only = bool(value.get("read_only", False)) + description = value.get('description', '') + read_only = bool(value.get('read_only', False)) validations = [] - validations_dict = value.get("validation", {}) + validations_dict = value.get('validation', {}) if is_fixed_type(defined_type): - validations_dict["size_lt<>"] = fixed_type_size(defined_type) + 1 + validations_dict['size_lt<>'] = fixed_type_size(defined_type) + 1 for func_name in validations_dict: args = validations_dict[func_name] @@ -691,9 +691,9 @@ class GenerateCode: def __init__(self, language: str): GenerateCode.templates = get_all_templates(language) self.language = language - self.namespace = "" - self.struct_tree = DeclareStruct("Params", []) - self.stack_struct_tree = DeclareStruct("StackParams", []) + self.namespace = '' + self.struct_tree = DeclareStruct('Params', []) + self.stack_struct_tree = DeclareStruct('StackParams', []) self.update_parameters = [] self.declare_parameters = [] self.declare_dynamic_parameters = [] @@ -702,17 +702,17 @@ def __init__(self, language: str): self.remove_dynamic_parameter = [] self.declare_parameter_sets = [] self.set_stack_params = [] - if language == "cpp": - self.comments = "// auto-generated DO NOT EDIT" - elif language == "rst": - self.comments = ".. auto-generated DO NOT EDIT" - elif language == "python" or language == "markdown": - self.comments = "# auto-generated DO NOT EDIT" + if language == 'cpp': + self.comments = '// auto-generated DO NOT EDIT' + elif language == 'rst': + self.comments = '.. auto-generated DO NOT EDIT' + elif language == 'python' or language == 'markdown': + self.comments = '# auto-generated DO NOT EDIT' else: raise compile_error( - "Invalid language, only c++ and python are currently supported." + 'Invalid language, only c++ and python are currently supported.' ) - self.user_validation_file = "" + self.user_validation_file = '' def parse(self, yaml_file, validate_header): with open(yaml_file) as f: @@ -726,7 +726,7 @@ def parse(self, yaml_file, validate_header): if len(doc) != 1: raise compile_error( - "The yaml definition must only have one root element" + 'The yaml definition must only have one root element' ) self.namespace = list(doc.keys())[0] self.user_validation_file = validate_header @@ -796,7 +796,7 @@ def parse_params(self, name, value, nested_name_list): isinstance(code_gen_variable, CodeGenFixedVariable) or not ( code_gen_variable.array_type - or code_gen_variable.defined_type == "string" + or code_gen_variable.defined_type == 'string' ) ): self.stack_struct_tree.add_field(var) @@ -838,38 +838,38 @@ def parse_dict(self, name, root_map, nested_name): def __str__(self): data = { - "user_validation_file": self.user_validation_file, - "comments": self.comments, - "namespace": self.namespace, - "field_content": self.struct_tree.sub_structs[0].field_content(), - "sub_struct_content": self.struct_tree.sub_structs[0].sub_struct_content(), - "stack_field_content": self.stack_struct_tree.sub_structs[ + 'user_validation_file': self.user_validation_file, + 'comments': self.comments, + 'namespace': self.namespace, + 'field_content': self.struct_tree.sub_structs[0].field_content(), + 'sub_struct_content': self.struct_tree.sub_structs[0].sub_struct_content(), + 'stack_field_content': self.stack_struct_tree.sub_structs[ 0 ].field_content(), - "stack_sub_struct_content": self.stack_struct_tree.sub_structs[ + 'stack_sub_struct_content': self.stack_struct_tree.sub_structs[ 0 ].sub_struct_content(), - "update_params_set": "\n".join([str(x) for x in self.update_parameters]), - "update_dynamic_parameters": "\n".join( + 'update_params_set': '\n'.join([str(x) for x in self.update_parameters]), + 'update_dynamic_parameters': '\n'.join( [str(x) for x in self.update_dynamic_parameters] ), - "declare_params": "\n".join([str(x) for x in self.declare_parameters]), - "declare_params_set": "\n".join( + 'declare_params': '\n'.join([str(x) for x in self.declare_parameters]), + 'declare_params_set': '\n'.join( [str(x) for x in self.declare_parameter_sets] ), - "declare_set_dynamic_params": "\n".join( + 'declare_set_dynamic_params': '\n'.join( [str(x) for x in self.declare_dynamic_parameters] ), - "update_declare_dynamic_parameters": "\n".join( + 'update_declare_dynamic_parameters': '\n'.join( [str(x) for x in self.update_declare_dynamic_parameter] ), - "set_stack_params": "\n".join([str(x) for x in self.set_stack_params]), + 'set_stack_params': '\n'.join([str(x) for x in self.set_stack_params]), # TODO support removing runtime parameters # "remove_dynamic_parameters": "\n".join( # [str(x) for x in self.remove_dynamic_parameter] # ), } - j2_template = Template(GenerateCode.templates["parameter_library_header"]) + j2_template = Template(GenerateCode.templates['parameter_library_header']) code = j2_template.render(data, trim_blocks=True) return code diff --git a/generate_parameter_library_py/generate_parameter_library_py/python_convertions.py b/generate_parameter_library_py/generate_parameter_library_py/python_convertions.py index 4790900..c171f6c 100644 --- a/generate_parameter_library_py/generate_parameter_library_py/python_convertions.py +++ b/generate_parameter_library_py/generate_parameter_library_py/python_convertions.py @@ -11,51 +11,51 @@ class PythonConvertions: def __init__(self): self.defined_type_to_lang_type = { - "none": lambda defined_type, templates: None, - "bool": lambda defined_type, templates: "bool", - "double": lambda defined_type, templates: "float", - "int": lambda defined_type, templates: "int", - "string": lambda defined_type, templates: "str", - "bool_array": lambda defined_type, templates: "[bool]", - "double_array": lambda defined_type, templates: "[float]", - "int_array": lambda defined_type, templates: "[int]", - "string_array": lambda defined_type, templates: "[str]", - "double_array_fixed": lambda defined_type, templates: "[float]", - "int_array_fixed": lambda defined_type, templates: "[int]", - "string_array_fixed": lambda defined_type, templates: "[str]", - "string_fixed": lambda defined_type, templates: "str", + 'none': lambda defined_type, templates: None, + 'bool': lambda defined_type, templates: 'bool', + 'double': lambda defined_type, templates: 'float', + 'int': lambda defined_type, templates: 'int', + 'string': lambda defined_type, templates: 'str', + 'bool_array': lambda defined_type, templates: '[bool]', + 'double_array': lambda defined_type, templates: '[float]', + 'int_array': lambda defined_type, templates: '[int]', + 'string_array': lambda defined_type, templates: '[str]', + 'double_array_fixed': lambda defined_type, templates: '[float]', + 'int_array_fixed': lambda defined_type, templates: '[int]', + 'string_array_fixed': lambda defined_type, templates: '[str]', + 'string_fixed': lambda defined_type, templates: 'str', } self.yaml_type_to_as_function = { - "none": None, - "string_array": "value", - "double_array": "value", - "int_array": "value", - "bool_array": "value", - "string": "value", - "double": "value", - "int": "value", - "bool": "value", - "bool_array_fixed": "value", - "double_array_fixed": "value", - "int_array_fixed": "value", - "string_array_fixed": "value", - "string_fixed": "value", + 'none': None, + 'string_array': 'value', + 'double_array': 'value', + 'int_array': 'value', + 'bool_array': 'value', + 'string': 'value', + 'double': 'value', + 'int': 'value', + 'bool': 'value', + 'bool_array_fixed': 'value', + 'double_array_fixed': 'value', + 'int_array_fixed': 'value', + 'string_array_fixed': 'value', + 'string_fixed': 'value', } self.lang_str_value_func = { - "none": self.no_code, - "bool": self.bool_to_str, - "double": self.float_to_str, - "int": self.int_to_str, - "string": self.str_to_str, - "bool_array": self.bool_array_to_str, - "double_array": self.float_array_to_str, - "int_array": self.int_array_to_str, - "string_array": self.str_array_to_str, - "bool_array_fixed": self.bool_array_fixed_to_str, - "double_array_fixed": self.float_array_fixed_to_str, - "int_array_fixed": self.int_array_fixed_to_str, - "string_array_fixed": self.str_array_fixed_to_str, - "string_fixed": self.str_fixed_to_str, + 'none': self.no_code, + 'bool': self.bool_to_str, + 'double': self.float_to_str, + 'int': self.int_to_str, + 'string': self.str_to_str, + 'bool_array': self.bool_array_to_str, + 'double_array': self.float_array_to_str, + 'int_array': self.int_array_to_str, + 'string_array': self.str_array_to_str, + 'bool_array_fixed': self.bool_array_fixed_to_str, + 'double_array_fixed': self.float_array_fixed_to_str, + 'int_array_fixed': self.int_array_fixed_to_str, + 'string_array_fixed': self.str_array_fixed_to_str, + 'string_fixed': self.str_fixed_to_str, } self.python_val_to_str_func = { "": self.bool_to_str, @@ -64,30 +64,30 @@ def __init__(self): "": self.str_to_str, } self.python_val_to_yaml_type = { - "": "bool", - "": "double", - "": "int", - "": "str", + "": 'bool', + "": 'double', + "": 'int', + "": 'str', } self.python_list_to_yaml_type = { - "": "bool_array", - "": "double_array", - "": "integer_array", - "": "string_array", + "": 'bool_array', + "": 'double_array', + "": 'integer_array', + "": 'string_array', } - self.open_bracket = "[" - self.close_bracket = "]" + self.open_bracket = '[' + self.close_bracket = ']' @typechecked def get_func_signature(self, function_name: str, base_type: str) -> str: - if function_name.__contains__("::"): + if function_name.__contains__('::'): # user defined function - function_name = function_name.replace("::", ".") + function_name = function_name.replace('::', '.') else: - function_name = "ParameterValidators." + function_name - if function_name.__contains__("<>"): - function_name = function_name.replace("<>", "") + function_name = 'ParameterValidators.' + function_name + if function_name.__contains__('<>'): + function_name = function_name.replace('<>', '') return function_name @typechecked @@ -96,104 +96,104 @@ def initialization_fail_validation(self, param_name: str) -> str: @typechecked def initialization_pass_validation(self, param_name: str) -> str: - return "" + return '' @typechecked def update_parameter_fail_validation(self) -> str: - return "return SetParametersResult(successful=False, reason=validation_result)" + return 'return SetParametersResult(successful=False, reason=validation_result)' @typechecked def update_parameter_pass_validation(self) -> str: - return "" + return '' @typechecked def no_code(self, s: Optional[str]): - return "" + return '' # value to c++ string conversion functions @typechecked def bool_to_str(self, cond: Optional[bool]): if cond is None: - return "" - return "True" if cond else "False" + return '' + return 'True' if cond else 'False' @typechecked def float_to_str(self, num: Optional[float]): if num is None: - return "" + return '' str_num = str(num) - if str_num == "nan": + if str_num == 'nan': str_num = "float('nan')" - elif str_num == "inf": + elif str_num == 'inf': str_num = "float('inf')" - elif str_num == "-inf": + elif str_num == '-inf': str_num = "-float('inf')" else: - if len(str_num.split(".")) == 1 and not str_num.__contains__("e"): - str_num += ".0" + if len(str_num.split('.')) == 1 and not str_num.__contains__('e'): + str_num += '.0' return str_num @typechecked def int_to_str(self, num: Optional[int]): if num is None: - return "" + return '' return str(num) @typechecked def str_to_str(self, s: Optional[str]): if s is None: - return "" + return '' return f'"{s}"' @typechecked def bool_array_to_str(self, values: Optional[list]): if values is None: - return "" - return "[" + ", ".join(self.bool_to_str(x) for x in values) + "]" + return '' + return '[' + ', '.join(self.bool_to_str(x) for x in values) + ']' @typechecked def float_array_to_str(self, values: Optional[list]): if values is None: - return "" - return "[" + ", ".join(self.float_to_str(x) for x in values) + "]" + return '' + return '[' + ', '.join(self.float_to_str(x) for x in values) + ']' @typechecked def int_array_to_str(self, values: Optional[list]): if values is None: - return "" - return "[" + ", ".join(self.int_to_str(x) for x in values) + "]" + return '' + return '[' + ', '.join(self.int_to_str(x) for x in values) + ']' @typechecked def str_array_to_str(self, s: Optional[list]): if s is None: - return "" - return "[" + ", ".join(self.str_to_str(x) for x in s) + "]" + return '' + return '[' + ', '.join(self.str_to_str(x) for x in s) + ']' @typechecked def str_array_fixed_to_str(self, s: Optional[list]): - raise compile_error("not implemented") + raise compile_error('not implemented') @typechecked def str_fixed_to_str(self, s: Optional[str]): if s is None: - return "" - return "%s" % self.str_to_str(s) + return '' + return '%s' % self.str_to_str(s) @typechecked def float_array_fixed_to_str(self, values: Optional[list]): if values is None: - return "" - return "[" + ", ".join(self.float_to_str(x) for x in values) + "]" + return '' + return '[' + ', '.join(self.float_to_str(x) for x in values) + ']' @typechecked def int_array_fixed_to_str(self, values: Optional[list]): if values is None: - return "" - return "[" + ", ".join(self.int_to_str(x) for x in values) + "]" + return '' + return '[' + ', '.join(self.int_to_str(x) for x in values) + ']' @typechecked def bool_array_fixed_to_str(self, values: Optional[list]): if values is None: - return "" - return "[" + ", ".join(self.bool_to_str(x) for x in values) + "]" + return '' + return '[' + ', '.join(self.bool_to_str(x) for x in values) + ']' diff --git a/generate_parameter_library_py/generate_parameter_library_py/python_validators.py b/generate_parameter_library_py/generate_parameter_library_py/python_validators.py index 629a740..03e44ec 100644 --- a/generate_parameter_library_py/generate_parameter_library_py/python_validators.py +++ b/generate_parameter_library_py/generate_parameter_library_py/python_validators.py @@ -36,88 +36,88 @@ class ParameterValidators: def lt(param, value): if not param.value < value: - tmp = "less than" + tmp = 'less than' return f"Parameter '{param.name}' with the value {param.value} must be {tmp} {value}" - return "" + return '' def gt(param, value): if not param.value > value: - tmp = "greater than" + tmp = 'greater than' return f"Parameter '{param.name}' with the value {param.value} must be {tmp} {value}" - return "" + return '' def lt_eq(param, value): if not param.value <= value: - tmp = "below upper bound of" + tmp = 'below upper bound of' return f"Parameter '{param.name}' with the value {param.value} must be {tmp} {value}" - return "" + return '' def gt_eq(param, value): if not param.value >= value: - tmp = "above lower bound of" + tmp = 'above lower bound of' return f"Parameter '{param.name}' with the value {param.value} must be {tmp} {value}" - return "" + return '' def not_empty(param): if len(param.value) == 0: - tmp = "above lower bound of" + tmp = 'above lower bound of' return f"Parameter '{param.name}' cannot be empty" - return "" + return '' def one_of(param, values): if not param.value in values: return f"Parameter '{param.name}' with the value '{param.value}' is not in the set {str(values)}" - return "" + return '' # Array validators def unique(param): if len(set(param.value)) != len(param.value): return f"Parameter '{param.name}' must only contain unique values" - return "" + return '' def subset_of(param, values): for val in param.value: if not val in values: return f"Entry '{val}' in parameter '{param.name}' is not in the set {str(values)}" - return "" + return '' def fixed_size(param, length): if not len(param.value) == length: - tmp = "equal to" + tmp = 'equal to' return f"Length of parameter '{param.name}' is '{len(param.value)}' but must be {tmp} {length}" - return "" + return '' def size_gt(param, length): if not len(param.value) > length: - tmp = "greater than" + tmp = 'greater than' return f"Length of parameter '{param.name}' is '{len(param.value)}' but must be {tmp} {length}" - return "" + return '' def size_lt(param, length): if not len(param.value) < length: - tmp = "less than" + tmp = 'less than' return f"Length of parameter '{param.name}' is '{len(param.value)}' but must be {tmp} {length}" - return "" + return '' def element_bounds(param, lower, upper): for val in param.value: if val > upper or val < lower: return f"Value {param.value} in parameter '{param.name}' must be within bounds [{lower}, {upper}]" - return "" + return '' def lower_element_bounds(param, lower): for val in param.value: if val < lower: return f"Value {val} in parameter '{param.name}' must be above lower bound of {lower}" - return "" + return '' def upper_element_bounds(param, upper): for val in param.value: if val > upper: return f"Value {val} in parameter '{param.name}' must be above lower bound of {upper}" - return "" + return '' def bounds(param, lower, upper): if param.value > upper or param.value < lower: return f"Value {param.value} in parameter '{param.name}' must be within bounds [{lower}, {upper}]" - return "" + return '' diff --git a/generate_parameter_library_py/generate_parameter_library_py/setup_helper.py b/generate_parameter_library_py/generate_parameter_library_py/setup_helper.py index 291fa0b..ed4de8e 100644 --- a/generate_parameter_library_py/generate_parameter_library_py/setup_helper.py +++ b/generate_parameter_library_py/generate_parameter_library_py/setup_helper.py @@ -32,14 +32,14 @@ from generate_parameter_library_py.generate_python_module import run -def generate_parameter_module(module_name, yaml_file, validation_module=""): +def generate_parameter_module(module_name, yaml_file, validation_module=''): # TODO there must be a better way to do this. I need to find the build directory so I can place the python # module there build_dir = None install_dir = None for i, arg in enumerate(sys.argv): # Look for the `--build-directory` option in the command line arguments - if arg == "--build-directory" or arg == "--build-base": + if arg == '--build-directory' or arg == '--build-base': build_arg = sys.argv[i + 1] path_split = os.path.split(build_arg) @@ -49,24 +49,24 @@ def generate_parameter_module(module_name, yaml_file, validation_module=""): colcon_ws = path_split[0] tmp = sys.version.split()[0] - tmp = tmp.split(".") - py_version = f"python{tmp[0]}.{tmp[1]}" + tmp = tmp.split('.') + py_version = f'python{tmp[0]}.{tmp[1]}' install_dir = os.path.join( colcon_ws, - "install", + 'install', pkg_name, - "lib", + 'lib', py_version, - "site-packages", + 'site-packages', pkg_name, ) - build_dir = os.path.join(colcon_ws, "build", pkg_name, pkg_name) + build_dir = os.path.join(colcon_ws, 'build', pkg_name, pkg_name) break if build_dir: - run(os.path.join(build_dir, module_name + ".py"), yaml_file, validation_module) + run(os.path.join(build_dir, module_name + '.py'), yaml_file, validation_module) if install_dir: run( - os.path.join(install_dir, module_name + ".py"), yaml_file, validation_module + os.path.join(install_dir, module_name + '.py'), yaml_file, validation_module ) diff --git a/generate_parameter_library_py/generate_parameter_library_py/test/YAML_parse_error_test.py b/generate_parameter_library_py/generate_parameter_library_py/test/YAML_parse_error_test.py index de5e6b9..b85af04 100644 --- a/generate_parameter_library_py/generate_parameter_library_py/test/YAML_parse_error_test.py +++ b/generate_parameter_library_py/generate_parameter_library_py/test/YAML_parse_error_test.py @@ -27,11 +27,11 @@ def set_up(yaml_test_file): full_file_path = os.path.join( - get_package_share_path("generate_parameter_library_py"), "test", yaml_test_file + get_package_share_path('generate_parameter_library_py'), 'test', yaml_test_file ) - testargs = [sys.argv[0], "/tmp/admittance_controller.h", full_file_path] + testargs = [sys.argv[0], '/tmp/admittance_controller.h', full_file_path] - with patch.object(sys, "argv", testargs): + with patch.object(sys, 'argv', testargs): args = parse_args() output_file = args.output_cpp_header_file yaml_file = args.input_yaml_file @@ -42,14 +42,14 @@ def set_up(yaml_test_file): # class TestViewValidCodeGen(unittest.TestCase): @pytest.mark.parametrize( - "test_input,expected", + 'test_input,expected', [ (file_name, YAMLSyntaxError) for file_name in [ - "wrong_default_type.yaml", - "missing_type.yaml", - "invalid_syntax.yaml", - "invalid_parameter_type.yaml", + 'wrong_default_type.yaml', + 'missing_type.yaml', + 'invalid_syntax.yaml', + 'invalid_parameter_type.yaml', ] ], ) @@ -62,15 +62,15 @@ def test_expected(test_input, expected): def test_parse_valid_parameter_file(): try: - yaml_test_file = "valid_parameters.yaml" + yaml_test_file = 'valid_parameters.yaml' set_up(yaml_test_file) except Exception as e: - assert False, f"failed to parse valid file, reason:{e}" + assert False, f'failed to parse valid file, reason:{e}' def test_parse_valid_parameter_file_including_none_type(): try: - yaml_test_file = "valid_parameters_with_none_type.yaml" + yaml_test_file = 'valid_parameters_with_none_type.yaml' set_up(yaml_test_file) except Exception as e: - assert False, f"failed to parse valid file, reason:{e}" + assert False, f'failed to parse valid file, reason:{e}' diff --git a/generate_parameter_library_py/setup.py b/generate_parameter_library_py/setup.py index 4fc0335..03519c7 100644 --- a/generate_parameter_library_py/setup.py +++ b/generate_parameter_library_py/setup.py @@ -2,95 +2,95 @@ from setuptools import find_packages from setuptools import setup -package_name = "generate_parameter_library_py" +package_name = 'generate_parameter_library_py' setup( name=package_name, - version="0.3.6", + version='0.3.6', packages=find_packages(), data_files=[ - ("share/" + package_name, ["package.xml"]), + ('share/' + package_name, ['package.xml']), ( - "share/" + package_name + "/test", - ["generate_parameter_library_py/test/wrong_default_type.yaml"], + 'share/' + package_name + '/test', + ['generate_parameter_library_py/test/wrong_default_type.yaml'], ), ( - "share/" + package_name + "/test", - ["generate_parameter_library_py/test/missing_type.yaml"], + 'share/' + package_name + '/test', + ['generate_parameter_library_py/test/missing_type.yaml'], ), ( - "share/" + package_name + "/test", - ["generate_parameter_library_py/test/invalid_syntax.yaml"], + 'share/' + package_name + '/test', + ['generate_parameter_library_py/test/invalid_syntax.yaml'], ), ( - "share/" + package_name + "/test", - ["generate_parameter_library_py/test/invalid_parameter_type.yaml"], + 'share/' + package_name + '/test', + ['generate_parameter_library_py/test/invalid_parameter_type.yaml'], ), ( - "share/" + package_name + "/test", - ["generate_parameter_library_py/test/valid_parameters.yaml"], + 'share/' + package_name + '/test', + ['generate_parameter_library_py/test/valid_parameters.yaml'], ), ( - "share/" + package_name + "/test", - ["generate_parameter_library_py/test/valid_parameters_with_none_type.yaml"], + 'share/' + package_name + '/test', + ['generate_parameter_library_py/test/valid_parameters_with_none_type.yaml'], ), - ("share/ament_index/resource_index/packages", ["resource/" + package_name]), + ('share/ament_index/resource_index/packages', ['resource/' + package_name]), ], - install_requires=["setuptools", "typeguard", "jinja2", "pyyaml"], + install_requires=['setuptools', 'typeguard', 'jinja2', 'pyyaml'], package_data={ - "": [ - "jinja_templates/cpp/declare_parameter", - "jinja_templates/cpp/declare_runtime_parameter", - "jinja_templates/cpp/declare_struct", - "jinja_templates/cpp/declare_variable", - "jinja_templates/cpp/parameter_library_header", - "jinja_templates/cpp/parameter_validation", - "jinja_templates/cpp/remove_runtime_parameter", - "jinja_templates/cpp/set_parameter", - "jinja_templates/cpp/set_runtime_parameter", - "jinja_templates/cpp/set_stack_params", - "jinja_templates/cpp/update_parameter", - "jinja_templates/cpp/update_runtime_parameter", - "jinja_templates/markdown/default_config", - "jinja_templates/markdown/documentation", - "jinja_templates/markdown/parameter_detail", - "jinja_templates/rst/default_config", - "jinja_templates/rst/documentation", - "jinja_templates/rst/parameter_detail", - "jinja_templates/python/declare_parameter", - "jinja_templates/python/declare_runtime_parameter", - "jinja_templates/python/declare_struct", - "jinja_templates/python/declare_variable", - "jinja_templates/python/parameter_library_header", - "jinja_templates/python/parameter_validation", - "jinja_templates/python/remove_runtime_parameter", - "jinja_templates/python/set_parameter", - "jinja_templates/python/set_runtime_parameter", - "jinja_templates/python/set_stack_params", - "jinja_templates/python/update_parameter", - "jinja_templates/python/update_runtime_parameter", + '': [ + 'jinja_templates/cpp/declare_parameter', + 'jinja_templates/cpp/declare_runtime_parameter', + 'jinja_templates/cpp/declare_struct', + 'jinja_templates/cpp/declare_variable', + 'jinja_templates/cpp/parameter_library_header', + 'jinja_templates/cpp/parameter_validation', + 'jinja_templates/cpp/remove_runtime_parameter', + 'jinja_templates/cpp/set_parameter', + 'jinja_templates/cpp/set_runtime_parameter', + 'jinja_templates/cpp/set_stack_params', + 'jinja_templates/cpp/update_parameter', + 'jinja_templates/cpp/update_runtime_parameter', + 'jinja_templates/markdown/default_config', + 'jinja_templates/markdown/documentation', + 'jinja_templates/markdown/parameter_detail', + 'jinja_templates/rst/default_config', + 'jinja_templates/rst/documentation', + 'jinja_templates/rst/parameter_detail', + 'jinja_templates/python/declare_parameter', + 'jinja_templates/python/declare_runtime_parameter', + 'jinja_templates/python/declare_struct', + 'jinja_templates/python/declare_variable', + 'jinja_templates/python/parameter_library_header', + 'jinja_templates/python/parameter_validation', + 'jinja_templates/python/remove_runtime_parameter', + 'jinja_templates/python/set_parameter', + 'jinja_templates/python/set_runtime_parameter', + 'jinja_templates/python/set_stack_params', + 'jinja_templates/python/update_parameter', + 'jinja_templates/python/update_runtime_parameter', ] }, zip_safe=False, - author="Paul Gesel", - author_email="paul.gesel@picknik.ai", - url="https://github.com/PickNikRobotics/generate_parameter_library", - download_url="https://github.com/PickNikRobotics/generate_parameter_library/releases", - keywords=["ROS"], + author='Paul Gesel', + author_email='paul.gesel@picknik.ai', + url='https://github.com/PickNikRobotics/generate_parameter_library', + download_url='https://github.com/PickNikRobotics/generate_parameter_library/releases', + keywords=['ROS'], classifiers=[ - "Intended Audience :: Developers", - "License :: OSI Approved :: BSD-3-Clause", - "Programming Language :: Python", - "Topic :: Software Development", + 'Intended Audience :: Developers', + 'License :: OSI Approved :: BSD-3-Clause', + 'Programming Language :: Python', + 'Topic :: Software Development', ], - description="Generate the ROS parameter struct in C++ and Python with callbacks for updating.", - license="BSD-3-Clause", - tests_require=["pytest"], + description='Generate the ROS parameter struct in C++ and Python with callbacks for updating.', + license='BSD-3-Clause', + tests_require=['pytest'], entry_points={ - "console_scripts": [ - "generate_parameter_library_cpp = generate_parameter_library_py.generate_cpp_header:main", - "generate_parameter_library_python = generate_parameter_library_py.generate_python_module:main", - "generate_parameter_library_markdown = generate_parameter_library_py.generate_markdown:main", + 'console_scripts': [ + 'generate_parameter_library_cpp = generate_parameter_library_py.generate_cpp_header:main', + 'generate_parameter_library_python = generate_parameter_library_py.generate_python_module:main', + 'generate_parameter_library_markdown = generate_parameter_library_py.generate_markdown:main', ], }, ) diff --git a/generate_parameter_library_py/test/test_copyright.py b/generate_parameter_library_py/test/test_copyright.py index 4a13c1e..858d8ea 100644 --- a/generate_parameter_library_py/test/test_copyright.py +++ b/generate_parameter_library_py/test/test_copyright.py @@ -19,10 +19,10 @@ # Remove the `skip` decorator once the source file(s) have a copyright header @pytest.mark.skip( - reason="No copyright header has been placed in the generated source file." + reason='No copyright header has been placed in the generated source file.' ) @pytest.mark.copyright @pytest.mark.linter def test_copyright(): - rc = main(argv=[".", "test"]) - assert rc == 0, "Found errors" + rc = main(argv=['.', 'test']) + assert rc == 0, 'Found errors'