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.

parse_summary.py 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. """Parse summary file."""
  16. import argparse
  17. import os
  18. import datetime
  19. from mindinsight.utils.command import BaseCommand
  20. from mindinsight.datavisual.data_transform.summary_parser.event_parser import EventParser
  21. class FilepathAction(argparse.Action):
  22. """File directory action class definition."""
  23. def __call__(self, parser_in, namespace, values, option_string=None):
  24. """
  25. Inherited __call__ method from argparse.Action.
  26. Args:
  27. parser_in (ArgumentParser): Passed-in argument parser.
  28. namespace (Namespace): Namespace object to hold arguments.
  29. values (object): Argument values with type depending on argument definition.
  30. option_string (str): Optional string for specific argument name. Default: None.
  31. """
  32. summary_dir = values
  33. if summary_dir.startswith('~'):
  34. summary_dir = os.path.realpath(os.path.expanduser(summary_dir))
  35. if not summary_dir.startswith('/'):
  36. summary_dir = os.path.realpath(os.path.join(os.getcwd(), summary_dir))
  37. summary_dir = os.path.realpath(summary_dir)
  38. setattr(namespace, self.dest, summary_dir)
  39. class OutputDirAction(argparse.Action):
  40. """File directory action class definition."""
  41. def __call__(self, parser_in, namespace, values, option_string=None):
  42. """
  43. Inherited __call__ method from argparse.Action.
  44. Args:
  45. parser_in (ArgumentParser): Passed-in argument parser.
  46. namespace (Namespace): Namespace object to hold arguments.
  47. values (object): Argument values with type depending on argument definition.
  48. option_string (str): Optional string for specific argument name. Default: None.
  49. """
  50. output = values
  51. if output.startswith('~'):
  52. output = os.path.realpath(os.path.expanduser(output))
  53. if not output.startswith('/'):
  54. output = os.path.realpath(os.path.join(os.getcwd(), output))
  55. output = os.path.realpath(output)
  56. setattr(namespace, self.dest, output)
  57. class Command(BaseCommand):
  58. """Start mindinsight service."""
  59. name = 'parse_summary'
  60. description = 'Parse summary file'
  61. def add_arguments(self, parser):
  62. """
  63. Add arguments to parser.
  64. Args:
  65. parser (ArgumentParser): Specify parser to which arguments are added.
  66. """
  67. parser.add_argument(
  68. '--summary-dir',
  69. type=str,
  70. action=FilepathAction,
  71. default=os.path.realpath(os.getcwd()),
  72. help="""
  73. Optional, specify path for summary file directory.
  74. Default directory is the current working directory.
  75. """)
  76. parser.add_argument(
  77. '--output',
  78. type=str,
  79. action=OutputDirAction,
  80. default=os.path.realpath(os.getcwd()),
  81. help="""
  82. Optional, specify path for converted file directory. Default output
  83. directory is `output` folder in the current working directory.
  84. """)
  85. def run(self, args):
  86. """
  87. Execute for start command.
  88. Args:
  89. args (Namespace): Parsed arguments to hold customized parameters.
  90. """
  91. date_time = datetime.datetime.now().strftime('output_%Y%m%d_%H%M%S_%f')
  92. date_time = os.path.join(args.output, date_time)
  93. eventparser = EventParser(args.summary_dir, date_time)
  94. eventparser.parse()