|
1 | | -""" |
2 | | -Sink module for `xml`, python's built-in function |
3 | | -""" |
4 | | - |
5 | | -import copy |
6 | | -import aikido_zen.importhook as importhook |
7 | 1 | from aikido_zen.helpers.extract_data_from_xml_body import ( |
8 | 2 | extract_data_from_xml_body, |
9 | 3 | ) |
10 | | -from aikido_zen.background_process.packages import is_package_compatible, ANY_VERSION |
11 | | - |
| 4 | +from aikido_zen.helpers.get_argument import get_argument |
| 5 | +from aikido_zen.sinks import on_import, after, patch_function |
12 | 6 |
|
13 | | -@importhook.on_import("lxml.etree") |
14 | | -def on_lxml_import(eltree): |
15 | | - """ |
16 | | - Hook 'n wrap on `lxml.etree`. |
17 | | - - Wrap on fromstring() function |
18 | | - - Wrap on |
19 | | - Returns : Modified `lxml.etree` object |
20 | | - """ |
21 | | - if not is_package_compatible("lxml", required_version=ANY_VERSION): |
22 | | - return eltree |
23 | | - modified_eltree = importhook.copy_module(eltree) |
24 | 7 |
|
25 | | - former_fromstring = copy.deepcopy(eltree.fromstring) |
| 8 | +@after |
| 9 | +def _fromstring(func, instance, args, kwargs, return_value): |
| 10 | + text = get_argument(args, kwargs, 0, "text") |
| 11 | + if text: |
| 12 | + extract_data_from_xml_body(user_input=text, root_element=return_value) |
26 | 13 |
|
27 | | - def aikido_fromstring(text, *args, **kwargs): |
28 | | - res = former_fromstring(text, *args, **kwargs) |
29 | | - extract_data_from_xml_body(user_input=text, root_element=res) |
30 | | - return res |
31 | 14 |
|
32 | | - former_fromstringlist = copy.deepcopy(eltree.fromstringlist) |
| 15 | +@after |
| 16 | +def _fromstringlist(func, instance, args, kwargs, return_value): |
| 17 | + strings = get_argument(args, kwargs, 0, "strings") |
| 18 | + for text in strings: |
| 19 | + extract_data_from_xml_body(user_input=text, root_element=return_value) |
33 | 20 |
|
34 | | - def aikido_fromstringlist(strings, *args, **kwargs): |
35 | | - res = former_fromstringlist(strings, *args, **kwargs) |
36 | | - for string in strings: |
37 | | - extract_data_from_xml_body(user_input=string, root_element=res) |
38 | | - return res |
39 | 21 |
|
40 | | - # pylint: disable=no-member |
41 | | - setattr(eltree, "fromstring", aikido_fromstring) |
42 | | - setattr(modified_eltree, "fromstring", aikido_fromstring) |
43 | | - |
44 | | - # pylint: disable=no-member |
45 | | - setattr(eltree, "fromstringlist", aikido_fromstringlist) |
46 | | - setattr(modified_eltree, "fromstringlist", aikido_fromstringlist) |
47 | | - return modified_eltree |
| 22 | +@on_import("lxml.etree", "lxml") |
| 23 | +def patch(m): |
| 24 | + """ |
| 25 | + patching module lxml.etree |
| 26 | + - patches function fromstring(text, ...) |
| 27 | + - patches function fromstringlist(strings, ...) |
| 28 | + (github src: https://github.com/lxml/lxml/blob/fe271a4b5a32e6e54d10983683f2f32b0647209a/src/lxml/etree.pyx#L3411) |
| 29 | + """ |
| 30 | + patch_function(m, "fromstring", _fromstring) |
| 31 | + patch_function(m, "fromstringlist", _fromstringlist) |
0 commit comments