def calculate_rrule_exclusions(start_date, end_date, exclusion_commands, settings): exclusion_ruleset = rruleset() exclusion_types = exclusion_commands.exclusionList for exclusion_type in exclusion_types: if hasattr(exclusion_type, "exclusionRange"): from_date, _ = convert_date_time(exclusion_type.exclusionRange.fromDateTime, settings) to_date, _ = convert_date_time(exclusion_type.exclusionRange.toDateTime, settings) exclusion_ruleset.rrule(rrule(freq=DAILY, dtstart=from_date, until=to_date)) elif hasattr(exclusion_type, "exclusionDateTime"): real_date, _ = convert_date_time(exclusion_type.exclusionDateTime, settings) exclusion_ruleset.rrule(rrule(freq=DAILY, dtstart=real_date, until=real_date)) elif hasattr(exclusion_type, "exclusionMacro"): macro_value = exclusion_type.exclusionMacro exclusion_rule = EXCLUSION_MAP[macro_value]['rule'](start=start_date, end=end_date) exclusion_ruleset.rrule(exclusion_rule) else: # in that case, I have no idea what this is (the parser should have caught it) so just # raise an error or something raise UnknownExclusionTypeError matched_dates = list(exclusion_ruleset.between(after=start_date, before=end_date, inc=True)) return len(matched_dates)
def do_subtraction(command, settings): date_time_1, output_format_1 = convert_date_time(command.dateTime1, settings) date_time_2, output_format_2 = convert_date_time(command.dateTime2, settings) # In a moment of madness, we've decided to allow operands in a date from date # subtraction. It's much easier to process these first. if hasattr(command, "operandList1"): for operand in command.operandList1: date_time_1 = delta_arithmetic(date_time_1, operand) if hasattr(command, "operandList2"): for operand in command.operandList2: date_time_2 = delta_arithmetic(date_time_2, operand) return normalised_days(command, date_time_1, date_time_2)
def do_timespans(command, settings): date_time, output_format = convert_date_time(command.dateTime, settings) for operand in command.operandList: date_time = delta_arithmetic(date_time, operand) return date_time.strftime(output_format)
def do_timespans(command, settings): date_time, output_format = convert_date_time(command.dateTime, settings) # Note the very first date in case there is an exclusion rule first_date_time = date_time for operand in command.operandList: date_time = delta_arithmetic(date_time, operand) last_date_time = date_time # Make sure the dates are the right way around, or the # following functions will not work correctly. first_date_time, last_date_time = later_date_last(first_date_time, last_date_time) if hasattr(command, "exclusionCommands"): rules = rruleset() exclusion_rules = build_exclusion_list(command, first_date_time, last_date_time, settings) for rule in exclusion_rules: rules.rrule(rule) days_to_exclude = len(rules.between(first_date_time, last_date_time, True)) operand = relativedelta(days=days_to_exclude) date_time += operand return date_time.strftime(output_format)
def do_formats(command, settings): date_time, _ = convert_date_time(command.dateTime, settings) if command.dateFormat.lower() in DATE_FORMATTERS_MAP: # noinspection PyCallingNonCallable return DATE_FORMATTERS_MAP[command.dateFormat.lower()](date_time) else: return "Invalid function . . . "
def do_subtraction(command, settings): date_time_1, output_format_1 = convert_date_time(command.dateTime1, settings) date_time_2, output_format_2 = convert_date_time(command.dateTime2, settings) # In a moment of madness, we've decided to allow operands in a date from date # subtraction. It's much easier to process these first. if hasattr(command, "operandList1"): for operand in command.operandList1: date_time_1 = delta_arithmetic(date_time_1, operand) if hasattr(command, "operandList2"): for operand in command.operandList2: date_time_2 = delta_arithmetic(date_time_2, operand) return normalised_days(command, date_time_1, date_time_2)
def do_formats(command, settings): date_time, _ = convert_date_time(command.dateTime, settings) if command.dateFormat.lower() in DATE_FORMATTERS_MAP: # noinspection PyCallingNonCallable return DATE_FORMATTERS_MAP[command.dateFormat.lower()](date_time) else: return "Invalid function . . . "
def do_timespans(command, settings): date_time, output_format = convert_date_time(command.dateTime, settings) original_date_time = date_time for operand in command.operandList: date_time = delta_arithmetic(date_time, operand) date_time = exclusion_check(original_date_time, date_time, command, settings) return date_time.strftime(output_format)
def build_exclusion_list(command, date_time_1, date_time_2, settings): """ If the user has build an exclusion list then we process it here :param command: :return: a set of rules that denote the exclusion """ exclusion_rules = [] if hasattr(command, "exclusionCommands"): for exclusion_command in command.exclusionCommands: # process the exclusion list for exclusion_item in exclusion_command.exclusionList: if hasattr(exclusion_item, "exclusionMacro"): start_date_time, end_date_time = later_date_last(date_time_1, date_time_2) # noinspection PyCallingNonCallable new_rule = DATE_EXCLUSION_RULES_MAP[exclusion_item.exclusionMacro](start=start_date_time, end=end_date_time) elif hasattr(exclusion_item, "exclusionDateTime"): date_time, _ = convert_date_time(exclusion_item.exclusionDateTime, settings) new_rule = rrule(freq=DAILY, dtstart=date_time, until=date_time) elif hasattr(exclusion_item, "exclusionRange"): exclusion_date_1, _ = convert_date_time(exclusion_item.exclusionRange.fromDateTime, settings) exclusion_date_2, _ = convert_date_time(exclusion_item.exclusionRange.toDateTime, settings) date_1, date_2 = later_date_last(exclusion_date_1, exclusion_date_2) new_rule = rrule(freq=DAILY, dtstart=date_1, until=date_2) else: raise UnknownExclusionTypeError exclusion_rules.append(new_rule) return exclusion_rules
def do_timespans(command, settings): date_time, output_format = convert_date_time(command.dateTime, settings) original_date_time = date_time for operand in command.operandList: date_time = delta_arithmetic(date_time, operand) date_time = exclusion_check(original_date_time, date_time, command, settings) return date_time.strftime(output_format)
def change_anniversary(anniversaries, command, date_mapping, wf): if "%Y" not in date_mapping["date-format"]: return FOUR_DIGIT_DATE_ERROR if command.anniversaryName not in anniversaries: output = "{anniversaryName} does not exist".format(anniversaryName=command.anniversaryName) else: date_time, output_format = convert_date_time(command.dateTime, wf.settings) anniversary_date = date_time.isoformat() anniversaries[command.anniversaryName.lower()] = anniversary_date wf.settings["anniversaries"] = anniversaries output = "{anniversaryName} changed".format(anniversaryName=command.anniversaryName) return output
def calculate_rrule_exclusions(start_date, end_date, exclusion_commands, settings): exclusion_ruleset = rruleset() exclusion_types = exclusion_commands.exclusionList for exclusion_type in exclusion_types: if hasattr(exclusion_type, "exclusionRange"): from_date, _ = convert_date_time( exclusion_type.exclusionRange.fromDateTime, settings) to_date, _ = convert_date_time( exclusion_type.exclusionRange.toDateTime, settings) exclusion_ruleset.rrule( rrule(freq=DAILY, dtstart=from_date, until=to_date)) elif hasattr(exclusion_type, "exclusionDateTime"): real_date, _ = convert_date_time(exclusion_type.exclusionDateTime, settings) exclusion_ruleset.rrule( rrule(freq=DAILY, dtstart=real_date, until=real_date)) elif hasattr(exclusion_type, "exclusionMacro"): macro_value = exclusion_type.exclusionMacro exclusion_rule = EXCLUSION_MAP[macro_value]['rule']( start=start_date, end=end_date) exclusion_ruleset.rrule(exclusion_rule) else: # in that case, I have no idea what this is (the parser should have caught it) so just # raise an error or something raise UnknownExclusionTypeError matched_dates = list( exclusion_ruleset.between(after=start_date, before=end_date, inc=True)) return len(matched_dates)
def change_anniversary(anniversaries, command, date_mapping, wf): if "%Y" not in date_mapping["date-format"]: return FOUR_DIGIT_DATE_ERROR if command.anniversaryName not in anniversaries: output = "{anniversaryName} does not exist".format( anniversaryName=command.anniversaryName) else: date_time, output_format = convert_date_time(command.dateTime, wf.settings) anniversary_date = date_time.isoformat() anniversaries[command.anniversaryName.lower()] = anniversary_date wf.settings["anniversaries"] = anniversaries output = "{anniversaryName} changed".format( anniversaryName=command.anniversaryName) return output