Manages a collection of python resources that will be written to files.
| 121 | |
| 122 | |
| 123 | class _ResourceFiles: |
| 124 | """Manages a collection of python resources that will be written to files.""" |
| 125 | |
| 126 | def __init__(self, resource_names: Sequence[str]) -> None: |
| 127 | """Load the resources with the provided names.""" |
| 128 | # Map each name to its contents. |
| 129 | self._files: Dict[str, bytes] = {} |
| 130 | for name in resource_names: |
| 131 | self._files[name] = importlib.resources.read_binary(__package__, name) |
| 132 | |
| 133 | def patch_files(self, patch_fn: Callable[[bytes], bytes]) -> None: |
| 134 | """Uses the provided patching function to update the contents of all |
| 135 | files. `patch_fn` takes the current contents of a file as input and |
| 136 | returns the new contents. |
| 137 | """ |
| 138 | for name in self._files.keys(): |
| 139 | self._files[name] = patch_fn(self._files[name]) |
| 140 | |
| 141 | def get(self, name: str) -> bytes: |
| 142 | """Returns the current contents of the named file.""" |
| 143 | return self._files[name] |
| 144 | |
| 145 | def write_to(self, out_dir: str) -> None: |
| 146 | """Writes the files to the specified directory. File names are based on |
| 147 | the original resource names. |
| 148 | """ |
| 149 | for name, data in self._files.items(): |
| 150 | with open(os.path.join(out_dir, name), "wb") as fp: |
| 151 | fp.write(data) |
| 152 | |
| 153 | |
| 154 | @dataclass |
no outgoing calls