-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.py
168 lines (142 loc) · 4.85 KB
/
notes.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
import os
from cryptography.fernet import Fernet
from os.path import exists
from time import sleep
# prints all files
nn = ['1','new', 'New', 'New Note', 'new note', 'n', 'nn']
on = ['2','open', 'Open', 'Open Note', 'open note', 'o', 'r']
gk = ['3', 'key', 'gk', 'get key', 'get']
path = "./notes"
class Note:
def __init__(self, title, msg):
self.title = title
self.msg = msg
def write():
m1 = input('Title: ')
m2 = input('Note: ')
print(m2)
note = Note(m1,m2)
print(f'{note.title} \n {note.msg}')
f = open(f'./notes/{note.title}.txt', 'xt')
f.write(f'{note.msg}')
f.close()
return print(f'{note.title} {note.msg}')
def read():
dir_list = os.listdir(path)
answer = input('Select a File: ')
if f'{answer}.txt' in dir_list:
f = open(f'./notes/{answer}.txt', "r")
print(f'Title: {answer}')
m = f.read()
print(m)
return answer
def delete(x):
if os.path.exists(f'./notes/{x}.txt'):
os.remove(f'./notes/{x}.txt')
else:
print("The file does not exist")
def encrypt(x):
# opening the key
with open('mykey.key', 'rb') as filekey:
key = filekey.read()
# using the generated key
fernet = Fernet(key)
# opening the original file to encrypt
with open(f'./notes/{x}.txt', 'rb') as file:
original = file.read()
# encrypting the file
encrypted = fernet.encrypt(original)
# opening the file in write mode and
# writing the encrypted data
with open(f'./notes/{x}.txt', 'wb') as encrypted_file:
encrypted_file.write(encrypted)
def decrypt(x):
# opening the key
with open('mykey.key', 'rb') as filekey:
key = filekey.read()
fernet = Fernet(key)
# opening the encrypted file
with open(f'./notes/{x}.txt', 'rb') as enc_file:
encrypted = enc_file.read()
# decrypting the file
decrypted = fernet.decrypt(encrypted)
# opening the file in write mode and
# writing the decrypted data
with open(f'./notes/{x}.txt', 'wb') as dec_file:
dec_file.write(decrypted)
def get_key():
if os.path.exists('mykey.key'):
answer = input(f'mykey.key already exists! \n Would you like to change it? (y/n): ')
if answer == 'n':
Note.doRun()
elif answer == 'y':
confirm = input('Are you sure!? (y/n)')
if confirm == 'n':
print('Aborted!')
sleep(2)
Note.doRun()
elif confirm == 'y':
key = Fernet.generate_key() #this is your "password"
with open('mykey.key', 'wb') as mykey:
mykey.write(key)
else:
key = Fernet.generate_key() #this is your "password"
with open('mykey.key', 'wb') as mykey:
mykey.write(key)
print('keyfile created!')
def start():
if exists('mykey.key'):
print('keyfile found!')
sleep(.3)
print('Checking Folders...')
sleep(.3)
if not os.path.exists('notes'):
print('Making notes directory...')
sleep(.3)
os.makedirs('./notes')
else:
print('notes folder exists!')
global dir_list
dir_list = os.listdir(path)
else:
Note.get_key()
def menu():
print(' ________________')
print('| shTTY NOTES |')
print('|----------------|')
print('| 1) new note |')
print('| 2) open note |')
print('| 3) get key |')
print('|________________|')
print("\n Files:")
dir_list = os.listdir(path)
print(dir_list)
global q
q = input('What would you like to do?: ')
def doRun():
Note.menu()
Note.selection(q)
def selection(x):
if x in nn:
Note.write()
print('-New Note-')
elif x in on:
print('-Open Note-')
t = Note.read()
back = input('hit ENTER to go back | d to Delete | e to Encrypt: | y to decrypt:\n')
if back == '':
pass
if back == 'd':
Note.delete(t)
if back == 'e':
Note.encrypt(t)
if back == 'y':
Note.decrypt(t)
elif x in gk:
print('-Get Key-')
sleep(.5)
Note.get_key()
else:
print('Sorry, an option!')
sleep(1)
Note.doRun()