Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
293 changes: 159 additions & 134 deletions checkTasks.py
Original file line number Diff line number Diff line change
@@ -1,154 +1,179 @@
import configparser, sendMail
from datetime import datetime
import logging
import logging.config

logging.basicConfig(level='DEBUG', filename='logs/app.log', filemode='a', format='%(name)s - %(levelname)s - %(asctime)s - %(message)s')
logging.config.fileConfig('logger.ini')
logger = logging.getLogger('root')

taskTypeList = ["daily", "weekly", "monthly", "weekdayofmonth", "yearly", "free"]
taskTypeList = ["daily", "weekly", "monthly", "yearly", "free"]
daysOfTheWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
monthsOfTheYear = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
"November", "December"]
occurrences = ["first", "second", "third", "fourth", "last"]

config = configparser.ConfigParser()
config.read('tasks.ini')

now = datetime.now()
day = now.strftime("%d")
month = now.strftime("%m")

def validate_task_type(tt):
if tt not in taskTypeList:
errorMessage = "Sorry, taskType should only be in {}".format(taskTypeList)
sendMail.sendNotification("Error", errorMessage)
raise ValueError(errorMessage)

def validate_weekday(wd):
if wd not in daysOfTheWeek:
errorMessage = "Sorry, Weekday should only be in {}".format(daysOfTheWeek)
sendMail.sendNotification("Error", errorMessage)
raise ValueError(errorMessage)

def validate_month(m):
if m not in monthsOfTheYear:
errorMessage = "Sorry, month should only be in {}".format(monthsOfTheYear)
sendMail.sendNotification("Error", errorMessage)
raise ValueError(errorMessage)

def validate_occurrence(o):
if o not in occurrences:
errorMessage = "Sorry, occurrence should only be in {}".format(occurrences)
sendMail.sendNotification("Error", errorMessage)
raise ValueError(errorMessage)

def validate_day(d):
try:
d = int(d)
except:
errorMessage = "day should be an int"
sendMail.sendNotification("Error", errorMessage)
raise ValueError(errorMessage)
if d < 1 or d > 31:
errorMessage = "Sorry, day should only be in [1, 31]"
sendMail.sendNotification("Error", errorMessage)
raise ValueError(errorMessage)

def extract_type(section):
return config.get(section, 'Type')

def extract_message(section):
return config.get(section, 'Message')

def extract_days(section):
return config.get(section, 'Day')

def extract_months(section):
return config.get(section, 'Month')

def extract_weekdays(section):
return config.get(section, 'Weekday')

def extract_occurrences(section):
return config.get(section, 'Occurrence')

for i in config.sections():

taskType = extract_type(i)

validate_task_type(taskType)

if taskType == 'daily':
message = extract_message(i)
logging.info("Notification triggered \"{}\" every day ".format(message))
sendMail.sendNotification(i, message)

if taskType == 'weekly':
for j in extract_weekdays(i).split(','):
validate_weekday(j)
if daysOfTheWeek.index(j) == now.weekday():
message = extract_message(i)
logging.info("Notification triggered \"{}\" every day ".format(message))
sendMail.sendNotification(i, message)
try:

if taskType == 'monthly':
for j in extract_days(i).split(','):
validate_day(j)
if day == j:
message = extract_message(i)
logging.info("Notification triggered \"{}\" every {} of the month ".format(message, j))
sendMail.sendNotification(i, message)

if taskType == 'weekdayofmonth':
for j in extract_weekdays(i).split(','):
validate_weekday(j)
for occurrence in extract_occurrences(i).split(','):
validate_occurrence(occurrence)
if daysOfTheWeek.index(j) == now.weekday():
message = extract_message(i)
if occurrence.lower() == "first" and 1 <= int(day) <= 7:
sendMail.sendNotification(i, message)
logging.info("Notification triggered \"{}\" every {} {} of the month ".format(message, occurrence, j))
elif occurrence.lower() == "second" and 8 <= int(day) <= 14:
sendMail.sendNotification(i, message)
logging.info("Notification triggered \"{}\" every {} {} of the month ".format(message, occurrence, j))
elif occurrence.lower() == "third" and 15 <= int(day) <= 21:
sendMail.sendNotification(i, message)
logging.info("Notification triggered \"{}\" every {} {} of the month ".format(message, occurrence, j))
elif occurrence.lower() == "fourth" and 22 <= int(day) <= 28:
sendMail.sendNotification(i, message)
logging.info("Notification triggered \"{}\" every {} {} of the month ".format(message, occurrence, j))
elif occurrence.lower() == "last" and 25 <= int(day) <= 31:
sendMail.sendNotification(i, message)
logging.info("Notification triggered \"{}\" every {} {} of the month ".format(message, occurrence, j))
else:
continue

if taskType == 'yearly':
m = extract_months(i)
d = extract_days(i)
if len(list(m.split(','))) > 1 or len(list(d.split(','))) > 1:
errorMessage = "Sorry, yearly task should only contain one specific date"
config = configparser.ConfigParser()
config.read('tasks.ini')
logger.debug("Reading config")


def validate_task_type(tt):
if tt not in taskTypeList:
errorMessage = "Sorry, taskType should only be in {}".format(taskTypeList)
sendMail.sendNotification("Error", errorMessage)
raise ValueError(errorMessage)


def validate_weekday(wd):
if wd not in daysOfTheWeek:
errorMessage = "Sorry, Weekday should only be in {}".format(daysOfTheWeek)
sendMail.sendNotification("Error", errorMessage)
raise ValueError(errorMessage)
validate_day(d)
validate_month(m)
if d == day and monthsOfTheYear.index(m) + 1 == int(month):


