Some minor changes
This commit is contained in:
parent
06e53e7972
commit
0ed8d1482b
38
huffman.py
38
huffman.py
@ -2,6 +2,7 @@ from os.path import dirname, basename, join, abspath as path
|
|||||||
from time import time
|
from time import time
|
||||||
from datetime import datetime as dt
|
from datetime import datetime as dt
|
||||||
import click
|
import click
|
||||||
|
from urllib.parse import quote as url
|
||||||
|
|
||||||
|
|
||||||
class Log:
|
class Log:
|
||||||
@ -23,6 +24,7 @@ class Log:
|
|||||||
|
|
||||||
|
|
||||||
log = Log(join(dirname(__file__), 'hfm.log'), 'hfm-py')
|
log = Log(join(dirname(__file__), 'hfm.log'), 'hfm-py')
|
||||||
|
output = ''
|
||||||
|
|
||||||
|
|
||||||
def huffman(data):
|
def huffman(data):
|
||||||
@ -107,6 +109,7 @@ def shichiro_decode(byts):
|
|||||||
|
|
||||||
|
|
||||||
def compress_file(filename):
|
def compress_file(filename):
|
||||||
|
global output
|
||||||
log.log(f"Loading '{filename}'...")
|
log.log(f"Loading '{filename}'...")
|
||||||
with open(filename, 'rb') as file: # get data
|
with open(filename, 'rb') as file: # get data
|
||||||
data = list(map(int, file.read()))
|
data = list(map(int, file.read()))
|
||||||
@ -135,11 +138,12 @@ def compress_file(filename):
|
|||||||
with open(f'{filename}.hfm', 'wb') as file: # save Haffman code
|
with open(f'{filename}.hfm', 'wb') as file: # save Haffman code
|
||||||
file.write(bytes(out))
|
file.write(bytes(out))
|
||||||
log.log('SUCCESSFULLY COMPRESSED')
|
log.log('SUCCESSFULLY COMPRESSED')
|
||||||
print(f'"origSize":{len(data)},')
|
output += f'"origSize":{len(data)},'
|
||||||
print(f'"compSize":{len(out)},')
|
output += f'"compSize":{len(out)},'
|
||||||
|
|
||||||
|
|
||||||
def decompress_file(filename):
|
def decompress_file(filename):
|
||||||
|
global output
|
||||||
log.log(f"Loading '{filename}'...")
|
log.log(f"Loading '{filename}'...")
|
||||||
with open(filename, 'rb') as file: # get data
|
with open(filename, 'rb') as file: # get data
|
||||||
data = [bin(byte)[2:].rjust(8, '0') for byte in file.read()]
|
data = [bin(byte)[2:].rjust(8, '0') for byte in file.read()]
|
||||||
@ -170,39 +174,45 @@ def decompress_file(filename):
|
|||||||
if stack in table:
|
if stack in table:
|
||||||
out.append(int(table[stack]))
|
out.append(int(table[stack]))
|
||||||
stack = ''
|
stack = ''
|
||||||
|
if filename[-4:] == '.hfm':
|
||||||
filename = filename[:-4]
|
filename = filename[:-4]
|
||||||
log.log(f"Saving to '{filename}'...")
|
log.log(f"Saving to '{filename}'...")
|
||||||
with open(f'{filename}', 'wb') as file: # save decoded data
|
with open(f'{filename}', 'wb') as file: # save decoded data
|
||||||
file.write(bytes(out))
|
file.write(bytes(out))
|
||||||
log.log(f'SUCCESSFULLY DECOMPRESSED')
|
log.log(f'SUCCESSFULLY DECOMPRESSED')
|
||||||
print(f'"compSize":{os},')
|
output += f'"compSize":{os},'
|
||||||
print(f'"origSize":{len(out)},')
|
output += f'"origSize":{len(out)},'
|
||||||
|
|
||||||
|
|
||||||
@click.command(options_metavar='[-c / -d]')
|
@click.command(options_metavar='[-c / -d]')
|
||||||
@click.argument('files', nargs=-1, metavar='<file [file [...]]>')
|
@click.argument('files', nargs=-1, metavar='<file [file [...]]>')
|
||||||
@click.option('-c/-d', 'comp', default=True, help='Compress/decompress mode selectors.')
|
@click.option('-c/-d', 'comp', default=True, help='Compress/decompress mode selectors.')
|
||||||
def CLI(files, comp):
|
def CLI(files, comp):
|
||||||
log.log(f'hfm {"-c" * comp}{"-d" * (not comp)} {" ".join(files)}')
|
global output
|
||||||
|
log.log(f'hfm {"-c" * comp}{"-d" * (not comp)} "' + "\" \"".join(files) + '"')
|
||||||
for file in files:
|
for file in files:
|
||||||
print('{')
|
output += '{'
|
||||||
stime = time()
|
stime = time()
|
||||||
if comp:
|
if comp:
|
||||||
|
try:
|
||||||
compress_file(path(file))
|
compress_file(path(file))
|
||||||
wtime = time() - stime
|
wtime = time() - stime
|
||||||
print('"status":true,')
|
output += '"status":true,'
|
||||||
print(f'"time":{round(wtime, 3)},')
|
output += f'"time":{round(wtime, 3)},'
|
||||||
print(f'"dlink":"./files/{basename(file) + ".hfm"}"')
|
output += f'"dlink":"./files/{url(basename(file)) + ".hfm"}"'
|
||||||
|
except Exception as e:
|
||||||
|
output += f'"status":false'
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
decompress_file(path(file))
|
decompress_file(path(file))
|
||||||
wtime = time() - stime
|
wtime = time() - stime
|
||||||
print('"status":true,')
|
output += '"status":true,'
|
||||||
print(f'"time":{round(wtime, 3)},')
|
output += f'"time":{round(wtime, 3)},'
|
||||||
print(f'"dlink":"./files/{basename(file)[:-4]}"')
|
output += f'"dlink":"./files/{url(basename(file)[:-4])}"'
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f'"status":false')
|
output += f'"status":false'
|
||||||
print('}')
|
output += '}'
|
||||||
|
print(output)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
@ -55,7 +55,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="error">
|
<div class="error">
|
||||||
<h3>Error!</h3>
|
<h3>Error!</h3>
|
||||||
<p class="status">Unable to decompress this file!</p>
|
<p class="status">Something went wrong!</p>
|
||||||
<div class="btncont">
|
<div class="btncont">
|
||||||
<button class="btn closebtn">Close</button>
|
<button class="btn closebtn">Close</button>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
19
script.js
19
script.js
@ -58,22 +58,25 @@ $('form').on('submit', function submit(e) {
|
|||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
data: form,
|
data: form,
|
||||||
success: function (resp) {
|
success: function (resp) {
|
||||||
if (resp.status) {
|
if (resp===null) {
|
||||||
$('.process').css('display', 'none')
|
$('.process').css('display', 'none');
|
||||||
$('.complete').css('display', 'block')
|
$('.error').css('display', 'block');
|
||||||
|
} else if (resp.status) {
|
||||||
|
$('.process').css('display', 'none');
|
||||||
|
$('.complete').css('display', 'block');
|
||||||
if ($('#mode').val() == 'compress') {
|
if ($('#mode').val() == 'compress') {
|
||||||
$('.complete .status').html(
|
$('.complete .status').html(
|
||||||
`Original size: ${resp.origSize} B<br>Compressed size: ${resp.compSize} B<br>Time: ${resp.time} s`
|
`Original size: ${resp.origSize} B<br>Compressed size: ${resp.compSize} B<br>Time: ${resp.time} s`
|
||||||
)
|
);
|
||||||
} else {
|
} else {
|
||||||
$('.complete .status').html(
|
$('.complete .status').html(
|
||||||
`Compressed size: ${resp.compSize} B<br>Original size: ${resp.origSize} B<br>Time: ${resp.time} s`
|
`Compressed size: ${resp.compSize} B<br>Original size: ${resp.origSize} B<br>Time: ${resp.time} s`
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
$('#dlink').attr('href', resp.dlink)
|
$('#dlink').attr('href', resp.dlink);
|
||||||
} else {
|
} else {
|
||||||
$('.process').css('display', 'none')
|
$('.process').css('display', 'none');
|
||||||
$('.error').css('display', 'block')
|
$('.error').css('display', 'block');
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user