-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
229 lines (206 loc) · 5.82 KB
/
main.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import sys
import os
import datetime
import backlog
import journosOut
import journosIn
import entry
import journosDate
import journosEncrypt
import journosSearch
import journosDir
from getpass import getpass
from Crypto.Cipher import DES
import signal
import editSpecialQ
# VALID COMMANDS:
# journos
# journos MM/DD/YYYY
# journos read
# journos read MM/DD/YYYY
# journos changepw
# journos dump
ENC_INIT_DONE=False
ENC_INIT_RUNNING=False
# Error Handling
def sigint_signal_handler(signal, frame):
quit_gracefully()
def eof_handler():
journosOut.printRed("Ctrl+C to quit")
return
def quit_gracefully():
if not enc.can_exit:
journosOut.printRed("Cannot quit right now")
return
journosOut.printRed("Closing down...")
if ENC_INIT_DONE:
enc.exit()
sys.exit(0)
signal.signal(signal.SIGINT, sigint_signal_handler)
# # # # # # # # #
def eatFlags():
flags=[]
newv=[]
for i in range(0,len(sys.argv)):
if sys.argv[i].startswith('-'):
flags+=[sys.argv[i]]
else:
newv+=[sys.argv[i]]
sys.argv=[newv[i] for i in range(0,len(newv))]
return flags
journosOut.printBlue(" __ ___ __ __ ____ __ __ ___ __ \n || // \\\\ || || || \\\\ ||\\ || // \\\\ (( \\\n || (( )) || || ||_// ||\\\\|| (( )) \\\\ \n |__|| \\\\_// \\\\_// || \\\\ || \|| \\\\_// \\_))")
# Get password, decrypt journal file
enc = journosEncrypt.JournosEncrypt()
ENC_INIT_RUNNING=True
enc.init()
ENC_INIT_RUNNING=False
ENC_INIT_DONE=True
RUNTYPE="WRITE"
DATEFORMATERROR="Please input your date as MM/DD or MM/DD/YYYY"
d=datetime.date.today()
DATE=journosDate.today()
''' (I) PARSE COMMAND '''
if len(sys.argv) > 1 and sys.argv[1] == "changepw":
enc.changePassword()
quit_gracefully()
if len(sys.argv) > 1 and sys.argv[1] == "dump":
enc.dump()
quit_gracefully()
if len(sys.argv) > 1 and sys.argv[1].startswith("q"):
editSpecialQ.editSpecialQ()
quit_gracefully()
if len(sys.argv) > 1 and sys.argv[1].startswith("s"):
flags=eatFlags()
params=journosSearch.SearchParams()
if len(sys.argv) > 2:
if '-questions' in flags:
params.questions=True
params.answers=False
elif '-answers' in flags:
params.questions=False
params.answers=True
if '-case' in flags:
params.case_sensitive=True
journosSearch.search(sys.argv[2], params)
quit_gracefully()
else:
journosOut.printRed("No search term specified")
quit_gracefully()
if len(sys.argv) > 1:
if sys.argv[1].lower().startswith("r"):
RUNTYPE="READ"
else:
DATE=journosDate.formatDate(sys.argv[1])
if DATE==-1: journosOut.printRed(DATEFORMATERROR)
if len(sys.argv) == 3:
DATE=journosDate.formatDate(sys.argv[2])
if DATE==-1: journosOut.printRed(DATEFORMATERROR)
''' (II) EXECUTE COMMAND '''
if RUNTYPE=="WRITE":
if DATE==journosDate.today():
for day in backlog.daysMissing():
journosOut.animPrintPurple("Would you like to fill out an entry for "+backlog.toString(day)+"? (Y/N)")
if journosIn.isYes(journosIn.getInput()):
ent=entry.Entry()
ent.get(day)
enc.can_exit=False
f=open(journosDir.plainTextJourn(),"a")
f.write(ent.to_s()+'\n')
f.close()
enc.can_exit=True
else:
ent=entry.Entry()
ent.date=day
enc.can_exit=False
f=open(journosDir.plainTextJourn(),"a")
f.write(ent.to_s()+'\n')
f.close()
enc.can_exit=True
journosOut.animPrintPurple("If you change your mind, type 'journos "+day+"'")
journosOut.endSection()
newEnt = entry.Entry()
entSuccess = newEnt.readEntry(DATE)
if entSuccess == 1:
journosOut.animPrintBlue("Here's what you've written for "+("today" if DATE==journosDate.today() else DATE)+":")
entry.printEntry(newEnt)
_day="today, "+DATE if DATE==journosDate.today() else DATE
_create_type="fill out an" if entSuccess == 0 else "edit your"
journosOut.animPrintPurple("Would you like to "+_create_type+" entry for "+_day+"? (Y/N)")
if journosIn.isYes(journosIn.getInput()):
if entSuccess == 0:
newEnt.get(DATE)
enc.can_exit=False
f=open(journosDir.plainTextJourn(),"a")
f.write(newEnt.to_s()+'\n')
f.close()
enc.can_exit=True
else:
newEnt.edit(DATE)
enc.can_exit=False
tmp=open(journosDir.plainTextJournTmp(),"w")
old=open(journosDir.plainTextJourn(),"r")
for line in old:
if line.startswith(DATE):
tmp.write(newEnt.to_s()+'\n')
else:
tmp.write(line)
tmp.close()
old.close()
os.rename(journosDir.plainTextJournTmp(),journosDir.plainTextJourn())
enc.can_exit=True
else:
if entSuccess == 0:
enc.can_exit=False
f=open(journosDir.plainTextJourn(),"a")
f.write(newEnt.to_s()+'\n')
f.close()
enc.can_exit=True
journosOut.animPrintPurple("If you change your mind, type 'journos "+journosDate.today()+"'")
else:
journosOut.animPrintPurple("If you change your mind, type 'journos "+journosDate.today()+"'")
journosOut.endSection()
elif RUNTYPE=="READ":
while True:
ent=entry.Entry()
success = ent.readEntry(DATE)
if success==1:
entry.printEntry(ent)
else:
journosOut.printRed("No entry found on "+DATE)
journosOut.animPrintBlue("n -- Next p -- Previous e -- Edit")
inp=journosIn.getInput().lower().strip()
if inp=='n':
DATE=journosDate.nextDate(DATE)
continue
elif inp=='p':
DATE=journosDate.prevDate(DATE)
continue
elif inp=='e':
newEnt = entry.Entry()
if success==1:
newEnt.edit(DATE)
enc.can_exit=False
tmp=open(journosDir.plainTextJournTmp(),"w")
old=open(journosDir.plainTextJourn(),"r")
for line in old:
if line.startswith(DATE):
tmp.write(newEnt.to_s()+'\n')
else:
tmp.write(line)
tmp.close()
old.close()
os.rename(journosDir.plainTextJournTmp(),journosDir.plainTextJourn())
enc.can_exit=True
else:
newEnt.get(DATE)
enc.can_exit=False
f=open(journosDir.plainTextJourn(),"a")
f.write(newEnt.to_s()+'\n')
f.close()
enc.can_exit=True
journosOut.endSection()
continue
else:
break
# encrypt again
enc.exit()