Browse Source

voixicron: implement json configuration file option (closes #2)

Helmut Pozimski 7 years ago
parent
commit
b1404bcac5
1 changed files with 32 additions and 10 deletions
  1. 32 10
      voixicron.py

+ 32 - 10
voixicron.py

@@ -1,4 +1,5 @@
 #! /usr/bin/env python3
+# -*- coding: utf8 -*-
 """
 This script checks for available updates on Void Linux using xbps and notifies
 the configured administrator account about them. It currently requires
@@ -12,6 +13,8 @@ import socket
 import re
 import logging
 import sys
+import json
+import argparse
 
 from email.mime.multipart import MIMEMultipart
 from email.mime.text import MIMEText
@@ -19,26 +22,46 @@ from email.mime.text import MIMEText
 __version__ = "0.3"
 
 # Configuration parameters used to send notifications and change the logging
-# behaviour
+# behaviour. The option configuration file may contain any of the keys used
+# below in a json object.
 CONFIG = {
     "admin_email": "root",
     "smtp_server": "localhost",
     "smtp_port": 25,
     "smtp_user": "",
     "smtp_password": "",
-    "log_level": logging.ERROR,
-
+    "log_level": logging.INFO
 }
 
-AVAILABLE_UPDATES = []
-
-DEVNULL = open("/dev/null", "w")
-
 LOGGER = logging.getLogger("voixicron")
 LOGGER.setLevel(CONFIG["log_level"])
 CONSOLE_HANDLER = logging.StreamHandler()
 LOGGER.addHandler(CONSOLE_HANDLER)
 
+PARSER = argparse.ArgumentParser(description="checks for available updates on "
+                                             "Void Linux systems.")
+PARSER.add_argument("-c", "--config", help="path to json configuration file")
+ARGS = PARSER.parse_args()
+
+if ARGS.config:
+    try:
+        with open(ARGS.config, "r") as conf_file:
+            try:
+                PARSED_JSON = json.load(conf_file)
+            except ValueError:
+                LOGGER.warn("The configuration is not a valid json document, "
+                            "it will be ignored.")
+            else:
+                CONFIG.update(PARSED_JSON)
+                LOGGER.setLevel(CONFIG["log_level"])
+    except IOError as error:
+        LOGGER.warn("Configuration file could not be opened, error: %s",
+                    error.strerror)
+
+AVAILABLE_UPDATES = []
+
+DEVNULL = open("/dev/null", "w")
+
 try:
     subprocess.call(["xbps-install", "-S"], stdout=DEVNULL, stderr=DEVNULL)
 except subprocess.CalledProcessError:
@@ -122,10 +145,9 @@ if AVAILABLE_UPDATES:
                  % HOSTNAME
     MSG_TEXT = MIMEText(MAIL_TEXT.encode("utf-8"), _charset="utf-8")
     MSG.attach(MSG_TEXT)
-    SERVER_CONNECTION = smtplib.SMTP(CONFIG["smtp_server"],
-                                     CONFIG["smtp_port"])
     try:
-        SERVER_CONNECTION.connect()
+        SERVER_CONNECTION = smtplib.SMTP(CONFIG["smtp_server"],
+                                         CONFIG["smtp_port"])
     except (smtplib.SMTPConnectError, smtplib.SMTPServerDisconnected,
             socket.error):
         LOGGER.error("Failed to establish a connection to the SMTP server")