From ecb1c2bcd0379a3c69a1afca2d63da202755dc53 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Sun, 22 Sep 2024 14:02:48 +0000 Subject: [PATCH] #12 PackageInfo responsible for collect_source_headers --- cppwg/__main__.py | 2 +- cppwg/generators.py | 41 ++++--------------------------------- cppwg/input/package_info.py | 41 +++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 38 deletions(-) diff --git a/cppwg/__main__.py b/cppwg/__main__.py index a6bbc99..34fc930 100644 --- a/cppwg/__main__.py +++ b/cppwg/__main__.py @@ -113,7 +113,7 @@ def generate(args: argparse.Namespace) -> None: castxml_cflags=castxml_cflags, ) - generator.generate_wrapper() + generator.generate_wrappers() def main() -> None: diff --git a/cppwg/generators.py b/cppwg/generators.py index e1a956a..b1e304b 100644 --- a/cppwg/generators.py +++ b/cppwg/generators.py @@ -1,6 +1,5 @@ """Contains the main interface for generating Python wrappers.""" -import fnmatch import logging import os import re @@ -18,7 +17,6 @@ from cppwg.utils import utils from cppwg.utils.constants import ( CPPWG_DEFAULT_WRAPPER_DIR, - CPPWG_EXT, CPPWG_HEADER_COLLECTION_FILENAME, ) from cppwg.writers.header_collection_writer import CppHeaderCollectionWriter @@ -164,37 +162,6 @@ def __init__( self.wrapper_root, CPPWG_HEADER_COLLECTION_FILENAME ) - def collect_source_hpp_files(self) -> None: - """ - Collect *.hpp files from the source root. - - Walk through the source root and add any files matching the provided - patterns e.g. "*.hpp". Skip the wrapper root and wrappers to - avoid pollution. - """ - logger = logging.getLogger() - - for root, _, filenames in os.walk(self.source_root, followlinks=True): - for pattern in self.package_info.source_hpp_patterns: - for filename in fnmatch.filter(filenames, pattern): - filepath = os.path.abspath(os.path.join(root, filename)) - - # Skip files in wrapper root dir - if Path(self.wrapper_root) in Path(filepath).parents: - continue - - # Skip files with the extensions like .cppwg.hpp - suffix = os.path.splitext(os.path.splitext(filename)[0])[1] - if suffix == CPPWG_EXT: - continue - - self.package_info.source_hpp_files.append(filepath) - - # Check if any source files were found - if not self.package_info.source_hpp_files: - logger.error(f"No header files found in source root: {self.source_root}") - raise FileNotFoundError() - def log_unknown_classes(self) -> None: """ Log unwrapped classes. @@ -297,15 +264,15 @@ def write_wrappers(self) -> None: ) module_writer.write() - def generate_wrapper(self) -> None: + def generate_wrappers(self) -> None: """ - Parse input yaml and C++ source to generate Python wrappers. + Parse yaml configuration and C++ source to generate Python wrappers. """ # Parse the input yaml for package, module, and class information self.parse_package_info() - # Search for header files in the source root - self.collect_source_hpp_files() + # Collect header files, skipping wrappers to avoid pollution + self.package_info.collect_source_headers(restricted_paths=[self.wrapper_root]) # Update modules with information from the source headers self.update_modules_from_source() diff --git a/cppwg/input/package_info.py b/cppwg/input/package_info.py index 82ad62d..0d26547 100644 --- a/cppwg/input/package_info.py +++ b/cppwg/input/package_info.py @@ -1,8 +1,13 @@ """Package information structure.""" +import fnmatch +import logging +import os +from pathlib import Path from typing import Any, Dict, List, Optional from cppwg.input.base_info import BaseInfo +from cppwg.utils.constants import CPPWG_EXT class PackageInfo(BaseInfo): @@ -69,3 +74,39 @@ def __init__( def parent(self) -> None: """Returns None as this is the top level object in the hierarchy.""" return None + + def collect_source_headers(self, restricted_paths: List[str]) -> None: + """ + Collect header files from the source root. + + Walk through the source root and add any files matching the provided + source file patterns e.g. "*.hpp". + + Parameters + ---------- + restricted_paths : List[str] + A list of restricted paths to skip when collecting header files. + """ + logger = logging.getLogger() + + for root, _, filenames in os.walk(self.source_root, followlinks=True): + for pattern in self.source_hpp_patterns: + for filename in fnmatch.filter(filenames, pattern): + filepath = os.path.abspath(os.path.join(root, filename)) + + # Skip files in restricted paths + for restricted_path in restricted_paths: + if Path(restricted_path) in Path(filepath).parents: + continue + + # Skip files with the extensions like .cppwg.hpp + suffix = os.path.splitext(os.path.splitext(filename)[0])[1] + if suffix == CPPWG_EXT: + continue + + self.source_hpp_files.append(filepath) + + # Check if any source files were found + if not self.source_hpp_files: + logger.error(f"No header files found in source root: {self.source_root}") + raise FileNotFoundError()