-
Notifications
You must be signed in to change notification settings - Fork 1
/
header.py
609 lines (430 loc) · 20.4 KB
/
header.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
import os
from os import path
import time
import ctypes
import sys
import zlib
import getopt
import logging
from sys import exit
import traceback
#############################################################################
# #
# #
# LIGHTFILE #
# #
# #
#############################################################################
################################################################
# TODO:
#
# Re-add ARDT support (maybe even make it better?)
#
# There are several TODOs all over the file, maybe take a look at them
#
# in doAutomation function do:
# beter exception handeling
# and make something better
#
################################################################
ctypes.windll.kernel32.SetConsoleTitleW("LightFile") # window title
#logging thing, idk how to describe it
logging.basicConfig(filename='myapp.log', format='%(name)s - %(levelname)s - %(message)s', level=logging.INFO,)
logging.basicConfig(filename='LFdebug.log', encoding='utf-8', level=logging.DEBUG)
#main variables
Debug_level = "" # debug option used for debuging
input_file_path = "" # name and path of the input file
output_file_path = "" # name and path of the output file
operation = 3 # the operation, be 0 to compress, 1 to decompress or 2 to configurate lightfile
compress_level = 0 # the bigger the more compressed, at the expense of time to compress
#file variables
history_file = "history.lfh"
chunk_size = 32768
compressed_ext = ".lfc"
input_file = ""
output_file = ""
config_file_name = "config.cfg"
config_file_path = ""
language_folder_name = "lang"
#exceptions
class generalerror(Exception): #use this exception when something really unexpected happen, like something we can't really check for
def __init__(self):
logging.critical("a critical error accured, user probably tried to compress a file with a space in the filename")
ctypes.windll.kernel32.SetConsoleTitleW("LightFile -- :(")
print("an unknown error accured and the app cannot continue")
print("if you are seeing this error you probably tried to commpres a file with a space ' ', sadly we don't support files with spaces yet.")
print("error code: 0")
time.sleep(5)
exit()
#and yes i am aware we could check for spaces in the file name and replace them with "_" but i am too lazy to do that,
#also i would probably completely break this while doing that... Too bad!
class generalerror2(Exception):
def __init__(self):
logging.critical("Something unexpected error")
ctypes.windll.kernel32.SetConsoleTitleW("LightFile -- :(")
print("sorry someting went wrong on our side and the app cannot recover", '\n' "error code: 0")
time.sleep(5)
exit()
class invalidvalueerror(Exception):
def __init__(self):
logging.critical("Something unexpected error")
ctypes.windll.kernel32.SetConsoleTitleW("LightFile -- :(")
print("an invalid value has been detected, check config files", '\n' "error code: NaN")
time.sleep(5)
exit()
class missingconfig(Exception):
def __init__(self):
logging.warning("could not find the config files, using defult ones")
ctypes.windll.kernel32.SetConsoleTitleW("LightFile -- :O")
print("Sorry but we couldn't find the config files/folder, the app will continue using defult settings", '\n')
print("with the defult settings the compression level is 6 and debug options are disabled")
time.sleep(5)
pass
#other variables
light_file_version = "LightFile 1.1"
#keywords to be detected in the getOp() function
validConfigSelectors = ["o", "config", "configuration"]
validCompressionSelectors = ["c", "compress", "compression"]
validDecompressionSelectors = ["d", "decompress", "decompression"]
#################
#This loads up the config file
try:
os.chdir("configs")
compress_level = open("level.cfg", 'r').read()
Debug_level = open("debug.cfg", 'r').read()
os.chdir("..")
except:
#oh it looks like we couldn't find these files, let's notify the user about it and continue with the defult
compress_level = 6
Debug_level = False
raise missingconfig
# refresh the config function
def config_reload():
global compress_level
global Debug_level
#os.chdir("..")
os.chdir("configs")
# compress_level.close()
#Debug_level.close()
compress_level = "0"
Debug_level = ""
compress_level = open("level.cfg", 'r').read()
Debug_level = open("debug.cfg", 'r').read()
os.chdir("..")
return
try:
###################
#checks if the argument op is a valid operation selector and returns accordingly
#if not valid raises a ValueError exception
def getOp(op):
for selector in validCompressionSelectors:
if (op.lower() == selector):
return 0
for selector in validDecompressionSelectors:
if (op.lower() == selector):
return 1
for selector in validConfigSelectors:
if (op.lower() == selector):
return 2
raise ValueError("Not a valid operaton!")
#####################
#puts the appropriate values in the main variables
#from the arguments passed to lightfile in a commandline
def doAutomation():
#get the arguments from the sys.argv array
#starting at 1 because the python file is considered
#kind of an argument so it would glitch it out
#options are as following
#-o is the output file. not strictly nescessary as it will be defaulted to the same as the input one if it doesn't exist
#-i is the input file. it is nescessary and the program should not run without it.
#-h is the help option. print out a help message and exit the program
#-L is the compression level option. not nescessary and will default to 6
#-c and -d are the operation options for compression and decompression. it is nescessary and the program should not run without it.
#set globals
global input_file_path
global output_file_path
global operation
global compress_level
try:
opts, args = getopt.getopt(sys.argv[1:], "o:i:hL:cd")
except getopt.GetoptError as msg:
print(msg)
exit()
#loop through every option and do the checks
#TODO: better exception handling
#on it!
try:
for opt, arg in opts:
if opt == "-h":
#help message argument
#just print a help message and exit the program
print("-o is the output file. not strictly nescessary as it will be defaulted to the same as the input one if it doesn't exist")
print("#-i is the input file. it is nescessary and the program should not run without it.")
print("-h is the help option. print out a help message and exit the program")
print("-L is the compression level option. not nescessary and will default to 6")
print("-c and -d are the operation options for compression and decompression. it is nescessary and the program should not run without it.")
exit()
if opt == "-L":
#compression levle argument
#check if it is a number and is inbetween 0 and 9
#if it is raise exceptions accordingly otherwise set the compress_level variable to the argument
if(int(arg) > 9 or int(arg) < 0):
raise ValueError("Value {0} for argument {1} is out of the accepted range.".format(arg, opt))
compress_level = int(arg)
if opt == "-c":
#compress operation argument
#set operation to compress
operation = 0
if opt == "-d":
#decompress operation argument
#set operation to decompress
operation = 1
if opt == "-i":
#input file argument
#set input_file_path to the path provided here
input_file_path = arg
if opt == "-o":
#output file argument
#set output_file_path to the path provided here
output_file_path = arg
#do a few checks on the data to see if it is valid
#if no operation argument was provided, raise a ValueError exception
if(operation > 2):
raise ValueError("You need to select an operation argument!")
#if the input file path is empty, raise a ValueError exception
if(input_file_path == ""):
raise ValueError("You need to select an input file!")
#if the output file path is empty, set it to the input file path
if(output_file_path == ""):
output_file_path = input_file_path
#if there is no '/' or '\' in the input file path then add the current path to it to make it a full path
#TODO make this better
current_path = os.getcwd()
if "\\" not in input_file_path:
input_file_path = current_path + "\\" + input_file_path
#if there is no '/' or '\' in the input file path then add the current path to it to make it a full path
#TODO make this better
if "\\" not in output_file_path:
output_file_path = current_path + "\\" +output_file_path
except ValueError as msg:
print(msg)
exit()
###############################################
#prints the standard config option header and asks the user for an input,
#returning the user input
def standardConfigFunction(opt_name, opt_allowed_values, opt_current_value, opt_description):
#print the standard header for config files
os.system('cls||clear')
print("---------LIGHTFILE CONFIG---------\n\n")
print("Selected Option: {0}".format(opt_name))
print("Allowed values: {0}".format(opt_allowed_values))
print("Current value: {0}".format(opt_current_value))
print("Description:\n")
print(opt_description)
# get the user input and return it
return input("Insert a new value or keep empty if no changes need to be made: ")
#########################
#does the handling of the compression level config selection
def config_level():
global compress_level
os.system('cls||clear')
os.chdir("configs")
print("##########|Compress level|##########")
print("Level 0-9, the current compression level is:", compress_level, " you can change this value for better compression. however higher values may take more time" )
cfglevel = ""
new_compression_level = 0
new_compression_level = input(": ")
if int(new_compression_level) <= 9:
cfglevel = open("level.cfg", 'w')
if int(new_compression_level) > 9:
raise invalidvalueerror
cfglevel.write(new_compression_level)
cfglevel.close()
os.chdir("..")
return
############################
#does the handling of the Debug config selection
def config_Debug():
os.system('cls||clear')
os.chdir("configs")
print("##########|Debug|##########")
print("options: true/false ,current value:", Debug_level, '\n' '\n' "if set to true you will get debug messages")
new_debug_level = input(": ")
cfgdebug = open("debug.cfg", 'w')
cfgdebug.write(new_debug_level)
os.chdir("..")
return
###############################################
#handles the main config window
def config():
getting_config = True
while getting_config == True:
#reload config
config_reload()
#clear the screen again for a better user experience
os.system('cls||clear')
#print the config options
print(os.getcwd())
print(compress_level)
print(Debug_level)
print("---------{0} config---------\n\n".format(light_file_version))
print("Available options:\n")
print("Level -- Level of compression")
print("Exit -- Exits the configuration screen")
print("\n")
option = input("Insert option: ")
#check for options and call the according functions
if option == "level":
config_level()
if option == "exit":
getting_config = False
os.system('cls||clear')
return
#write the changes to disk
###################
#puts the appropriate vavlues in the main variables
#by getting them from the user and
def doUserInput():
#clean the screen since we are gonna get
#the input from the user and
#it will be cleaner that way
os.system('cls||clear')
logging.info("app was started")
#first off all get the operation
#using the getOp() function to check if the user input is valid
#and keep on asking the user until it is fully valid
print(compress_level)
print(Debug_level)
print(os.getcwd())
print("(C)ompression, (D)ecompression or C(O)nfig")
gettingOp = True
while (gettingOp == True):
try:
global operation
operation = getOp(input(": "))
except ValueError as noOp:
print(noOp)
else:
if(operation != 2): #if not config
gettingOp = False
else:
config()
print("(C)ompression, (D)ecompression or C(O)nfig")
#now we will get the total file path from the user
print("Insert the input file (example C:\\ExampleFolder\\ExampleFile.txt)")
global input_file_path
input_file_path = input(": ")
#now get the output file path and name
print("Insert the output file path and name (example C:\\ExampleFolder\\ExampleOutputFile)")
global output_file_path
output_file_path = input(": ")
#now we already have enough information to continue, so return
####################
# takes a path to the file to be compressed and the output file
# compresses the file in chunks and then appends the data to the
# output file
def compressFile(inpath, outpath):
cmpr = zlib.compressobj(compress_level)
try:
outfile = open(outpath + compressed_ext, "w")
infile = open(inpath, "rb")
except FileNotFoundError:
print("File {0} does not exist!".format(inpath))
exit()
except:
raise generalerror
#empty the output file and close it to be reopened in a new mode
outfile.write("")
outfile.close()
#reopen it in append mode
outfile = open(outpath + compressed_ext, "ab")
#read the first chunk
data = infile.read(chunk_size)
#loop through every chunk, compressing and flushing into the file
#every loop until the entire file was read
while data:
outfile.write(cmpr.compress(data))
outfile.flush()
data = infile.read(chunk_size)
#write the last bits to the file and close it
outfile.write(cmpr.flush())
outfile.close()
##########################
# takes a path to the file to be decompressed and the output file
# decompresses the file in chunks and then appends the data to the
# output file
def decompressFile(inpath, outpath):
dcmpr = zlib.decompressobj()
try:
outfile = open(outpath, "w")
infile = open(inpath, "rb")
except FileNotFoundError:
logging.critical('This file does not exist!')
print("File {0} does not exist!".format(inpath))
exit()
#empty the output file and close it to be reopened in a new mode
outfile.write("")
outfile.close()
#reopen it in append mode
outfile = open(outpath, "ab")
#read the first chunk
data = infile.read(chunk_size)
#loop through every chunk, decompressing and flushing into the file
#every loop until the entire file was read
while data:
outfile.write(dcmpr.decompress(data))
outfile.flush()
data = infile.read(chunk_size)
#write the last bits to the file and close it
outfile.write(dcmpr.flush())
outfile.close()
#######################################
#get the path to the location of the main executable
#depending on if we're executing through a .py file or through an executable
#we will have to use different methods to get it
if getattr(sys, 'frozen', False): #PyInstaller Executable
exec_path = sys.executable
else: # on a .py file
exec_path = getattr(sys, '_MEIPASS', __file__)
#now get the path to the installation directory from the path to the main executable
#and then create the path to the history file and config file
exec_path, exec_file_name = os.path.split(exec_path)
history_path = exec_path + "\\" + history_file
config_file_path = exec_path + "\\" + config_file_name
#then we are going to call the config load function to
#load the appropriate variables.
#config_load()
#now that we loaded the config into the correct variables,
#we will load the language lines from the appropriate file
#check if there are any arguments
#if there are, get the information from the arguments
#otherwise go with the user input inside the app
if(len(sys.argv) > 1):
doAutomation()
else:
doUserInput()
#now check whether we are compressing or decompressing
#and call the function accordingly
start_time = time.time() # time so we can get the elapsed seconds later
if(operation == 0):
compressFile(input_file_path, output_file_path)
#since we are doing compression,
#write the path to the history file
try:
history_file = open(history_path, "w")
except:
#if not found print an error message and continue with executing the program
print("an error occured while opening the history ({0}) file!".format(history_path))
else:
#write the path
history_file.write(input_file_path)
history_file.close()
else:
decompressFile(input_file_path, output_file_path)
#print the elapsed time rounded to two decimal points in second
print("Elapsed time: {0} seconds".format(round(time.time() - start_time, 2)))
except KeyboardInterrupt:
logging.critical('user pressed ctrl+C, application terminated')
print("keybord interupt deteced!")
except:
raise generalerror2