Skip to content

Commit 1781320

Browse files
committed
refactor: replace tangerine with shell script for lazy loader init generation
Remove tangerine dependency by implementing docstring preservation and template generation using shell commands. The new approach: - Extracts existing docstrings using sed and diff commands - Preserves docstrings when generating __init__.py files - Adds 'del lazy' statement to clean up namespace - Maintains same functionality without external tool dependency This change simplifies the build process and removes an external dependency while maintaining the same lazy loading behavior.
1 parent 774048c commit 1781320

2 files changed

Lines changed: 22 additions & 16 deletions

File tree

template/.config/copier/mise-tasks/gen/init.sh

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,42 @@ set -o errexit
55
set -o nounset
66
set -o pipefail
77

8+
function get-docstring() {
9+
local file=$1
10+
docstring="$(sed '/"""$/q' "$file")"
11+
if diff --brief "$file" <(echo "$docstring") > /dev/null; then
12+
return
13+
else
14+
echo -n "$docstring"
15+
fi
16+
}
17+
818
function has() {
919
type "$@" &> /dev/null
1020
}
1121

1222
TEMPLATE="\
13-
# >>> tangerine-start: lazy-loader.py >>>
1423
import lazy_loader as lazy
1524
1625
__getattr__, __dir__, __all__ = lazy.attach_stub(__name__, __file__)
17-
# <<< tangerine-end <<<
18-
"
26+
del lazy"
1927

2028
git_root=$(git rev-parse --show-toplevel)
2129
src_dir="$git_root/src"
2230
if [[ -d $src_dir ]]; then
2331
while IFS= read -d '' -r file; do
2432
file="${file/%'.pyi'/'.py'}"
25-
if has tangerine; then
26-
if [[ ! -f $file ]]; then
27-
tangerine <<< "$TEMPLATE" > "$file"
28-
else
29-
tangerine --in-place "$file"
30-
fi
33+
if [[ ! -f $file ]]; then
34+
echo "$TEMPLATE" > "$file"
3135
else
32-
if [[ ! -f $file ]]; then
33-
printf '%s' "$TEMPLATE" > "$file"
34-
else
35-
: # skip existing files
36-
fi
36+
docstring=$(get-docstring "$file")
37+
{
38+
if [[ -n $docstring ]]; then
39+
echo "$docstring"
40+
echo
41+
fi
42+
echo "$TEMPLATE"
43+
} > "$file"
3744
fi
3845
done < <(find "$src_dir" -name '__init__.pyi' -type f -print0)
3946
fi
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# >>> tangerine-start: lazy-loader.py >>>
21
import lazy_loader as lazy
32

43
__getattr__, __dir__, __all__ = lazy.attach_stub(__name__, __file__)
5-
# <<< tangerine-end <<<
4+
del lazy

0 commit comments

Comments
 (0)