Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/openpyxl/xml/functions.py: 73%

38 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1# Copyright (c) 2010-2022 openpyxl 

2 

3""" 

4XML compatability functions 

5""" 

6 

7# Python stdlib imports 

8import re 

9from functools import partial 

10 

11from openpyxl import DEFUSEDXML, LXML 

12 

13if LXML is True: 13 ↛ 14line 13 didn't jump to line 14, because the condition on line 13 was never true

14 from lxml.etree import ( 

15 Element, 

16 SubElement, 

17 register_namespace, 

18 QName, 

19 xmlfile, 

20 XMLParser, 

21 ) 

22 from lxml.etree import fromstring, tostring 

23 # do not resolve entities 

24 safe_parser = XMLParser(resolve_entities=False) 

25 fromstring = partial(fromstring, parser=safe_parser) 

26 

27else: 

28 from xml.etree.ElementTree import ( 

29 Element, 

30 SubElement, 

31 fromstring, 

32 tostring, 

33 QName, 

34 register_namespace 

35 ) 

36 from et_xmlfile import xmlfile 

37 if DEFUSEDXML is True: 37 ↛ 40line 37 didn't jump to line 40, because the condition on line 37 was never false

38 from defusedxml.ElementTree import fromstring 

39 

40from xml.etree.ElementTree import iterparse 

41if DEFUSEDXML is True: 41 ↛ 44line 41 didn't jump to line 44, because the condition on line 41 was never false

42 from defusedxml.ElementTree import iterparse 

43 

44from openpyxl.xml.constants import ( 

45 CHART_NS, 

46 DRAWING_NS, 

47 SHEET_DRAWING_NS, 

48 CHART_DRAWING_NS, 

49 SHEET_MAIN_NS, 

50 REL_NS, 

51 VTYPES_NS, 

52 COREPROPS_NS, 

53 DCTERMS_NS, 

54 DCTERMS_PREFIX, 

55 XML_NS 

56) 

57 

58register_namespace(DCTERMS_PREFIX, DCTERMS_NS) 

59register_namespace('dcmitype', 'http://purl.org/dc/dcmitype/') 

60register_namespace('cp', COREPROPS_NS) 

61register_namespace('c', CHART_NS) 

62register_namespace('a', DRAWING_NS) 

63register_namespace('s', SHEET_MAIN_NS) 

64register_namespace('r', REL_NS) 

65register_namespace('vt', VTYPES_NS) 

66register_namespace('xdr', SHEET_DRAWING_NS) 

67register_namespace('cdr', CHART_DRAWING_NS) 

68register_namespace('xml', XML_NS) 

69 

70 

71tostring = partial(tostring, encoding="utf-8") 

72 

73NS_REGEX = re.compile("({(?P<namespace>.*)})?(?P<localname>.*)") 

74 

75def localname(node): 

76 if callable(node.tag): 76 ↛ 77line 76 didn't jump to line 77, because the condition on line 76 was never true

77 return "comment" 

78 m = NS_REGEX.match(node.tag) 

79 return m.group('localname') 

80 

81 

82def whitespace(node): 

83 if node.text != node.text.strip(): 

84 node.set("{%s}space" % XML_NS, "preserve")