47 lines
1.1 KiB
Text
47 lines
1.1 KiB
Text
local target="${1:-}"
|
|
if [[ -z "$target" ]]; then
|
|
echo "usage: npython <new_script_name.py>"
|
|
return 1
|
|
fi
|
|
|
|
# PREVENT OVERWRITES
|
|
# exit immediately if the target file already exists
|
|
if [[ -f "$target" ]]; then
|
|
echo "error: file '$target' already exists."
|
|
return 1
|
|
fi
|
|
|
|
# CREATE DIRECTORIES
|
|
# ensure the parent directory structure exists before creating the file
|
|
mkdir -p "$(dirname "$target")"
|
|
|
|
# TEMPLATE SELECTION
|
|
# use the external template if available, otherwise generate inline
|
|
local template="$HOME/.python_templates/script.py"
|
|
if [[ -f "$template" ]]; then
|
|
cp "$template" "$target"
|
|
else
|
|
cat > "$target" <<EOF
|
|
#!/usr/bin/env python3
|
|
# AUTHOR: Daesorin
|
|
# CREATED: $(date +"%F %R %z")
|
|
|
|
import sys
|
|
|
|
def main():
|
|
# main execution block
|
|
print("Hello, world!")
|
|
return 0
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|
|
EOF
|
|
fi
|
|
|
|
# FILE PERMISSIONS
|
|
# grant read, write, and execute to the owner, and read to others
|
|
chmod u=rwx,go=r "$target"
|
|
|
|
# LAUNCH EDITOR
|
|
# open the target in the user environment editor
|
|
${EDITOR:-vim} "$target"
|