0.需求產生原因
我們項目需要一個思維導圖的模塊,通過前段上傳xmind文件,后端解析xmind文件數據為json格式返回給前端,但是前端的kityminder這個插件產生的數據和后端使用的第三方包讀取數據的格式不一樣,所以有了下面的代碼.
1.xmind轉換格式
from xmindparser import xmind_to_dict
xmind_file = "/root/test.xmind"
out_file = xmind_to_dict(xmind_file) # 這個直接輸出字段格式數據
print(out_file)
xmind_file = "/root/test.xmind"
out_file = xmind_to_json(xmind_file) # 這個很奇怪他直接同級目錄生成同名.json文件.
這個第三方包很簡單直接看源碼很方便
2.前段使用的kityminder需要的格式
{"root":{"data":{"id":"c7iz2u1vbxk0","created":1606980595399,"text":"自動化測試平台"},"children":[{"data":{"id":"c7iz2vexxv40","created":1606980598366,"text":"實時系統監控"},"children":[{"data":{"id":"c7izjttyb4o0","created":1606981927111,"text":"測試項目實時數據展示"},"children":[]},{"data":{"id":"c7izjuyfpw80","created":1606981929559,"text":"內存等信息"},"children":[]}]},{"data":{"id":"c7iz2z92fgg0","created":1606980606718,"text":"測試工具"},"children":[{"data":{"id":"c7izjvqiwew0","created":1606981931257,"text":"終端產品工具"},"children":[]},{"data":{"id":"c7izjwmj0ug0","created":1606981933192,"text":"其他產品常用測試工具"},"children":[]}]},{"data":{"id":"c7izjq28a540","created":1606981918904,"text":"測試環境"},"children":[{"data":{"id":"c7izkzsh6e80","created":1606982018447,"text":"IP地址"},"children":[]},{"data":{"id":"c7izl0u6tj40","created":1606982020727,"text":"port端口"},"children":[]},{"data":{"id":"c7izl1gk4340","created":1606982022079,"text":"user用戶名"},"children":[]},{"data":{"id":"c7izl22dwcg0","created":1606982023399,"text":"PassWord"},"children":[]}]}]},"template":"default","theme":"fresh-blue","version":"1.4.43"}
3.通過1中的工具我拿到的數據格式
(通過python第三方包xmindparser讀取上傳的xmind文件拿到的數據)
[{'topic': {'title': '自動化測試平台', 'topics': [{'title': '實時監控系統', 'topics': [{'title': '測試項目實時數據展示'}, {'title': '內存等信息'}]}, {'title': '測試工具', 'topics': [{'title': '終端產品工具'}, {'title': '其它產品常用測試工具'}]}, {'title': '測試環境', 'topics': [{'title': 'IP地址'}, {'title': 'port端口號'}, {'title': 'user用戶名'}, {'title': 'PassWord'}]}]}}]
4.代碼實現
我需要將3中的格式轉換為2中前段可以識別的格式,由於我發現我現有的python知識解決不了它了(我使用了遞歸解決他,但是前段告訴我思維導圖的節點值可能重復,所以我需要一種結構來存儲它,剛好我正在學習數據結構,因此我想到了樹這種結構),我自己設計了一個樹的數據類型,將數據初始化進入樹種,之后通過遞歸遍歷樹形結構轉換獲取數據.(如果有什么好的想法可以評論,我知道我寫的這個效率應該不是很高)
import time
import json
class Tree(object):
root = None
def show(self):
if self.root != None:
self.root.show()
def templete(self, node):
templete = {
"data": {
"id": "c7izjuyfpw80",
"created": int(time.mktime(time.localtime()) * 1000),
"text": node.title
},
"children": []
}
return templete
def create_xmind(self, data_dict, node):
for child in node.children:
child_dict = self.templete(child)
data_dict["children"].append(child_dict)
self.create_xmind(child_dict, child)
return data_dict
def get_data(self):
root_dict = {
"root": None,
"template": "default",
"theme": "fresh-blue",
"version": "1.4.43"
}
data_dict = self.create_xmind(self.templete(self.root), self.root)
root_dict["root"] = data_dict
return root_dict
def init_data(self, data):
if data:
self.root = Node(data[0]["topic"]["title"])
self.add_children(self.root, data[0]["topic"]["topics"])
def add_children(self, parent, data):
for item in data:
title = item["title"]
node = Node(title)
parent.add(node)
if item.get("topics", None):
self.add_children(node, item.get("topics"))
class Node(object):
def __init__(self, title):
self.title = title
self.children = []
def add(self, node):
if self.title != None:
self.children.append(node)
def show(self):
print(self.title)
if self.children:
for item in self.children:
item.show()
d1 = [{
'topic': {
'title': '自動化測試平台',
'topics': [{
'title': '實時監控系統',
'topics': [{
'title': '測試項目實時數據展示'
}, {
'title': '內存等信息'
}]
}, {
'title': '測試工具',
'topics': [{
'title': '終端產品工具'
}, {
'title': '其它產品常用測試工具'
}]
}, {
'title': '測試環境',
'topics': [{
'title': 'IP地址'
}, {
'title': 'port端口號'
}, {
'title': 'user用戶名'
}, {
'title': 'PassWord'
}]
}]
}
}]
tree = Tree()
tree.init_data(d1)
print(json.dumps(tree.get_data()))
""" 結果
{
'root': {
'data': {
'id': 'c7izjuyfpw80',
'created': 1607061896000,
'text': '自動化測試平台'
},
'children': [{
'data': {
'id': 'c7izjuyfpw80',
'created': 1607061896000,
'text': '實時監控系統'
},
'children': [{
'data': {
'id': 'c7izjuyfpw80',
'created': 1607061896000,
'text': '測試項目實時數據展示'
},
'children': []
}, {
'data': {
'id': 'c7izjuyfpw80',
'created': 1607061896000,
'text': '內存等信息'
},
'children': []
}]
}, {
'data': {
'id': 'c7izjuyfpw80',
'created': 1607061896000,
'text': '測試工具'
},
'children': [{
'data': {
'id': 'c7izjuyfpw80',
'created': 1607061896000,
'text': '終端產品工具'
},
'children': []
}, {
'data': {
'id': 'c7izjuyfpw80',
'created': 1607061896000,
'text': '其它產品常用測試工具'
},
'children': []
}]
}, {
'data': {
'id': 'c7izjuyfpw80',
'created': 1607061896000,
'text': '測試環境'
},
'children': [{
'data': {
'id': 'c7izjuyfpw80',
'created': 1607061896000,
'text': 'IP地址'
},
'children': []
}, {
'data': {
'id': 'c7izjuyfpw80',
'created': 1607061896000,
'text': 'port端口號'
},
'children': []
}, {
'data': {
'id': 'c7izjuyfpw80',
'created': 1607061896000,
'text': 'user用戶名'
},
'children': []
}, {
'data': {
'id': 'c7izjuyfpw80',
'created': 1607061896000,
'text': 'PassWord'
},
'children': []
}]
}]
},
'template': 'default',
'theme': 'fresh-blue',
'version': '1.4.43'
}
"""