|
5 | 5 | from typing import Any, Union, Generator |
6 | 6 | from typing_extensions import Self |
7 | 7 |
|
8 | | -import sys |
9 | 8 | import os |
10 | 9 | import re |
11 | 10 | import tempfile |
|
36 | 35 | vtkcalls.. |
37 | 36 | captured_log.seek(0) # be kind let's just rewind |
38 | 37 | captured = captured_log.read().decode() |
39 | | - |
| 38 | +
|
40 | 39 | logger.error(captured.strip()) |
41 | 40 |
|
42 | 41 | """ |
43 | 42 |
|
44 | | -class RegexExceptionFilter(logging.Filter): |
45 | | - """ |
46 | | - Class to regexp VTK messages rethrown into logger by VTKCaptureLog. |
47 | | - """ |
48 | 43 |
|
49 | | - pattern : str = r"vtkExecutive.cxx" #pattern captured that will raise a vtkError |
| 44 | +class RegexExceptionFilter( logging.Filter ): |
| 45 | + """Class to regexp VTK messages rethrown into logger by VTKCaptureLog.""" |
50 | 46 |
|
51 | | - def __init__(self): |
| 47 | + pattern: str = r"vtkExecutive.cxx" #pattern captured that will raise a vtkError |
| 48 | + |
| 49 | + def __init__( self ) -> None: |
| 50 | + """Init filter with class based pattern as this is patch to logging logic.""" |
52 | 51 | super().__init__() |
53 | | - self.regex = re.compile(self.pattern) |
| 52 | + self.regex = re.compile( self.pattern ) |
54 | 53 |
|
55 | | - def filter(self, record : logging.LogRecord): |
56 | | - """ |
57 | | - Filter VTK Error from stdErr. |
58 | | - |
59 | | - Args: |
60 | | - record(loggging.LogRecord) |
| 54 | + def filter( self, record: logging.LogRecord ) -> None: |
| 55 | + """Filter VTK Error from stdErr. |
61 | 56 |
|
62 | | - Raises: |
63 | | - VTKError(geos.utils.Error) if a pattern symbol is caught in the stderr. |
| 57 | + Args: |
| 58 | + record(loggging.LogRecord) : record that logger will provide as evaluated |
| 59 | +
|
| 60 | + Raises: |
| 61 | + VTKError(geos.utils.Error) if a pattern symbol is caught in the stderr. |
64 | 62 | """ |
65 | 63 | message = record.getMessage() |
66 | | - if self.regex.search(message): |
67 | | - raise VTKError(f"Log message matched forbidden pattern: {message}") |
| 64 | + if self.regex.search( message ): |
| 65 | + raise VTKError( f"Log message matched forbidden pattern: {message}" ) |
68 | 66 | return True # Allow other messages to pass |
69 | 67 |
|
70 | 68 |
|
71 | 69 | @contextmanager |
72 | | -def VTKCaptureLog()->Generator[Any,Any,Any]: |
73 | | - """ |
74 | | - Hard way of adapting C-like vtkLogger to logging class by throwing in |
75 | | - stderr and reading back from it. |
| 70 | +def VTKCaptureLog() -> Generator[ Any, Any, Any ]: |
| 71 | + """Hard way of adapting C-like vtkLogger to logging class by throwing in stderr and reading back from it. |
76 | 72 |
|
77 | | - Return: |
78 | | - Generator buffering os.stderr |
| 73 | + Returns: |
| 74 | + Generator: buffering os stderr. |
79 | 75 |
|
80 | 76 | """ |
81 | | - #equiv to pyvista's |
| 77 | + #equiv to pyvista's |
82 | 78 | # from pyvista.utilities import VtkErrorCatcher |
83 | 79 | # with VtkErrorCatcher() as err: |
84 | 80 | # append_filter.Update() |
85 | 81 | # print(err) |
86 | | - |
87 | | - # Save original stderr file descriptor |
88 | 82 | # original_stderr_fd = sys.stderr.fileno() |
89 | 83 | original_stderr_fd = 2 |
90 | | - saved_stderr_fd = os.dup(original_stderr_fd) |
| 84 | + saved_stderr_fd = os.dup( original_stderr_fd ) |
91 | 85 |
|
92 | 86 | # Create a temporary file to capture stderr |
93 | | - with tempfile.TemporaryFile(mode='w+b') as tmp: |
94 | | - os.dup2(tmp.fileno(), original_stderr_fd) |
| 87 | + with tempfile.TemporaryFile( mode='w+b' ) as tmp: |
| 88 | + os.dup2( tmp.fileno(), original_stderr_fd ) |
95 | 89 | try: |
96 | 90 | yield tmp |
97 | 91 | finally: |
98 | 92 | # Restore original stderr |
99 | | - os.dup2(saved_stderr_fd, original_stderr_fd) |
100 | | - os.close(saved_stderr_fd) |
| 93 | + os.dup2( saved_stderr_fd, original_stderr_fd ) |
| 94 | + os.close( saved_stderr_fd ) |
101 | 95 |
|
102 | 96 |
|
103 | 97 | class CountWarningHandler( logging.Handler ): |
|
0 commit comments