forked from mathLab/PyGeM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_formatter.sh
More file actions
executable file
·68 lines (57 loc) · 1.79 KB
/
code_formatter.sh
File metadata and controls
executable file
·68 lines (57 loc) · 1.79 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
#!/bin/bash
#######################################
required_command="yapf unexpand"
code_directories="pygem tests"
#######################################
usage() {
echo
echo -e "\tUsage: $0 [files]"
echo
echo -e "\tIf not files are specified, script formats all ".py" files"
echo -e "\tin code directories ($code_directories); otherwise, formats"
echo -e "\tall given files"
echo
echo -e "\tRequired command: $required_command"
echo
exit 0
}
[[ $1 == "-h" ]] && usage
# Test for required program
for comm in $required_command; do
command -v $comm >/dev/null 2>&1 || {
echo "I require $comm but it's not installed. Aborting." >&2;
exit 1
}
done
# Find all python files in code directories
python_files=""
for dir in $code_directories; do
python_files="$python_files `ls $dir/*.py`"
done
[[ $# != 0 ]] && python_files=$@
# The files unvhandler.py and params.py can not be formatted because there are
# strings with spaces inside that otherwise would be converted in tabs.
python_files_true=""
for file in $python_files; do
if [ $file != "pygem/unvhandler.py" ] && [ $file != "pygem/params.py" ]; then
python_files_true="$python_files_true $file"
fi
done
# Here the important part:
# - first, yapf format the files; it works very well just setting few option
# by command line, but at the moment it has bugs for tabs, so uses spaces.
# - second, convert 4 spaces to tab character
# - third, you can look a very pretty code
for file in $python_files_true; do
echo "Making beatiful $file..."
[[ ! -f $file ]] && echo "$file does not exist; $0 -h for more info" && exit
yapf --style='{
based_on_style: pep8,
indent_width: 4,
dedent_closing_brackets = true,
coalesce_brackets = true,
column_limit = 80
}' -i $file
unexpand -t 4 $file > tmp.py
mv tmp.py $file
done