|
34 | 34 |
|
35 | 35 |
|
36 | 36 | def main():
|
37 |
| - """Format all tracked .py files. |
38 |
| -
|
39 |
| - Black + isort + docformatter. |
40 |
| - """ |
41 |
| - parser = argparse.ArgumentParser( |
42 |
| - description=__doc__, |
43 |
| - formatter_class=argparse.RawDescriptionHelpFormatter, |
44 |
| - ) |
45 |
| - parser.add_argument('path', nargs='+', help='File or directory path') |
46 |
| - parser.add_argument('--exclude', nargs='+', help='Exclude glob patterns') |
47 |
| - parser.add_argument( |
48 |
| - '--no-recursive', dest='recursive', action='store_false', |
49 |
| - help='Search directories recursively' |
50 |
| - ) |
51 |
| - parser.add_argument( |
52 |
| - '--ignore-invalid', action='store_true', help='Ignore invalid paths' |
53 |
| - ) |
54 |
| - parser.add_argument( |
55 |
| - '--pycharm', action='store_true', help='Enable PyCharm integration' |
56 |
| - ) |
57 |
| - parser.add_argument( |
58 |
| - '--debug', action='store_true', help='Debug level logging' |
59 |
| - ) |
60 |
| - |
61 |
| - args = parser.parse_args() |
62 |
| - d1_common.util.log_setup(args.debug) |
63 |
| - |
64 |
| - repo_path = d1_dev.util.find_repo_root_by_path(__file__) |
65 |
| - repo = git.Repo(repo_path) |
66 |
| - |
67 |
| - specified_file_path_list = get_specified_file_path_list(args) |
68 |
| - tracked_path_list = list(d1_dev.util.get_tracked_files(repo)) |
69 |
| - format_path_list = sorted( |
70 |
| - set(specified_file_path_list).intersection(tracked_path_list) |
71 |
| - ) |
72 |
| - format_all(args, format_path_list) |
| 37 | + """Format all tracked .py files. |
73 | 38 |
|
| 39 | + Black + isort + docformatter. |
| 40 | + """ |
| 41 | + parser = argparse.ArgumentParser( |
| 42 | + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter |
| 43 | + ) |
| 44 | + parser.add_argument('path', nargs='+', help='File or directory path') |
| 45 | + parser.add_argument('--exclude', nargs='+', help='Exclude glob patterns') |
| 46 | + parser.add_argument( |
| 47 | + '--no-recursive', |
| 48 | + dest='recursive', |
| 49 | + action='store_false', |
| 50 | + help='Search directories recursively', |
| 51 | + ) |
| 52 | + parser.add_argument( |
| 53 | + '--ignore-invalid', action='store_true', help='Ignore invalid paths' |
| 54 | + ) |
| 55 | + parser.add_argument( |
| 56 | + '--pycharm', action='store_true', help='Enable PyCharm integration' |
| 57 | + ) |
| 58 | + parser.add_argument('--debug', action='store_true', help='Debug level logging') |
74 | 59 |
|
75 |
| -def get_specified_file_path_list(args): |
76 |
| - specified_file_path_list = [ |
77 |
| - os.path.realpath(p) |
78 |
| - for p in d1_common.iter.path.path_generator( |
79 |
| - path_list=args.path, |
80 |
| - include_glob_list=['*.py'], |
81 |
| - exclude_glob_list=args.exclude, |
82 |
| - recursive=args.recursive, |
83 |
| - ignore_invalid=args.ignore_invalid, |
84 |
| - default_excludes=False, |
85 |
| - return_dir_paths=True, |
| 60 | + args = parser.parse_args() |
| 61 | + d1_common.util.log_setup(args.debug) |
| 62 | + |
| 63 | + repo_path = d1_dev.util.find_repo_root_by_path(__file__) |
| 64 | + repo = git.Repo(repo_path) |
| 65 | + |
| 66 | + specified_file_path_list = get_specified_file_path_list(args) |
| 67 | + tracked_path_list = list(d1_dev.util.get_tracked_files(repo)) |
| 68 | + format_path_list = sorted( |
| 69 | + set(specified_file_path_list).intersection(tracked_path_list) |
86 | 70 | )
|
87 |
| - ] |
88 |
| - return specified_file_path_list |
| 71 | + format_all(args, format_path_list) |
| 72 | + |
| 73 | + |
| 74 | +def get_specified_file_path_list(args): |
| 75 | + specified_file_path_list = [ |
| 76 | + os.path.realpath(p) |
| 77 | + for p in d1_common.iter.path.path_generator( |
| 78 | + path_list=args.path, |
| 79 | + include_glob_list=['*.py'], |
| 80 | + exclude_glob_list=args.exclude, |
| 81 | + recursive=args.recursive, |
| 82 | + ignore_invalid=args.ignore_invalid, |
| 83 | + default_excludes=False, |
| 84 | + return_dir_paths=True, |
| 85 | + ) |
| 86 | + ] |
| 87 | + return specified_file_path_list |
89 | 88 |
|
90 | 89 |
|
91 | 90 | def format_all(args, format_path_list):
|
92 |
| - for format_path in format_path_list: |
93 |
| - logging.info('Formatting file. path="{}"'.format(format_path)) |
94 |
| - format_single(args, format_path) |
| 91 | + for format_path in format_path_list: |
| 92 | + logging.info('Formatting file. path="{}"'.format(format_path)) |
| 93 | + format_single(args, format_path) |
95 | 94 |
|
96 | 95 |
|
97 | 96 | def format_single(args, format_path):
|
98 |
| - run_cmd('black', '--skip-string-normalization', format_path) |
99 |
| - run_cmd('isort', format_path) |
100 |
| - run_cmd('docformatter', '-i', format_path) |
| 97 | + run_cmd('black', '--skip-string-normalization', format_path) |
| 98 | + run_cmd('isort', format_path) |
| 99 | + run_cmd('docformatter', '-i', format_path) |
101 | 100 |
|
102 | 101 |
|
103 | 102 | def run_cmd(*cmd_list):
|
104 |
| - print('Running command: {}'.format(' '.join(cmd_list))) |
105 |
| - try: |
106 |
| - subprocess.check_call(cmd_list) |
107 |
| - except subprocess.CalledProcessError as e: |
108 |
| - print('Failed: {}'.format(str(e))) |
109 |
| - raise |
| 103 | + print('Running command: {}'.format(' '.join(cmd_list))) |
| 104 | + try: |
| 105 | + subprocess.check_call(cmd_list) |
| 106 | + except subprocess.CalledProcessError as e: |
| 107 | + print('Failed: {}'.format(str(e))) |
| 108 | + raise |
110 | 109 |
|
111 | 110 |
|
112 | 111 | if __name__ == '__main__':
|
113 |
| - sys.exit(main()) |
| 112 | + sys.exit(main()) |
0 commit comments