-
Notifications
You must be signed in to change notification settings - Fork 0
/
mf2tojf2.py
48 lines (42 loc) · 1.48 KB
/
mf2tojf2.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# mf2 to jf2 converter
# licence cc0
# 2015 Kevin Marks
import logging
def flattenProperties(items):
if type(items) is list:
if len(items) <1:
return {}
if len(items)== 1:
item = items[0]
if type(item) is dict:
if item.has_key("type"):
props ={"type":'-'.join(item.get("type",["-"])[0].split("-")[1:])}
properties = item.get("properties",{})
for prop in properties:
props[prop] = flattenProperties(properties[prop])
children = item.get("children",[])
if children:
if len(children) == 1:
props["children"] =[flattenProperties(children)]
else:
props["children"] =flattenProperties(children)["children"]
return props
elif item.has_key("value"):
return item["value"]
else:
return ''
else:
return item
else:
return {"children":[flattenProperties([child]) for child in items]}
else:
return items #not a list, so string
def mf2tojf2(mf2):
"""I'm going to have to recurse here"""
jf2={}
items = mf2.get("items",[])
jf2=flattenProperties(items)
#print jf2
return jf2