forked from TKCERT/pfFocus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
49 lines (40 loc) · 1.33 KB
/
util.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
#!/usr/bin/env python3
from collections import OrderedDict
class DataNode(object):
@property
def data(self):
attr_filter = lambda x: not x[0].startswith('_')
data_items = filter(attr_filter, self.__dict__.items())
data = {}
for key, value in data_items:
if isinstance(value, DataNode):
data[key] = value.data
else:
data[key] = value
return data
class DataList(list, DataNode):
@property
def data(self):
data = []
for value in self:
if isinstance(value, DataNode):
data.append(value.data)
else:
data.append(value)
return data
def dict_to_dict(data, attributes):
data_items = [(attribute, data.get(attribute, '')) for attribute in attributes]
return OrderedDict(data_items)
def dict_to_list(data, attributes):
data_values = [data.get(attribute, '') for attribute in attributes]
return list(data_values)
def obj_to_dict(obj, attributes):
return dict_to_dict(obj.__dict__, attributes)
def obj_to_list(obj, attributes):
return dict_to_list(obj.__dict__, attributes)
def hasattr_r(obj, attribute):
for attr in attribute.split('.'):
if not hasattr(obj, attr):
return False
obj = getattr(obj, attr)
return True