# HG changeset patch # User Paul Boddie # Date 1512427657 -3600 # Node ID 28fe0ad38cd487845c6bd5ae04e9117dccfe9a45 # Parent 94338c98305bf4bd35566f49505d9015dfa34889 Introduced validation of qualifier arguments using the check_values function. diff -r 94338c98305b -r 28fe0ad38cd4 vRecurrence.py --- a/vRecurrence.py Mon Dec 04 22:35:42 2017 +0100 +++ b/vRecurrence.py Mon Dec 04 23:47:37 2017 +0100 @@ -221,13 +221,20 @@ # Accept interval indicators for frequency qualifier parameterisation. elif key == "INTERVAL": - interval = int(value) + interval = max(1, int(value)) continue # Accept result set selection, truncation and enumerators as qualifiers. elif key in ("BYSETPOS", "COUNT") or enum.has_key(key): - qualifier = (key, {"values" : get_qualifier_values(key, value)}) + values = get_qualifier_values(key, value) + + # Ignore bad qualifier values. + + if not values: + continue + + qualifier = (key, {"values" : values}) # Ignore other items. @@ -250,31 +257,50 @@ suitable values. """ - # For non-weekday selection, obtain a list of numbers. - - if qualifier != "BYDAY": - return map(int, value.split(",")) - - # For weekday selection, obtain the weekday number and instance number. - values = [] - for part in value.split(","): - index, weekday = part[:-2], part[-2:] + for v in value.split(","): + try: + # For non-weekday selection, obtain a list of numbers, each in a tuple. - weekday_number = weekdays.get(weekday) - if not weekday_number: - continue + if qualifier != "BYDAY": + to_check = (int(v),) + + # For weekday selection, obtain a list of tuples containing the weekday + # and instance number. + + else: + # Split the two-letter weekday code from the preceding text. - if index: - index = int(index) - else: - index = None + index, weekday = v[:-2], v[-2:] + to_check = (weekday, int_or_empty(index)) + + except ValueError: + return None + + checked = check_values(qualifier, to_check) - values.append((weekday_number, index)) + # Get single values for non-weekday details. + + if qualifier != "BYDAY": + checked = checked[0] + + if not checked: + return None + + values.append(checked) return values +def int_or_empty(value): + + "Return 'value' as an integer or None if null. Raises ValueError." + + if value: + return int(value) + else: + return None + def make_selectors(qualifiers): """