From 0ed8d1482b66165cb5f5cfe96bde7532f3426042 Mon Sep 17 00:00:00 2001 From: Masahiko AMANO Date: Thu, 9 Dec 2021 16:16:33 +0300 Subject: [PATCH] Some minor changes --- huffman.py | 44 +++++++++++++++++++++++++++----------------- index.html | 2 +- script.js | 19 +++++++++++-------- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/huffman.py b/huffman.py index cb732ab..ac4ec5e 100644 --- a/huffman.py +++ b/huffman.py @@ -2,6 +2,7 @@ from os.path import dirname, basename, join, abspath as path from time import time from datetime import datetime as dt import click +from urllib.parse import quote as url class Log: @@ -23,6 +24,7 @@ class Log: log = Log(join(dirname(__file__), 'hfm.log'), 'hfm-py') +output = '' def huffman(data): @@ -107,6 +109,7 @@ def shichiro_decode(byts): def compress_file(filename): + global output log.log(f"Loading '{filename}'...") with open(filename, 'rb') as file: # get data 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 file.write(bytes(out)) log.log('SUCCESSFULLY COMPRESSED') - print(f'"origSize":{len(data)},') - print(f'"compSize":{len(out)},') + output += f'"origSize":{len(data)},' + output += f'"compSize":{len(out)},' def decompress_file(filename): + global output log.log(f"Loading '{filename}'...") with open(filename, 'rb') as file: # get data data = [bin(byte)[2:].rjust(8, '0') for byte in file.read()] @@ -170,39 +174,45 @@ def decompress_file(filename): if stack in table: out.append(int(table[stack])) stack = '' - filename = filename[:-4] + if filename[-4:] == '.hfm': + filename = filename[:-4] log.log(f"Saving to '{filename}'...") with open(f'{filename}', 'wb') as file: # save decoded data file.write(bytes(out)) log.log(f'SUCCESSFULLY DECOMPRESSED') - print(f'"compSize":{os},') - print(f'"origSize":{len(out)},') + output += f'"compSize":{os},' + output += f'"origSize":{len(out)},' @click.command(options_metavar='[-c / -d]') @click.argument('files', nargs=-1, metavar='') @click.option('-c/-d', 'comp', default=True, help='Compress/decompress mode selectors.') 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: - print('{') + output += '{' stime = time() if comp: - compress_file(path(file)) - wtime = time() - stime - print('"status":true,') - print(f'"time":{round(wtime, 3)},') - print(f'"dlink":"./files/{basename(file) + ".hfm"}"') + try: + compress_file(path(file)) + wtime = time() - stime + output += '"status":true,' + output += f'"time":{round(wtime, 3)},' + output += f'"dlink":"./files/{url(basename(file)) + ".hfm"}"' + except Exception as e: + output += f'"status":false' else: try: decompress_file(path(file)) wtime = time() - stime - print('"status":true,') - print(f'"time":{round(wtime, 3)},') - print(f'"dlink":"./files/{basename(file)[:-4]}"') + output += '"status":true,' + output += f'"time":{round(wtime, 3)},' + output += f'"dlink":"./files/{url(basename(file)[:-4])}"' except Exception as e: - print(f'"status":false') - print('}') + output += f'"status":false' + output += '}' + print(output) if __name__ == '__main__': diff --git a/index.html b/index.html index 4c62493..c9778df 100644 --- a/index.html +++ b/index.html @@ -55,7 +55,7 @@

Error!

-

Unable to decompress this file!

+

Something went wrong!

diff --git a/script.js b/script.js index 9dd6e39..1c9c1be 100644 --- a/script.js +++ b/script.js @@ -58,22 +58,25 @@ $('form').on('submit', function submit(e) { dataType: 'json', data: form, success: function (resp) { - if (resp.status) { - $('.process').css('display', 'none') - $('.complete').css('display', 'block') + if (resp===null) { + $('.process').css('display', 'none'); + $('.error').css('display', 'block'); + } else if (resp.status) { + $('.process').css('display', 'none'); + $('.complete').css('display', 'block'); if ($('#mode').val() == 'compress') { $('.complete .status').html( `Original size:   ${resp.origSize} B
Compressed size: ${resp.compSize} B
Time:            ${resp.time} s` - ) + ); } else { $('.complete .status').html( `Compressed size: ${resp.compSize} B
Original size:   ${resp.origSize} B
Time:            ${resp.time} s` - ) + ); } - $('#dlink').attr('href', resp.dlink) + $('#dlink').attr('href', resp.dlink); } else { - $('.process').css('display', 'none') - $('.error').css('display', 'block') + $('.process').css('display', 'none'); + $('.error').css('display', 'block'); }; }, });