forked from openhive-network/hive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.py
109 lines (89 loc) · 2.42 KB
/
format.py
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/python3.6
import sys
import os
INDENT = int(sys.argv[1])
PATH = ""
FILENAME = os.path.split( PATH )[1]
TMP_FILENAME = "____TEMPORARY_FILE_____"
INDENTATION_CHARACHTERS = [ '\t', ' ' ]
FILE_INDENTATION : int = None
DO_NOT_TOUCH_FOLDERS = [
"libraries/fc",
"libraries/vendor",
"libraries/net",
"build",
"out",
".git",
".vs"
]
INPUT_LINE : str = None
def has_indent( line ):
if len(line) == 0:
return False
return line[0] in INDENTATION_CHARACHTERS
def detect_indentation():
first = None
second = None
with open( PATH, 'r' ) as INPUT:
for line in INPUT:
if has_indent(line) and first is not None:
second = count_indents(line)
if second > first:
return second - first
if len( line ) > 1:
first = None
second = None
if line[ len( line ) - 2 ] == "{":
first = count_indents(line)
return -1
def count_indents( line ):
count = 0
for char in line:
if char in INDENTATION_CHARACHTERS:
count += 1
else:
return count
def process( line ):
count = count_indents( line )
ret = line[ count : ]
if count % FILE_INDENTATION != 0:
count = int( count / FILE_INDENTATION ) + 1
else:
count = int( count / FILE_INDENTATION )
return str( " " * count * INDENT ) + ret
def processable( line : str ):
for var in DO_NOT_TOUCH_FOLDERS:
if line.find( var ) != -1:
print("{}: {}".format(var, line))
return False
return True
i = 0
# gather input files
os.system( 'find $PWD -type f | grep -E ".+\.[hc]((pp|xx|c|f)?)$" > ____list_of_files' )
with open( "____list_of_files", 'r') as list_of_files:
for file_to_process in list_of_files:
fname = file_to_process[:-1] # remove \n
# fname = fname.replace("./", os.getcwd() + "/")
if processable( fname ):
PATH = fname
FILENAME = os.path.split( PATH )[1]
# detect
FILE_INDENTATION = detect_indentation()
if FILE_INDENTATION == -1:
print( "cannot determine indentation: {}; skipping".format(PATH) )
continue
if FILE_INDENTATION == INDENT:
print( "nothing to change: {}; skipping".format(PATH) )
continue
if FILE_INDENTATION != 3:
print( "unusual indentation of {}: {}".format(FILE_INDENTATION, PATH) )
# process
with open( PATH, 'r' ) as INPUT:
with open( TMP_FILENAME, 'w' ) as OUTPUT:
for line in INPUT:
OUTPUT.write( process( line ) )
print("succesfully updated indent: {}".format(PATH))
# save
import shutil
shutil.move( TMP_FILENAME, PATH )
os.remove("____list_of_files")