-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxml_parser.py
65 lines (56 loc) · 2.35 KB
/
xml_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
import xml.etree.ElementTree as ET
import tkinter as tk
from utilities import Utilities
class XmlParser:
"""
A class to parse XML files and extract sprite data.
Currently only supports XML files in the Starling/Sparrow format.
Attributes:
- directory: The directory where the XML file is located.
- xml_filename: The name of the XML file to parse.
- listbox_data: The Tkinter listbox to populate with extracted names.
Methods:
- get_data(): Parses the XML file and populates the listbox with names.
- extract_names(xml_root): Extracts names from the XML root element.
- get_names(names): Populates the listbox with the given names.
- parse_xml_data(file_path): Static method to parse XML data from a file and return sprite information.
"""
def __init__(self, directory, xml_filename, listbox_data):
self.directory = directory
self.xml_filename = xml_filename
self.listbox_data = listbox_data
def get_data(self):
tree = ET.parse(os.path.join(self.directory, self.xml_filename))
xml_root = tree.getroot()
names = self.extract_names(xml_root)
self.get_names(names)
def extract_names(self, xml_root):
names = set()
for subtexture in xml_root.findall(".//SubTexture"):
name = subtexture.get('name')
name = Utilities.strip_trailing_digits(name)
names.add(name)
return names
def get_names(self, names):
for name in names:
self.listbox_data.insert(tk.END, name)
@staticmethod
def parse_xml_data(file_path):
tree = ET.parse(file_path)
xml_root = tree.getroot()
sprites = [
{
'name': sprite.get('name'),
'x': int(sprite.get('x')),
'y': int(sprite.get('y')),
'width': int(sprite.get('width')),
'height': int(sprite.get('height')),
'frameX': int(sprite.get('frameX', 0)),
'frameY': int(sprite.get('frameY', 0)),
'frameWidth': int(sprite.get('frameWidth', sprite.get('width'))),
'frameHeight': int(sprite.get('frameHeight', sprite.get('height'))),
'rotated': sprite.get('rotated', 'false') == 'true'
} for sprite in xml_root.findall('SubTexture')
]
return sprites