Skip to content

Commit

Permalink
directive_is_included: account for '%%' directives
Browse files Browse the repository at this point in the history
'%%' represents an escaped directive that is a literal '%' character,
so it needs to be excluded in the date format check. Using the regex
negative lookbehind assertion suggested by @tsibley in a
previous review.¹

¹ nextstrain/mpox#45 (comment)
  • Loading branch information
joverlee521 committed Jan 20, 2023
1 parent db4f42c commit 605ddcf
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion augur/curate/format_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Format date fields to ISO 8601 dates (YYYY-MM-DD), where incomplete dates
are masked with 'XX' (e.g. 2023 -> 2023-XX-XX).
"""
import re
from datetime import datetime
from augur.io.print import print_err

Expand Down Expand Up @@ -49,13 +50,19 @@ def directive_is_included(potential_directives, date_format):
False
>>> directive_is_included(potential_directives, '%y-%m')
False
>>> directive_is_included(potential_directives, '%%y-%m-%d')
False
>>> directive_is_included(potential_directives, '%y-%m-%d')
True
>>> directive_is_included(potential_directives, '%y-%m-%dT%H:%M:%SZ')
True
"""
return any(
all(sub_directive in date_format for sub_directive in directive)
all(
# Exclude escaped directives (e.g. '%%Y' means literal '%Y' not a four digit year)
bool(re.search(f"(?<!%){re.escape(sub_directive)}", date_format))
for sub_directive in directive
)
for directive in potential_directives
)

Expand Down

0 comments on commit 605ddcf

Please sign in to comment.