105 lines
2.8 KiB
Python
105 lines
2.8 KiB
Python
|
|
"""
|
|||
|
|
@project:userbuild
|
|||
|
|
@File:treeClass.py
|
|||
|
|
@IDE:PyCharm
|
|||
|
|
@Author:徐彬程
|
|||
|
|
@Date:2023/11/27 16:00
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
|
|||
|
|
class PythonCodeTreeNode:
|
|||
|
|
def __init__(self, data, parent=None, last_node=None, indentation=0, serial_number=0, extent_data=None):
|
|||
|
|
if extent_data is None:
|
|||
|
|
extent_data = {}
|
|||
|
|
self.data = data
|
|||
|
|
self.parent = parent
|
|||
|
|
self.children = []
|
|||
|
|
self.indentation = indentation
|
|||
|
|
self.extent_data = extent_data
|
|||
|
|
self.serial_number = serial_number
|
|||
|
|
self.last_node = last_node
|
|||
|
|
|
|||
|
|
|
|||
|
|
# 树结构类(非平衡非二叉树)
|
|||
|
|
class PythonCodeTreeController:
|
|||
|
|
|
|||
|
|
# 增
|
|||
|
|
@classmethod
|
|||
|
|
def add_data(cls, node):
|
|||
|
|
node.parent.children.append(node)
|
|||
|
|
|
|||
|
|
# 删
|
|||
|
|
@classmethod
|
|||
|
|
def del_data(cls):
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
# 改
|
|||
|
|
@classmethod
|
|||
|
|
def edit_data(cls):
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
# 查
|
|||
|
|
@classmethod
|
|||
|
|
def find_data(cls, root_node: PythonCodeTreeNode, i, s):
|
|||
|
|
if root_node.indentation == i and root_node.serial_number == s:
|
|||
|
|
return root_node
|
|||
|
|
for child in root_node.children:
|
|||
|
|
result = cls.find_data(child, i, s)
|
|||
|
|
if result:
|
|||
|
|
return result
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
@classmethod
|
|||
|
|
def find_data_by_path(cls, root_node, path, depth=0):
|
|||
|
|
if root_node is None:
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
components = path.split('/')
|
|||
|
|
current_node = root_node
|
|||
|
|
for component in components:
|
|||
|
|
if not component: # 跳过空的组件(例如,路径开头为 '/')
|
|||
|
|
continue
|
|||
|
|
|
|||
|
|
found = False
|
|||
|
|
for child in current_node.children:
|
|||
|
|
if child.data["name"] == component:
|
|||
|
|
current_node = child
|
|||
|
|
found = True
|
|||
|
|
break
|
|||
|
|
|
|||
|
|
if not found:
|
|||
|
|
return None # 如果未找到路径中的某个组件,返回None
|
|||
|
|
|
|||
|
|
return current_node
|
|||
|
|
|
|||
|
|
# 输出
|
|||
|
|
@classmethod
|
|||
|
|
def to_array(cls, node):
|
|||
|
|
result = {}
|
|||
|
|
if not isinstance(node, str):
|
|||
|
|
result = node.data.copy()
|
|||
|
|
result["indentation"] = node.indentation
|
|||
|
|
result["serial_number"] = node.serial_number
|
|||
|
|
result["children"] = [cls.to_array(child) for child in node.children]
|
|||
|
|
return result
|
|||
|
|
|
|||
|
|
# 输出
|
|||
|
|
@classmethod
|
|||
|
|
def to_array2(cls, node):
|
|||
|
|
result = {}
|
|||
|
|
if not isinstance(node, str):
|
|||
|
|
result = node.data.copy()
|
|||
|
|
result["indentation"] = node.indentation
|
|||
|
|
result["serial_number"] = node.serial_number
|
|||
|
|
return result
|
|||
|
|
|
|||
|
|
@classmethod
|
|||
|
|
def to_string(cls, code_json):
|
|||
|
|
# 输出当前节点的值
|
|||
|
|
if not code_json['syntax_statement'] == 'Root':
|
|||
|
|
print(" " * (code_json["syntax_indentation"] - 4) + code_json['syntax_statement'])
|
|||
|
|
|
|||
|
|
# 递归遍历子节点
|
|||
|
|
for child in code_json['children']:
|
|||
|
|
cls.to_string(child)
|