going-flying.com gemini git repository
18200dff598a806a40b358c6346d3adc62559e39 - Matthew Ernisse - 1612578920
converterrrrr
diff --git a/cgi-bin/converter b/cgi-bin/converter index c901029..deb59e1 100755 --- a/cgi-bin/converter +++ b/cgi-bin/converter @@ -32,22 +32,27 @@ import sys from urllib.parse import parse_qs, urlparse +BACK_LINK = '/cgi-bin/converter/' + + class GeminiCGI(object): class Response(object): codes = { - 10: '10', - 20: '20', - 50: '50 PERMANENT FAILURE', - 51: '51 NOT FOUND', - 59: '59 BAD REQUEST', + 10: ('10', ''), + 20: ('20', ''), + 50: ('50', 'PERMANENT FAILURE'), + 51: ('51', 'NOT FOUND'), + 59: ('59', 'BAD REQUEST'), } def __init__(self, code=10, meta=''): ''' Construct the response header per spec 3.1 ''' - self.header = self.codes[code] - if code <= 20: + self.header = self.codes[code][0] + if meta: self.header += ' ' + meta + elif self.codes[code][1]: + self.header += ' ' + self.codes[code][1] self.header += '\r\n' @@ -60,8 +65,8 @@ class GeminiCGI(object): return cls(20, meta) @classmethod - def Fail(cls): - return cls(50) + def Fail(cls, meta=None): + return cls(50, meta) @classmethod def NotFound(cls): @@ -89,57 +94,123 @@ def display_main_menu(cgi): response.print_header() print('''# Matt's Gemini Toolbox. -=> bintohex Binary to Hexadecimal -=> dectohex Decimal to Hexadecimal -=> hextobin Hexadecimal to Binary +## Display a number in Binary, Decimal and Hexadecimal. +=> bin Binary Input +=> dec Decimal Input +=> hex Hexadecimal Input => / Home ''') +def pretty_binary(i): + j = bin(i)[2:] + out = [] + + while len(j) > 8: + print(j) + out.insert(0, j[len(j) - 8:]) + j = j[:len(j) - 8] + + + return j + ' ' + ' '.join(out) + + +def result_table(input, b, d, h): + ''' Pretty print a table''' + global BACK_LINK + + output = '```\n' + output += f'# Input\n{input}\n\n' + output += f'## Binary Representation\n{b}\n\n' + output += f'## Decimal Representation\n{d}\n\n' + output += f'## Hexadecimal Representation\n0x{h}\n\n' + output += f'=> {BACK_LINK} Back\n' + output += f'=> / Home' + print(output) + + if __name__ == '__main__': cgi = GeminiCGI() + if not cgi.query_string: if not cgi.path_info: display_main_menu(cgi) sys.exit() - elif cgi.path_info == 'bintohex': + elif cgi.path_info == 'bin': response = cgi.Response.Input('Binary Number?') response.print_header() sys.exit() - elif cgi.path_info == 'dectohex': + elif cgi.path_info == 'dec': response = cgi.Response.Input('Number?') response.print_header() sys.exit() - elif cgi.path_info == 'hextobin': + elif cgi.path_info == 'hex': response = cgi.Response.Input('Hex Number?') response.print_header() sys.exit() else: if not cgi.path_info: - response = cgi.Response.BadRequest + response = cgi.Response.BadRequest() response.print_header() - elif cgi.path_info == 'dectohex': - response = cgi.Response.Ok('text/plain') + elif cgi.path_info == 'bin': + s = cgi.query_string + + try: + d = int(s, 2) + b = pretty_binary(d) + h = f'{d:02X}' + except Exception: + response = cgi.Response.Fail('Invalid input') + response.print_header() + raise + sys.exit() + + response = cgi.Response.Ok('text/gemini') response.print_header() - print(cgi.query_string) - sys.exit() + result_table(s, b, d, h) - elif cgi.path_info == 'bintohex': - response = cgi.Response.Ok('text/plain') + elif cgi.path_info == 'dec': + s = cgi.query_string + + try: + d = int(s) + b = pretty_binary(d) + h = f'{d:02X}' + except Exception: + response = cgi.Response.Fail('Invalid input') + response.print_header() + sys.exit() + + response = cgi.Response.Ok('text/gemini') response.print_header() - print(cgi.query_string) - sys.exit() + result_table(s, b, d, h) + + elif cgi.path_info == 'hex': + s = cgi.query_string + + try: + d = int(s, 16) + b = pretty_binary(d) + h = f'{d:02X}' + except Exception: + response = cgi.Response.Fail('Invalid input') + response.print_header() + sys.exit() + + response = cgi.Response.Ok('text/gemini') + response.print_header() + result_table(s, b, d, h) - elif cgi.path_info == 'hextobin': - response = cgi.Response.Ok('text/plain') + else: + response = cgi.Response.NotFound() response.print_header() - print(cgi.query_string) - sys.exit() + +