-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtplotyamler.py
50 lines (39 loc) · 1.47 KB
/
tplotyamler.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
"""Create a yaml document out of a dictionary.
The information related to any process running in the system are parsed
out as a dictionary. These are yet to be converted to be yaml documents
to be sent out.
"""
import yaml
class ProcYaml(yaml.YAMLObject):
"""ProcYaml class for creating yaml doc from dictionaries.
"""
yaml_header = u'!ProcYaml'
namespace = {}
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
self.yml = None
def __repr__(self):
return '{k}({v})'.format(
k=self.__class__.__name__,
v=', '.join(
['='.join(
[str(item)
for item in item]) for item in self.__dict__.items()]))
def write_to_yaml(self, tag_name):
"""write_to_yaml method of ProcYaml class.
Takes the dict which is initialized in the object as
argument. Stores the dict as a yaml document in the yml
attribute.
"""
self.namespace[tag_name] = type(
tag_name,
(ProcYaml,),
{'yaml_header': u'!{n}'.format(n=tag_name)})
self.yml = yaml.dump([self.namespace[tag_name](**self.__dict__)],
default_flow_style=False)
def read_from_yaml(self, yml):
"""read_from_yaml method of ProcYaml class.
Takes the yaml document as argument and stores the resulting
dict in the yml attribute of the class.
"""
self.yml = yaml.load(yml)