From be57302e3763e7f98e2478770ae7ea151856c06c Mon Sep 17 00:00:00 2001 From: Phil Davies Date: Fri, 3 Apr 2015 16:42:14 +0100 Subject: [PATCH] added CreditParser and switched appropriate arguments --- commands/__init__.py | 19 +++++++++++++++++++ commands/buy_cmd.py | 4 ++-- commands/run_cmd.py | 8 ++++---- commands/sell_cmd.py | 4 ++-- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/commands/__init__.py b/commands/__init__.py index e28df09a..7a580b59 100644 --- a/commands/__init__.py +++ b/commands/__init__.py @@ -46,6 +46,22 @@ def __call__(self, parser, namespace, values, option_string=None): parser.format_help() ) +class CreditParser(int): + """ + argparse type helper for handling suffixes for thousand, million and "billion" + """ + + suffixes = dict(zip("kmb", map(lambda x: 10**x, (3, 6, 9)))) # damn american billion. What's wrong with milliard? + + def __new__(cls, val, **kwargs): + if isinstance(val, str): + if val[-1] in CreditParser.suffixes: + val = int(float(val[:-1]) * CreditParser.suffixes[val[-1]]) + return super(CreditParser, cls).__new__(cls, val, **kwargs) + + @classmethod + def register(cls, target): + target.register('type', 'credits', CreditParser) def addArguments(group, options, required, topGroup=None): """ @@ -57,6 +73,7 @@ def addArguments(group, options, required, topGroup=None): for option in options: if isinstance(option, parsing.MutuallyExclusiveGroup): exGrp = (topGroup or group).add_mutually_exclusive_group() + CreditParser.register(exGrp) addArguments(exGrp, option.arguments, required, topGroup=group) else: assert not required in option.kwargs @@ -170,6 +187,7 @@ def error(self, message): ) ) parser.set_defaults(_editing=False) + CreditParser.register(parser) subParsers = parser.add_subparsers(title='Command Options') subParser = subParsers.add_parser(cmdModule.name, @@ -177,6 +195,7 @@ def error(self, message): add_help=False, epilog=cmdModule.epilog, ) + CreditParser.register(subParser) arguments = cmdModule.arguments if arguments: diff --git a/commands/buy_cmd.py b/commands/buy_cmd.py index c8e591ef..4d9e7c9c 100644 --- a/commands/buy_cmd.py +++ b/commands/buy_cmd.py @@ -84,14 +84,14 @@ help='Limit to prices above Ncr', metavar='N', dest='gt', - type=int, + type="credits", ), ParseArgument( '--lt', help='Limit to prices below Ncr', metavar='N', dest='lt', - type=int, + type="credits", ), ] diff --git a/commands/run_cmd.py b/commands/run_cmd.py index 3f6758f1..8acf0e6a 100644 --- a/commands/run_cmd.py +++ b/commands/run_cmd.py @@ -25,7 +25,7 @@ ParseArgument('--credits', help='Starting credits.', metavar='CR', - type=int, + type="credits", ), ] @@ -150,13 +150,13 @@ ParseArgument('--gain-per-ton', '--gpt', help='Specify the minimum gain per ton of cargo', dest='minGainPerTon', - type=int, + type="credits", default=1 ), ParseArgument('--max-gain-per-ton', '--mgpt', help='Specify the maximum gain per ton of cargo', dest='maxGainPerTon', - type=int, + type="credits", default=10000 ), ParseArgument('--unique', @@ -176,7 +176,7 @@ help='Reserve at least this many credits to cover insurance.', default=0, metavar='CR', - type=int, + type="credits", ), ParseArgument('--routes', help='Maximum number of routes to show. DEFAULT: 1', diff --git a/commands/sell_cmd.py b/commands/sell_cmd.py index 71b62ee1..a7d3abc4 100644 --- a/commands/sell_cmd.py +++ b/commands/sell_cmd.py @@ -47,13 +47,13 @@ help='Limit to prices above Ncr', metavar='N', dest='gt', - type=int, + type="credits", ), ParseArgument('--lt', help='Limit to prices below Ncr', metavar='N', dest='lt', - type=int, + type="credits", ), ]