-
Notifications
You must be signed in to change notification settings - Fork 6
/
chooser.py
executable file
·83 lines (69 loc) · 2.1 KB
/
chooser.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
81
82
83
#!/usr/bin/env python
"""
chooser.py - a parsescript for todo.py
Takes in a file path or URL.
"""
__author__ = "Craig M. Stimmel"
__license__ = "GPL"
import sys
import os
import subprocess
import tempfile
import getopt
import openanything
import todo
# Where are our todos stored?
if todo.TODO_DIR:
todo_dir = todo.TODO_DIR
else:
try:
todo_dir = "%s/todo/" % os.environ['HOME']
except:
print "Your todo directory cannot be found!"
# Figure out what editor to use, if not that, default to vi
try:
if(os.environ['TODO_EDITOR']):
editor = os.environ['TODO_EDITOR']
else:
editor = os.environ['EDITOR']
except:
editor = 'vi'
def getFile(source):
""" Use openanything to open a file or url and return a dictionary of info about it """
file = openanything.fetch(source)
return file
def parseFile(file):
""" Process the text and return todo items that can be written to the todo.txt data file """
# Create a tempfile and write our data into it
tfile = tempfile.mkstemp()[1]
thandle = open(tfile, 'w')
thandle.write(file['data'])
thandle.close()
# Open our tempfile and edit it
subprocess.call([editor, tfile])
# Read the tempfile back in as todos
thandle = open(tfile, 'r')
todos = thandle.read()
thandle.close()
# Clean up the tempfile, since mkstemp doesn't do that for us.
cleanup = subprocess.Popen("rm " + tfile, shell=True)
sts = os.waitpid(cleanup.pid, 0)
return todos
def writeTodos(todos):
""" Write out the todo items into the todo.txt storage """
fhandle = open(todo_dir+'todo.txt', 'a')
fhandle.write(todos)
fhandle.close()
def main(source):
todos = parseFile(source)
writeTodos(todos)
if __name__ == "__main__":
try:
source = sys.argv[1]
source = getFile(source)
except IndexError:
source = sys.stdin.read()
source = {'data':source,} # Turn this into a dictionary with a "data" key just so it matches what openanything.py returns
main(source)
reset = subprocess.Popen("reset", shell=True)
rsts = os.waitpid(reset.pid, 0)