def validate_month(m):
if m not in monthsOfTheYear:
errorMessage = "Sorry, month should only be in {}".format(monthsOfTheYear)
sendMail.sendNotification("Error", errorMessage)
raise ValueError(errorMessage)


def validate_occurrence(o):
if o not in occurrences:
errorMessage = "Sorry, occurrence should only be in {}".format(occurrences)
sendMail.sendNotification("Error", errorMessage)
raise ValueError(errorMessage)


def validate_day(d):
try:
d = int(d)
except Exception as e:
errorMessage = "day should be an int"
sendMail.sendNotification("Error", errorMessage)
raise ValueError(errorMessage)
if d < 1 or d > 31:
errorMessage = "Sorry, day should only be in [1, 31]"
sendMail.sendNotification("Error", errorMessage)
raise ValueError(errorMessage)


def extract_type(section):
return config.get(section, 'Type')


def extract_message(section):
return config.get(section, 'Message')


def extract_days(section):
return config.get(section, 'Day')


def extract_months(section):
return config.get(section, 'Month')


def extract_weekdays(section):
return config.get(section, 'Weekday')


def extract_occurrences(section):
return config.get(section, 'Occurrence')


for i in config.sections():

taskType = extract_type(i)

validate_task_type(taskType)

if taskType == 'daily':
message = extract_message(i)
logging.info(
"Notification triggered \"{}\" every year, the {} of the month {}".format(message, day,
month))
logger.info("Notification triggered \"{}\" every day ".format(message))
sendMail.sendNotification(i, message)

if taskType == 'free':
for j in extract_months(i).split(','):
validate_month(j)
if monthsOfTheYear.index(j) + 1 == int(month):
for k in extract_days(i).split(','):
validate_day(k)
if day == k:
if taskType == 'weekly':
for j in extract_weekdays(i).split(','):
validate_weekday(j)
if daysOfTheWeek.index(j) == now.weekday():
message = extract_message(i)
logger.info("Notification triggered \"{}\" every day ".format(message))
sendMail.sendNotification(i, message)

if taskType == 'monthly':
for j in extract_days(i).split(','):
validate_day(j)
if day == j:
message = extract_message(i)
logger.info("Notification triggered \"{}\" every {} of the month ".format(message, j))
sendMail.sendNotification(i, message)

if taskType == 'weekdayofmonth':
for j in extract_weekdays(i).split(','):
validate_weekday(j)
for occurrence in extract_occurrences(i).split(','):
validate_occurrence(occurrence)
if daysOfTheWeek.index(j) == now.weekday():
message = extract_message(i)
logging.info(
"Notification triggered \"{}\" every {} of the months {}".format(message,
day,
month))
sendMail.sendNotification(i, message)
if occurrence.lower() == "first" and 1 <= int(day) <= 7:
sendMail.sendNotification(i, message)
logger.info(
"Notification triggered \"{}\" every {} {} of the month ".format(message, occurrence,
j))
elif occurrence.lower() == "second" and 8 <= int(day) <= 14:
sendMail.sendNotification(i, message)
logger.info(
"Notification triggered \"{}\" every {} {} of the month ".format(message, occurrence,
j))
elif occurrence.lower() == "third" and 15 <= int(day) <= 21:
sendMail.sendNotification(i, message)
logger.info(
"Notification triggered \"{}\" every {} {} of the month ".format(message, occurrence,
j))
elif occurrence.lower() == "fourth" and 22 <= int(day) <= 28:
sendMail.sendNotification(i, message)
logger.info(
"Notification triggered \"{}\" every {} {} of the month ".format(message, occurrence,
j))
elif occurrence.lower() == "last" and 25 <= int(day) <= 31:
sendMail.sendNotification(i, message)
logger.info(
"Notification triggered \"{}\" every {} {} of the month ".format(message, occurrence,
j))
else:
continue

if taskType == 'yearly':
m = extract_months(i)
d = extract_days(i)
if len(list(m.split(','))) > 1 or len(list(d.split(','))) > 1:
errorMessage = "Sorry, yearly task should only contain one specific date"
sendMail.sendNotification("Error", errorMessage)
raise ValueError(errorMessage)
validate_day(d)
validate_month(m)
if d == day and monthsOfTheYear.index(m) + 1 == int(month):
message = extract_message(i)
logger.info(
"Notification triggered \"{}\" every year, the {} of the month {}".format(message, day, month))
sendMail.sendNotification(i, message)

if taskType == 'free':
for j in extract_months(i).split(','):
validate_month(j)
if monthsOfTheYear.index(j) + 1 == int(month):
for k in extract_days(i).split(','):
validate_day(k)
if day == k:
message = extract_message(i)
logger.info(
"Notification triggered \"{}\" every {} of the months {}".format(message, day, month))
sendMail.sendNotification(i, message)
except Exception as e:
logger.error(e, exc_info=True)
28 changes: 28 additions & 0 deletions logger.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[loggers]
keys=root

[handlers];stream_handler is needed to see logs in console, file_handler to put them on logfile
keys=stream_handler,file_handler

[formatters]
keys=formatter

[logger_root]
level=DEBUG
handlers=stream_handler,file_handler

[handler_stream_handler]
class=StreamHandler
level=DEBUG
formatter=formatter
args=(sys.stderr,)

[handler_file_handler]
class=FileHandler
level=DEBUG
formatter=formatter
args=("logs/app.log", "a")

[formatter_formatter]
format=%(asctime)s %(levelname)-8s %(message)s
datefmt=