forked from jmjeong/alfred-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
80 lines (65 loc) · 2.1 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# util.py, v1.0
#
# Jaemok Jeong, 2016/11/15
import re
from datetime import date, timedelta
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def parsedate(q):
if len(q) == 0: return None
q = ''.join(q)
dow_str = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
dow_pt = re.compile(' *(mon|tue|wed|thu|fri|sat|sun)')
day_pt = re.compile(' *(\d+)[\.\-/](\d+)([\.\-/](\d+))*')
add_pt = re.compile(' *(-?\d+)([dwm])*')
today = date.today()
next = dow_pt.match(q)
if next:
dows = next.group(1)
ts = date.today().weekday()
ws = dow_str.index(dows)
nextday = (ws - ts + 7) % 7
ret = today + timedelta(days=nextday)
return ret
next = day_pt.match(q)
if next:
(month, day, year) = [x and int(x) for x in next.group(1,2,4)]
if year == None:
ret = date(today.year, month, day)
if (ret < today):
ret = date(today.year+1, month, day)
else:
if year<100: year+= 2000
ret = date(year, month, day)
return ret
next = add_pt.match(q)
if next:
(num, metric) = next.group(1,2)
if metric == 'w':
ret = today + timedelta(weeks=int(num))
elif metric == 'm':
ret = today + timedelta(days=int(num)*30)
else:
ret = today + timedelta(days=int(num))
return ret
return today
def parse(q):
title_pt = re.compile('([^#>:@]*)[#>:@]?.*$')
tag_pt = re.compile('#([^\s]+)')
area_pt = re.compile('@(\w)')
day_pt = re.compile('>([^>]*?)$')
note_pt = re.compile('::([^>#]*)')
title = ''.join(title_pt.findall(q))
tag = ','.join(tag_pt.findall(q))
day = parsedate(day_pt.findall(q))
area = ''.join(area_pt.findall(q))
note = ''.join(note_pt.findall(q))
if area == 's': area = 'TMSomedayListSource'
elif area == 'a' or area == 'n': area = 'TMNextListSource'
elif area == 't': area = 'TMTodayListSource'
else: area = 'TMInboxListSource'
return (title, area, tag, day, note)