going-flying.com gemini git repository
24416092d14d1a722644344c83eb84b98c64fe9a - Matthew Ernisse - 1612580802
make the cgi stuff a little neater
diff --git a/cgi-bin/converter b/cgi-bin/converter index 7901c7b..b7c61fa 100755 --- a/cgi-bin/converter +++ b/cgi-bin/converter @@ -33,11 +33,36 @@ from urllib.parse import parse_qs, urlparse BACK_LINK = '/cgi-bin/converter/' +MAIN_SCREEN = ''' +# Matt's Gemini CGI Toolbox. +I wanted to see what kind of things I could do with a CGI script. Think a collection of little oneliners. This is that. As I think of little toys I'll probably add to this. It happens to be written in Python. + +## Number base converters + +Display a number in binary, decimal and hexadecimal. If your input cannot be recognized, the script will return a permanent failure. + +=> bin Binary Input +=> dec Decimal Input +=> hex Hexadecimal Input + + +## Suggestions? +Want to see something here? Feel free to drop me a line. +=> mailto:matt@going-flying.com + + +=> / Home +''' class GeminiCGI(object): - class Response(object): + '''A convenience class to handle CGI under Molly Brown. + Contains a Response class to make it easy to not forget the + nuances of the Gemini header line. + ''' + class Response(object): + ''' Convenience class to create a Gemini Response. ''' codes = { 10: ('10', ''), 20: ('20', ''), @@ -47,14 +72,16 @@ class GeminiCGI(object): } def __init__(self, code=10, meta=''): - ''' Construct the response header per spec 3.1 ''' - self.header = self.codes[code][0] + ''' Construct and print a response header per spec + section 3.1 + ''' + header = self.codes[code][0] if meta: - self.header += ' ' + meta + header += ' ' + meta elif self.codes[code][1]: - self.header += ' ' + self.codes[code][1] + header += ' ' + self.codes[code][1] - self.header += '\r\n' + print(header + '\r\n') @classmethod def Input(cls, meta): @@ -76,9 +103,6 @@ class GeminiCGI(object): def BadRequest(cls): return cls(59) - def print_header(self): - print(self.header) - def __init__(self): self.path_info = os.environ.get('PATH_INFO') @@ -90,26 +114,8 @@ class GeminiCGI(object): def display_main_menu(cgi): - response = cgi.Response.Ok('text/gemini') - response.print_header() - print('''# Matt's Gemini Toolbox. - -## Number base converters - -Display a number in binary, decimal and hexadecimal. If your input cannot be recognized, the script will return a permanent failure. - -=> bin Binary Input -=> dec Decimal Input -=> hex Hexadecimal Input - - -## Suggestions? -Want to see something here? Feel free to drop me a line. -=> mailto:matt@going-flying.com - - -=> / Home -''') + cgi.Response.Ok('text/gemini') + print(MAIN_SCREEN) def ender(i): @@ -140,8 +146,7 @@ def pretty_binary(i): def result_table(cgi, input, b, d, h): ''' Pretty print a table''' global BACK_LINK - response = cgi.Response.Ok('text/gemini') - response.print_header() + cgi.Response.Ok('text/gemini') output = f'# Input\n{input}\n\n' output += f'## Binary Representation\n{b}\n\n' @@ -162,24 +167,20 @@ if __name__ == '__main__': sys.exit() elif cgi.path_info == 'bin': - response = cgi.Response.Input('Binary Number?') - response.print_header() + cgi.Response.Input('Binary Number?') sys.exit() elif cgi.path_info == 'dec': - response = cgi.Response.Input('Number?') - response.print_header() + cgi.Response.Input('Number?') sys.exit() elif cgi.path_info == 'hex': - response = cgi.Response.Input('Hex Number?') - response.print_header() + cgi.Response.Input('Hex Number?') sys.exit() else: if not cgi.path_info: - response = cgi.Response.BadRequest() - response.print_header() + cgi.Response.BadRequest() elif cgi.path_info == 'bin': s = cgi.query_string @@ -189,8 +190,7 @@ if __name__ == '__main__': b = pretty_binary(d) h = f'{d:02X}' except Exception: - response = cgi.Response.Fail() - response.print_header() + cgi.Response.Fail() sys.exit() result_table(cgi, s, b, d, h) @@ -203,8 +203,7 @@ if __name__ == '__main__': b = pretty_binary(d) h = f'{d:02X}' except Exception: - response = cgi.Response.Fail() - response.print_header() + cgi.Response.Fail() sys.exit() result_table(cgi, s, b, d, h) @@ -217,14 +216,12 @@ if __name__ == '__main__': b = pretty_binary(d) h = f'{d:02X}' except Exception: - response = cgi.Response.Fail() - response.print_header() + cgi.Response.Fail() sys.exit() result_table(cgi, s, b, d, h) else: - response = cgi.Response.NotFound() - response.print_header() + cgi.Response.NotFound()