|
1 | 1 | # -*- coding: utf-8 -*-
|
2 |
| -import hashlib |
3 |
| -import inspect |
4 | 2 | import logging
|
5 | 3 | import re
|
6 |
| -import sys |
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,29 +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_{hashlib.sha256(callback_filepath.encode()).hexdigest()}" |
175 |
| - if name not in sys.modules: |
176 |
| - sys.modules[name] = import_script(callback_filepath, name=name) |
177 |
| - try: |
178 |
| - return getattr(sys.modules[name], callback.__name__) |
179 |
| - except AttributeError: |
180 |
| - error_msg = ( |
181 |
| - f"The converter callback `{callback.__name__}` is a nested function in `{callback.__module__}`.\n" |
182 |
| - "Move it outside the `migrate()` function to make it top-level." |
183 |
| - ) |
184 |
| - raise MigrationError(error_msg) from None |
185 |
| - |
186 |
| - |
187 | 160 | class BaseConverter:
|
188 | 161 | def __init__(self, callback, selector=None):
|
189 | 162 | self.callback = callback
|
|
0 commit comments