r"""Register an import hook that is called whenever a persistent object is being unpickled. A typical use case is to patch the pickled source code to avoid errors and inconsistencies when the API of some imported module has changed. The hook should have the following signature:
(hook)
| 145 | #---------------------------------------------------------------------------- |
| 146 | |
| 147 | def import_hook(hook): |
| 148 | r"""Register an import hook that is called whenever a persistent object |
| 149 | is being unpickled. A typical use case is to patch the pickled source |
| 150 | code to avoid errors and inconsistencies when the API of some imported |
| 151 | module has changed. |
| 152 | |
| 153 | The hook should have the following signature: |
| 154 | |
| 155 | hook(meta) -> modified meta |
| 156 | |
| 157 | `meta` is an instance of `dnnlib.EasyDict` with the following fields: |
| 158 | |
| 159 | type: Type of the persistent object, e.g. `'class'`. |
| 160 | version: Internal version number of `torch_utils.persistence`. |
| 161 | module_src Original source code of the Python module. |
| 162 | class_name: Class name in the original Python module. |
| 163 | state: Internal state of the object. |
| 164 | |
| 165 | Example: |
| 166 | |
| 167 | @persistence.import_hook |
| 168 | def wreck_my_network(meta): |
| 169 | if meta.class_name == 'MyNetwork': |
| 170 | print('MyNetwork is being imported. I will wreck it!') |
| 171 | meta.module_src = meta.module_src.replace("True", "False") |
| 172 | return meta |
| 173 | """ |
| 174 | assert callable(hook) |
| 175 | _import_hooks.append(hook) |
| 176 | |
| 177 | #---------------------------------------------------------------------------- |
| 178 |