diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..354dbab --- /dev/null +++ b/.flake8 @@ -0,0 +1,8 @@ +[flake8] +max_line_length = 120 +exclude= + .git, + .github, + build, + doc, + examples, diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index f0aaa1f..68f4d45 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -36,7 +36,10 @@ jobs: - name: Install cppwg run: | python -m pip install --upgrade pip - pip install . + pip install .[dev] + + - name: Lint with flake8 + run: python -m flake8 - name: Test wrapper generation run: python -m unittest tests/test_shapes.py diff --git a/cppwg/__init__.py b/cppwg/__init__.py index bdd5e3f..6fb6f19 100644 --- a/cppwg/__init__.py +++ b/cppwg/__init__.py @@ -1 +1,5 @@ -from cppwg.generators import * \ No newline at end of file +from .generators import CppWrapperGenerator + +__all__ = [ + "CppWrapperGenerator", +] diff --git a/cppwg/generators.py b/cppwg/generators.py index 488a562..6ea402b 100644 --- a/cppwg/generators.py +++ b/cppwg/generators.py @@ -5,7 +5,7 @@ import subprocess from pathlib import Path -from typing import Dict, List, Optional +from typing import List, Optional from pygccxml import __version__ as pygccxml_version from pygccxml.declarations.namespace import namespace_t diff --git a/cppwg/input/base_info.py b/cppwg/input/base_info.py index 24cfa2a..b2f3515 100644 --- a/cppwg/input/base_info.py +++ b/cppwg/input/base_info.py @@ -132,8 +132,8 @@ def hierarchy_attribute_gather(self, attribute_name: str) -> List[Any]: if hasattr(self, attribute_name) and getattr(self, attribute_name) is not None: att_list.extend(getattr(self, attribute_name)) - + if hasattr(self, "parent") and self.parent is not None: att_list.extend(self.parent.hierarchy_attribute_gather(attribute_name)) - + return att_list diff --git a/cppwg/input/class_info.py b/cppwg/input/class_info.py index 865d36e..8a59e22 100644 --- a/cppwg/input/class_info.py +++ b/cppwg/input/class_info.py @@ -1,3 +1,4 @@ + from typing import Any, Dict, Optional from cppwg.input.cpp_type_info import CppTypeInfo diff --git a/cppwg/input/cpp_type_info.py b/cppwg/input/cpp_type_info.py index 2832b8a..8dc7343 100644 --- a/cppwg/input/cpp_type_info.py +++ b/cppwg/input/cpp_type_info.py @@ -141,7 +141,7 @@ def get_full_names(self) -> List[str]: return full_names - # TODO: This method is not used, remove it? + # TODO: This method is not used, remove it? def needs_header_file_instantiation(self): """ Does this class need to be instantiated in the header file @@ -153,7 +153,7 @@ def needs_header_file_instantiation(self): and (self.needs_instantiation) ) - # TODO: This method is not used, remove it? + # TODO: This method is not used, remove it? def needs_header_file_typdef(self): """ Does this type need to be typdef'd with a nicer name in the header @@ -162,7 +162,7 @@ def needs_header_file_typdef(self): return (self.template_arg_lists is not None) and (not self.include_file_only) - # TODO: This method is not used, remove it? + # TODO: This method is not used, remove it? def needs_auto_wrapper_generation(self): """ Does this class need a wrapper to be autogenerated. diff --git a/cppwg/input/info_helper.py b/cppwg/input/info_helper.py index 0acb7d3..52238db 100644 --- a/cppwg/input/info_helper.py +++ b/cppwg/input/info_helper.py @@ -2,7 +2,7 @@ import re import logging -from typing import Any +from typing import Any, Dict, List from cppwg.input.base_info import BaseInfo from cppwg.input.class_info import CppClassInfo diff --git a/cppwg/input/module_info.py b/cppwg/input/module_info.py index 75a2b97..2bd94c2 100644 --- a/cppwg/input/module_info.py +++ b/cppwg/input/module_info.py @@ -77,5 +77,5 @@ def is_decl_in_source_path(self, decl: declaration_t) -> bool: full_path = os.path.join(self.package_info.source_root, source_location) if full_path in decl.location.file_name: return True - + return False diff --git a/cppwg/input/variable_info.py b/cppwg/input/variable_info.py index 97b35a7..14690f0 100644 --- a/cppwg/input/variable_info.py +++ b/cppwg/input/variable_info.py @@ -1,4 +1,4 @@ -from typing import Any, Optional +from typing import Any, Dict, Optional from cppwg.input.cpp_type_info import CppTypeInfo diff --git a/cppwg/parsers/package_info.py b/cppwg/parsers/package_info.py index 252e2a9..eb80bbc 100644 --- a/cppwg/parsers/package_info.py +++ b/cppwg/parsers/package_info.py @@ -4,7 +4,7 @@ import sys import yaml -from typing import Any, Optional +from typing import Any, Dict, Optional import cppwg.templates.custom @@ -72,7 +72,7 @@ def check_for_custom_generators(self, info: BaseInfo) -> None: # string if needed. For example, a custom generator might be specified # as `custom_generator: CPPWG_SOURCEROOT/path/to/CustomGenerator.py` filepath: str = info.custom_generator.replace( - CPPWG_SOURCEROOT_STRING, self.source_root + CPPWG_SOURCEROOT_STRING, self.source_root ) filepath = os.path.abspath(filepath) @@ -194,7 +194,7 @@ def parse(self) -> PackageInfo: self.package_info.module_info_collection.append(module_info) # Parse the class data and create class info objects. - # Note: if module_config["use_all_classes"] == True, class info + # Note: if module_config["use_all_classes"] == True, class info # objects will be added later after parsing the C++ source code. if not module_config["use_all_classes"]: if module_config["classes"]: @@ -215,9 +215,8 @@ def parse(self) -> PackageInfo: class_info.module_info = module_info module_info.class_info_collection.append(class_info) - - # Parse the free function data and create free function info objects. - # Note: if module_config["use_all_free_functions"] == True, free function + # Parse the free function data and create free function info objects. + # Note: if module_config["use_all_free_functions"] == True, free function # info objects will be added later after parsing the C++ source code. if not module_config["use_all_free_functions"]: if module_config["free_functions"]: diff --git a/cppwg/templates/custom.py b/cppwg/templates/custom.py index e350ad0..868421a 100644 --- a/cppwg/templates/custom.py +++ b/cppwg/templates/custom.py @@ -1,23 +1,34 @@ -""" -This class returns custom code snippets for use during the wrapper -generation processes. It can be used as a base classs for -custom code generators. -""" - class Custom: - + """ + This class returns custom code snippets for use during the wrapper + generation processes. It can be used as a base classs for + custom code generators. + """ + def __init__(self): - + pass - - def get_class_cpp_pre_code(self, *args, **kwargs): - + + def get_class_cpp_pre_code(self, *args, **kwargs) -> str: + """ + Return a string of C++ code to be inserted before the class + definition. + """ + return "" - - def get_class_cpp_def_code(self, *args, **kwargs): - + + def get_class_cpp_def_code(self, *args, **kwargs) -> str: + """ + Return a string of C++ code to be inserted in the class + definition. + """ + + return "" + + def get_module_code(self) -> str: + """ + Return a string of C++ code to be inserted in the module + definition. + """ + return "" - - def get_module_code(self): - - return "" \ No newline at end of file diff --git a/cppwg/templates/pybind11_default.py b/cppwg/templates/pybind11_default.py index 56f6ffa..eaa9a63 100644 --- a/cppwg/templates/pybind11_default.py +++ b/cppwg/templates/pybind11_default.py @@ -1,4 +1,3 @@ - class_cpp_header = """\ #include #include @@ -64,17 +63,19 @@ class {class_short_name}_Overloads : public {class_short_name}{{ class_method = """\ .def{def_adorn}( - "{method_name}", - ({return_type}({self_ptr})({arg_signature}){const_adorn}) &{class_short_name}::{method_name}, + "{method_name}", + ({return_type}({self_ptr})({arg_signature}){const_adorn}) &{class_short_name}::{method_name}, {method_docs} {default_args} {call_policy}) """ -template_collection = {'class_cpp_header': class_cpp_header, - 'free_function': free_function, - 'class_hpp_header': class_hpp_header, - 'class_method': class_method, - 'class_definition': class_definition, - 'class_virtual_override_header': class_virtual_override_header, - 'class_virtual_override_footer': class_virtual_override_footer, - 'smart_pointer_holder': smart_pointer_holder, - 'method_virtual_override': method_virtual_override} +template_collection = { + "class_cpp_header": class_cpp_header, + "free_function": free_function, + "class_hpp_header": class_hpp_header, + "class_method": class_method, + "class_definition": class_definition, + "class_virtual_override_header": class_virtual_override_header, + "class_virtual_override_footer": class_virtual_override_footer, + "smart_pointer_holder": smart_pointer_holder, + "method_virtual_override": method_virtual_override, +} diff --git a/cppwg/writers/constructor_writer.py b/cppwg/writers/constructor_writer.py index f981565..8f2f94f 100644 --- a/cppwg/writers/constructor_writer.py +++ b/cppwg/writers/constructor_writer.py @@ -143,7 +143,7 @@ def add_self(self, cpp_string: str) -> str: cpp_string += ", ".join(arg_types) cpp_string += " >()" - + # Default args e.g. py::arg("i") = 1 default_args = "" if not self.default_arg_exclusion_criteria(): diff --git a/cppwg/writers/free_function_writer.py b/cppwg/writers/free_function_writer.py index 2a417b2..806066f 100644 --- a/cppwg/writers/free_function_writer.py +++ b/cppwg/writers/free_function_writer.py @@ -1,3 +1,5 @@ +from typing import Dict, List + from cppwg.input.free_function_info import CppFreeFunctionInfo from cppwg.writers.base_writer import CppBaseWrapperWriter diff --git a/cppwg/writers/header_collection_writer.py b/cppwg/writers/header_collection_writer.py index ed1234f..2a3f3cd 100644 --- a/cppwg/writers/header_collection_writer.py +++ b/cppwg/writers/header_collection_writer.py @@ -1,9 +1,12 @@ import os +from typing import Dict + from cppwg.input.class_info import CppClassInfo from cppwg.input.free_function_info import CppFreeFunctionInfo from cppwg.input.package_info import PackageInfo + class CppHeaderCollectionWriter: """ This class manages the generation of the header collection file, which diff --git a/cppwg/writers/module_writer.py b/cppwg/writers/module_writer.py index aa4ef34..c0e6f71 100644 --- a/cppwg/writers/module_writer.py +++ b/cppwg/writers/module_writer.py @@ -1,7 +1,7 @@ import os import logging -from typing import Dict +from typing import Dict, List from pygccxml.declarations.class_declaration import class_t from pygccxml.declarations.namespace import namespace_t