Update ptw

git-svn-id: http://photonzero.com/dotfiles/trunk@95 23f722f6-122a-0410-8cef-c75bd312dd78
This commit is contained in:
michener 2011-04-16 00:40:29 +00:00
parent 9b73f092e8
commit 6dc5e5b89c
2 changed files with 43 additions and 3 deletions

44
bin/ptw
View file

@ -2,6 +2,7 @@
import json
import os.path
import time
import sys
from optparse import OptionParser
from operator import itemgetter
@ -120,11 +121,11 @@ a_qty = CL("a").setParseAction(replaceWith(1))
integer = Word(nums).setParseAction(lambda t:int(t[0]))
int4 = Group(Word(nums,exact=4).setParseAction(lambda t: [int(t[0][:2]),int(t[0][2:])] ))
qty = integer | couple | a_qty
dayName = oneOf( list(calendar.day_name) )
dayName = oneOf( list(calendar.day_name) + [x.lower() for x in calendar.day_name] )
dayOffset = (qty("qty") + (week | day)("timeunit"))
dayFwdBack = (from_ + now.suppress() | ago)("dir")
weekdayRef = (Optional(next_ | last_,-1)("dir") + dayName("day"))
weekdayRef = (Optional(next_ | last_, -1 )("dir") + dayName("day"))
dayRef = Optional( (dayOffset + (before | after | from_)("dir") ).setParseAction(convertToTimedelta) ) + \
((yesterday | today | tomorrow)("name")|
weekdayRef("wkdayRef")).setParseAction(convertToDay)
@ -161,7 +162,8 @@ def db_append(data):
db_file = open(os.path.expanduser("~/.ptwdb"), "r")
db = json.load(db_file)
db_file.close()
db.append(data)
if not timestamp_and_text_exists(data["timestamp"], data["text"], db):
db.append(data)
db_file = open(os.path.expanduser("~/.ptwdb"), "w")
json.dump(db, db_file, indent=2)
db_file.close()
@ -191,6 +193,13 @@ def db_get(count=None, since=None):
count -= 1
return out
def timestamp_and_text_exists(timestamp, text, db):
for x in db:
if x["timestamp"] == timestamp:
if x["text"] == text:
return True
return False
def format(l):
out = []
@ -210,6 +219,8 @@ parser.add_option("--client", dest="client", help="Client for post",
default="Command Line", metavar="CLIENT")
parser.add_option("-n", dest="num", help="Number of posts to show",
type="int", default=None, metavar="N")
parser.add_option("--source", dest="import_source", help="Source of git imports",
type="str", default="", metavar="STR")
parser.add_option("-s", "--since", dest="since", help="Number of posts to show",
default=None, metavar="SINCE")
parser.add_option("-v", "--verbose", dest="verbose", help="Debug output",
@ -240,5 +251,32 @@ elif command == "get":
options.since = None
for x in format(db_get(options.num, options.since)):
print x
elif command == "since":
if len(args) < 2:
parser.print_help()
exit()
p = nlTimeExpression.parseString(text)
if "calculatedTime" in p:
options.since = time.mktime(p.calculatedTime.timetuple())
if options.verbose:
print p.calculatedTime.isoformat()
else:
options.since = None
for x in format(db_get(options.num, options.since)):
print x
elif command == "import":
for line in sys.stdin:
fields = line.strip().split("\t")
ts = int(fields[0])
text = fields[1]
if options.import_source != "":
text = "%s: %s" % (options.import_source, text)
data = {"timestamp" : ts, "text" : text, "client" : options.client,
"hostname" : options.hostname }
if not os.path.exists(os.path.expanduser("~/.ptwdb")):
db_new()
db_append(data)
else:
print "Invalid Command"
parser.print_help()
exit()