Move EnumAction to different file

This will make it easier to move main() in the future.
This commit is contained in:
Jeremy Saklad 2021-08-03 13:54:36 -05:00
parent 9cfed0f839
commit a17135d85d
Signed by: Jeremy Saklad
GPG Key ID: 9CA2149583EDBF84
2 changed files with 31 additions and 27 deletions

View File

@ -19,6 +19,7 @@ from data.declarations import Declaration
from data.embellishments import Embellishment from data.embellishments import Embellishment
from data.skulls import Skull from data.skulls import Skull
from data.torsos import Torso from data.torsos import Torso
from objects.enumaction import EnumAction
# This multiplier is applied to the profit margin to avoid losing precision due to rounding. # This multiplier is applied to the profit margin to avoid losing precision due to rounding.
PROFIT_MARGIN_MULTIPLIER = 10000000 PROFIT_MARGIN_MULTIPLIER = 10000000
@ -1283,33 +1284,6 @@ def Solve(shadowy_level, bone_market_fluctuations = None, zoological_mania = Non
return printer.PrintableSolution(solver) return printer.PrintableSolution(solver)
class EnumAction(argparse.Action):
def __init__(self, **kwargs):
# Pop off the type value
enum = kwargs.pop('type', None)
nargs = kwargs.pop('nargs', None)
# Generate choices from the Enum
kwargs.setdefault('choices', tuple(member.name.lower() for member in enum))
super(EnumAction, self).__init__(**kwargs)
self._enum = enum
self._nargs = nargs
def __call__(self, parser, namespace, values, option_string=None):
# Convert value back into an Enum
enum = self._enum[values.upper()]
if self._nargs is None or self._nargs == '?':
setattr(namespace, self.dest, enum)
else:
items = getattr(namespace, self.dest, list())
items.append(enum)
setattr(namespace, self.dest, items)
def main(): def main():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
prog='Bone Market Solver', prog='Bone Market Solver',

30
objects/enumaction.py Normal file
View File

@ -0,0 +1,30 @@
__all__ = ['EnumAction']
__author__ = "Jeremy Saklad"
from argparse import Action
class EnumAction(Action):
def __init__(self, **kwargs):
# Pop off the type value
enum = kwargs.pop('type', None)
nargs = kwargs.pop('nargs', None)
# Generate choices from the Enum
kwargs.setdefault('choices', tuple(member.name.lower() for member in enum))
super(EnumAction, self).__init__(**kwargs)
self._enum = enum
self._nargs = nargs
def __call__(self, parser, namespace, values, option_string=None):
# Convert value back into an Enum
enum = self._enum[values.upper()]
if self._nargs is None or self._nargs == '?':
setattr(namespace, self.dest, enum)
else:
items = getattr(namespace, self.dest, list())
items.append(enum)
setattr(namespace, self.dest, items)