#!/usr/bin/python # usage: chores.py [start [end]] # where start and end are dates. # The default for start is today. # The default for end is start. # If the script is named (linked) "plans.py" # then the default for end is the end of time. # Limitation: dates must be of the form yyyy-mm-dd. # Wait, shouldn't the usage be "chores.py [[-]n|date] like chores.sh? # But I like chores = today and plans = on into the future. - elb 2011-11-11 # Nah. Don't need all these parameters. # Instead how about plans, chores (today), yesterday, tomorrow, # thisweek, lastweek, nextweek, # lastmonth, thismonth, nextmonth? # Someday baby? # Disallow (with usage message) second date for week and month day aliases. # Allow month aliases to specify just yyy-mm # Maybe other date formats could be accepted, like "today". # Maybe "." could stand for today? # Maybe could take + or - to indicate days from today. # If so, the default integer should be 0 so "chores.py - tomorrow" would work. import sys def main(argv=None): if argv is None: argv = sys.argv import datetime import re todo = re.compile(r'[ABC]') aDateString = re.compile(r'^20[0-9][0-9]-(0?[1-9]|1[0-2])-(0?[1-9]|[12][0-9]|30|31)$') firstdate = datetime.date.today() if 1 < len(sys.argv): # then a beginning was specified if aDateString.match(sys.argv[1]): f = sys.argv[1].split('-') i = map(int, f) firstdate = datetime.date(i[0], i[1], i[2]) else: print "usage: %s [yyyy-mm-dd [yyyy-mm-dd]]" % sys.argv[0] return 1 if re.compile(r'[^a-z]*yester').match(argv[0]): firstdate -= datetime.timedelta(1) if re.compile(r'[^a-z]*tomorrow').match(argv[0]): firstdate += datetime.timedelta(1) lastdate = firstdate if re.compile(r'[^a-z]*[a-z]{4,4}week').match(argv[0]): firstdate -= datetime.timedelta(firstdate.weekday()) lastdate = firstdate + datetime.timedelta(6) if re.compile(r'[^a-z]*lastweek').match(argv[0]): firstdate -= datetime.timedelta(7) lastdate -= datetime.timedelta(7) if re.compile(r'[^a-z]*nextweek').match(argv[0]): firstdate += datetime.timedelta(7) lastdate += datetime.timedelta(7) if re.compile(r'[^a-z]*[a-z]{4,4}month').match(argv[0]): firstdayofmonth = firstdate.replace(day=1) if re.compile(r'[^a-z]*lastmonth').match(argv[0]): thismonth = firstdayofmonth.month thisyear = firstdayofmonth.year if 1 == thismonth: thismonth = 12 thisyear -= 1 else: thismonth -= 1 firstdayofmonth = firstdayofmonth.replace(year=thisyear,month=thismonth) if re.compile(r'[^a-z]*nextmonth').match(argv[0]): thismonth = firstdayofmonth.month thisyear = firstdayofmonth.year if 12 == thismonth: thismonth = 1 thisyear += 1 else: thismonth += 1 firstdayofmonth = firstdayofmonth.replace(year=thisyear,month=thismonth) firstdate = firstdayofmonth lastdate = firstdayofmonth + datetime.timedelta(31) while firstdate.month != lastdate.month: lastdate -= datetime.timedelta(1) if re.compile(r'[^a-z]*plans').match(argv[0]): lastdate = datetime.date.max if 2 < len(sys.argv): # then an ending was specified if aDateString.match(sys.argv[2]): f = sys.argv[1].split('-') i = map(int, f) lastdate = datetime.date(i[0], i[1], i[2]) else: print "usage: %s [yyyy-mm-dd [yyyy-mm-dd]]" % sys.argv[0] return 1 for line in sys.stdin: l = line.split(' ') f = l[0].split('-') i = map(int, f) d = datetime.date(i[0], i[1], i[2]) if (firstdate <= d <= lastdate): print line, continue if (todo.match(l[1]) and d <= lastdate): print line, if __name__ == '__main__': sys.exit(main())