fsfwgen/fsfwgen/parserbase/file_list_parser.py

78 lines
2.7 KiB
Python
Raw Normal View History

2021-08-02 12:49:10 +02:00
"""Generic File Parser class
2021-06-08 12:37:10 +02:00
Used by parse header files. Implemented as class in case header parser becomes more complex
"""
import os
import re
2022-06-20 16:56:05 +02:00
from pathlib import Path
from typing import Union, List
2021-06-08 12:37:10 +02:00
from fsfwgen.logging import get_console_logger
2021-08-02 12:49:10 +02:00
LOGGER = get_console_logger()
2021-06-08 12:37:10 +02:00
# pylint: disable=too-few-public-methods
class FileListParser:
2021-08-02 12:49:10 +02:00
"""Generic header parser which takes a directory name or directory name list
2021-06-08 12:37:10 +02:00
and parses all included header files recursively.
2021-08-02 12:49:10 +02:00
TODO: Filter functionality for each directory to filter out files or folders
2021-06-08 12:37:10 +02:00
"""
2022-06-20 16:56:05 +02:00
def __init__(self, directory_list_or_name: Union[Path, List[Path]]):
2021-08-02 12:49:10 +02:00
self.directory_list = []
2022-06-20 16:56:05 +02:00
if isinstance(directory_list_or_name, Path):
2021-08-02 12:49:10 +02:00
self.directory_list.append(directory_list_or_name)
2022-06-20 16:56:05 +02:00
elif isinstance(directory_list_or_name, List):
2021-08-02 12:49:10 +02:00
self.directory_list.extend(directory_list_or_name)
2021-06-08 12:37:10 +02:00
else:
2021-08-02 12:49:10 +02:00
LOGGER.warning(
"Header Parser: Passed directory list is not a header name or list of header names"
)
2021-06-08 12:37:10 +02:00
self.header_files = []
def parse_header_files(
self,
search_recursively: bool = False,
printout_string: str = "Parsing header files: ",
print_current_dir: bool = False,
2022-06-20 16:56:05 +02:00
) -> List[Path]:
2021-08-02 12:49:10 +02:00
"""This function is called to get a list of header files
2021-06-08 12:37:10 +02:00
:param search_recursively:
:param printout_string:
:param print_current_dir:
:return:
"""
print(printout_string, end="")
for directory in self.directory_list:
self.__get_header_file_list(
directory, search_recursively, print_current_dir
)
2021-06-08 12:37:10 +02:00
print(str(len(self.header_files)) + " header files were found.")
# g.PP.pprint(self.header_files)
return self.header_files
def __get_header_file_list(
self,
2022-06-20 16:56:05 +02:00
base_directory: Path,
seach_recursively: bool = False,
print_current_dir: bool = False,
):
2021-06-08 12:37:10 +02:00
local_header_files = []
if print_current_dir:
2022-06-20 16:56:05 +02:00
print(f"Parsing header files in: {base_directory}")
2021-06-08 12:37:10 +02:00
# g.PP.pprint(base_list)
2022-06-20 16:56:05 +02:00
for entry in base_directory.iterdir():
# header_file_match = re.match(r"[_.]*.*\.h", entry.as_posix())
if (
entry.is_file()
and entry.suffix == ".h"
and entry.as_posix()[0] not in [".", "_"]
):
local_header_files.append(entry)
2021-06-08 12:37:10 +02:00
if seach_recursively:
2022-06-20 16:56:05 +02:00
if entry.is_dir():
self.__get_header_file_list(entry, seach_recursively)
2021-06-08 12:37:10 +02:00
# print("Files found in: " + base_directory)
# g.PP.pprint(local_header_files)
self.header_files.extend(local_header_files)