Skip to content

Commit 2bbe602

Browse files
blackm0reflashcode
authored andcommitted
beinc.py 4.4: add some minor fixes and code cleanup
1 parent 15bddbc commit 2bbe602

File tree

1 file changed

+31
-35
lines changed

1 file changed

+31
-35
lines changed

python/beinc.py

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
# -*- coding: utf-8 -*-
2-
3-
# Blackmore's Enhanced IRC-Notification Collection (BEINC) v4.3
4-
# Copyright (C) 2013-2022 Simeon Simeonov
1+
# Blackmore's Enhanced IRC-Notification Collection (BEINC)
2+
# Copyright (C) 2013-2024 Simeon Simeonov
53

64
# This program is free software: you can redistribute it and/or modify
75
# it under the terms of the GNU General Public License as published by
@@ -16,19 +14,19 @@
1614
# You should have received a copy of the GNU General Public License
1715
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1816
"""BEINC client for Weechat"""
17+
1918
import datetime
2019
import io
2120
import json
2221
import os
23-
import socket
2422
import ssl
2523
import urllib.parse
2624
import urllib.request
2725

2826
import weechat
2927

3028
__author__ = 'Simeon Simeonov'
31-
__version__ = '4.3'
29+
__version__ = '4.4'
3230
__license__ = 'GPL3'
3331

3432

@@ -178,19 +176,18 @@ def send_private_message_notification(self, values):
178176
try:
179177
title = self._fetch_formatted_str(self._pm_title_template, values)
180178
message = self._fetch_formatted_str(
181-
self._pm_message_template,
182-
values,
179+
self._pm_message_template, values
183180
)
184181
if not self._send_beinc_message(title, message) and self._debug:
185182
beinc_prnt(
186183
f'BEINC DEBUG: send_private_message_notification-ERROR '
187184
f'for "{self._name}": _send_beinc_message -> False'
188185
)
189-
except Exception as e:
186+
except Exception as exp:
190187
if self._debug:
191188
beinc_prnt(
192189
f'BEINC DEBUG: send_private_message_notification-ERROR '
193-
f'for "{self._name}": {e}'
190+
f'for "{self._name}": {exp}'
194191
)
195192

196193
def send_channel_message_notification(self, values):
@@ -210,11 +207,11 @@ def send_channel_message_notification(self, values):
210207
f'BEINC DEBUG: send_channel_message_notification-ERROR '
211208
f'for "{self._name}": _send_beinc_message -> False'
212209
)
213-
except Exception as e:
210+
except Exception as exp:
214211
if self._debug:
215212
beinc_prnt(
216213
f'BEINC DEBUG: send_channel_message_notification-ERROR '
217-
f'for "{self._name}": {e}'
214+
f'for "{self._name}": {exp}'
218215
)
219216

220217
def send_notify_message_notification(self, values):
@@ -234,11 +231,11 @@ def send_notify_message_notification(self, values):
234231
f'BEINC DEBUG: send_notify_message_notification-ERROR '
235232
f'for "{self._name}": _send_beinc_message -> False'
236233
)
237-
except Exception as e:
234+
except Exception as exp:
238235
if self._debug:
239236
beinc_prnt(
240237
f'BEINC DEBUG: send_notify_message_notification-ERROR '
241-
f'for "{self._name}": {e}'
238+
f'for "{self._name}": {exp}'
242239
)
243240

244241
def send_broadcast_notification(self, message):
@@ -256,11 +253,11 @@ def send_broadcast_notification(self, message):
256253
f'BEINC DEBUG: send_broadcast_notification-ERROR '
257254
f'for "{self._name}": _send_beinc_message -> False'
258255
)
259-
except Exception as e:
256+
except Exception as exp:
260257
if self._debug:
261258
beinc_prnt(
262259
f'BEINC DEBUG: send_broadcast_notification-ERROR '
263-
f'for "{self._name}": {e}'
260+
f'for "{self._name}": {exp}'
264261
)
265262

