hw: replace with python that uses due
also move due to a .py so it can be imported
This commit is contained in:
parent
c36a301d08
commit
07d05db080
83
due
83
due
@ -1,83 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
from datetime import datetime
|
||||
import parsedatetime
|
||||
|
||||
TAGS = ['t', 'due']
|
||||
|
||||
class todo_item:
|
||||
tag = []
|
||||
full_tag = ""
|
||||
date = None
|
||||
text = ""
|
||||
fancy_text = ""
|
||||
|
||||
def __init__(self, text):
|
||||
self.text = text
|
||||
try:
|
||||
self.full_tag = re.findall("[^ ]+:\\d\\d\\d\\d-\\d\\d-\\d\\d", text)[0].strip()
|
||||
self.tag = self.full_tag.split(":")
|
||||
self.date = datetime.strptime(self.tag[1], "%Y-%m-%d")
|
||||
except IndexError:
|
||||
pass
|
||||
|
||||
def __repr__(self):
|
||||
if self.text:
|
||||
return self.text
|
||||
else:
|
||||
return ''
|
||||
|
||||
def fancy_print(self, relative=False):
|
||||
if self.date < datetime.today():
|
||||
#if the date has already passed, wrap with bold/unbold escapes
|
||||
fancy_text = self.text.replace(self.full_tag,
|
||||
'\033[1m' + self.full_tag + '\033[22m')
|
||||
else:
|
||||
fancy_text = self.text
|
||||
|
||||
if relative:
|
||||
difference = (self.date - datetime.now()).days + 1
|
||||
fancy_text = fancy_text.replace(self.full_tag,
|
||||
"{}:{} days".format(self.tag[0],
|
||||
difference))
|
||||
|
||||
return fancy_text
|
||||
|
||||
def add(date_input, desc):
|
||||
cal = parsedatetime.Calendar()
|
||||
time_struct, parse_status = cal.parse(date_input)
|
||||
date = datetime(*time_struct[:6]).strftime("%Y-%m-%d")
|
||||
subprocess.call(["todo.sh", "add", desc, "t:" + date])
|
||||
|
||||
def due(search_term="", relative=False):
|
||||
command = ["todo.sh", "list"]
|
||||
command.append(search_term)
|
||||
lines = subprocess.check_output(command).decode(encoding='UTF-8').split('\n')
|
||||
items = []
|
||||
|
||||
for line in lines:
|
||||
if " t:" in line:
|
||||
item = todo_item(line)
|
||||
if item.date is not None:
|
||||
items.append(item)
|
||||
|
||||
items.sort(key= lambda x: x.date)
|
||||
|
||||
for i in items:
|
||||
print(i.fancy_print(relative))
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) >= 3:
|
||||
if sys.argv[2] == "-r":
|
||||
relative = True
|
||||
del sys.argv[2]
|
||||
else:
|
||||
relative = False
|
||||
search_term = " ".join(sys.argv[2:])
|
||||
due(search_term, relative)
|
||||
else:
|
||||
due()
|
83
due.py
Executable file
83
due.py
Executable file
@ -0,0 +1,83 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
from datetime import datetime
|
||||
import parsedatetime
|
||||
|
||||
TAGS = ['t', 'due']
|
||||
|
||||
class todo_item:
|
||||
tag = []
|
||||
full_tag = ""
|
||||
date = None
|
||||
text = ""
|
||||
fancy_text = ""
|
||||
|
||||
def __init__(self, text):
|
||||
self.text = text
|
||||
try:
|
||||
self.full_tag = re.findall("[^ ]+:\\d\\d\\d\\d-\\d\\d-\\d\\d", text)[0].strip()
|
||||
self.tag = self.full_tag.split(":")
|
||||
self.date = datetime.strptime(self.tag[1], "%Y-%m-%d")
|
||||
except IndexError:
|
||||
pass
|
||||
|
||||
def __repr__(self):
|
||||
if self.text:
|
||||
return self.text
|
||||
else:
|
||||
return ''
|
||||
|
||||
def fancy_print(self, relative=False):
|
||||
if self.date < datetime.today():
|
||||
#if the date has already passed, wrap with bold/unbold escapes
|
||||
fancy_text = self.text.replace(self.full_tag,
|
||||
'\033[1m' + self.full_tag + '\033[22m')
|
||||
else:
|
||||
fancy_text = self.text
|
||||
|
||||
if relative:
|
||||
difference = (self.date - datetime.now()).days + 1
|
||||
fancy_text = fancy_text.replace(self.full_tag,
|
||||
"{}:{} days".format(self.tag[0],
|
||||
difference))
|
||||
|
||||
return fancy_text
|
||||
|
||||
def add(date_input, desc):
|
||||
cal = parsedatetime.Calendar()
|
||||
time_struct, parse_status = cal.parse(date_input)
|
||||
date = datetime(*time_struct[:6]).strftime("%Y-%m-%d")
|
||||
subprocess.call(["todo.sh", "add", desc, "t:" + date])
|
||||
|
||||
def due(search_term="", relative=False):
|
||||
command = ["todo.sh", "list"]
|
||||
command.append(search_term)
|
||||
lines = subprocess.check_output(command).decode(encoding='UTF-8').split('\n')
|
||||
items = []
|
||||
|
||||
for line in lines:
|
||||
if " t:" in line:
|
||||
item = todo_item(line)
|
||||
if item.date is not None:
|
||||
items.append(item)
|
||||
|
||||
items.sort(key= lambda x: x.date)
|
||||
|
||||
for i in items:
|
||||
print(i.fancy_print(relative))
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) >= 3:
|
||||
if sys.argv[2] == "-r":
|
||||
relative = True
|
||||
del sys.argv[2]
|
||||
else:
|
||||
relative = False
|
||||
search_term = " ".join(sys.argv[2:])
|
||||
due(search_term, relative)
|
||||
else:
|
||||
due()
|
113
hw
113
hw
@ -1,72 +1,47 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env python3
|
||||
|
||||
me="$(basename $0)"
|
||||
import sys
|
||||
import os
|
||||
from datetime import datetime
|
||||
import subprocess
|
||||
import parsedatetime
|
||||
|
||||
textToDate ()
|
||||
{
|
||||
if [ ! "$(grep -P "(\d\d\.?){3}" <<< "$1")" ]
|
||||
then
|
||||
d=$(sed 's/^\(next\|n\)$/tomorrow/' <<< "$1")
|
||||
date --date="$d" +"%Y-%m-%d" || exit 1
|
||||
else
|
||||
echo "$1";
|
||||
fi
|
||||
}
|
||||
import due
|
||||
|
||||
if [ "$1" = "usage" ]
|
||||
then
|
||||
echo " hw"
|
||||
echo " list homework"
|
||||
echo " hw add"
|
||||
echo " prompt for adding an assignment"
|
||||
echo " hw [due date] [class] [description]"
|
||||
echo " add an assignment without a prompt"
|
||||
echo " hw [date]"
|
||||
echo " list assignments due on [date]"
|
||||
echo " hw rel"
|
||||
echo " list assignments with the dates in days relative to today"
|
||||
exit 0
|
||||
fi
|
||||
shift
|
||||
|
||||
if [ "$1" = "add" ]
|
||||
then
|
||||
read -p "Due: " due
|
||||
due="$(textToDate "$date")"
|
||||
read -p "Class: " class
|
||||
read -p "Description: " desc
|
||||
"$TODO_FULL_SH" add "(B) +hw" "class:$class $due $desc"
|
||||
elif [ "$1" = "rel" ]
|
||||
then
|
||||
"$TODO_FULL_SH" "$me" | sed -e "s/.*/echo \'&\'/g;s/due:\([0-9-]*\)/due:\'"'$(( ($(date --date="\1" +%s) - $(date --date="00:00" +%s)) \/ 86400 ))'"\'d/ge"
|
||||
elif [ -n "$1" -a -n "$2" -a -n "$3" ]
|
||||
then
|
||||
due="due:$(textToDate "$1")" || { echo "invalid date" ; exit 1; }
|
||||
shift
|
||||
class="class:$1"
|
||||
shift
|
||||
"$TODO_FULL_SH" add "(B) +hw $due $class $@"
|
||||
else
|
||||
IFS=$'\n'
|
||||
today=$(textToDate "today" | tr -d '-')
|
||||
if [ -n "$1" ]
|
||||
then
|
||||
inDate="$(textToDate "$1" | tr -d '-' | cut -f1)"
|
||||
fi
|
||||
for i in $("$TODO_FULL_SH" list | grep "+hw")
|
||||
do
|
||||
date=$(grep -oP "due:\K[0-9-]*" <<< "$i"| tr -d '-' | cut -f1)
|
||||
if [ "$date" -le "$today" ]
|
||||
then
|
||||
echo "$i" | sed "s/\(due:[0-9-]*\)/$(tput bold)\1$(tput sgr0)/g"
|
||||
elif [ -n "$inDate" ]
|
||||
then
|
||||
if [ "$date" -eq "$inDate" ]
|
||||
then
|
||||
echo "$i"
|
||||
fi
|
||||
else
|
||||
echo "$i"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
PRIORITY = "(B)"
|
||||
|
||||
if len(sys.argv) < 2:
|
||||
print("Not enough args!")
|
||||
exit(0)
|
||||
|
||||
if sys.argv[1] == "usage":
|
||||
print(" hw\n"
|
||||
" list homework\n"
|
||||
" hw rel\n"
|
||||
" list assignments with the dates in days relative to today\n"
|
||||
" hw due [date]\n"
|
||||
" list assignments due on [date]\n"
|
||||
" hw add\n"
|
||||
" prompt for adding an assignment\n"
|
||||
" hw add [due date] [class] [description]\n"
|
||||
" add an assignment without a prompt\n")
|
||||
exit(0)
|
||||
|
||||
if len(sys.argv) < 3 or sys.argv[2] == "due": #default case
|
||||
due.due("+hw")
|
||||
elif sys.argv[2] == "rel":
|
||||
due.due("+hw", True)
|
||||
elif sys.argv[2] == "add":
|
||||
if len(sys.argv) == 3: #prompt
|
||||
c = input("Class")
|
||||
date = input("Date")
|
||||
desc = input("Assignment")
|
||||
due.add(date, "{} +hw class:{} {}".format(PRIORITY, c, desc))
|
||||
elif len(sys.argv) >= 6:
|
||||
print(sys.argv[3:])
|
||||
due.add(sys.argv[3],
|
||||
"{} +hw class:{} {}".format(PRIORITY,
|
||||
sys.argv[4],
|
||||
" ".join(sys.argv[5:])))
|
||||
else:
|
||||
print("Invalid args")
|
||||
|
Loading…
Reference in New Issue
Block a user