-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml_parser.py
More file actions
75 lines (64 loc) · 2.24 KB
/
Copy pathxml_parser.py
File metadata and controls
75 lines (64 loc) · 2.24 KB
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
66
67
68
69
70
71
72
73
74
75
import xmltodict
def get_xml_from_path(path):
"""
Create dictionary with the following items:
name - Puzzle name
width - Puzzle width
height - Puzzle height
colors - List of RGB values [str]
paths - dictionary where the keys are the colors and the values are
lists of lists of paths in the key color
{ RED : [[(i1,j1), (i2,j2)...], [(i3,j3), (i4, j4)...]...]}
:param path: Path to xml file
:return: The above dictionary
"""
with open(path, 'rb') as file:
my_dict = dict(xmltodict.parse(file)['puzzle'])
ret_dict = {
'name': my_dict['header']['properties']['text']['#text'],
'width': int(my_dict['data']['dimensions']['@width']),
'height': int(my_dict['data']['dimensions']['@height']),
'colors': [],
'paths': dict()
}
color_dict = my_dict['data']['palette']['color']
color_list = create_colors_list(color_dict)
ret_dict['colors'] = color_list
path_dict = my_dict['data']['solution']['path']
paths_dict = create_paths_dict(path_dict, color_list)
ret_dict['paths'] = paths_dict
return ret_dict
def create_paths_dict(paths_dict, color_list):
"""
Creates a dictionary {"color" : [paths]}
:param color_list:
:param paths_dict:
:return:
"""
result = {i: [] for i in range(len(color_list))}
for i in range(len(paths_dict)):
str_path = paths_dict[i]['#text']
color = int(paths_dict[i]['@color'])
result[color].append(create_tuple_path(str_path))
return result
def create_tuple_path(str_path):
"""
Gets a path as a string - for example : '1 2 3 4' and creates a list of tuples.
:param str_path:
:return: a list of tuples - [(1,2), (3,4)]
"""
result = []
path_list = str_path.split()
for i in range(0, len(path_list), 2):
result.append((int(path_list[i + 1]), int(path_list[i])))
return result
def create_colors_list(color_dict):
"""
extract the colors from their section in the xml file, and puts them in a list
:param color_dict:
:return: the list of the colors
"""
ret = []
for i in range(len(color_dict)):
ret.append('#' + color_dict[i]['@rgb'])
return ret