|
1 | | -""" |
2 | | -Sink module for `xml`, python's built-in function |
3 | | -""" |
4 | | - |
5 | | -import copy |
6 | | -import aikido_zen.importhook as importhook |
7 | | -from aikido_zen.helpers.logging import logger |
8 | 1 | from aikido_zen.helpers.extract_data_from_xml_body import ( |
9 | 2 | extract_data_from_xml_body, |
10 | 3 | ) |
| 4 | +from aikido_zen.helpers.get_argument import get_argument |
| 5 | +from aikido_zen.sinks import on_import, patch_function, after |
11 | 6 |
|
12 | 7 |
|
13 | | -@importhook.on_import("xml.etree.ElementTree") |
14 | | -def on_xml_import(eltree): |
15 | | - """ |
16 | | - Hook 'n wrap on `xml.etree.ElementTree`, python's built-in xml lib |
17 | | - Our goal is to create a new and mutable aikido parser class |
18 | | - Returns : Modified ElementTree object |
19 | | - """ |
20 | | - modified_eltree = importhook.copy_module(eltree) |
21 | | - copy_xml_parser = copy.deepcopy(eltree.XMLParser) |
22 | | - |
23 | | - class MutableAikidoXMLParser: |
24 | | - """Mutable connection class used to instrument `xml` by Zen""" |
25 | | - |
26 | | - def __init__(self, *args, **kwargs): |
27 | | - self._former_xml_parser = copy_xml_parser(*args, **kwargs) |
28 | | - self._feed_func_copy = copy.deepcopy(self._former_xml_parser.feed) |
29 | | - |
30 | | - def __getattr__(self, name): |
31 | | - if name != "feed": |
32 | | - return getattr(self._former_xml_parser, name) |
33 | | - |
34 | | - # Return aa function dynamically |
35 | | - def feed(data): |
36 | | - former_feed_result = self._feed_func_copy(data) |
37 | | - |
38 | | - # Fetch the data, this should just return an internal attribute |
39 | | - # and not close a stream or something that is noticable by the end-user |
40 | | - parsed_xml = self.target.close() |
41 | | - extract_data_from_xml_body(user_input=data, root_element=parsed_xml) |
42 | | - |
43 | | - return former_feed_result |
44 | | - |
45 | | - return feed |
| 8 | +@after |
| 9 | +def _feed(func, instance, args, kwargs, return_value): |
| 10 | + # Fetches XML data, this should just return an internal attribute |
| 11 | + # and not close a stream or something that is noticable by the end-user |
| 12 | + parsed_xml = instance.target.close() |
| 13 | + data = get_argument(args, kwargs, 0, "data") |
| 14 | + extract_data_from_xml_body(user_input=data, root_element=parsed_xml) |
46 | 15 |
|
47 | | - # pylint: disable=no-member |
48 | | - setattr(eltree, "XMLParser", MutableAikidoXMLParser) |
49 | | - setattr(modified_eltree, "XMLParser", MutableAikidoXMLParser) |
50 | 16 |
|
51 | | - return modified_eltree |
| 17 | +@on_import("xml.etree.ElementTree") |
| 18 | +def patch(m): |
| 19 | + patch_function(m, "XMLParser.feed", _feed) |
0 commit comments