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

debugger.py 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Debugger hook."""
  16. import argparse
  17. from mindinsight.conf import settings
  18. from mindinsight.utils.hook import BaseHook
  19. def enable_debugger_string(string):
  20. """Convert str to bool"""
  21. if string.lower() in ('false', '0'):
  22. return False
  23. if string.lower() in ('true', '1'):
  24. return True
  25. raise ValueError
  26. class EnableDebuggerAction(argparse.Action):
  27. """Enable debugger action class definition."""
  28. def __call__(self, parser, namespace, values, option_string=None):
  29. """
  30. Inherited __call__ method from argparse.Action.
  31. Args:
  32. parser (ArgumentParser): Passed-in argument parser.
  33. namespace (Namespace): Namespace object to hold arguments.
  34. values (object): Argument values with type depending on argument definition.
  35. option_string (str): Optional string for specific argument name. Default: None.
  36. """
  37. enable_debugger = values
  38. setattr(namespace, self.dest, enable_debugger)
  39. class PortAction(argparse.Action):
  40. """Port action class definition."""
  41. MIN_PORT = 1
  42. MAX_PORT = 65535
  43. OPEN_PORT_LIMIT = 1024
  44. def __call__(self, parser, namespace, values, option_string=None):
  45. """
  46. Inherited __call__ method from argparse.Action.
  47. Args:
  48. parser (ArgumentParser): Passed-in argument parser.
  49. namespace (Namespace): Namespace object to hold arguments.
  50. values (object): Argument values with type depending on argument definition.
  51. option_string (str): Optional string for specific argument name. Default: None.
  52. """
  53. port = values
  54. if not self.MIN_PORT <= port <= self.MAX_PORT:
  55. parser.error(f'{option_string} should be chosen from {self.MIN_PORT} to {self.MAX_PORT}')
  56. setattr(namespace, self.dest, port)
  57. class Hook(BaseHook):
  58. """Hook class definition."""
  59. def register_startup_arguments(self, parser):
  60. """
  61. Hook function to register startup arguments.
  62. Args:
  63. parser (ArgumentParser): Specify parser to which arguments are added.
  64. """
  65. parser.add_argument(
  66. '--enable-debugger',
  67. type=enable_debugger_string,
  68. action=EnableDebuggerAction,
  69. default=False,
  70. help="""
  71. Enable debugger or not. The value can be True/False/1/0 (case insensitive).
  72. Default is False.""")
  73. parser.add_argument(
  74. '--debugger-port',
  75. type=int,
  76. action=PortAction,
  77. help="""
  78. Debugger port ranging from %s to %s. Default value is %s.
  79. """ % (PortAction.MIN_PORT, PortAction.MAX_PORT, settings.DEBUGGER_PORT))