-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.py
More file actions
125 lines (92 loc) · 2.75 KB
/
parse.py
File metadata and controls
125 lines (92 loc) · 2.75 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# encoding=utf8
import json
import re
from pprint import pprint
import sys
reload(sys)
sys.setdefaultencoding('utf8')
class OpsParser:
debug = False
grades = {}
subjects = []
parent_map = []
meta = {}
def debug(self, level, str):
print ' ' * (level * 3) + str
def parse_grades(self, items):
"""Parse grades"""
self.debug(0, "Vuosiluokat")
for item in items:
name = item['nimi']['fi']
item['name'] = name # Default name for faster access
self.debug(1, name)
self.grades[int(item['id'])] = item
def parse_subjects(self, items):
"""Parse subjects"""
self.debug(0, "Oppiaineet")
for item in items:
name = item['nimi']['fi']
item['name'] = name
self.subjects.append(item)
self.debug(1, name)
# Loop target areas
if "kohdealueet" in item:
self.debug(2, "Kohdealueet:")
for area in item['kohdealueet']:
self.debug(3, area['nimi']['fi'])
# Loop target areas
if "oppimaarat" in item:
self.debug(2, "Oppimäärät:")
for tmp in item['oppimaarat']:
self.debug(3, tmp['nimi']['fi'])
# Get grades material
if 'vuosiluokkakokonaisuudet' in item:
for info in item['vuosiluokkakokonaisuudet']:
# Print target grades
self.debug(2, self.grades[int(info['_vuosiluokkaKokonaisuus'])]['name'])
if info['sisaltoalueinfo']:
self.debug(3, info['sisaltoalueinfo']['otsikko']['fi'])
# Targets
self.debug(2, "Tavoitteet:")
for target in info['tavoitteet']:
self.debug(3, re.sub('<[^<]+?>', '', target['tavoite']['fi']))
# Content areas
self.debug(2, "Sisältöalueet:")
for area in info['sisaltoalueet']:
self.debug(3, area['nimi']['fi'])
def parse_metadata(self, items):
"""Parse metadata"""
self.debug(0, "Laaja-alaiset osaamiset:")
for item in items:
name = item['nimi']['fi']
item['name'] = name
self.debug(1, name)
self.meta[item['id']] = item
def parse_tree(self, items, depth=0):
self.debug(0, "Suunnitelma:")
self.recurse(items)
def recurse(self, items, depth=0):
for item in items:
self.debug(depth + 1 , item['perusteenOsa']['nimi']['fi'])
if 'lapset' in item:
self.recurse(item['lapset'], depth+1)
def get_subjects(self):
"""Returns list of subjects"""
pass
def get_grades(self):
"""Returns list of grades"""
pass
def get_subjects_by_grade(self, id):
"""Returns list of subjects"""
pass
def main():
with open('eperusteet.json') as file:
data = json.load(file)
# Reader
p = OpsParser()
p.parse_grades(data['perusopetus']['vuosiluokkakokonaisuudet'])
p.parse_subjects(data['perusopetus']['oppiaineet'])
p.parse_metadata(data['perusopetus']['laajaalaisetosaamiset'])
p.parse_tree(data['perusopetus']['sisalto']['lapset'])
if __name__ == "__main__":
main()