Creates AttackLogManager from an AttackArgs object.
(cls, args)
| 401 | |
| 402 | @classmethod |
| 403 | def create_loggers_from_args(cls, args): |
| 404 | """Creates AttackLogManager from an AttackArgs object.""" |
| 405 | assert isinstance( |
| 406 | args, cls |
| 407 | ), f"Expect args to be of type `{type(cls)}`, but got type `{type(args)}`." |
| 408 | |
| 409 | # Create logger |
| 410 | attack_log_manager = textattack.loggers.AttackLogManager(args.metrics) |
| 411 | |
| 412 | # Get current time for file naming |
| 413 | timestamp = time.strftime("%Y-%m-%d-%H-%M") |
| 414 | |
| 415 | # if '--log-to-txt' specified with arguments |
| 416 | if args.log_to_txt is not None: |
| 417 | if args.log_to_txt.lower().endswith(".txt"): |
| 418 | txt_file_path = args.log_to_txt |
| 419 | else: |
| 420 | txt_file_path = os.path.join(args.log_to_txt, f"{timestamp}-log.txt") |
| 421 | |
| 422 | dir_path = os.path.dirname(txt_file_path) |
| 423 | dir_path = dir_path if dir_path else "." |
| 424 | if not os.path.exists(dir_path): |
| 425 | os.makedirs(os.path.dirname(txt_file_path)) |
| 426 | |
| 427 | color_method = "file" |
| 428 | attack_log_manager.add_output_file(txt_file_path, color_method) |
| 429 | |
| 430 | # if '--log-to-csv' specified with arguments |
| 431 | if args.log_to_csv is not None: |
| 432 | if args.log_to_csv.lower().endswith(".csv"): |
| 433 | csv_file_path = args.log_to_csv |
| 434 | else: |
| 435 | csv_file_path = os.path.join(args.log_to_csv, f"{timestamp}-log.csv") |
| 436 | |
| 437 | dir_path = os.path.dirname(csv_file_path) |
| 438 | dir_path = dir_path if dir_path else "." |
| 439 | if not os.path.exists(dir_path): |
| 440 | os.makedirs(dir_path) |
| 441 | |
| 442 | color_method = ( |
| 443 | None if args.csv_coloring_style == "plain" else args.csv_coloring_style |
| 444 | ) |
| 445 | attack_log_manager.add_output_csv(csv_file_path, color_method) |
| 446 | |
| 447 | # if '--log-summary-to-json' specified with arguments |
| 448 | if args.log_summary_to_json is not None: |
| 449 | if args.log_summary_to_json.lower().endswith(".json"): |
| 450 | summary_json_file_path = args.log_summary_to_json |
| 451 | else: |
| 452 | summary_json_file_path = os.path.join( |
| 453 | args.log_summary_to_json, f"{timestamp}-attack_summary_log.json" |
| 454 | ) |
| 455 | |
| 456 | dir_path = os.path.dirname(summary_json_file_path) |
| 457 | dir_path = dir_path if dir_path else "." |
| 458 | if not os.path.exists(dir_path): |
| 459 | os.makedirs(os.path.dirname(summary_json_file_path)) |
| 460 |
no test coverage detected