You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import argparse
  2. import copy
  3. import logging
  4. import sys
  5. from config import Config as Conf
  6. from argv.checkpoints import init_checkpoints_sub_parser, check_args_checkpoints_parser, set_args_checkpoints_parser
  7. from argv.common import arg_help, arg_debug, arg_version
  8. from argv.daemon import init_daemon_sub_parser, check_args_daemon_parser, set_args_daemon_parser
  9. from argv.gpu_info import init_gpu_info_sub_parser
  10. from argv.run import init_run_parser
  11. from argv.run.config import set_args_run_parser, check_args_run_parser
  12. from utils import setup_log, json_to_argv
  13. class Parser:
  14. parser = None
  15. def run():
  16. Parser.parser = init_parser()
  17. if len(sys.argv) == 1:
  18. Parser.parser.print_usage()
  19. Parser.parser.exit()
  20. args = Parser.parser.parse_args()
  21. Conf.log = setup_log(logging.DEBUG) if args.debug else setup_log()
  22. args = config_args(Parser.parser, args)
  23. Conf.log.debug(args)
  24. Conf.args = vars(args)
  25. args.func(args)
  26. def init_parser():
  27. parser = argparse.ArgumentParser(
  28. description="Dreampower CLI application that allow to transform photos of people for private entertainment",
  29. add_help=False
  30. )
  31. # add args main
  32. arg_help(parser)
  33. arg_debug(parser)
  34. arg_version(parser)
  35. # add subparsers
  36. subparsers = parser.add_subparsers(dest="mode")
  37. init_run_parser(subparsers)
  38. init_checkpoints_sub_parser(subparsers)
  39. init_gpu_info_sub_parser(subparsers)
  40. init_daemon_sub_parser(subparsers)
  41. return parser
  42. def config_args(parser, args, json_data=None):
  43. args = copy.deepcopy(args)
  44. set_args_parser(parser, args)
  45. check_args_parser(parser, args)
  46. if args.mode == "run":
  47. return merge_json_args(parser, args, json_data)
  48. else:
  49. return args
  50. def check_args_parser(parser, args):
  51. if args.mode == "run":
  52. check_args_run_parser(parser, args)
  53. if args.mode == "checkpoints":
  54. check_args_checkpoints_parser(parser, args)
  55. if args.mode == "daemon":
  56. check_args_daemon_parser(parser, args)
  57. return parser
  58. def set_args_parser(parser, args):
  59. if args.mode == "run":
  60. set_args_run_parser(args)
  61. if args.mode == "checkpoints":
  62. set_args_checkpoints_parser(args)
  63. if args.mode == "daemon":
  64. set_args_daemon_parser(args)
  65. return parser
  66. def merge_json_args(parser, args, json_data=None):
  67. def filter_conflict_args(l1, l2):
  68. # l2 args got priority on l1
  69. l1 = copy.copy(l1)
  70. l2 = copy.copy(l2)
  71. # Handle special cases for ignoring arguments in json file if provided in command line
  72. if "--cpu" in l2 or "--gpu" in l2:
  73. l1 = list(filter(lambda x: x not in ("--cpu", "--gpu"), l1))
  74. if "--auto-resize" in l2 or "--auto-resize-crop" in l2 \
  75. or "--auto-rescale" in l2 or "--overlay" in l2:
  76. l1 = list(filter(lambda x: x not in ("--auto-resize", "--auto-resize-crop", "--auto-rescale"), l1))
  77. if "--overlay" in l1:
  78. del l1[l1.index("--overlay"):l1.index("--overlay") + 1]
  79. return l1 + l2
  80. # merge args
  81. cmdline_args = []
  82. if not json_data and not args.json_args:
  83. return args
  84. elif json_data and args.json_args:
  85. cmdline_args = filter_conflict_args(json_to_argv(json_data), json_to_argv(args.json_args))
  86. elif json_data and not args.json_args:
  87. cmdline_args = json_to_argv(json_data)
  88. elif not json_data and args.json_args:
  89. cmdline_args = json_to_argv(args.json_args)
  90. cmdline_args = filter_conflict_args(cmdline_args, sys.argv[1:])
  91. # remove json-args arg
  92. i = 0
  93. while i < len(cmdline_args):
  94. if "--json-args" == cmdline_args[i]:
  95. del cmdline_args[i:i + 2]
  96. i += 1
  97. # replace run arg
  98. cmdline_args.index('run')
  99. cmdline_args.remove('run')
  100. cmdline_args.insert(0, 'run')
  101. return config_args(parser, parser.parse_args(cmdline_args))