-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.py
More file actions
executable file
·94 lines (63 loc) · 2.42 KB
/
format.py
File metadata and controls
executable file
·94 lines (63 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
import argparse
import os
##################################################################
code_ext = ['hpp', 'cpp', 'c', 'h', 'hh', 'cc', 'c++', 'cxx']
cmd = 'clang-format -i --style="{BasedOnStyle: Microsoft, IndentWidth: 2}" '
##################################################################
def run_format(files_string, is_print, is_int):
flist = files_string.split()
check_list_ftype(flist)
for elem in flist:
is_form = True
if is_int:
print(f'\033[33mFormat "{elem}"? (Y/N): ', end='')
answer = input()
is_form = answer[0].upper() in ['Y', 'Д']
if is_form:
os.system(cmd + elem)
if is_print:
print(f'\033[32mFormatted: {elem}')
elif is_print:
print(f'\033[31mInterrupted fromatting {elem}.')
def check_list_ftype(flist):
if type(flist) != list:
return False
for name in flist:
if not check_ftype(name):
return False
return True
def check_ftype(filename):
#filename = filename.split('/')
#filename = filename[len(filename) - 1]
if '.' not in filename:
return False
splatted = filename.split('.')
return splatted[len(splatted) - 1] in code_ext
def check_fdir(lst_dir, dname):
flist = ''
for elem in lst_dir:
new_dname = dname + '/' + elem
if os.path.isdir(new_dname):
flist += check_fdir(os.listdir(new_dname), new_dname)
continue
if check_ftype(elem):
flist += new_dname + ' '
return flist
def main():
parser = argparse.ArgumentParser(description='Clang-format code formatting script.\nBy derzhavin3016.')
group = parser.add_mutually_exclusive_group()
parser.add_argument('--loc', '-l', metavar='PATH', type=str, default='.', help='Start location (file or dir)')
group.add_argument('--quiet', '-q', action='store_false', help='Quiet mode (no print)')
group.add_argument('--interactive', '-i', action='store_true', help='Interactive mode')
args = parser.parse_args()
if os.path.isfile(args.loc):
run_format(args.loc, args.quiet, args.interactive)
return
elif os.path.isdir(args.loc):
ldir = os.listdir(args.loc)
run_format(check_fdir(ldir, args.loc), args.quiet, args.interactive)
return
print(f'Strange path: {args.loc}')
if __name__ == '__main__':
main()