-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththoughtlogger.pyw
347 lines (283 loc) · 12.5 KB
/
thoughtlogger.pyw
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# Thought Logger v3.1.1
# Source Code
# Developer: moiSentineL (www.github.com/moiSentineL)
# Website: www.github.com/moiSentineL/ThoughtLogger
# Imports
import datetime
import os
from glob import glob
import time
import webbrowser as wb
import sys
import pyautogui as gui
import logging
from configparser import ConfigParser
import tkinter as tk
from tkinter.constants import TRUE
from tkinter import Toplevel
from tkinter import StringVar, font, messagebox, ttk
from tkinter.ttk import Style, Button
def thoughtlogger():
# Tkinter window initialize
main = tk.Tk()
# Essential Variables/Functions:
# Variables for log file.
userlocation = os.path.expanduser('~')
documentslocation = str(os.path.join(userlocation, 'Documents'))
thoughtloggerdir = os.path.join(documentslocation, "Thought Logger")
if os.path.exists(thoughtloggerdir) == True:
pass
else:
os.mkdir(thoughtloggerdir)
profile1log = "".join([documentslocation, r"\Thought Logger\profile1.log"])
profile2log = "".join([documentslocation, r"\Thought Logger\profile2.log"])
# Variable for .ini file.
inifile = "".join([documentslocation, r"\Thought Logger\thoughtlogger.ini"])
ini = ConfigParser()
ini.read(inifile)
# Functions
def logfilecreation():
# Function to create the log files for the first time with headings.
for i in [profile1log, profile2log]:
if os.path.exists(i) == True:
pass
else:
log1 = open(profile1log, 'w')
log1.write('--------ThoughtLogger--------\n----------Profile 1----------\n\n')
log1.close()
log2 = open(profile2log, 'w')
log2.write('--------ThoughtLogger--------\n----------Profile 2----------\n\n')
log2.close()
logfilecreation()
def logimport():
# Function to import old (2.1.1 or older) logs into new one.
thoughtloggerlog = "".join([documentslocation, r"\Thought Logger\thoughtlogger.log"])
if os.path.exists(thoughtloggerlog) == True:
askyesorno = tk.messagebox.askyesno("Old log found.", "Do you wish to import your old log onto the new one?")
if askyesorno == True:
file1 = open(thoughtloggerlog, 'r')
count = 0
while True:
count += 1
line = file1.readline()
if not line:
break
else:
writeold = "{}\n".format(line.strip())
file2 = open(profile1log, 'a')
file2.write(writeold)
file2.close()
file1.close()
os.remove(thoughtloggerlog)
else:
pass
else:
pass
def inifileconfig():
# Function to create and configure .ini file
if os.path.exists(inifile) == True:
if ini.has_option('CustomText', 'text'):
customtextprev =ini.get('CustomText', 'text')
ini["ThoughtLogger"]={'CustomText': customtextprev,
'BackgroundColor': '#000000',
'FontColor': '#fff'}
cfgfile = open(inifile,'w')
ini.remove_section('CustomText')
ini.write(cfgfile)
cfgfile.close()
else:
pass
else:
ini["ThoughtLogger"]={'CustomText': 'ThoughtLogger',
'BackgroundColor': '#000000',
'FontColor': '#fff'}
cfgfile = open(inifile,'w')
ini.write(cfgfile)
cfgfile.close()
inifileconfig()
def findlatestlog():
# Function to find the latest edited log file to open.
folder_path = thoughtloggerdir
file_type = '\*log'
global files
files = glob(folder_path + file_type)
if files:
global latestlog, head, tail
latestlog = max(files, key=os.path.getmtime)
else:
pass
def seelog():
# Function to open log file.
try:
findlatestlog()
if os.path.exists(latestlog) == True:
wb.open(latestlog)
time.sleep(0.1)
gui.hotkey('ctrl', 'end')
else:
tk.messagebox.showerror("File Does Not Exist", "No log file exists. Restart the Application.")
except FileNotFoundError as e:
tk.messagebox.showerror("File Does Not Exist", "No log file exists. Restart the Application.")
except NameError as e:
tk.messagebox.showerror("File Already Does Not Exist", "No log file exists. Restart the application")
def exit(a):
# Function to exit program.
main.destroy()
def deletelogwindow():
# Function to delete the log file.
# New window initialise
new = Toplevel(main)
def deletelog():
if str(var.get()) == '1':
if os.path.exists(profile1log) == True:
askyesorno = tk.messagebox.askyesno("Are you sure?", "Do you really want to delete your log?")
if askyesorno == True:
os.remove(profile1log)
tk.messagebox.showinfo("Log Deleted", "Your log has been deleted successfully.")
else:
pass
else:
tk.messagebox.showerror("File Already Does Not Exist", "The file 'profile1.log' already does not exist.")
elif str(var.get()) == '2':
if os.path.exists(profile2log) == True:
askyesorno = tk.messagebox.askyesno("Are you sure?", "Do you really want to delete your log?")
if askyesorno == True:
os.remove(profile2log)
tk.messagebox.showinfo("Log Deleted", "Your log has been deleted successfully.")
else:
pass
else:
tk.messagebox.showerror("File Already Does Not Exist", "The file 'profile2.log' already does not exist.")
else:
tk.messagebox.showerror("File Already Does Not Exist", "The log files already does not exist.")
logfilecreation()
def sel():
selection = "You selected the Profile " + str(var.get())
label.config(text = selection)
var = tk.IntVar()
# Radiobuttons
R1 = tk.Radiobutton(new, text="Profile 1", variable=var, value=1,font=("Arial",10), command=sel, bg= bgcolor, fg= fcolor)
R1.grid(row = 2, column = 1, sticky = tk.W, padx= 5)
R2 = tk.Radiobutton(new, text="Profile 2", variable=var, value=2,font=("Arial",10), command=sel, bg= bgcolor, fg= fcolor)
R2.grid(row = 3, column = 1, sticky = tk.W, padx= 5)
# Labels
# Label for delete which profile
delete = tk.Label(new, text = "Delete which profile?", font=("Arial",12,'bold'), bg= bgcolor, fg= fcolor)
delete.grid(row = 1, column = 1, columnspan=3, sticky = tk.W, pady = 5, padx= 5)
# Label for selected profile.
label = tk.Label(new, font=("Arial",12,'bold'), bg= bgcolor, fg= fcolor)
label.grid(row=4, column=1, columnspan=3, sticky=tk.W, padx= 5, pady = 5)
# Buttons
# Button to delete.
delbutton = Button(new, text= "Delete", command=deletelog, width= 9)
delbutton.grid(row= 5, column= 1, sticky= tk.W, pady = 5, padx= 10)
# (RECOMMENDED TO NOT TOUCH) Window Placement tweaks.
window_height = 175
window_width = 235
screen_width = main.winfo_screenwidth()
screen_height = main.winfo_screenheight()
x_cordinate = int((screen_width/2) - (window_width/2))
y_cordinate = int((screen_height/2.5) - (window_height/2))
# (RECOMMENDED TO NOT TOUCH) Window Metadeta.
new.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))
new.title('Really?')
new.resizable(False, False)
new.configure(background=bgcolor)
new.grab_set()
# Profiles
class profile1:
# Class for Profile 1
def entry(key):
# Function to make entrying with a keypress possible.
if logentry.get() == '':
pass
else:
ts = time.time()
sttime = datetime.datetime.fromtimestamp(ts).strftime('%d/%m/%y; %I:%M:%S %p : ')
log = open(profile1log, "a")
logwrite = log.write(sttime + logentry.get() + '\n')
log.close()
logentry.delete(0, 'end')
class profile2:
# Class for Profile 2
def entry(key):
# Function to make entrying with a keypress possible.
if logentry.get() == '':
pass
else:
ts = time.time()
sttime = datetime.datetime.fromtimestamp(ts).strftime('%d/%m/%y; %I:%M:%S %p : ')
log = open(profile2log, "a")
logwrite = log.write(sttime + logentry.get() + '\n')
log.close()
logentry.delete(0, 'end')
# Customizations
customtextvar = StringVar()
customtext = ini.get("ThoughtLogger", "CustomText")[0:22]
customtextvar.set(customtext)
bgcolor = ini.get("ThoughtLogger", "BackgroundColor")
fcolor = ini.get("ThoughtLogger", "FontColor")
buttonstyle = Style()
buttonstyle.configure('TButton', font= ('Arial', 10),activeforeground = fcolor)
# Keybinds
main.bind('<Return>', profile1.entry)
main.bind('<Shift-Return>', profile2.entry)
main.bind('<Escape>', exit)
# Labels and Entries
# Label text
l1 = tk.Label(main, text = "Entry:", font=("Arial",12,'bold'), bg= bgcolor, fg= fcolor)
l1.grid(row = 1, column = 1, sticky = tk.W, pady= 15, padx = 5)
# Entry for text
logentry = tk.Entry(main, width= 47, bd=3)
logentry.grid(row = 1, column = 2, pady = 2,columnspan= 5, sticky=tk.E)
logentry.focus()
# Custom Text
custom = tk.Label(main, textvariable= customtextvar, font=("Arial",12, 'bold'), bg= bgcolor, fg= fcolor, anchor='w', width=22)
custom.grid(row = 2, column = 1, sticky = tk.W, columnspan=3, padx = 5)
# Buttons
# Button to open log file.
seelog = Button(main, text= "See Log", command=seelog, width= 8)
seelog.grid(row= 2, column= 5, sticky= tk.E)
# Button to delete log file.
deletelog = Button(main, text= "Delete Log", command=deletelogwindow, width= 10)
deletelog.grid(row= 2, column= 4, sticky= tk.E, padx= 15)
# (RECOMMENDED TO NOT TOUCH) Window Placement tweaks.
window_height = 100
window_width = 462
screen_width = main.winfo_screenwidth()
screen_height = main.winfo_screenheight()
x_cordinate = int((screen_width/2) - (window_width/2))
y_cordinate = int((screen_height/2.5) - (window_height/2))
# (RECOMMENDED TO NOT TOUCH) Window Metadeta.
main.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))
main.title('ThoughtLogger')
main.resizable(False, False)
# main.iconbitmap('thoughtlogger.ico')
main.tk.call('tk', 'scaling', 2.0)
main.configure(background=bgcolor)
logimport()
main.mainloop()
# Logging configuration
logging.basicConfig(filename='logging.log', level=logging.ERROR, format="%(asctime)s: %(exc_type)s (%(testtraceback)s): %(message)s in Line No %(errorline)s")
old_factory = logging.getLogRecordFactory()
def setcustomattr(*args, **kwargs):
# Function to set custom attributes
exception_type, exception_object, exception_traceback = sys.exc_info()
line_number = exception_traceback.tb_lineno
strexc = str(exception_type)
strobj = str(exception_object)
record = old_factory(*args, **kwargs)
record.exc_type = strexc.split("'")[1]
record.errorline = line_number
record.testtraceback = strobj.split("'")[0]
return record
logging.setLogRecordFactory(setcustomattr)
# Exception Handling
try:
thoughtlogger()
except Exception as e:
messagebox.showerror("Error","Something Went Wrong.\n\nIf this issue persists, please check the program log file ('logging.log') and report it on https://github.com/moiSentineL/ThoughtLogger")
logging.error('occurred')
sys.exit()
# thoughtlogger()
# Thank you for downloading and supporting moiSentineL.