-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
73 lines (58 loc) · 2.43 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
from colorama import init, Fore, Style
import argparse
import bsc
import os
from threading import Thread
import random
endStr = ["Thanks, come again!", "😊", "© by Michelle Bacher and Aaron Schmid",
"We <3 Huffman", "provided by Kinderschokolade", "Your advertisement could be here!"]
def compress(file, output, pw):
"""Wrapper for compress function"""
try:
bsc.compress_file(file, output, pw)
print(Fore.GREEN + "Compressed!")
except bsc.FrequencyOverflowException as err:
print(err)
except FileNotFoundError:
print(Fore.RED + "File not found!")
def decompress(file, output, pw):
"""Wrapper for decompress function"""
try:
bsc.decompress_file(file, output, pw)
print(Fore.GREEN + "Decompressed!")
except bsc.InvalidPasswordException:
print(Fore.RED + "Password is invalid!")
except bsc.InvalidFileFormatException:
print(Fore.RED + "File not compressed with BSC!")
except FileNotFoundError:
print(Fore.RED + "File not found!")
def wheel(text, func, args):
"""Execute func and wait till finished"""
animation = "|/-\\"
idx = 0
thread = Thread(target=func, args=args)
thread.start()
while thread.join(0.1) or thread.isAlive():
print(Fore.WHITE + text + " " + animation[idx % len(animation)], end="\r")
idx += 1
parser = argparse.ArgumentParser(description='Compress files with our favorite huffman')
parser.add_argument('file', metavar='file', help='Input file to compress or decompress')
parser.add_argument('--out', '-o', dest='out', metavar='file', help='Output file from compression or decompression')
parser.add_argument('--decompress', '-d', dest='decompress',
help='Decompress input file', action='store_true', default=False)
parser.add_argument('--password', '-p', dest='pw', metavar='password',
help='Password for encryption / decryption', default=None)
args = parser.parse_args()
init()
print(Fore.LIGHTBLACK_EX + Style.DIM + "Welcome to BSC compressor v1.0")
if args.decompress:
output = os.path.splitext(args.file)[0]
if args.out:
output = args.out
wheel("Decompressing", decompress, (args.file, output, args.pw))
else:
output = os.path.splitext(args.file)[0] + '.bsc'
if args.out:
output = args.out
wheel("Compressing", compress, (args.file, output, args.pw))
print(Fore.LIGHTBLACK_EX + Style.DIM + random.choice(endStr))