# ============================================================================ # Python Project Template Functions # ============================================================================ source "${0:A:h}/../lib/utils.zsh" 2>/dev/null || \ source "$HOME/.dotfiles/zsh/lib/utils.zsh" 2>/dev/null typeset -g PY_PYTHON="${PY_PYTHON:-python3}" typeset -g PY_VENV="${PY_VENV:-venv}" typeset -g PY_GIT_INIT="${PY_GIT_INIT:-true}" _py_check_name() { [[ -z "$1" ]] && { df_print_warning "Project name required"; return 1; } [[ -d "$1" ]] && { df_print_warning "Directory '$1' exists"; return 1; } return 0 } _py_venv() { df_print_step "Creating virtual environment" "$PY_PYTHON" -m venv "$1/$PY_VENV" df_print_success "Created: $PY_VENV" } _py_gitignore() { cat > "$1/.gitignore" << 'EOF' __pycache__/ *.py[cod] *.so build/ dist/ *.egg-info/ venv/ .venv/ .env *.log .pytest_cache/ .mypy_cache/ EOF df_print_success "Created .gitignore" } _py_git() { [[ "$PY_GIT_INIT" == "true" ]] && { cd "$1"; git init; git add .; git commit -m "Initial commit"; df_print_success "Git initialized"; } } _py_next() { echo "" df_print_section "Next steps" df_print_indent "cd $1" df_print_indent "source $PY_VENV/bin/activate" } py-new() { _py_check_name "$1" || return 1 df_print_func_name "Python Project: $1" mkdir -p "$1"/{src,tests} touch "$1/src/__init__.py" "$1/tests/__init__.py" cat > "$1/src/main.py" << 'EOF' #!/usr/bin/env python3 def main(): print("Hello!") if __name__ == "__main__": main() EOF echo "# Dependencies" > "$1/requirements.txt" _py_venv "$1"; _py_gitignore "$1"; _py_git "$1" df_print_success "Created: $1" _py_next "$1" } py-flask() { _py_check_name "$1" || return 1 df_print_func_name "Flask Project: $1" mkdir -p "$1"/{app/{templates,static},tests} _py_venv "$1" df_print_step "Installing Flask" "$1/$PY_VENV/bin/pip" install flask -q cat > "$1/app/__init__.py" << 'EOF' from flask import Flask def create_app(): app = Flask(__name__) from app.routes import main app.register_blueprint(main) return app EOF cat > "$1/app/routes.py" << 'EOF' from flask import Blueprint, render_template main = Blueprint('main', __name__) @main.route('/') def index(): return render_template('index.html') EOF echo '