Skip to content

Commit

Permalink
#12 PackageInfo responsible for collect_source_headers
Browse files Browse the repository at this point in the history
  • Loading branch information
kwabenantim committed Sep 22, 2024
1 parent 8f288df commit ecb1c2b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 38 deletions.
2 changes: 1 addition & 1 deletion cppwg/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def generate(args: argparse.Namespace) -> None:
castxml_cflags=castxml_cflags,
)

generator.generate_wrapper()
generator.generate_wrappers()


def main() -> None:
Expand Down
41 changes: 4 additions & 37 deletions cppwg/generators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Contains the main interface for generating Python wrappers."""

import fnmatch
import logging
import os
import re
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down
41 changes: 41 additions & 0 deletions cppwg/input/package_info.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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()

0 comments on commit ecb1c2b

Please sign in to comment.