from __future__ import unicode_literals

import logging
import json
import re
import smtplib
import hashlib
import os


def setupLogging():
    if getConfigValue("settings", "loglevel") == "ERROR":
        logging.basicConfig(format="%(asctime)s %(message)s", level=logging.ERROR)
    else:
        logging.basicConfig(format="%(asctime)s %(message)s", level=logging.DEBUG)


def checkForVariable(mail, raw):
    varInRaw = re.findall(r"\$([A-Z]*)", raw)
    if varInRaw:
        for var in varInRaw:
            if var in mail.variables:
                raw = raw.replace("$" + var, checkForVariable(mail, mail.variables[var]))
    return raw


def imapCommand(imapmail, command, uid, *args):
    logging.debug("\t%s %s %s" % (command, uid, " ".join(args)))

    # IMAP Command caller with error handling
    if uid:
        code, ids = imapmail.uid(command, uid, *args)
    else:
        code, ids = imapmail.uid(command, *args)

    if "OK" in code:
        return [singleID.decode("utf-8") if isinstance(singleID, str) else singleID for singleID in ids]
    else:
        logging.error("Server responded with Code '%s' for '%s %s %s'." % (code, command, uid, args))
        raise OSError.ConnectionError("Server responded with Code '%s' for '%s %s %s'." % (code, command, uid, args))
        return []


def getConfigValue(*args):
    defaultsValue = json.load(open("config_default.json"))

    for path in args:
        if path not in defaultsValue:
            logging.error("Tried to access config_default.json element '%s' which doesn't exist." % ("/".join(args)))
            return
        else:
            defaultsValue = defaultsValue[path]

    if os.path.isfile("config.json"):
        userValue = json.load(open("config.json"))
        for path in args:
            if path not in userValue:
                return defaultsValue
            else:
                userValue = userValue[path]

        return userValue
    else:
        return defaultsValue


def smtpMail(to, what):
    smtpmail = smtplib.SMTP(getConfigValue("login", "smtpmail_server"))
    smtpmail.ehlo()
    smtpmail.starttls()
    smtpmail.login(getConfigValue("login", "mail_address"), getConfigValue("login", "mail_password"))
    smtpmail.sendmail(getConfigValue("login", "mail_address"), to, what)
    smtpmail.quit()


def md5sum(what):
    hashobj = hashlib.md5()
    hashobj.update(what.encode("utf-8"))
    return hashobj.hexdigest()

@@ -1396,251 +1396,168 @@
     
-for path in args:%0A        if path not in defaultsValue:%0A            logging.error(%22Tried to access config_default.json element '%25s' which doesn't exist.%22 %25 (%22/%22.join(args)))%0A            return%0A        else:%0A            defaultsValue = defaults
+if os.path.isfile(%22config.json%22):%0A        userValue = json.load(open(%22config.json%22))%0A        try:%0A            for path in args:%0A                userValue = user
 Valu
@@ -1568,54 +1568,26 @@
 th%5D%0A
-%0A
     
-if os.path.isfile(%22config.json%22):%0A
+ 
        
+return
  use
@@ -1596,80 +1596,68 @@
 alue
- = json.load(open(%22config.json%22))%0A        for path in args:%0A    
+%0A        except KeyError:%0A            pass%0A    try:%0A
         
 if p
@@ -1656,32 +1656,24 @@
     
-i
 f
+or
  path 
-not in userValue
+in args
 :%0A  
@@ -1678,35 +1678,24 @@
             
-    return 
 defaultsValu
@@ -1699,59 +1699,19 @@
 alue
-%0A            else:%0A                userValue = user
+ = defaults
 Valu
@@ -1718,17 +1718,16 @@
 e%5Bpath%5D%0A
-%0A
         
@@ -1725,36 +1725,40 @@
 %0A        return 
-user
+defaults
 Value%0A    else:%0A
@@ -1756,41 +1756,137 @@
    e
-lse:%0A        return defaultsValue
+xcept KeyError:%0A        pass%0A%0A    logging.error(%22Tried to access config value at path %25s, which doesn't exist.%22 %25 %22/%22.join(args))
 %0A%0A%0Ad

Rewrite getConfigValue, add error handling