Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 1 | import argparse |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 2 | import os |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 3 | import sys |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 4 | |
| 5 | from cfg_checker.common.exception import ConfigException |
| 6 | |
| 7 | |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 8 | class MyParser(argparse.ArgumentParser): |
| 9 | def error(self, message): |
| 10 | sys.stderr.write('Error: {0}\n\n'.format(message)) |
| 11 | self.print_help() |
| 12 | |
| 13 | |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 14 | def get_skip_args(args): |
| 15 | if hasattr(args, "skip_nodes"): |
| 16 | _skip = getattr(args, "skip_nodes") |
| 17 | if _skip: |
| 18 | _skip = _skip.split(',') |
| 19 | else: |
| 20 | _skip = None |
| 21 | if hasattr(args, "skip_nodes_file"): |
| 22 | _skip_file = getattr(args, "skip_nodes_file") |
| 23 | else: |
| 24 | _skip_file = None |
| 25 | return _skip, _skip_file |
| 26 | |
| 27 | |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 28 | def get_arg(args, str_arg): |
| 29 | _attr = getattr(args, str_arg) |
| 30 | if _attr: |
| 31 | return _attr |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 32 | else: |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 33 | _c = args.command if hasattr(args, 'command') else '' |
| 34 | _t = args.type if hasattr(args, 'type') else '' |
| 35 | raise ConfigException( |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 36 | "Argument '{}' not found executing: mcp-check {} {}".format( |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 37 | str_arg, |
| 38 | _c, |
| 39 | _t |
| 40 | ) |
| 41 | ) |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 42 | |
| 43 | |
| 44 | def get_path_arg(path): |
| 45 | if os.path.exists(path): |
| 46 | return path |
| 47 | else: |
| 48 | raise ConfigException("'{}' not exists".format(path)) |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 49 | |
| 50 | |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 51 | def get_network_map_type_and_filename(args): |
| 52 | if args.html or args.text: |
| 53 | if args.html and args.text: |
| 54 | raise ConfigException("Multuple report types not supported") |
| 55 | if args.html is not None: |
| 56 | return 'html', args.html |
| 57 | if args.text is not None: |
| 58 | return 'text', args.text |
| 59 | else: |
| 60 | return 'console', None |
| 61 | |
| 62 | |
| 63 | def get_package_report_type_and_filename(args): |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 64 | if args.html or args.csv: |
| 65 | if args.html and args.csv: |
| 66 | raise ConfigException("Multuple report types not supported") |
| 67 | if args.html is not None: |
| 68 | return 'html', args.html |
| 69 | if args.csv is not None: |
| 70 | return 'csv', args.csv |
| 71 | else: |
| 72 | raise ConfigException("Report type and filename not set") |