9
9
import traceback
10
10
from concurrent .futures import ThreadPoolExecutor
11
11
from pathlib import Path
12
- from typing import Optional , Union
12
+ from typing import Optional , TYPE_CHECKING , Union
13
13
14
14
import requests
15
15
24
24
25
25
logger = logging .getLogger (__name__ )
26
26
27
+ if TYPE_CHECKING :
28
+ from .reporters import ConfigReportersList
29
+ from .storage import ConfigReportEmail , ConfigReportEmailSmtp , ConfigReportTelegram , ConfigReportXmpp
30
+
27
31
28
32
class UrlwatchCommand :
29
33
def __init__ (self , urlwatcher : Urlwatch ) -> None :
@@ -89,7 +93,8 @@ def edit_hooks(self) -> int:
89
93
print (f'Saved edits in { self .urlwatch_config .hooks } ' )
90
94
return 0
91
95
92
- def show_features (self ) -> int :
96
+ @staticmethod
97
+ def show_features () -> int :
93
98
print (f'Please see full documentation at { __docs_url__ } ' )
94
99
print ()
95
100
print ('Supported jobs:\n ' )
@@ -310,9 +315,9 @@ def edit_config(self) -> int:
310
315
return result
311
316
312
317
def check_telegram_chats (self ) -> None :
313
- config = self .urlwatcher .config_storage .config ['report' ]. get ( 'telegram' )
318
+ config : ConfigReportTelegram = self .urlwatcher .config_storage .config ['report' ][ 'telegram' ]
314
319
315
- bot_token = config . get ( 'bot_token' )
320
+ bot_token = config [ 'bot_token' ]
316
321
if not bot_token :
317
322
print ('You need to set up your bot token first (see documentation)' )
318
323
self ._exit (1 )
@@ -354,8 +359,8 @@ def check_test_reporter(self) -> None:
354
359
print (f'\n Supported reporters:\n { ReporterBase .reporter_documentation ()} \n ' )
355
360
self ._exit (1 )
356
361
357
- cfg = self .urlwatcher .config_storage .config ['report' ]. get ( name , { 'enabled' : False })
358
- if not cfg . get ( 'enabled' , False ) :
362
+ cfg : ConfigReportersList = self .urlwatcher .config_storage .config ['report' ][ name ] # type: ignore[misc]
363
+ if not cfg [ 'enabled' ] :
359
364
print (f'Reporter is not enabled/configured: { name } ' )
360
365
print (f'Use { __project_name__ } --edit-config to configure reporters' )
361
366
self ._exit (1 )
@@ -427,38 +432,38 @@ def set_error(job_state: 'JobState', message: str) -> JobState:
427
432
self ._exit (0 )
428
433
429
434
def check_smtp_login (self ) -> None :
430
- config = self .urlwatcher .config_storage .config ['report' ]['email' ]
431
- smtp_config = config ['smtp' ]
435
+ config : ConfigReportEmail = self .urlwatcher .config_storage .config ['report' ]['email' ]
436
+ smtp_config : ConfigReportEmailSmtp = config ['smtp' ]
432
437
433
438
success = True
434
439
435
- if not config . get ( 'enabled' ) :
440
+ if not config [ 'enabled' ] :
436
441
print ('Please enable e-mail reporting in the config first.' )
437
442
success = False
438
443
439
- if config . get ( 'method' ) != 'smtp' :
444
+ if config [ 'method' ] != 'smtp' :
440
445
print ('Please set the method to SMTP for the e-mail reporter.' )
441
446
success = False
442
447
443
- smtp_auth = smtp_config . get ( 'auth' )
448
+ smtp_auth = smtp_config [ 'auth' ]
444
449
if not smtp_auth :
445
450
print ('Authentication must be enabled for SMTP.' )
446
451
success = False
447
452
448
- smtp_hostname = smtp_config . get ( 'host' )
453
+ smtp_hostname = smtp_config [ 'host' ]
449
454
if not smtp_hostname :
450
455
print ('Please configure the SMTP hostname in the config first.' )
451
456
success = False
452
457
453
- smtp_username = smtp_config . get ( 'user' ) or config ['from' ]
458
+ smtp_username = smtp_config [ 'user' ] or config ['from' ]
454
459
if not smtp_username :
455
460
print ('Please configure the SMTP user in the config first.' )
456
461
success = False
457
462
458
463
if not success :
459
464
self ._exit (1 )
460
465
461
- insecure_password = smtp_config . get ( 'insecure_password' )
466
+ insecure_password = smtp_config [ 'insecure_password' ]
462
467
if insecure_password :
463
468
print ('The SMTP password is set in the config file (key "insecure_password")' )
464
469
elif smtp_have_password (smtp_hostname , smtp_username ):
@@ -468,8 +473,8 @@ def check_smtp_login(self) -> None:
468
473
else :
469
474
smtp_set_password (smtp_hostname , smtp_username )
470
475
471
- smtp_port = smtp_config . get ( 'port' )
472
- smtp_tls = smtp_config . get ( 'starttls' )
476
+ smtp_port = smtp_config [ 'port' ]
477
+ smtp_tls = smtp_config [ 'starttls' ]
473
478
474
479
mailer = SMTPMailer (smtp_username , smtp_hostname , smtp_port , smtp_tls , smtp_auth , insecure_password )
475
480
print ('Trying to log into the SMTP server...' )
@@ -479,20 +484,20 @@ def check_smtp_login(self) -> None:
479
484
self ._exit (0 )
480
485
481
486
def check_xmpp_login (self ) -> None :
482
- xmpp_config = self .urlwatcher .config_storage .config ['report' ]['xmpp' ]
487
+ xmpp_config : ConfigReportXmpp = self .urlwatcher .config_storage .config ['report' ]['xmpp' ]
483
488
484
489
success = True
485
490
486
491
if not xmpp_config ['enabled' ]:
487
492
print ('Please enable XMPP reporting in the config first.' )
488
493
success = False
489
494
490
- xmpp_sender = xmpp_config . get ( 'sender' )
495
+ xmpp_sender = xmpp_config [ 'sender' ]
491
496
if not xmpp_sender :
492
497
print ('Please configure the XMPP sender in the config first.' )
493
498
success = False
494
499
495
- if not xmpp_config . get ( 'recipient' ) :
500
+ if not xmpp_config [ 'recipient' ] :
496
501
print ('Please configure the XMPP recipient in the config first.' )
497
502
success = False
498
503
0 commit comments