266263
def _context_setup(self):
@@ -282,12 +279,12 @@ def _context_setup(self):
282279
context.set_ciphers(self._ssl_ciphers)
283280
self._context = context
284281
return True
285-
except ssl.SSLError as e:
282+
except ssl.SSLError as err:
286283
if self._debug:
287-
beinc_prnt(f'BEINC DEBUG: SSL/TLS error: {e}\n')
288-
except Exception as e:
284+
beinc_prnt(f'BEINC DEBUG: SSL/TLS error: {err}\n')
285+
except Exception as exp:
289286
if self._debug:
290-
beinc_prnt(f'BEINC DEBUG: Generic context error: {e}\n')
287+
beinc_prnt(f'BEINC DEBUG: Generic context error: {exp}\n')
291288
self._context = None
292289
return False
293290

@@ -351,23 +348,23 @@ def _send_beinc_message(self, title, message):
351348
)
352349
response_dict = json.loads(response.read().decode('utf-8'))
353350
if response.code != 200:
354-
raise socket.error(response_dict.get('message', ''))
351+
raise OSError(response_dict.get('message', ''))
355352
if self._debug:
356353
beinc_prnt(
357354
"BEINC DEBUG: Server responded: "
358355
f"{response_dict.get('message')}"
359356
)
360357
self._last_message = datetime.datetime.now()
361358
return True
362-
except ssl.SSLError as e:
359+
except ssl.SSLError as err:
363360
if self._debug:
364-
beinc_prnt(f'BEINC DEBUG: SSL/TLS error: {e}\n')
365-
except socket.error as e:
361+
beinc_prnt(f'BEINC DEBUG: SSL/TLS error: {err}\n')
362+
except OSError as err:
366363
if self._debug:
367-
beinc_prnt(f'BEINC DEBUG: Connection error: {e}\n')
368-
except Exception as e:
364+
beinc_prnt(f'BEINC DEBUG: Connection error: {err}\n')
365+
except Exception as exp:
369366
if self._debug:
370-
beinc_prnt(f'BEINC DEBUG: Unable to send message: {e}\n')
367+
beinc_prnt(f'BEINC DEBUG: Unable to send message: {exp}\n')
371368
return False
372369

373370

@@ -398,7 +395,7 @@ def beinc_cmd_target_handler(cmd_tokens):
398395
if cmd_tokens[0] == 'list':
399396
beinc_prnt('--- Globals ---')
400397
for key, value in global_values.items():
401-
beinc_prnt(f'{key} -> {str(value)}')
398+
beinc_prnt(f'{key} -> {value}')
402399
beinc_prnt('--- Targets ---')
403400
for target in target_list:
404401
beinc_prnt(str(target))
@@ -539,8 +536,7 @@ def beinc_init():
539536

540537
try:
541538
beinc_config_file_str = os.path.join(
542-
weechat.info_get('weechat_dir', ''),
543-
'beinc_weechat.json',
539+
weechat.info_get('weechat_dir', ''), 'beinc_weechat.json'
544540
)
545541
beinc_prnt(f'Parsing {beinc_config_file_str}...')
546542
custom_error = 'load error'
@@ -565,8 +561,8 @@ def beinc_init():
565561
for target in config_dict['irc_client']['targets']:
566562
try:
567563
new_target = WeechatTarget(target)
568-
except Exception as e:
569-
beinc_prnt(f'Unable to add target: {e}')
564+
except Exception as exp:
565+
beinc_prnt(f'Unable to add target: {exp}')
570566
continue
571567
if new_target.channel_messages_policy:
572568
global_values['global_channel_messages_policy'] = True
@@ -577,10 +573,10 @@ def beinc_init():
577573
target_list.append(new_target)
578574
beinc_prnt(f'BEINC target "{new_target.name}" added')
579575
beinc_prnt('Done!')
580-
except Exception as e:
576+
except Exception as exp:
581577
beinc_prnt(
582578
f'ERROR: unable to parse {beinc_config_file_str}: '
583-
f'{custom_error} - {e}\nBEINC is now disabled'
579+
f'{custom_error} - {exp}\nBEINC is now disabled'
584580
)
585581
enabled = False
586582
# do not return error / exit the script
@@ -594,7 +590,7 @@ def beinc_init():
594590
__author__,
595591
__version__,
596592
__license__,
597-
'Blackmore\'s Extended IRC Notification Collection (Weechat Client)',
593+
"Blackmore's Extended IRC Notification Collection (Weechat Client)",
598594
'',
599595
'',
600596
)

0 commit comments

Comments
 (0)