fd66b5c2e5ef194e286ddad190ff5c1886d3b670
[bottlenecks.git] / vstf / vstf / controller / settings / mail_settings.py
1 #!/usr/bin/env python
2 # -*- coding: utf8 -*-
3 # author: wly
4 # date: 2015-09-06
5 # see license for license details
6
7 import logging
8 import pprint
9
10 import vstf.controller.settings.settings as sets
11 import vstf.common.decorator as deco
12 from vstf.common.input import raw_choice
13
14 LOG = logging.getLogger(__name__)
15
16
17 class MailSettings(sets.Settings):
18     def __init__(self, path="/etc/vstf", filename="reporters.mail.mail-settings", mode=sets.SETS_DEFAULT):
19         super(MailSettings, self).__init__(path, filename, mode)
20
21     def _register_func(self):
22         super(MailSettings, self)._register_func()
23         body = set(
24             self._fset['body'].keys()
25         )
26         LOG.debug(body)
27         for item in body:
28             item = item.encode()
29             func_name = "set_%s" % item
30             setattr(self, func_name, self._setting_file(func_name, self._mset['body'], self._fset['body'], item))
31         other = {"attach", "content", "subtype"}
32         for item in other:
33             func_name = "mset_%s" % item
34             setattr(self, func_name, self._setting_memory(func_name, self._mset['body'], item))
35
36         LOG.debug(self.__dict__)
37
38     def sinput(self):
39         if raw_choice("if set mail server"):
40             server = self.raw_server()
41             self.set_server(server)
42
43         if raw_choice("if set mail body"):
44             body = self.raw_body()
45             self.set_body(body)
46         print "%s set finish: " % (self._filename)
47         print "+++++++++++++++++++++++++++++++++++++++++"
48         pprint.pprint(self.settings, indent=4)
49         print "+++++++++++++++++++++++++++++++++++++++++"
50
51     @deco.vstf_input("password", types=str)
52     @deco.vstf_input("username", types=str)
53     @deco.vstf_input('host', types=str)
54     def raw_server(self):
55         print "---------------------------------------"
56         print "Please vstf set mail server info like:"
57         print "    'host': 'localhost',"
58         print "    'username': 'user',['\\n' = None]"
59         print "    'password': '******',['\\n' = None]"
60         print "---------------------------------------"
61
62     @deco.vstf_input("subject", types=str, default='vstf mail')
63     @deco.vstf_input("bcc", types=list, default=[])
64     @deco.vstf_input("cc", types=list, default=[])
65     @deco.vstf_input("to", types=list, default=[])
66     @deco.vstf_input('from', types=list, default=['vstf_from@vstf.com'])
67     def raw_body(self):
68         print "----------------------------------------------------"
69         print "Please vstf set mail server info like:"
70         print "    'from': ['vstf_from@vstf.com'],"
71         print "    'to': ['vstf_to@vstf.com'],"
72         print "    'cc': ['vstf_cc@vstf.com']"
73         print "    'bcc': ['vstf_bcc@vstf.com']"
74         print "    'subject': Vstf Performance Test Report"
75         print "----------------------------------------------------"
76
77
78 def unit_test():
79     from vstf.common.log import setup_logging
80     setup_logging(level=logging.DEBUG, log_file="/var/log/vstf/vstf-mail-settings.log", clevel=logging.INFO)
81
82     mail_settings = MailSettings()
83     mail_settings.sinput()
84
85     return
86
87     mail_server = {
88         "host": "localhost",
89         "username": None,
90         "password": None
91     }
92     mail_settings.set_server(mail_server)
93
94     from_list = ['vstf_from@vstf.com']
95     mail_settings.set_from(from_list)
96     to_list = ['wangli11@huawei.com']
97     mail_settings.set_to(to_list)
98     cc_list = ['wangli11@huawei.com']
99     mail_settings.set_cc(cc_list)
100     bcc_list = ['wangli11@huawei.com']
101     mail_settings.set_bcc(bcc_list)
102     bcc_list = ['wangli11@huawei.com']
103     mail_settings.set_bcc(bcc_list)
104
105     subject = "Virtual Switching Performance Test Report"
106     mail_settings.set_subject(subject)
107
108     subtype = "plain"
109     mail_settings.mset_subtype(subtype)
110
111     attach_list = []
112     mail_settings.mset_attach(attach_list)
113
114     content = "this is a test"
115     mail_settings.mset_content(content)
116
117     LOG.info(mail_settings.settings)
118
119
120 if __name__ == '__main__':
121     unit_test()