Coverage for /home/runner/work/torchcvnn/torchcvnn/src/torchcvnn/datasets/atrnet_star/parse_xml.py: 0%

31 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-26 05:19 +0000

1# MIT License 

2 

3# Copyright (c) 2025 Rodolphe Durand 

4 

5# Permission is hereby granted, free of charge, to any person obtaining a copy 

6# of this software and associated documentation files (the "Software"), to deal 

7# in the Software without restriction, including without limitation the rights 

8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 

9# copies of the Software, and to permit persons to whom the Software is 

10# furnished to do so, subject to the following conditions: 

11 

12# The above copyright notice and this permission notice shall be included in 

13# all copies or substantial portions of the Software. 

14 

15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 

20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 

21# SOFTWARE. 

22 

23# Most of the code in this file is from : 

24# https://code.activestate.com/recipes/410469-xml-as-dictionary/ 

25 

26from typing import Any 

27from xml.etree import ElementTree 

28 

29 

30class XmlListConfig(list): 

31 def __init__(self, aList): 

32 for element in aList: 

33 if element: 

34 # treat like dict 

35 if len(element) == 1 or element[0].tag != element[1].tag: 

36 self.append(XmlDictConfig(element)) 

37 # treat like list 

38 elif element[0].tag == element[1].tag: 

39 self.append(XmlListConfig(element)) 

40 elif element.text: 

41 text = element.text.strip() 

42 if text: 

43 self.append(text) 

44 

45 

46class XmlDictConfig(dict): 

47 """ 

48 Example usage: 

49 

50 >>> tree = ElementTree.parse('your_file.xml') 

51 >>> root = tree.getroot() 

52 >>> xmldict = XmlDictConfig(root) 

53 

54 Or, if you want to use an XML string: 

55 

56 >>> root = ElementTree.XML(xml_string) 

57 >>> xmldict = XmlDictConfig(root) 

58 

59 And then use xmldict for what it is... a dict. 

60 """ 

61 

62 def __init__(self, parent_element): 

63 if parent_element.items(): 

64 self.update(dict(parent_element.items())) 

65 for element in parent_element: 

66 if element: 

67 # treat like dict - we assume that if the first two tags 

68 # in a series are different, then they are all different. 

69 if len(element) == 1 or element[0].tag != element[1].tag: 

70 aDict = XmlDictConfig(element) 

71 # treat like list - we assume that if the first two tags 

72 # in a series are the same, then the rest are the same. 

73 else: 

74 # here, we put the list in dictionary; the key is the 

75 # tag name the list elements all share in common, and 

76 # the value is the list itself 

77 aDict = {element[0].tag: XmlListConfig(element)} 

78 # if the tag has attributes, add those to the dict 

79 if element.items(): 

80 aDict.update(dict(element.items())) 

81 self.update({element.tag: aDict}) 

82 # this assumes that if you've got an attribute in a tag, 

83 # you won't be having any text. This may or may not be a 

84 # good idea -- time will tell. It works for the way we are 

85 # currently doing XML configuration files... 

86 elif element.items(): 

87 self.update({element.tag: dict(element.items())}) 

88 # finally, if there are no child tags and no attributes, extract 

89 # the text 

90 else: 

91 self.update({element.tag: element.text}) 

92 

93 

94def xml_to_dict(path: str) -> dict[str, Any]: 

95 """ 

96 Given a XML file path, returns the contents of the file as a dictionnary 

97 """ 

98 return XmlDictConfig(ElementTree.parse(path).getroot())