-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmaterials.py
36 lines (32 loc) · 1.08 KB
/
materials.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
#!/usr/bin/env python3
from collections import defaultdict
import xml.etree.ElementTree as Et
class Material:
def __init__(self):
self.type = ''
self.title = ''
self.url = ''
self.translate_lang = ''
self.translate_url = ''
def parsexml(filepath):
tree = Et.parse(filepath)
root = tree.getroot()
materials = defaultdict(lambda: [])
for mat_desc in root:
if mat_desc.tag == 'include':
materials.update(parsexml(mat_desc.attrib['filepath']))
continue
material = Material()
material.type = mat_desc.tag
for item in mat_desc:
if item.tag == 'title':
material.title = item.text
if item.tag == 'url':
material.url = item.text
if item.tag == 'translate':
material.translate_lang = item.attrib['lang']
material.translate_url = item.attrib['url']
if item.tag == 'tags':
for tag in item:
materials[tag.text].append(material)
return materials