• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SPF-OST / pytrnsys_process / 12763622959

14 Jan 2025 08:17AM UTC coverage: 97.928%. Remained the same
12763622959

push

github

web-flow
Removing duplicate run from pull request creation.

898 of 917 relevant lines covered (97.93%)

1.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/pytrnsys_process/headers.py
1
import pathlib as _pl
2✔
2
from abc import ABC
2✔
3
from collections import defaultdict as _defaultdict
2✔
4
from concurrent.futures import ProcessPoolExecutor
2✔
5

6
from pytrnsys_process import utils
2✔
7
from pytrnsys_process.readers import HeaderReader
2✔
8

9

10
def _process_sim_file(sim_file):
2✔
11
    try:
1✔
12
        headers = HeaderReader().read_headers(sim_file)
1✔
13
        return headers, sim_file.parents[1], sim_file
1✔
14
    except Exception as e:  # pylint: disable=broad-exception-caught
1✔
15
        print(f"Could not read {sim_file}: {e}")
1✔
16
        return None
1✔
17

18

19
class Headers:
2✔
20

21
    header_index: _defaultdict[str, list]
2✔
22

23
    def __init__(self, path_to_results: _pl.Path):
2✔
24
        self.path_to_results = path_to_results
2✔
25
        self.header_index = _defaultdict(list)
2✔
26

27
    def init_headers(self):
2✔
28
        sim_files = utils.get_files(
2✔
29
            utils.get_sim_folders(self.path_to_results)
30
        )
31
        for sim_file in sim_files:
2✔
32
            try:
2✔
33
                headers = HeaderReader().read_headers(sim_file)
2✔
34
                self._index_headers(headers, sim_file.parents[1], sim_file)
2✔
35
            except Exception as e:  # pylint: disable=broad-exception-caught
2✔
36

37
                print(f"Could not read {sim_file}: {e}")
2✔
38

39
    def init_headers_multi_process(self):
2✔
40
        sim_files = utils.get_files(
2✔
41
            utils.get_sim_folders(self.path_to_results)
42
        )
43

44
        with ProcessPoolExecutor() as executor:
2✔
45
            results = executor.map(_process_sim_file, sim_files)
2✔
46
            for result in results:
2✔
47
                if result:
2✔
48
                    headers, sim_folder, sim_file = result
2✔
49
                    self._index_headers(headers, sim_folder, sim_file)
2✔
50

51
    # TODO: Discuss if something like this is needed # pylint: disable=fixme
52
    def search_header(self, header_name: str):  # pragma: no cover
53
        if header_name in self.header_index:
54
            print(f"Header '{header_name}' found in:")
55
            for folder, file in self.header_index[header_name]:
56
                print(f"- Folder: {folder}, File: {file}")
57
        else:
58
            print(f"Header '{header_name}' not found in any files.")
59

60
    def _index_headers(
2✔
61
        self, headers: list[str], sim_folder: _pl.Path, sim_file: _pl.Path
62
    ):
63
        for header in headers:
2✔
64
            self.header_index[header].append((sim_folder.name, sim_file.name))
2✔
65

66
    # TODO: Add function to validate headers and log files with invalid headers  #pylint: disable=fixme
67

68

69
class HeaderValidationMixin(ABC):
2✔
70
    def validate_headers(
2✔
71
        self, headers: Headers, columns: list[str]
72
    ) -> tuple[bool, list[str]]:
73
        """Validates that all columns exist in the headers index.
74

75
        Args:
76
            headers: Headers instance containing the index of available headers
77
            columns: List of column names to validate
78

79
        Returns:
80
            Tuple of (is_valid, missing_columns)
81
            - is_valid: True if all columns exist
82
            - missing_columns: List of column names that are missing
83
        """
84
        missing_columns = []
2✔
85
        for column in columns:
2✔
86
            if column not in headers.header_index:
2✔
87
                missing_columns.append(column)
2✔
88

89
        return len(missing_columns) == 0, missing_columns
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc