<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Fetches email account information from the GleSYS email API.

Compares the account disk usage ratio to a given threshold and
notifies about account(s) that are over the thresshold, via STDOUT or
email to the account in question.

MIT license applies, see the file COPYING.

TODO:
  - Write configmaker tool
  - Add configuration setting to include all accounts in fetch.
  - Move mail subject to configuration.
  - Move mail body to configuration.
  - Move "information" string to configuration, "XX av YY (ZZ%)"
  - More verbose exception handling.
  - Whitelist and domain lists; clean empty records.

<NAME> <<EMAIL>>
<NAME>, KAOS <<EMAIL>>
"""
from os.path import isfile
import sys
import ConfigParser
import json
import httplib2
from urllib import urlencode
import smtplib
from email.mime.text import MIMEText
from time import strftime

if isfile("config.ini") is False:
    print '''Abort: No configuration file found.
             Copy config.ini.exemple to config.ini and edit it.
             Optionally you can use ./makeconfig.py to edit the config file
             in place.'''
    sys.exit(1)

CONFIG = ConfigParser.RawConfigParser()
CONFIG.read("config.ini")

if len(CONFIG.sections()) != 4:
    print "Configuration not correct"
    sys.exit(1)

DEBUGMODE = False
try:
    DEBUGMODE = CONFIG.getboolean("Other", "debug")
except ConfigParser.NoOptionError:
    pass

TALKATIVE = False
try:
    TALKATIVE = CONFIG.getboolean("Other", "verbose")
except ConfigParser.NoOptionError:
    pass

def debuginfo(outstring):
    """If debug mode is enabled in config.ini section Other a more verbose
    information flow will be used when parsing the quota info list.
    All messages will be prepended with 'DEBUG: '.

    Arguments:
        outstring (str): The informational message to be printed to STDOUT.
    """
    if DEBUGMODE is True:
        print "DEBUG: %s" % outstring

debuginfo("Fetching API settings.")
try:
    APIURL = CONFIG.get("API", "URL")
    APIUSER = CONFIG.get("API", "user")
    APIKEY = CONFIG.get("API", "key")
except ConfigParser.NoOptionError:
    print "Everything in API config section must be set, no defaults available."
    sys.exit(4)

debuginfo("Fetching domain information.")
try:
    DOMAINNAME = [x.strip() for x in CONFIG.get("Quota", "domainname").split(",")]
except ConfigParser.NoOptionError:
    print """Either a domain name or a comma separated list of domains must be
    provided in Quota section. No default suggestions."""
    sys.exit(4)
MAXRATIO = 84.9
try:
    MAXRATIO = CONFIG.getfloat("Quota", "ratio")
except ValueError:
    print "Ambigous option. Can not parse to float."
    sys.exit(4)
except ConfigParser.NoOptionError:
    debuginfo("Will use 84.9 as default ratio, config missing.")

try:
    WHITELIST = [x.strip() for x in CONFIG.get("Quota", "whitelist").split(",")]
    debuginfo("Whitelist contains %d addresses." % len(WHITELIST))
except ConfigParser.NoOptionError:
    debuginfo("No addresses found in whitelist.")

debuginfo("Fetching mail settings.")<commit_msg>FEATURE: Make sure APIURL have a slash at the end.<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Fetches email account information from the GleSYS email API.

Compares the account disk usage ratio to a given threshold and
notifies about account(s) that are over the thresshold, via STDOUT or
email to the account in question.

MIT license applies, see the file COPYING.

TODO:
  - Write configmaker tool
  - Add configuration setting to include all accounts in fetch.
  - Move mail subject to configuration.
  - Move mail body to configuration.
  - Move "information" string to configuration, "XX av YY (ZZ%)"
  - More verbose exception handling.
  - Whitelist and domain lists; clean empty records.

<NAME> <<EMAIL>>
<NAME>, KAOS <<EMAIL>>
"""
from os.path import isfile
import sys
import ConfigParser
import json
import httplib2
from urllib import urlencode
import smtplib
from email.mime.text import MIMEText
from time import strftime

if isfile("config.ini") is False:
    print '''Abort: No configuration file found.
             Copy config.ini.exemple to config.ini and edit it.
             Optionally you can use ./makeconfig.py to edit the config file
             in place.'''
    sys.exit(1)

CONFIG = ConfigParser.RawConfigParser()
CONFIG.read("config.ini")

if len(CONFIG.sections()) != 4:
    print "Configuration not correct"
    sys.exit(1)

DEBUGMODE = False
try:
    DEBUGMODE = CONFIG.getboolean("Other", "debug")
except ConfigParser.NoOptionError:
    pass

TALKATIVE = False
try:
    TALKATIVE = CONFIG.getboolean("Other", "verbose")
except ConfigParser.NoOptionError:
    pass

def debuginfo(outstring):
    """If debug mode is enabled in config.ini section Other a more verbose
    information flow will be used when parsing the quota info list.
    All messages will be prepended with 'DEBUG: '.

    Arguments:
        outstring (str): The informational message to be printed to STDOUT.
    """
    if DEBUGMODE is True:
        print "DEBUG: %s" % outstring

debuginfo("Fetching API settings.")
try:
    APIURL = CONFIG.get("API", "URL")
    if APIURL.endswith("/") is False:
        debuginfo("APIURL must end with /, will add it runtime.")
        APIURL += "/"
    APIUSER = CONFIG.get("API", "user")
    APIKEY = CONFIG.get("API", "key")
except ConfigParser.NoOptionError:
    print "Everything in API config section must be set, no defaults available."
    sys.exit(4)

debuginfo("Fetching domain information.")
try:
    DOMAINNAME = [x.strip() for x in CONFIG.get("Quota", "domainname").split(",")]
except ConfigParser.NoOptionError:
    print """Either a domain name or a comma separated list of domains must be
    provided in Quota section. No default suggestions."""
    sys.exit(4)
MAXRATIO = 84.9
try:
    MAXRATIO = CONFIG.getfloat("Quota", "ratio")
except ValueError:
    print "Ambigous option. Can not parse to float."
    sys.exit(4)
except ConfigParser.NoOptionError:
    debuginfo("Will use 84.9 as default ratio, config missing.")

try:
    WHITELIST = [x.strip() for x in CONFIG.get("Quota", "whitelist").split(",")]
    debuginfo("Whitelist contains %d addresses." % len(WHITELIST))
except ConfigParser.NoOptionError:
    debuginfo("No addresses found in whitelist.")

debuginfo("Fetching mail settings.")
{'fraction_non_alphanumeric': 0.10378395352488616, 'fraction_numerical': 0.004396294551734966, 'mean_word_length': 3.6769456681350956, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1483906', 'n_tokens_mistral': 1891, 'n_tokens_neox': 1853, 'n_words': 709}