Browse Source

voixicron: change configuration to dictionary

Helmut Pozimski 7 years ago
parent
commit
4b8efc69bd
1 changed files with 22 additions and 20 deletions
  1. 22 20
      voixicron.py

+ 22 - 20
voixicron.py

@@ -16,27 +16,26 @@ import sys
 from email.mime.multipart import MIMEMultipart
 from email.mime.text import MIMEText
 
-__version__ = "0.2"
+__version__ = "0.3"
 
-# Email server to be used to send the notification
-ADMIN_EMAIL = "root"
-# Mail server to be used to send the notification
-MAIL_SERVER = "localhost"
-# Port to use on the mail server
-MAIL_SERVER_PORT = 25
-# User name to log in to the mail server
-MAIL_SERVER_USER = ""
-# Password to log in to the mail server
-MAIL_SERVER_PASSWORD = ""
-# Log level for the stderr/stdout output
-LOG_LEVEL = logging.ERROR
+# Configuration parameters used to send notifications and change the logging
+# behaviour
+CONFIG = {
+    "admin_email": "root",
+    "smtp_server": "localhost",
+    "smtp_port": 25,
+    "smtp_user": "",
+    "smtp_password": "",
+    "log_level": logging.ERROR,
+
+}
 
 AVAILABLE_UPDATES = []
 
 DEVNULL = open("/dev/null", "w")
 
 LOGGER = logging.getLogger("voixicron")
-LOGGER.setLevel(LOG_LEVEL)
+LOGGER.setLevel(CONFIG["log_level"])
 CONSOLE_HANDLER = logging.StreamHandler()
 LOGGER.addHandler(CONSOLE_HANDLER)
 
@@ -112,7 +111,7 @@ if AVAILABLE_UPDATES:
     MSG["Subject"] = "%s updates available on host %s"\
                      % (len(AVAILABLE_UPDATES), HOSTNAME)
     MSG["From"] = "voixicron@%s" % HOSTNAME
-    MSG["To"] = ADMIN_EMAIL
+    MSG["To"] = CONFIG["admin_email"]
     for update in AVAILABLE_UPDATES:
         MAIL_TEXT += "%s (%s -> %s)\n"\
                      % (update["name"],
@@ -123,7 +122,8 @@ if AVAILABLE_UPDATES:
                  % HOSTNAME
     MSG_TEXT = MIMEText(MAIL_TEXT.encode("utf-8"), _charset="utf-8")
     MSG.attach(MSG_TEXT)
-    SERVER_CONNECTION = smtplib.SMTP(MAIL_SERVER, MAIL_SERVER_PORT)
+    SERVER_CONNECTION = smtplib.SMTP(CONFIG["smtp_server"],
+                                     CONFIG["smtp_port"])
     try:
         SERVER_CONNECTION.connect()
     except (smtplib.SMTPConnectError, smtplib.SMTPServerDisconnected,
@@ -134,16 +134,18 @@ if AVAILABLE_UPDATES:
             SERVER_CONNECTION.starttls()
         except smtplib.SMTPException:
             pass
-        if MAIL_SERVER_USER:
+        if CONFIG["smtp_user"]:
             try:
-                SERVER_CONNECTION.login(MAIL_SERVER_USER, MAIL_SERVER_PASSWORD)
+                SERVER_CONNECTION.login(CONFIG["smtp_user"],
+                                        CONFIG["smtp_password"])
             except smtplib.SMTPAuthenticationError:
                 LOGGER.error("Authentication on the mail server with user %s "
-                             "failed.", MAIL_SERVER_USER)
+                             "failed.", CONFIG["smtp_user"])
             except smtplib.SMTPException:
                 LOGGER.error("Error during authentication on the mail server.")
         try:
-            SERVER_CONNECTION.sendmail("voixicron@%s" % HOSTNAME, ADMIN_EMAIL,
+            SERVER_CONNECTION.sendmail("voixicron@%s" % HOSTNAME,
+                                       CONFIG["admin_email"],
                                        MSG.as_string())
         except smtplib.SMTPRecipientsRefused:
             LOGGER.error("The mail server refused the recipient.")