diff --git a/progscheduler/app.py b/progscheduler/app.py index 7222193..b3b6487 100644 --- a/progscheduler/app.py +++ b/progscheduler/app.py @@ -1,9 +1,9 @@ import os import sys -from datetime import datetime from progscheduler.jobs import open_program from progscheduler.settings.manager import Manager +from progscheduler.utils.date import Date from progscheduler.utils.log import show from progscheduler.scheduler import Scheduler @@ -71,11 +71,10 @@ def job_will_run(program): def is_time_to_stop(program_name, time_to_stop): if time_to_stop != 'off'.lower(): - now = datetime.now() - time = time_to_stop.split(':') - if now.hour >= int(time[0]) and now.minute > int(time[1]): + validate_time = Date(time=time_to_stop, time_separator=':') + if validate_time.time_greater_than_today(): show('Option: \"time-to-stop\" is enabled for \'' + program_name + '\' and it will not run. Defined time: ' + - time_to_stop + '. Current time: ' + str(now.hour).zfill(2) + ':' + str(now.minute).zfill(2)) + time_to_stop + '. Current time: ' + str(validate_time.now.hour).zfill(2) + ':' + str(validate_time.now.minute).zfill(2)) return True return False @@ -88,7 +87,7 @@ def is_scheduled_today(days_to_schedule, program_name, included_days, days, excl return False # this condition needs to be the last one - if datetime.now().strftime("%A").lower() not in days_to_schedule: + if Date.get_current_day_name() not in days_to_schedule: return False return True @@ -107,21 +106,17 @@ def is_excluded_day(program_name, excluded_days): def is_included_day(program_name, included_days, days): if check_days(program_name, included_days, ['', 'included']): - days.append(datetime.now().strftime("%A").lower()) + days.append(Date.get_current_day_name()) return True return False def check_days(program_name, days, message): for date in days: - saved_date = date.split('/') - validate_date = datetime( - day=int(saved_date[0]), - month=int(saved_date[1]), - year=int(saved_date[2]) - ) - if validate_date == datetime.combine(datetime.today().date(), datetime.min.time()): - show('Today \"' + program_name + '\" will ' + message[0] + ' run. ' + validate_date.strftime('%d/%m/%Y') + ' is an ' + message[1] + ' date.') + validate_date = Date(date=date, date_separator='/') + if validate_date.equals_to_today(): + show('Today \"' + program_name + '\" will ' + message[0] + ' run. ' + validate_date.converted_date.strftime('%d/%m/%Y') + ' is an ' + message[1] + + ' date.') return True return False diff --git a/progscheduler/settings/specific_arguments.py b/progscheduler/settings/specific_arguments.py index a949cb2..d94054a 100644 --- a/progscheduler/settings/specific_arguments.py +++ b/progscheduler/settings/specific_arguments.py @@ -1,10 +1,9 @@ import argparse -from collections import Counter -from datetime import datetime from margument.argument import Argument from margument.arguments import Arguments +from progscheduler.utils.date import Date from progscheduler.utils.directory import Directory from progscheduler.utils.log import throw, show from progscheduler.utils.reflection import get_class_variables, convert_to_dict @@ -173,12 +172,11 @@ def __validate_days(self, user_arguments): @staticmethod def __validate_dates(user_list, current_dates): for date in user_list: - user_date = date.split('/') + validate_date = Date(date=date, date_separator='/') try: - validate_date = datetime(day=int(user_date[0]), month=int(user_date[1]), year=int(user_date[2])) - if validate_date < datetime.combine(datetime.today().date(), datetime.min.time()): + if validate_date.lesser_than_today(): show('\'' + date + '\': is an old date. It will be ignored.', to_exit=True) - current_dates.append(validate_date) + current_dates.append(validate_date.converted_date) except ValueError: throw('\'' + date + '\': date not valid.') @@ -195,8 +193,17 @@ def __validate_exclude_include_dates(self, file, user_arguments): def __get_dates_list(self, file, user_arguments, name): dates = [] + + if self.include.name not in user_arguments and self.exclude.name not in user_arguments: + return dates + if self.alias.name in user_arguments and user_arguments.alias in file: try: + file_list = file[user_arguments.alias][name] + for old_date in file_list: + validate_date = Date(date=old_date, date_separator='/') + if validate_date.lesser_than_today(): + file_list.pop(file_list.index(old_date)) self.__validate_dates(file[user_arguments.alias][name], dates) except KeyError: pass diff --git a/progscheduler/utils/date.py b/progscheduler/utils/date.py new file mode 100644 index 0000000..d8503ec --- /dev/null +++ b/progscheduler/utils/date.py @@ -0,0 +1,78 @@ +from datetime import datetime + + +class Date: + + def __init__(self, date='', time='', date_separator='', time_separator=''): + self.date_str = date + self.time_str = time + self.date_separator = date_separator + self.time_separator = time_separator + self.now = datetime.now() + self.converted_date = self.convert_date() + self.converted_time = self.convert_time() + + def convert_date(self): + try: + splitted_date = self.date_str.split(self.date_separator) + self.converted_date = datetime( + day=int(splitted_date[0]), + month=int(splitted_date[1]), + year=int(splitted_date[2]), + ) + return self.converted_date + except ValueError: + pass + + def convert_time(self): + try: + splitted_time = self.time_str.split(self.time_separator) + self.converted_time = datetime( + day=datetime.min.day, + month=datetime.min.month, + year=datetime.min.year, + + hour=int(splitted_time[0]), + minute=int(splitted_time[1]), + second=int(splitted_time[2]) + ) + return self.converted_time + except ValueError: + pass + + def greater_than_today(self): + if self.converted_date is None: + return False + + if self.converted_date > datetime.combine(datetime.today().date(), self.converted_date.time()): + return True + return False + + def lesser_than_today(self): + if self.converted_date is None: + return False + + if self.converted_date < datetime.combine(datetime.today().date(), self.converted_date.time()): + return True + return False + + def equals_to_today(self): + if self.converted_date is None: + return False + + if self.converted_date == datetime.combine(datetime.today().date(), self.converted_date.time()): + return True + return False + + def time_greater_than_today(self): + if self.converted_time is None: + return False + + self.now = datetime.now() + if self.now.hour >= int(self.converted_time.hour) and self.now.minute >= int(self.converted_time.minute): + return True + return False + + @staticmethod + def get_current_day_name(): + return datetime.now().strftime("%A").lower()