|
1 | 1 | # -*- coding: utf-8 -*- |
2 | | -import inspect |
3 | 2 | import logging |
4 | 3 | import re |
5 | | -import sys |
6 | | -import uuid |
7 | 4 | from concurrent.futures import ProcessPoolExecutor |
8 | 5 |
|
9 | 6 | from lxml import etree, html |
|
12 | 9 | from psycopg2.extras import Json |
13 | 10 |
|
14 | 11 | from .const import NEARLYWARN |
15 | | -from .exceptions import MigrationError |
16 | 12 | from .helpers import table_of_model |
17 | | -from .misc import import_script, log_progress |
| 13 | +from .misc import log_progress, make_pickleable_callback |
18 | 14 | from .pg import column_exists, column_type, get_max_workers, table_exists |
19 | 15 |
|
20 | 16 | _logger = logging.getLogger(__name__) |
@@ -161,28 +157,6 @@ def html_converter(transform_callback, selector=None): |
161 | 157 | return HTMLConverter(make_pickleable_callback(transform_callback), selector) |
162 | 158 |
|
163 | 159 |
|
164 | | -def make_pickleable_callback(callback): |
165 | | - """ |
166 | | - Make a callable importable. |
167 | | -
|
168 | | - `ProcessPoolExecutor.map` arguments needs to be pickleable |
169 | | - Functions can only be pickled if they are importable. |
170 | | - However, the callback's file is not importable due to the dash in the filename. |
171 | | - We should then put the executed function in its own importable file. |
172 | | - """ |
173 | | - callback_filepath = inspect.getfile(callback) |
174 | | - name = f"_upgrade_{uuid.uuid4().hex}" |
175 | | - mod = sys.modules[name] = import_script(callback_filepath, name=name) |
176 | | - try: |
177 | | - return getattr(mod, callback.__name__) |
178 | | - except AttributeError: |
179 | | - error_msg = ( |
180 | | - f"The converter callback `{callback.__name__}` is a nested function in `{callback.__module__}`.\n" |
181 | | - "Move it outside the `migrate()` function to make it top-level." |
182 | | - ) |
183 | | - raise MigrationError(error_msg) from None |
184 | | - |
185 | | - |
186 | 160 | class BaseConverter: |
187 | 161 | def __init__(self, callback, selector=None): |
188 | 162 | self.callback = callback |
|
0 commit comments