Embed dateregex instead of using a submodule

This commit is contained in:
Guilherme Victal
2016-01-27 11:01:57 -02:00
parent 2535cf2ece
commit d769f12710
4 changed files with 185 additions and 3 deletions

View File

@@ -0,0 +1,11 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File: __init__.py
# Author: Guilherme Victal <guilherme at victal.eti.br>
# Description: Dateregex library entry point
# License: Vim license
# Website: http://github.com/freitass/todo.txt-vim
# Version: 0.1
from after import regex_date_after
from before import regex_date_before

View File

@@ -0,0 +1,95 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File: after.py
# Author: Guilherme Victal <guilherme at victal.eti.br>
# Description: Generates regexes after a certain date
# License: Vim license
# Website: http://github.com/freitass/todo.txt-vim
# Version: 0.1
from datetime import date, timedelta, MAXYEAR
def _year_regex_after(year):
if int(year) > MAXYEAR:
return None
year_regex = r'(\d+\d{%s}' % len(year)
for idx, digit in enumerate(year):
if digit != '9':
regex = '|' + year[0:idx]
regex += '9' if digit == '8' else '[%s-9]' % str(int(digit) + 1)
if idx < len(year) - 1:
regex += '\d{%s}' % (len(year) - (idx + 1))
year_regex += regex
year_regex += ')'
return '-'.join((year_regex, r'\d{2}', r'\d{2}'))
def _month_regex_after(year, month):
if month == '12':
return None
digit1, digit2 = month
if digit1 == '1':
month_regex = r'12' if month == '11' else r'1[12]'
else:
month_regex = r'1[0-2]'
if digit2 != '9':
if digit2 == '8':
month_regex = r'(' + month_regex + r'|09)'
else:
month_regex = r'(' + month_regex + r'|0[%s-9])'
month_regex = month_regex % str(int(digit2) + 1)
return '-'.join((year, month_regex, r'\d{2}'))
def _day_regex_after(year, month, day):
last_month_day = str((date(int(year), (int(month) + 1) % 12, 1) + - date.resolution).day)
if day == last_month_day:
return None
day_regex = r'('
digit1, digit2 = day
last_digit1, last_digit2 = last_month_day
if digit1 == last_digit1:
day_regex = last_month_day if int(digit2) == int(last_digit2) - 1 else last_digit1 + r'[%s-%s]' % (str(int(digit2) + 1), last_digit2)
else:
day_regex = r'('
day_regex += last_digit1 if int(digit1) == int(last_digit1) - 1 else r'[%s-%s]' % (str(int(digit1) + 1), last_digit1)
day_regex +=r'\d'
if digit2 < '9':
day_regex += '|' + digit1
day_regex += '9' if digit2 == '8' else r'[%s-9]' % str(int(digit2) + 1)
day_regex += ')'
return '-'.join((year, month, day_regex))
def regex_date_after(given_date):
year, month, day = given_date.isoformat().split('-')
year_regex = _year_regex_after(year)
month_regex = _month_regex_after(year, month)
day_regex = _day_regex_after(year, month, day)
date_regex = '(' + year_regex if year_regex else '('
date_regex += ('|' + month_regex) if month_regex else ''
date_regex += ('|' + day_regex) if day_regex else ''
date_regex += ')'
return date_regex
def __main():
import re
date_regex = regex_date_after(date(1999,12,31))
print(date_regex)
pattern = re.compile(date_regex)
d = date.today() + date.resolution
assert pattern.match(date.strftime(d, '%Y-%m-%d')) is not None
print(date.strftime(d, '%Y-%m-%d') + ' is okay')
d += date.resolution
if __name__ == '__main__':
__main()

View File

@@ -0,0 +1,79 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File: before.py
# Author: Guilherme Victal <guilherme at victal.eti.br>
# Description: Generates regexes before a certain date
# License: Vim license
# Website: http://github.com/freitass/todo.txt-vim
# Version: 0.1
from datetime import date, timedelta, MINYEAR
def _year_regex_before(year):
if int(year) <= MINYEAR:
return None
year_regex = r'('
year_regex += r'\d{1,%s}' % str(len(year) - 1) if len(year) > 1 else ''
for idx, digit in enumerate(year):
if digit != '0':
regex = '|' + year[0:idx]
regex += '0' if digit == '1' else '[0-%s]' % str(int(digit) - 1)
if idx < len(year) - 1:
regex += '\d{%s}' % (len(year) - (idx + 1))
year_regex += regex
year_regex += ')'
return '-'.join((year_regex, r'\d{2}', r'\d{2}'))
def _month_regex_before(year, month):
if month == '01':
return None
digit1, digit2 = month
if digit1 == '0':
month_regex = '01' if month == '02' else r'0[1-%s]' % str(int(digit2) - 1)
elif month == '10':
month_regex = r'0\d'
elif month == '11':
month_regex = r'(0\d|10)'
else:
month_regex = r'(0\d|1[01])'
return '-'.join((year, month_regex, r'\d{2}'))
def _day_regex_before(year, month, day):
if day == '01':
return None
last_month_day = str((date(int(year), (int(month) + 1) % 12, 1) + - date.resolution).day)
last_digit1, last_digit2 = last_month_day
digit1, digit2 = day
if digit1 == '0':
day_regex = '01' if day == '02' else r'0[1-%s]' % str(int(digit2) - 1)
else:
day_regex = r'('
day_regex += '0' if digit1 == '1' else r'[0-%s]' % str(int(digit1) - 1)
day_regex += r'\d'
if digit2 != '0':
day_regex += '|'
day_regex += digit1
day_regex += '0' if digit2 == '1' else r'[0-%s]' % str(int(digit2) - 1)
day_regex += ')'
return '-'.join((year, month, day_regex))
def regex_date_before(given_date):
year, month, day = given_date.isoformat().split('-')
year_regex = _year_regex_before(year)
month_regex = _month_regex_before(year, month)
day_regex = _day_regex_before(year, month, day)
date_regex = '(' + year_regex if year_regex else '('
date_regex += ('|' + month_regex) if month_regex else ''
date_regex += ('|' + day_regex) if day_regex else ''
date_regex += ')'
return date_regex