From 884b22c0fc713ac6152ab1c9a1fef2dbd99ee237 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Fri, 17 Nov 2023 11:43:09 +0000 Subject: [PATCH] Apply style fixes Signed-off-by: Ian Hunter --- src/python/code/gnoll/__init__.py | 3 +- src/python/code/gnoll/__main__.py | 62 ++++++++++++++++++++----------- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/python/code/gnoll/__init__.py b/src/python/code/gnoll/__init__.py index a8f4cdcc0..16afc4f91 100644 --- a/src/python/code/gnoll/__init__.py +++ b/src/python/code/gnoll/__init__.py @@ -124,7 +124,8 @@ def extract_from_dice_file(lines, seperator): finally: if not keep_temp_file: if verbose: - print("Deleting:", out_file) + print("Deleting:", out_file) + os.remove(die_file) diff --git a/src/python/code/gnoll/__main__.py b/src/python/code/gnoll/__main__.py index 00027f46e..84fbec94b 100644 --- a/src/python/code/gnoll/__main__.py +++ b/src/python/code/gnoll/__main__.py @@ -1,40 +1,57 @@ -'''Roll some dice with GNOLL.''' +"""Roll some dice with GNOLL.""" + +import sys +import argparse +import gnoll -import sys, argparse, gnoll def parse_cmdline_args(args): + """Extract values from the commandline + @param args - the arguments from the commandline (excluding the python3 call) + """ p = argparse.ArgumentParser( - description = __doc__, - usage = 'python3 -m gnoll [options] EXPR', - add_help = False) + description=__doc__, + usage='python3 -m gnoll [options] EXPR', + add_help=False) + - p.add_argument('EXPR', nargs = '+', - help = 'a dice expression to evaluate ' - '(multiple arguments will be joined with spaces)') + p.add_argument( + 'EXPR', + nargs='+', + help='a dice expression to evaluate (multiple arguments will be joined with spaces)' + ) g = p.add_argument_group('main options') - g.add_argument('-h', '--help', action = 'help', - help = 'show this help message and exit') - g.add_argument('-b', '--breakdown', action = 'store_true', - help = 'show a breakdown into individual dice') - g.add_argument('--no-builtins', action = 'store_true', - help = 'disable built-in macros') + g.add_argument('-h', '--help', action='help', + help='show this help message and exit') + g.add_argument('-b', '--breakdown', action='store_true', + help='show a breakdown into individual dice') + g.add_argument('--no-builtins', action='store_true', + help='disable built-in macros') g = p.add_argument_group('debugging options') - g.add_argument('-v', '--verbose', action = 'store_true', - help = 'enable verbosity') - g.add_argument('--keep-temp-file', action = 'store_true', - help = "don't delete the created temporary file") - g.add_argument('--mock', metavar = 'TYPE', type = int, - help = 'mocking type') - g.add_argument('--mock-const', metavar = 'N', type = int, default = 3, - help = 'mocking constant') + g.add_argument('-v', '--verbose', action='store_true', + help='enable verbosity') + g.add_argument('--keep-temp-file', action='store_true', + help="don't delete the created temporary file") + g.add_argument('--mock', metavar='TYPE', type=int, + help='mocking type') + g.add_argument('--mock-const', metavar='N', type=int, default=3, + help='mocking constant') + a = p.parse_args(args) a.EXPR = ' '.join(a.EXPR) return a + def main(EXPR, no_builtins, **kwargs): + """ + The entry point for gnoll when called via `python -m gnoll` + @param EXPR - the expression + @param no_builtins - a flag to disable builtins + @param **kwargs - other key word arguments to be passed to gnoll.roll + """ _, [[result]], breakdown = gnoll.roll( EXPR, builtins = not no_builtins, @@ -44,5 +61,6 @@ def main(EXPR, no_builtins, **kwargs): else: print(result) + if __name__ == '__main__': main(**vars(parse_cmdline_args(sys.argv[1:])))