Total Complexity | 21 |
Total Lines | 170 |
Duplicated Lines | 42.35 % |
Coverage | 0% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | """ |
||
19 | class MailOperatorEventHandler: |
||
20 | """ |
||
21 | An event handler for event were an mail must be send to the operators. |
||
22 | """ |
||
23 | |||
24 | # ------------------------------------------------------------------------------------------------------------------ |
||
25 | @staticmethod |
||
26 | def __send_mail(to, subject, body): |
||
27 | """ |
||
28 | Sends an email to the operators. |
||
29 | |||
30 | :param str|list[str] to: The email addresses of the operator(s). |
||
31 | :param str subject: The subject op the email. |
||
32 | :param str body: The email body. |
||
33 | """ |
||
34 | config = ConfigParser() |
||
35 | config.read(os.path.join(enarksh.HOME, 'etc/enarksh.cfg')) |
||
36 | |||
37 | from_email = config.get('controller', 'email') |
||
38 | |||
39 | # Concat To mail addresses |
||
40 | to_email = '' |
||
41 | if isinstance(to, list): |
||
42 | for email in to: |
||
43 | if to_email: |
||
44 | to_email += ', ' |
||
45 | to_email += email |
||
46 | else: |
||
47 | to_email = to |
||
48 | |||
49 | msg = MIMEText(body) |
||
50 | msg['Subject'] = subject |
||
51 | msg['To'] = to_email |
||
52 | msg['From'] = from_email |
||
53 | |||
54 | # Send the message via our local SMTP server. |
||
55 | s = smtplib.SMTP('localhost') |
||
|
|||
56 | s.send_message(msg) |
||
57 | s.quit() |
||
58 | |||
59 | # ------------------------------------------------------------------------------------------------------------------ |
||
60 | View Code Duplication | @staticmethod |
|
1 ignored issue
–
show
|
|||
61 | def __send_mail_simple_node_failed(rnd_id): |
||
62 | """ |
||
63 | Sends a mail to operators that a simple node has terminated with an error. |
||
64 | |||
65 | :param int rnd_id: The ID of the run node. |
||
66 | """ |
||
67 | node = DataLayer.enk_back_run_node_get_details(rnd_id) |
||
68 | operators = DataLayer.enk_back_get_operators() |
||
69 | |||
70 | if operators: |
||
71 | body = """Dear Enarksh operator, |
||
72 | |||
73 | Job {} has run unsuccessfully. |
||
74 | |||
75 | Greetings from Enarksh""".format(str(node['nod_name'], 'utf-8')) |
||
76 | |||
77 | subject = "Job of schedule {} failed".format(str(node['sch_name'], 'utf-8')) |
||
78 | |||
79 | to = [] |
||
80 | for operator in operators: |
||
81 | to.append(operator['usr_email']) |
||
82 | |||
83 | MailOperatorEventHandler.__send_mail(to, subject, body) |
||
84 | |||
85 | # ------------------------------------------------------------------------------------------------------------------ |
||
86 | View Code Duplication | @staticmethod |
|
1 ignored issue
–
show
|
|||
87 | def __send_mail_schedule_node_failed(rnd_id): |
||
88 | """ |
||
89 | Sends a mail to operators that a schedule has terminated unsuccessfully. |
||
90 | |||
91 | :param int rnd_id: The ID of the schedule. |
||
92 | """ |
||
93 | node = DataLayer.enk_back_run_node_get_details(rnd_id) |
||
94 | operators = DataLayer.enk_back_get_operators() |
||
95 | |||
96 | if operators: |
||
97 | body = """Dear Enarksh operator, |
||
98 | |||
99 | Schedule {} finished unsuccessfully. |
||
100 | |||
101 | Greetings from Enarksh""".format(str(node['sch_name'], 'utf-8')) |
||
102 | |||
103 | subject = "Schedule {} finished unsuccessfully".format(str(node['sch_name'], 'utf-8')) |
||
104 | |||
105 | to = [] |
||
106 | for operator in operators: |
||
107 | to.append(operator['usr_email']) |
||
108 | |||
109 | MailOperatorEventHandler.__send_mail(to, subject, body) |
||
110 | |||
111 | # ------------------------------------------------------------------------------------------------------------------ |
||
112 | View Code Duplication | @staticmethod |
|
1 ignored issue
–
show
|
|||
113 | def __send_mail_schedule_node_success(rnd_id): |
||
114 | """ |
||
115 | Sends a mail to operators that a schedule has terminated successfully. |
||
116 | |||
117 | :param int rnd_id: The ID of the schedule. |
||
118 | """ |
||
119 | node = DataLayer.enk_back_run_node_get_details(rnd_id) |
||
120 | operators = DataLayer.enk_back_get_operators() |
||
121 | |||
122 | if operators: |
||
123 | body = """Dear Enarksh operator, |
||
124 | |||
125 | Schedule {} finished successfully. |
||
126 | |||
127 | Greetings from Enarksh""".format(str(node['sch_name'], 'utf-8')) |
||
128 | |||
129 | subject = "Schedule {} finished successfully".format(str(node['sch_name'], 'utf-8')) |
||
130 | |||
131 | to = [] |
||
132 | for operator in operators: |
||
133 | to.append(operator['usr_email']) |
||
134 | |||
135 | MailOperatorEventHandler.__send_mail(to, subject, body) |
||
136 | |||
137 | # ------------------------------------------------------------------------------------------------------------------ |
||
138 | @staticmethod |
||
139 | def __handle_simple_node_stop(_event, event_data, _listener_data): |
||
140 | """ |
||
141 | Handles the termination of a simple node. |
||
142 | |||
143 | :param * _event: Not used. |
||
144 | :param tuple[dict] event_data: The old and new node status. |
||
145 | :param * _listener_data: Not used. |
||
146 | """ |
||
147 | del _event, _listener_data |
||
148 | |||
149 | if event_data[1]['rst_id'] == enarksh.ENK_RST_ID_ERROR: |
||
150 | MailOperatorEventHandler.__send_mail_simple_node_failed(event_data[1]['rnd_id']) |
||
151 | |||
152 | # ------------------------------------------------------------------------------------------------------------------ |
||
153 | @staticmethod |
||
154 | def __handle_schedule_stop(_event, event_data, _listener_data): |
||
155 | """ |
||
156 | Handles the termination of a schedule node. |
||
157 | |||
158 | :param * _event: Not used. |
||
159 | :param tuple[dict] event_data: The old and new node status. |
||
160 | :param * _listener_data: Not used. |
||
161 | """ |
||
162 | del _event, _listener_data |
||
163 | |||
164 | # If status is error send mail. |
||
165 | if event_data[1]['rst_id'] == enarksh.ENK_RST_ID_ERROR: |
||
166 | MailOperatorEventHandler.__send_mail_schedule_node_failed(event_data[1]['rnd_id']) |
||
167 | |||
168 | # If status is success send mail. |
||
169 | if event_data[1]['rst_id'] == enarksh.ENK_RST_ID_COMPLETED: |
||
170 | MailOperatorEventHandler.__send_mail_schedule_node_success(event_data[1]['rnd_id']) |
||
171 | |||
172 | # ------------------------------------------------------------------------------------------------------------------ |
||
173 | @staticmethod |
||
174 | def handle_node_creation(_event, node, _listener_data): |
||
175 | """ |
||
176 | Handles a node creation event. |
||
177 | |||
178 | :param * _event : Not used. |
||
179 | :param enarksh.controller.node.Node.Node node: The created node. |
||
180 | :param * _listener_data: Not used. |
||
181 | """ |
||
182 | del _event, _listener_data |
||
183 | |||
184 | if isinstance(node, SimpleNode): |
||
185 | node.event_state_change.register_listener(MailOperatorEventHandler.__handle_simple_node_stop) |
||
186 | |||
187 | if isinstance(node, ScheduleNode): |
||
188 | node.event_state_change.register_listener(MailOperatorEventHandler.__handle_schedule_stop) |
||
189 | |||
192 |
This check looks for invalid names for a range of different identifiers.
You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.
If your project includes a Pylint configuration file, the settings contained in that file take precedence.
To find out more about Pylint, please refer to their site.