-
Notifications
You must be signed in to change notification settings - Fork 0
/
pformat
executable file
·51 lines (39 loc) · 1.31 KB
/
pformat
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
#!/usr/bin/env bash
# Initialize the FILENAME variable
FILENAME=""
# Iterate through the arguments
for arg in "$@"; do
# Check if the argument is not a flag
if [[ ${arg:0:2} != "--" ]]; then
# Set the FILENAME variable and break the loop
FILENAME="$arg"
break
fi
done
# Function to move Python imports to the top
move_imports() {
local FILENAME="$1"
# Create a temporary file
local TMP_FILE=$(mktemp)
local FIRST_LINE=$(head -n 1 "$FILENAME")
if [[ $FIRST_LINE == "#%%"* || $FIRST_LINE == "# %%"* ]]; then
# Remove the first line
sed -i '1d' "$FILENAME"
# Prepend the first line at the beginning of the original file
echo "$FIRST_LINE" >"$TMP_FILE"
fi
# Extract lines matching the pattern and store them in a temporary file
sed -n -e '/^import.*\|^from.*import.*/p' "$FILENAME" >>"$TMP_FILE"
# Remove the extracted lines from the original file
sed -i '/^import.*\|^from.*import.*/d' "$FILENAME"
# Prepend the extracted lines at the beginning of the original file
cat "$TMP_FILE" "$FILENAME" >temp && mv temp "$FILENAME"
}
# Uncompromising Python code formatter -- Google 2-space style
cblack "$FILENAME" &
sleep 0.1
# Move all imports to the top
move_imports "$FILENAME" &
sleep 0.1
# Sort imports
isort "$FILENAME" &