generated from Code-Institute-Org/python-essentials-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
348 lines (310 loc) · 10.1 KB
/
run.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
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
348
"""
Rapid Silver is a mini business utility tool for users who are self-employed,
freelancers, or for small to medium sized businesses users. They can use this
tool too calculate and forecast profits for their products,
create mailing lists, send emails, create an organization structure,
design employee spread sheets
etc.
"""
import sys
import time
from console import clear_console
from rapid_silver.password_manager import PasswordManager
from rapid_silver.data_manager import DataManager
from rapid_silver.text_art import TextArt
# class instances
COLOR = TextArt()
LOADING = COLOR # used for loading animations
# global variables
INVALID = False
def welcome_msg():
"""
Prints welcome text to the terminal. Helps direct the user and lead
on to navigation options.
"""
try:
file = open('assets/text/welcome.txt', encoding='utf8')
welcome = file.read()
file.close()
except IOError:
return "Welcome to Rapid Silver" # in the event an error occurs
return welcome
def open_login_portal():
"""
When called it displays the log in screen to the terminal.
Gets the user type. Sets up a new account for new users and
logs in returning users. Calls login_user() or create_account()
and assigns it to variable user.
"""
try:
clear_console()
file = open('assets/text/login.txt', encoding='utf8')
login_msg = file.read()
file.close()
print('\n' + COLOR.blue_fore(login_msg))
except IOError:
print('Log in screen') # in event reading log in message fails
print(COLOR.yellow_fore(
"\n\t\tHit [ e ] + Enter to login"))
print(COLOR.yellow_fore(
"\t\tHit [ d ] + Enter to create new account"))
print(COLOR.yellow_fore(
"\t\tHit [ b ] + Enter to read how your data is stored"))
try:
result = input(COLOR.green_fore('\n\t\tHere: '))
if result not in ('e', 'd', 'b'):
print(COLOR.red_fore('\t\tInvalid input, enter e or d'))
time.sleep(2)
clear_console()
open_login_portal()
if result == 'e':
user = login_user()
return user
if result == 'd':
user = create_account()
return user
if result == 'b':
pull_up_data_protection()
input(COLOR.red_fore('\t\tHit Enter to go back to log in portal'))
open_login_portal() # uses recursion until valid login input
except ValueError:
open_login_portal() # uses recursion until valid login input
except TypeError:
open_login_portal() # uses recursion until valid login input
def login_user():
"""
When called it pulls up the log in screen for the user.
"""
print('\n\n')
LOADING.money_loading('\t\tOpening login now')
clear_console()
to_login = PasswordManager('old') # runs route for new user
return to_login
def create_account():
"""
When called it pulls up the log in for creating a user account.
"""
print('\n\n')
LOADING.money_loading('\t\tAccount creation enabled')
clear_console()
to_login = PasswordManager('new') # runs route for new user
return to_login
def pull_up_data_protection():
"""
Opens data protection information for the user to look at.
Reassures user of entering sensitive information and logging in.
"""
try:
clear_console()
file = open('assets/text/data_protection.txt', encoding='utf8')
data_protection = file.read()
file.close()
print(
COLOR.cyan_fore(data_protection)
)
except IOError:
print("ERROR: reading dataprotection failed..")
print('We are GDPR compliant')
def load_details():
"""
At the beginning of the program this describes the purpose of the CLI
application and what the intended functionality is.
"""
clear_console()
print('\n\n\n\n\n')
print(COLOR.cyan_fore('\n\t\t\tAccessing internal database now.\n'))
LOADING.star_loading('\t\t\tLoading results ')
time.sleep(0.3)
clear_console()
print(COLOR.red_fore('\n\t\t\tWelcome to Rapid Silver\n\n'))
try:
file = open('assets/text/rapid_details.txt', encoding='utf8')
details = file.read()
print(COLOR.yellow_fore(details))
input('\n\n\n\t\t\t\tHit enter to continue')
except IOError:
print(COLOR.red_fore("""
\n\nOops.. fetching details failed, trying again."""))
time.sleep(2.5)
clear_console()
load_details()
def get_options():
"""
Displays available options to the user for the Rapid Silver
CLI application.
"""
clear_console()
try:
file = open('assets/text/options.txt', encoding='utf8')
option_screen = file.read()
file.close()
print('\n\n' + COLOR.yellow_fore(option_screen) + '\n\n')
except IOError:
print(COLOR.yellow_fore('\t\t\tOptions\n\n\n\n'))
def open_selection_menu(validated_user):
"""
Once a user is logged in and validated the main route menu
is shown and available options are displayed to the user.
"""
clear_console()
user = validated_user
get_options()
print(
COLOR.red_fore(
'\tAvailable options:'
)
)
print(
COLOR.yellow_fore(
'\n\tHit [ q ] + Enter for > Set up user profile'
)
)
print(
COLOR.yellow_fore(
'\tHit [ w ] + Enter for > To do list'
)
)
print(
COLOR.yellow_fore(
'\tHit [ f ] + Enter for > Storing inventory/ updating inventory'
)
)
print(
COLOR.yellow_fore(
'\tHit [ h ] + Enter for > Information on data protection'
)
)
print(
COLOR.yellow_fore(
'\tHit [ i ] + Enter for > How data is stored and protected'
)
)
print(
COLOR.cyan_fore(
'\tHit [ e ] + Enter for > Logging out and closing Rapid Silver'
)
)
try:
result = input(
COLOR.green_fore('\n\t\tEnter here: ')
)
if result not in ('q', 'w', 'f', 'h', 'i', 'e'):
raise ValueError(
'INVALID INPUT. Try again.'
)
if result is TypeError:
raise TypeError(
'INVALID INPUT. Try again.'
)
except ValueError as error:
print(
COLOR.red_fore(f'{error}')
)
open_selection_menu(validated_user) # recursion until valid
except TypeError as error:
print(
COLOR.red_fore(f'{error}')
)
open_selection_menu(validated_user) # recursion until valid
if result == 'q':
set_up_profile(user)
elif result == 'w':
set_up_to_do_list(user)
elif result == 'f':
store_update_stock_for(user)
elif result == 'h':
pull_up_data_protection()
input(COLOR.red_fore('\t\tHit enter to go back to selection menu'))
open_selection_menu(validated_user) # recursive call to back to menu
elif result == 'i':
clear_console()
explain_data_storage_to(validated_user)
input(COLOR.yellow_fore('\t\tHit enter to go back to selection menu'))
open_selection_menu(validated_user) # recursive call to back to menu
elif result == 'e':
clear_console()
sys.exit()
def set_up_profile(validated_user):
"""
Sets up a logged in users profile and returns to the selection menu.
"""
try:
clear_console()
print('\n\n\n\n\nLets check to see if you have a profile first.')
LOADING.star_loading('Checking for user profile')
DataManager(
validated_user.username, 'profile') # opens the data manager
time.sleep(2)
except AttributeError:
set_up_profile(validated_user)
open_selection_menu(validated_user)
def set_up_to_do_list(validated_user):
"""
The user can set up or update their to do list.
"""
try:
clear_console()
LOADING.hash_loading('Checking for to do list now')
DataManager(validated_user.username, 'to_do') # opens the data manager
time.sleep(2)
except AttributeError:
set_up_to_do_list(validated_user)
open_selection_menu(validated_user)
def store_update_stock_for(validated_user):
"""
Sets up a logged in users profile and returns to the selection menu.
"""
try:
clear_console()
LOADING.money_loading('Checking your inventory now')
DataManager(
validated_user.username, 'inventory') # opens the data manager
time.sleep(2)
except AttributeError:
store_update_stock_for(validated_user)
open_selection_menu(validated_user)
def explain_data_storage_to(validated_user):
"""
Sets up a logged in users profile and returns to the selection menu.
"""
try:
file = open('assets/text/data_storage.txt', encoding='utf8')
data_notice = file.read()
file.close()
print(
COLOR.red_fore(
data_notice
)
)
except IOError:
print('Oops an error has occurred')
time.sleep(2)
print('Fetching files again.')
print('Please wait')
open_selection_menu(validated_user)
def main():
"""
Runs the program.
"""
# Add some new lines before loader
for _ in range(5):
print('\n')
LOADING.hash_loading('\t\tLoading terminal for Rapid Silver')
clear_console()
# Load welcome message and login screen
print(COLOR.red_fore(welcome_msg()))
input(COLOR.cyan_fore('\n\n\n\t\t\t<Press enter to continue to log in>'))
load_details()
clear_console()
LOADING.star_loading('Opening login portal now')
# gets or sets up a user profile
validated_user = open_login_portal()
clear_console()
# once user has either created an account or returned
# they will have to log in
# brings the user to all the available options and its the last call
# in the main function.
# user can navigate back to the selection menu from all of the available
# options
open_selection_menu(validated_user)
main()