Fix auto logging multiple argparse calls before Task.init

This commit is contained in:
allegroai 2020-07-02 01:17:36 +03:00
parent b2fb5a3425
commit efd843d863

View File

@ -54,26 +54,29 @@ class PatchArgumentParser:
if PatchArgumentParser._calling_current_task: if PatchArgumentParser._calling_current_task:
# if we are here and running remotely by now we should try to parse the arguments # if we are here and running remotely by now we should try to parse the arguments
if original_parse_fn: if original_parse_fn:
PatchArgumentParser._last_parsed_args = \ PatchArgumentParser._add_last_parsed_args(original_parse_fn(self, args=args, namespace=namespace))
original_parse_fn(self, args=args, namespace=namespace) return PatchArgumentParser._last_parsed_args[-1]
return PatchArgumentParser._last_parsed_args
PatchArgumentParser._calling_current_task = True PatchArgumentParser._calling_current_task = True
# Store last instance and result # Store last instance and result
PatchArgumentParser._last_arg_parser = self PatchArgumentParser._add_last_arg_parser(self)
parsed_args = None parsed_args = None
# parse if we are running in dev mode # parse if we are running in dev mode
if not running_remotely() and original_parse_fn: if not running_remotely() and original_parse_fn:
parsed_args = original_parse_fn(self, args=args, namespace=namespace) parsed_args = original_parse_fn(self, args=args, namespace=namespace)
PatchArgumentParser._last_parsed_args = parsed_args PatchArgumentParser._add_last_parsed_args(parsed_args)
# noinspection PyBroadException # noinspection PyBroadException
try: try:
# sync to/from task # sync to/from task
PatchArgumentParser._current_task._connect_argparse(self, args=args, namespace=namespace, # noinspection PyProtectedMember
parsed_args=parsed_args[0] PatchArgumentParser._current_task._connect_argparse(
if isinstance(parsed_args, tuple) else parsed_args) self, args=args, namespace=namespace,
parsed_args=parsed_args[0] if isinstance(parsed_args, tuple) else parsed_args
)
except Exception: except Exception:
pass pass
# sync back and parse # sync back and parse
if running_remotely() and original_parse_fn: if running_remotely() and original_parse_fn:
# if we are running python2 check if we have subparsers, # if we are running python2 check if we have subparsers,
@ -105,18 +108,26 @@ class PatchArgumentParser:
if a.default not in args: if a.default not in args:
args.append(a.default) args.append(a.default)
PatchArgumentParser._last_parsed_args = original_parse_fn(self, args=args, namespace=namespace) PatchArgumentParser._add_last_parsed_args(original_parse_fn(self, args=args, namespace=namespace))
else: else:
PatchArgumentParser._last_parsed_args = parsed_args or {} PatchArgumentParser._add_last_parsed_args(parsed_args or {})
PatchArgumentParser._calling_current_task = False PatchArgumentParser._calling_current_task = False
return PatchArgumentParser._last_parsed_args return PatchArgumentParser._last_parsed_args[-1]
# Store last instance and result # Store last instance and result
PatchArgumentParser._last_arg_parser = self PatchArgumentParser._add_last_arg_parser(self)
PatchArgumentParser._last_parsed_args = {} if not original_parse_fn else \ PatchArgumentParser._add_last_parsed_args(
original_parse_fn(self, args=args, namespace=namespace) {} if not original_parse_fn else original_parse_fn(self, args=args, namespace=namespace))
return PatchArgumentParser._last_parsed_args return PatchArgumentParser._last_parsed_args[-1]
@staticmethod
def _add_last_parsed_args(parsed_args):
PatchArgumentParser._last_parsed_args = (PatchArgumentParser._last_parsed_args or []) + [parsed_args]
@staticmethod
def _add_last_arg_parser(a_argparser):
PatchArgumentParser._last_arg_parser = (PatchArgumentParser._last_arg_parser or []) + [a_argparser]
def patch_argparse(): def patch_argparse():
@ -152,9 +163,11 @@ def argparser_update_currenttask(task):
def get_argparser_last_args(): def get_argparser_last_args():
return (PatchArgumentParser._last_arg_parser, if not PatchArgumentParser._last_arg_parser or not PatchArgumentParser._last_parsed_args:
PatchArgumentParser._last_parsed_args[0] if isinstance(PatchArgumentParser._last_parsed_args, tuple) else return []
PatchArgumentParser._last_parsed_args)
return [(parser, args[0] if isinstance(args, tuple) else args)
for parser, args in zip(PatchArgumentParser._last_arg_parser, PatchArgumentParser._last_parsed_args)]
def add_params_to_parser(parser, params): def add_params_to_parser(parser, params):