提交 c7520b1d 编写于 作者: HansBug's avatar HansBug 😆

add new part

上级 03d84373
from .common import *
from .tree import *
from functools import wraps
from typing import Dict, Any, Union, List
from .base import BaseTree
......@@ -5,6 +6,7 @@ from ...utils import init_magic
def _to_tree_decorator(init_func):
@wraps(init_func)
def _new_init_func(data):
if isinstance(data, Tree):
_new_init_func(data.to_json())
......
from .tree import TreeValue
from functools import wraps
from ..common import BaseTree, Tree
from ...utils import init_magic
_DATA_PROPERTY = '_property__data'
_PRESERVED_PROPERTIES = {
_DATA_PROPERTY,
}
def _get_data_property(t: 'TreeValue') -> BaseTree:
return getattr(t, _DATA_PROPERTY)
def _init_decorate(init_func):
@wraps(init_func)
def _new_init_func(data):
if isinstance(data, TreeValue):
_new_init_func(_get_data_property(data))
elif isinstance(data, dict):
_new_init_func(Tree(data))
elif isinstance(data, Tree):
init_func(data)
else:
raise TypeError(
"Unknown initialization type for tree value - {type}.".format(type=repr(type(data).__name__)))
return _new_init_func
@init_magic(_init_decorate)
class TreeValue:
def __init__(self, data: BaseTree):
setattr(self, _DATA_PROPERTY, data)
def __getattr__(self, key):
if key in _PRESERVED_PROPERTIES:
return object.__getattribute__(key)
else:
return _get_data_property(self).__getitem__(key)
def __setattr__(self, key, value):
if key in _PRESERVED_PROPERTIES:
object.__setattr__(self, key, value)
else:
return _get_data_property(self).__setattr__(key, value)
def __delattr__(self, key):
if key in _PRESERVED_PROPERTIES:
object.__delattr__(self, key)
else:
return _get_data_property(self).__delattr__(key)
from .tree import TreeValue, _get_data_property
def to_json(tree: TreeValue):
return _get_data_property(tree).to_json()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册