From 441fe33aa6cb897ce38565cfe48c9beae51a5279 Mon Sep 17 00:00:00 2001 From: Paola351 Date: Sat, 10 Oct 2020 12:28:00 +0200 Subject: [PATCH 01/14] added validations --- checkTasks.py | 91 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 65 insertions(+), 26 deletions(-) diff --git a/checkTasks.py b/checkTasks.py index eccafa8..4024597 100644 --- a/checkTasks.py +++ b/checkTasks.py @@ -1,8 +1,10 @@ import configparser, sendMail from datetime import datetime +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"] +monthsOfTheYear = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", + "November", "December"] config = configparser.ConfigParser() config.read('tasks.ini') @@ -11,29 +13,66 @@ day = now.strftime("%d") month = now.strftime("%m") + +def validate_task_type(tt): + if tt not in taskTypeList: + raise ValueError("Sorry, taskType should only be in {}" % taskTypeList) + + +def validate_weekday(wd): + if wd not in daysOfTheWeek: + raise ValueError("Sorry, Weekday should only be in {}" % daysOfTheWeek) + + +def validate_month(m): + if m not in monthsOfTheYear: + raise ValueError("Sorry, month should only be in {}" % monthsOfTheYear) + + +def validate_day(d): + try: + d = int(d) + except: + raise ValueError("day should be an int") + if d < 1 or d > 31: + raise ValueError("Sorry, day should only be in [1, 31]") + + for i in config.sections(): - taskType = config.get(i, 'Type') - - if taskType == 'daily': - sendMail.sendNotification(i, config.get(i, 'Message')) - - if taskType == 'weekly': - for j in config.get(i, 'Weekday').split(','): - if daysOfTheWeek.index(j) == now.weekday(): - sendMail.sendNotification(i, config.get(i, 'Message')) - - if taskType == 'monthly': - for j in config.get(i, 'Day').split(','): - if day == j: - sendMail.sendNotification(i, config.get(i, 'Message')) - - if taskType == 'yearly': - if config.get(i, 'Day') == day and monthsOfTheYear.index(config.get(i, 'Month')) + 1 == int(month): - sendMail.sendNotification(i, config.get(i, 'Message')) - - if taskType == 'free': - for j in config.get(i, 'Month').split(','): - if monthsOfTheYear.index(j) + 1 == int(month): - for k in config.get(i, 'Day').split(','): - if day == k: - sendMail.sendNotification(i, config.get(i, 'Message')) + + taskType = config.get(i, 'Type') + validate_task_type(taskType) + + if taskType == 'daily': + sendMail.sendNotification(i, config.get(i, 'Message')) + + if taskType == 'weekly': + for j in config.get(i, 'Weekday').split(','): + validate_weekday(j) + if daysOfTheWeek.index(j) == now.weekday(): + sendMail.sendNotification(i, config.get(i, 'Message')) + + if taskType == 'monthly': + for j in config.get(i, 'Day').split(','): + validate_day(j) + if day == j: + sendMail.sendNotification(i, config.get(i, 'Message')) + + if taskType == 'yearly': + month = config.get(i, 'Month') + day = config.get(i, 'Day') + if len(month) > 1 or len(day) > 1: + raise ValueError("Sorry, yearly task should only contain one specific date") + validate_day(day) + validate_month(month) + if day == day and monthsOfTheYear.index(month) + 1 == int(month): + sendMail.sendNotification(i, config.get(i, 'Message')) + + if taskType == 'free': + for j in config.get(i, 'Month').split(','): + validate_month(j) + if monthsOfTheYear.index(j) + 1 == int(month): + for k in config.get(i, 'Day').split(','): + validate_day(k) + if day == k: + sendMail.sendNotification(i, config.get(i, 'Message')) From 6887635369e1f039e0fea5579a46053a187cccd3 Mon Sep 17 00:00:00 2001 From: Paola351 Date: Sat, 10 Oct 2020 12:43:02 +0200 Subject: [PATCH 02/14] added logging --- checkTasks.py | 60 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 12 deletions(-) diff --git a/checkTasks.py b/checkTasks.py index 4024597..83c5ede 100644 --- a/checkTasks.py +++ b/checkTasks.py @@ -1,5 +1,6 @@ import configparser, sendMail from datetime import datetime +import logging taskTypeList = ["daily", "weekly", "monthly", "yearly", "free"] daysOfTheWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] @@ -38,41 +39,76 @@ def validate_day(d): raise ValueError("Sorry, day should only be in [1, 31]") +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') + + for i in config.sections(): - taskType = config.get(i, 'Type') + taskType = extract_type(i) validate_task_type(taskType) if taskType == 'daily': - sendMail.sendNotification(i, config.get(i, 'Message')) + message = extract_message(i) + logging.info("Everything is OK! :D I'll remember you \"{}\" every day " % message) + sendMail.sendNotification(i, message) if taskType == 'weekly': - for j in config.get(i, 'Weekday').split(','): + for j in extract_weekdays(i).split(','): validate_weekday(j) if daysOfTheWeek.index(j) == now.weekday(): - sendMail.sendNotification(i, config.get(i, 'Message')) + message = extract_message(i) + logging.info("Everything is OK! :D I'll remember you \"{}\" every day ".format(message)) + sendMail.sendNotification(i, message) if taskType == 'monthly': - for j in config.get(i, 'Day').split(','): + for j in extract_days(i).split(','): validate_day(j) if day == j: - sendMail.sendNotification(i, config.get(i, 'Message')) + message = extract_message(i) + logging.info("Everything is OK! :D I'll remember you \"{}\" every {} of the month ".format(message, j)) + sendMail.sendNotification(i, message) if taskType == 'yearly': - month = config.get(i, 'Month') - day = config.get(i, 'Day') + month = extract_months(i) + day = extract_days(i) if len(month) > 1 or len(day) > 1: raise ValueError("Sorry, yearly task should only contain one specific date") validate_day(day) validate_month(month) if day == day and monthsOfTheYear.index(month) + 1 == int(month): - sendMail.sendNotification(i, config.get(i, 'Message')) + message = extract_message(i) + logging.info( + "Everything is OK! :D I'll remember you \"{}\" every year, the {} of the month {}".format(message, day, + month)) + sendMail.sendNotification(i, message) if taskType == 'free': - for j in config.get(i, 'Month').split(','): + for j in extract_months(i).split(','): validate_month(j) if monthsOfTheYear.index(j) + 1 == int(month): - for k in config.get(i, 'Day').split(','): + for k in extract_days(i).split(','): validate_day(k) if day == k: - sendMail.sendNotification(i, config.get(i, 'Message')) + message = extract_message(i) + logging.info( + "Everything is OK! :D I'll remember you \"{}\" every {} of the months {}".format(message, + day, + month)) + sendMail.sendNotification(i, message) From 9978245eefcd7b4e2f25e3c0320e067ee0599093 Mon Sep 17 00:00:00 2001 From: Paola351 Date: Sat, 10 Oct 2020 13:36:58 +0200 Subject: [PATCH 03/14] fixed checks --- checkTasks.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/checkTasks.py b/checkTasks.py index 4024597..8fc65ec 100644 --- a/checkTasks.py +++ b/checkTasks.py @@ -16,17 +16,17 @@ def validate_task_type(tt): if tt not in taskTypeList: - raise ValueError("Sorry, taskType should only be in {}" % taskTypeList) + raise ValueError("Sorry, taskType should only be in {}".format(taskTypeList)) def validate_weekday(wd): if wd not in daysOfTheWeek: - raise ValueError("Sorry, Weekday should only be in {}" % daysOfTheWeek) + raise ValueError("Sorry, Weekday should only be in {}" .format(daysOfTheWeek)) def validate_month(m): if m not in monthsOfTheYear: - raise ValueError("Sorry, month should only be in {}" % monthsOfTheYear) + raise ValueError("Sorry, month should only be in {}",format(monthsOfTheYear)) def validate_day(d): @@ -61,7 +61,7 @@ def validate_day(d): if taskType == 'yearly': month = config.get(i, 'Month') day = config.get(i, 'Day') - if len(month) > 1 or len(day) > 1: + if len(list(month.split(','))) > 1 or len(list(day.split(','))) > 1: raise ValueError("Sorry, yearly task should only contain one specific date") validate_day(day) validate_month(month) From 601092ea66d9610dd6e4650b89984983d69f3136 Mon Sep 17 00:00:00 2001 From: Paola351 Date: Sat, 10 Oct 2020 14:26:45 +0200 Subject: [PATCH 04/14] fixed comments --- checkTasks.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/checkTasks.py b/checkTasks.py index 8fc65ec..99f80ad 100644 --- a/checkTasks.py +++ b/checkTasks.py @@ -21,12 +21,12 @@ def validate_task_type(tt): def validate_weekday(wd): if wd not in daysOfTheWeek: - raise ValueError("Sorry, Weekday should only be in {}" .format(daysOfTheWeek)) + raise ValueError("Sorry, Weekday should only be in {}".format(daysOfTheWeek)) def validate_month(m): if m not in monthsOfTheYear: - raise ValueError("Sorry, month should only be in {}",format(monthsOfTheYear)) + raise ValueError("Sorry, month should only be in {}".format(monthsOfTheYear)) def validate_day(d): @@ -59,13 +59,13 @@ def validate_day(d): sendMail.sendNotification(i, config.get(i, 'Message')) if taskType == 'yearly': - month = config.get(i, 'Month') - day = config.get(i, 'Day') - if len(list(month.split(','))) > 1 or len(list(day.split(','))) > 1: + m = config.get(i, 'Month') + d = config.get(i, 'Day') + if len(list(month.split(','))) > 1 or len(list(d.split(','))) > 1: raise ValueError("Sorry, yearly task should only contain one specific date") - validate_day(day) - validate_month(month) - if day == day and monthsOfTheYear.index(month) + 1 == int(month): + validate_day(d) + validate_month(m) + if day == d and monthsOfTheYear.index(m) + 1 == int(month): sendMail.sendNotification(i, config.get(i, 'Message')) if taskType == 'free': From 6cdfff92d78ee5cef303014245c3f06152107053 Mon Sep 17 00:00:00 2001 From: Paola351 Date: Sat, 10 Oct 2020 14:28:12 +0200 Subject: [PATCH 05/14] merged with master --- checkTasks.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/checkTasks.py b/checkTasks.py index da9f594..45e137d 100644 --- a/checkTasks.py +++ b/checkTasks.py @@ -22,12 +22,12 @@ def validate_task_type(tt): def validate_weekday(wd): if wd not in daysOfTheWeek: - raise ValueError("Sorry, Weekday should only be in {}" .format(daysOfTheWeek)) + raise ValueError("Sorry, Weekday should only be in {}".format(daysOfTheWeek)) def validate_month(m): if m not in monthsOfTheYear: - raise ValueError("Sorry, month should only be in {}",format(monthsOfTheYear)) + raise ValueError("Sorry, month should only be in {}".format(monthsOfTheYear)) def validate_day(d): @@ -86,13 +86,13 @@ def extract_weekdays(section): sendMail.sendNotification(i, message) if taskType == 'yearly': - month = extract_months(i) - day = extract_days(i) - if len(list(month.split(','))) > 1 or len(list(day.split(','))) > 1: + m = extract_months(i) + d = extract_days(i) + if len(list(m.split(','))) > 1 or len(list(d.split(','))) > 1: raise ValueError("Sorry, yearly task should only contain one specific date") - validate_day(day) - validate_month(month) - if day == day and monthsOfTheYear.index(month) + 1 == int(month): + validate_day(d) + validate_month(m) + if d == day and monthsOfTheYear.index(m) + 1 == int(month): message = extract_message(i) logging.info( "Everything is OK! :D I'll remember you \"{}\" every year, the {} of the month {}".format(message, day, From 283d0284ad6f0160ce041c06a05fb1ef3713c389 Mon Sep 17 00:00:00 2001 From: Paola351 Date: Sat, 10 Oct 2020 16:17:56 +0200 Subject: [PATCH 06/14] fixed log message --- checkTasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/checkTasks.py b/checkTasks.py index 45e137d..5913b1b 100644 --- a/checkTasks.py +++ b/checkTasks.py @@ -66,7 +66,7 @@ def extract_weekdays(section): if taskType == 'daily': message = extract_message(i) - logging.info("Everything is OK! :D I'll remember you \"{}\" every day " % message) + logging.info("Everything is OK! :D I'll remember you \"{}\" every day ".format(message)) sendMail.sendNotification(i, message) if taskType == 'weekly': From 57a0e629ce5b6d2a00ab3ceea1beeccfb74b1f60 Mon Sep 17 00:00:00 2001 From: Paola351 Date: Sat, 10 Oct 2020 16:36:19 +0200 Subject: [PATCH 07/14] added logfile --- checkTasks.py | 2 ++ logs/.gitkeep | 0 2 files changed, 2 insertions(+) create mode 100644 logs/.gitkeep diff --git a/checkTasks.py b/checkTasks.py index 5913b1b..00cc084 100644 --- a/checkTasks.py +++ b/checkTasks.py @@ -2,6 +2,8 @@ from datetime import datetime import logging +logging.basicConfig(level='DEBUG', filename='logs/app.log', filemode='a', format='%(name)s - %(levelname)s - %(asctime)s - %(message)s') + 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", diff --git a/logs/.gitkeep b/logs/.gitkeep new file mode 100644 index 0000000..e69de29 From b33f63dc8c7035799e0140b21f6c492db6795b1d Mon Sep 17 00:00:00 2001 From: error401de <34032466+error401de@users.noreply.github.com> Date: Sat, 10 Oct 2020 16:55:08 +0200 Subject: [PATCH 08/14] Update tasks.ini --- tasks.ini | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tasks.ini b/tasks.ini index 73aaa4d..340e5c4 100644 --- a/tasks.ini +++ b/tasks.ini @@ -12,14 +12,14 @@ ;Day = 10 ;Message = Sends a notification on a specific day of the month. You can set multiple days comma separated (e.g. 1,15). -[Yearly test task] -Type = yearly -Day = 10 -Month = November -Message = A notification will be triggered on a specific day each year +;[Yearly test task] +;Type = yearly +;Day = 10 +;Month = November +;Message = A notification will be triggered on a specific day each year ;[Free test task] ;Type = free ;Day = 1,10 ;Month = January,October -;Message = A notification will be send in all specified months at the configured days \ No newline at end of file +;Message = A notification will be send in all specified months at the configured days From af92cb7a8db1e51ab321c964f8e51d3952aaa80a Mon Sep 17 00:00:00 2001 From: error401de <34032466+error401de@users.noreply.github.com> Date: Sat, 10 Oct 2020 16:56:26 +0200 Subject: [PATCH 09/14] Update tasks.ini --- tasks.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks.ini b/tasks.ini index 340e5c4..0054da5 100644 --- a/tasks.ini +++ b/tasks.ini @@ -15,7 +15,7 @@ ;[Yearly test task] ;Type = yearly ;Day = 10 -;Month = November +;Month = October ;Message = A notification will be triggered on a specific day each year ;[Free test task] From ff5510debb009b3f59f39b4c87de9a76f7e78ab6 Mon Sep 17 00:00:00 2001 From: error401de <34032466+error401de@users.noreply.github.com> Date: Sat, 10 Oct 2020 16:56:42 +0200 Subject: [PATCH 10/14] Update tasks.ini From 6b45607149dc34f0122aaf56c5b1e20c854f14b4 Mon Sep 17 00:00:00 2001 From: Paola351 Date: Sat, 10 Oct 2020 17:01:59 +0200 Subject: [PATCH 11/14] added loggerfile --- checkTasks.py | 171 ++++++++++++++++++++++++++------------------------ logger.ini | 28 +++++++++ 2 files changed, 116 insertions(+), 83 deletions(-) create mode 100644 logger.ini diff --git a/checkTasks.py b/checkTasks.py index 00cc084..5711485 100644 --- a/checkTasks.py +++ b/checkTasks.py @@ -1,116 +1,121 @@ 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('consoleHandler') -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"] +try: -config = configparser.ConfigParser() -config.read('tasks.ini') + 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"] -now = datetime.now() -day = now.strftime("%d") -month = now.strftime("%m") + 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: - raise ValueError("Sorry, taskType should only be in {}".format(taskTypeList)) + def validate_task_type(tt): + if tt not in taskTypeList: + raise ValueError("Sorry, taskType should only be in {}".format(taskTypeList)) -def validate_weekday(wd): - if wd not in daysOfTheWeek: - raise ValueError("Sorry, Weekday should only be in {}".format(daysOfTheWeek)) + def validate_weekday(wd): + if wd not in daysOfTheWeek: + raise ValueError("Sorry, Weekday should only be in {}".format(daysOfTheWeek)) -def validate_month(m): - if m not in monthsOfTheYear: - raise ValueError("Sorry, month should only be in {}".format(monthsOfTheYear)) + def validate_month(m): + if m not in monthsOfTheYear: + raise ValueError("Sorry, month should only be in {}".format(monthsOfTheYear)) -def validate_day(d): - try: - d = int(d) - except: - raise ValueError("day should be an int") - if d < 1 or d > 31: - raise ValueError("Sorry, day should only be in [1, 31]") + def validate_day(d): + try: + d = int(d) + except: + raise ValueError("day should be an int") + if d < 1 or d > 31: + raise ValueError("Sorry, day should only be in [1, 31]") -def extract_type(section): - return config.get(section, 'Type') + def extract_type(section): + return config.get(section, 'Type') -def extract_message(section): - return config.get(section, 'Message') + def extract_message(section): + return config.get(section, 'Message') -def extract_days(section): - return config.get(section, 'Day') + def extract_days(section): + return config.get(section, 'Day') -def extract_months(section): - return config.get(section, 'Month') + def extract_months(section): + return config.get(section, 'Month') -def extract_weekdays(section): - return config.get(section, 'Weekday') + def extract_weekdays(section): + return config.get(section, 'Weekday') -for i in config.sections(): - taskType = extract_type(i) - validate_task_type(taskType) + for i in config.sections(): - if taskType == 'daily': - message = extract_message(i) - logging.info("Everything is OK! :D I'll remember you \"{}\" every day ".format(message)) - sendMail.sendNotification(i, message) + taskType = extract_type(i) + validate_task_type(taskType) - 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("Everything is OK! :D I'll remember you \"{}\" every day ".format(message)) - sendMail.sendNotification(i, message) + if taskType == 'daily': + message = extract_message(i) + logger.info("Everything is OK! :D I'll remember you \"{}\" every day ".format(message)) + sendMail.sendNotification(i, message) - if taskType == 'monthly': - for j in extract_days(i).split(','): - validate_day(j) - if day == j: + 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("Everything is OK! :D I'll remember you \"{}\" 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("Everything is OK! :D I'll remember you \"{}\" every {} of the month ".format(message, j)) + sendMail.sendNotification(i, message) + + if taskType == 'yearly': + m = extract_months(i) + d = extract_days(i) + if len(list(m.split(','))) > 1 or len(list(d.split(','))) > 1: + raise ValueError("Sorry, yearly task should only contain one specific date") + validate_day(d) + validate_month(m) + if d == day and monthsOfTheYear.index(m) + 1 == int(month): message = extract_message(i) - logging.info("Everything is OK! :D I'll remember you \"{}\" every {} of the month ".format(message, j)) + logger.info( + "Everything is OK! :D I'll remember you \"{}\" every year, the {} of the month {}".format(message, day, + month)) sendMail.sendNotification(i, message) - if taskType == 'yearly': - m = extract_months(i) - d = extract_days(i) - if len(list(m.split(','))) > 1 or len(list(d.split(','))) > 1: - raise ValueError("Sorry, yearly task should only contain one specific date") - validate_day(d) - validate_month(m) - if d == day and monthsOfTheYear.index(m) + 1 == int(month): - message = extract_message(i) - logging.info( - "Everything is OK! :D I'll remember you \"{}\" 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) - logging.info( - "Everything is OK! :D I'll remember you \"{}\" every {} of the months {}".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( + "Everything is OK! :D I'll remember you \"{}\" every {} of the months {}".format(message, + day, + month)) + sendMail.sendNotification(i, message) +except Exception as e: + logger.error(e, exc_info=True) diff --git a/logger.ini b/logger.ini new file mode 100644 index 0000000..e60ef4f --- /dev/null +++ b/logger.ini @@ -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= \ No newline at end of file From 640f6c4dc88a74ea12c07a61bc42cfe7409f70b1 Mon Sep 17 00:00:00 2001 From: Paola351 Date: Sat, 10 Oct 2020 18:15:55 +0200 Subject: [PATCH 12/14] merged from original main --- checkTasks.py | 162 +++++++++++++++++++++++++------------------------- 1 file changed, 80 insertions(+), 82 deletions(-) diff --git a/checkTasks.py b/checkTasks.py index e0ea317..dac12b1 100644 --- a/checkTasks.py +++ b/checkTasks.py @@ -6,22 +6,20 @@ logger = logging.getLogger('consoleHandler') 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"] - -try: +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"] +now = datetime.now() +day = now.strftime("%d") +month = now.strftime("%m") +try: 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) @@ -82,84 +80,84 @@ def extract_occurrences(section): for i in config.sections(): - taskType = extract_type(i) + taskType = extract_type(i) - validate_task_type(taskType) + 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) - - 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 == 'daily': + message = extract_message(i) + logging.info("Notification triggered \"{}\" every day ".format(message)) + 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 taskType == 'weekly': + for j in extract_weekdays(i).split(','): + validate_weekday(j) 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" - 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) - logging.info( - "Notification triggered \"{}\" every year, the {} of the month {}".format(message, day, - month)) - sendMail.sendNotification(i, message) + logging.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 == '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) - 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) + 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" + 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) + logging.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) + logging.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) From cb16aaee325872402535f1843c341363beeb2ed4 Mon Sep 17 00:00:00 2001 From: Paola351 Date: Sat, 10 Oct 2020 18:21:03 +0200 Subject: [PATCH 13/14] fixed indentation --- checkTasks.py | 51 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/checkTasks.py b/checkTasks.py index dac12b1..567a29d 100644 --- a/checkTasks.py +++ b/checkTasks.py @@ -20,25 +20,27 @@ config = configparser.ConfigParser() config.read('tasks.ini') + 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) + 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) + 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) + sendMail.sendNotification("Error", errorMessage) + raise ValueError(errorMessage) + def validate_occurrence(o): if o not in occurrences: @@ -50,7 +52,7 @@ def validate_occurrence(o): def validate_day(d): try: d = int(d) - except: + except Exception as e: errorMessage = "day should be an int" sendMail.sendNotification("Error", errorMessage) raise ValueError(errorMessage) @@ -63,21 +65,27 @@ def validate_day(d): 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) @@ -114,19 +122,29 @@ def extract_occurrences(section): 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)) + 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)) + 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)) + 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)) + 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)) + logging.info( + "Notification triggered \"{}\" every {} {} of the month ".format(message, occurrence, + j)) else: continue @@ -142,10 +160,9 @@ def extract_occurrences(section): if d == day and monthsOfTheYear.index(m) + 1 == int(month): message = extract_message(i) logging.info( - "Notification triggered \"{}\" every year, the {} of the month {}".format(message, day, - month)) + "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) @@ -155,9 +172,7 @@ def extract_occurrences(section): if day == k: message = extract_message(i) logging.info( - "Notification triggered \"{}\" every {} of the months {}".format(message, - day, - month)) + "Notification triggered \"{}\" every {} of the months {}".format(message, day, month)) sendMail.sendNotification(i, message) except Exception as e: logger.error(e, exc_info=True) From bfa065c350c5825150f32ddc6210ff537cfd6ea0 Mon Sep 17 00:00:00 2001 From: Paola351 Date: Sat, 10 Oct 2020 22:27:16 +0200 Subject: [PATCH 14/14] added logging through logger.ini --- checkTasks.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/checkTasks.py b/checkTasks.py index 567a29d..a682a5d 100644 --- a/checkTasks.py +++ b/checkTasks.py @@ -3,7 +3,7 @@ import logging.config logging.config.fileConfig('logger.ini') -logger = logging.getLogger('consoleHandler') +logger = logging.getLogger('root') taskTypeList = ["daily", "weekly", "monthly", "yearly", "free"] daysOfTheWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] @@ -19,6 +19,7 @@ config = configparser.ConfigParser() config.read('tasks.ini') + logger.debug("Reading config") def validate_task_type(tt): @@ -94,7 +95,7 @@ def extract_occurrences(section): if taskType == 'daily': message = extract_message(i) - logging.info("Notification triggered \"{}\" every day ".format(message)) + logger.info("Notification triggered \"{}\" every day ".format(message)) sendMail.sendNotification(i, message) if taskType == 'weekly': @@ -102,7 +103,7 @@ def extract_occurrences(section): validate_weekday(j) if daysOfTheWeek.index(j) == now.weekday(): message = extract_message(i) - logging.info("Notification triggered \"{}\" every day ".format(message)) + logger.info("Notification triggered \"{}\" every day ".format(message)) sendMail.sendNotification(i, message) if taskType == 'monthly': @@ -110,7 +111,7 @@ def extract_occurrences(section): validate_day(j) if day == j: message = extract_message(i) - logging.info("Notification triggered \"{}\" every {} of the month ".format(message, j)) + logger.info("Notification triggered \"{}\" every {} of the month ".format(message, j)) sendMail.sendNotification(i, message) if taskType == 'weekdayofmonth': @@ -122,27 +123,27 @@ def extract_occurrences(section): message = extract_message(i) if occurrence.lower() == "first" and 1 <= int(day) <= 7: sendMail.sendNotification(i, message) - logging.info( + 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) - logging.info( + 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) - logging.info( + 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) - logging.info( + 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) - logging.info( + logger.info( "Notification triggered \"{}\" every {} {} of the month ".format(message, occurrence, j)) else: @@ -159,7 +160,7 @@ def extract_occurrences(section): validate_month(m) if d == day and monthsOfTheYear.index(m) + 1 == int(month): message = extract_message(i) - logging.info( + logger.info( "Notification triggered \"{}\" every year, the {} of the month {}".format(message, day, month)) sendMail.sendNotification(i, message) @@ -171,7 +172,7 @@ def extract_occurrences(section): validate_day(k) if day == k: message = extract_message(i) - logging.info( + logger.info( "Notification triggered \"{}\" every {} of the months {}".format(message, day, month)) sendMail.sendNotification(i, message) except Exception as e: