Skip to content

Commit

Permalink
Moved credit parser to parsing.py
Browse files Browse the repository at this point in the history
All parser helpers should be in parsing.py, moved registration function there too
  • Loading branch information
kfsone committed May 3, 2015
1 parent 112173b commit 16cd764
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
22 changes: 3 additions & 19 deletions commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,6 @@ 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):
"""
Expand All @@ -73,7 +57,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)
parsing.registerParserHelpers(exGrp)
addArguments(exGrp, option.arguments, required, topGroup=group)
else:
assert not required in option.kwargs
Expand Down Expand Up @@ -187,15 +171,15 @@ def error(self, message):
)
)
parser.set_defaults(_editing=False)
CreditParser.register(parser)
parsing.registerParserHelpers(parser)

subParsers = parser.add_subparsers(title='Command Options')
subParser = subParsers.add_parser(cmdModule.name,
help=cmdModule.help,
add_help=False,
epilog=cmdModule.epilog,
)
CreditParser.register(subParser)
parsing.registerParserHelpers(subParser)

arguments = cmdModule.arguments
if arguments:
Expand Down
24 changes: 23 additions & 1 deletion commands/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,26 @@ def __init__(self, *args, **kwargs):

class MutuallyExclusiveGroup(object):
def __init__(self, *args):
self.arguments = list(args)
self.arguments = list(args)

# Derived classes

class CreditParser(int):
"""
argparse helper for parsing numeric prefixes, i.e.
'k' for thousand, 'm' for million and 'b' for billion.
"""
suffixes = {'k': 10**3, 'm': 10**6, 'b': 10**9}
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().__new__(cls, val, **kwargs)

__tdParserHelpers = {
'credits': CreditParser,
}

def registerParserHelpers(into):
for typeName, helper in __tdParserHelpers.items():
into.register('type', typeName, helper)

0 comments on commit 16cd764

Please sign in to comment.