Only allows pure-read and pure-write operations; Intercepts all writes for later inspection, and logs all reads. The constructor takes the to-be-wrapped fslike object.
| 55 | |
| 56 | |
| 57 | class CodegenDirWrapper(Wrapper): |
| 58 | """ |
| 59 | Only allows pure-read and pure-write operations; |
| 60 | |
| 61 | Intercepts all writes for later inspection, and logs all reads. |
| 62 | |
| 63 | The constructor takes the to-be-wrapped fslike object. |
| 64 | """ |
| 65 | |
| 66 | def __init__(self, obj): |
| 67 | super().__init__(obj) |
| 68 | |
| 69 | # stores tuples (parts, intercept_obj), where intercept_obj is a FIFO. |
| 70 | self.writes = [] |
| 71 | |
| 72 | # stores a list of parts. |
| 73 | self.reads = [] |
| 74 | |
| 75 | def open_r(self, parts): |
| 76 | self.reads.append(parts) |
| 77 | return super().open_r(parts) |
| 78 | |
| 79 | def open_w(self, parts): |
| 80 | intercept_obj = WriteCatcher() |
| 81 | self.writes.append((parts, intercept_obj)) |
| 82 | return intercept_obj |
| 83 | |
| 84 | def get_reads(self) -> None: |
| 85 | """ |
| 86 | Returns an iterable of all path component tuples for files that have |
| 87 | been read. |
| 88 | """ |
| 89 | for parts in self.reads: |
| 90 | yield parts |
| 91 | |
| 92 | self.reads.clear() |
| 93 | |
| 94 | def get_writes(self) -> None: |
| 95 | """ |
| 96 | Returns an iterable of all (path components, data_written) tuples for |
| 97 | files that have been written. |
| 98 | """ |
| 99 | for parts, intercept_obj in self.writes: |
| 100 | yield parts, intercept_obj.read() |
| 101 | |
| 102 | self.writes.clear() |
| 103 | |
| 104 | def __repr__(self): |
| 105 | return f"CodegenDirWrapper({repr(self.obj)})" |
| 106 | |
| 107 | |
| 108 | def codegen(mode: CodegenMode, input_dir: str, output_dir: str) -> tuple[list[str], list[str]]: |