-
Notifications
You must be signed in to change notification settings - Fork 1
/
children.py
70 lines (51 loc) · 2.37 KB
/
children.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class _ObjInfoChildren:
def __init__(self):
self.filters = [self._default_filter]
@staticmethod
def _default_filter(objInfo):
return not objInfo.internal() and not objInfo.from_builtin()
def filters_check(self, objInfo):
""" Check all filter funcs in self.filters.
:param generallibrary.ObjInfo self:
:param objInfo: """
return all([func(objInfo) for func in self.filters])
def get_attrs(self, depth=1, _all_objInfo=None, _top_objInfo=None):
""" Generate attributes for this ObjInfo's obj.
Uses filters of top objInfo (First given).
Can generate recursively based on depth.
Existing attribute ObjInfos will be replaced as the name key is unique.
Returns a list of all generated ObjInfos' identifiers.
:param generallibrary.ObjInfo self:
:param depth: Depth to iterate, -1 is infinite.
:param _all_objInfo:
:param _filters:
:rtype: list[generallibrary.ObjInfo] """
if not depth:
return
if _all_objInfo is None:
_all_objInfo = []
_all_objInfo.append(self.identifier())
if _top_objInfo is None:
_top_objInfo = self
# for name in getattr(self.obj, "__dict__", {}).keys():
for name in dir(self.obj):
if name in ("__dict__", ):
continue
sentinel = object()
attr = getattr(self.obj, name, sentinel)
if attr is sentinel or not self.check_if_parent_eligible(parent_obj=self.obj, child_obj=attr, name=name):
continue
objInfo = self.ObjInfo(obj=attr, parent=self, name=name)
if _top_objInfo.filters_check(objInfo):
if objInfo.identifier() not in _all_objInfo:
objInfo.get_attrs(depth=depth - 1, _all_objInfo=_all_objInfo, _top_objInfo=_top_objInfo)
else:
objInfo.remove()
return _all_objInfo
# def __iter__(self): # Todo: Define TreeDiagram.__iter__ as well, but without generation.
# """ Generate attributes with a depth of 1 and returns a list of direct children.
#
# :param generallibrary.ObjInfo self:
# :rtype: collections.Iterable[generallibrary.ObjInfo] """
# self.get_attrs()
# return self.get_children()