From 22a41b49f10d627ead1b6da7180b4a3e24dd14d0 Mon Sep 17 00:00:00 2001 From: tosu Date: Mon, 3 Jun 2024 21:35:46 +0200 Subject: [PATCH] Make distributable --- .gitignore | 1 + requirements.txt | 8 ++++++++ woxi | 47 ++++++++++++++++++++++++++++++++++++++++------- woxi.py | 12 ++++++++---- 4 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 .gitignore create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bdaab25 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +env/ diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4593cf6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,8 @@ +beautifulsoup4==4.12.3 +certifi==2024.6.2 +charset-normalizer==3.3.2 +idna==3.7 +Pygments==2.18.0 +requests==2.32.3 +soupsieve==2.5 +urllib3==2.2.1 diff --git a/woxi b/woxi index e7717cc..adc6028 100755 --- a/woxi +++ b/woxi @@ -1,9 +1,42 @@ -#!/bin/bash +#! /bin/sh - +# POSIX-compliant wrapper script that executes $(basename $0).py +# Allows you to put symlinks (even differently named ones) to this script in +# any directory (e.g. /usr/local/bin), but the venv will still be created in +# the directory of the resolved symlink (the regular file). -W=$(which woxi.py) +set -eEu # exit on error, inherit that property to functions, unset variables are error -if [ -n "${W}" ]; then - $W "$@" | jq -else - printf "Couldn't find woxi.py" -fi +# POSIX trap doesn't have the ERR trap +trap '[ $? = 0 ] || echo "There was an error executing '"'"'$0'"'"'"' EXIT + +# Bit overkill, but needed: https://stackoverflow.com/a/33266819/13823467 +rreadlink()(set +eu; target=$1 fname= targetDir= CDPATH=; { \unalias command; \unset -f command; } >/dev/null 2>&1; [ -n "${ZSH_VERSION}" ] && options[POSIX_BUILTINS]=on; while :; do [ -L "$target" ] || [ -e "$target" ] || { command printf '%s\n' "ERROR: '$target' does not exist." >&2; return 1; }; command cd "$(command dirname -- "$target")"; fname=$(command basename -- "$target"); [ "$fname" = '/' ] && fname=''; if [ -L "$fname" ]; then target=$(command ls -l "$fname"); target=${target#* -> }; continue; fi; break; done; targetDir=$(command pwd -P); if [ "$fname" = '.' ]; then command printf '%s\n' "${targetDir%/}"; elif [ "$fname" = '..' ]; then command printf '%s\n' "$(command dirname -- "${targetDir}")"; else command printf '%s\n' "${targetDir%/}/$fname"; fi) + +realpath="$(rreadlink "$0")" +prefix="$(dirname -- "${realpath}")" + +log () { + case "$1" in + err) clr="31" ;; + warn) clr="33" ;; + info) clr="34" ;; + *) clr="41;30" ;; + esac + printf "\033[${clr}m$0: %s\033[m\n" "$2" +} + +# try python3 -m venv and virtualenv to create venv, +# then activate and install requirements.txt +create_env () { + rm -rf "${prefix}"/env + command -v python3 >/dev/null 2>&1 || { log err "failed to find python3."; exit 2; } + python3 -m venv "${prefix}/env" || virtualenv --python "$(command -v python3)" "${prefix}/env" + command . "${prefix}"/env/bin/activate || { log err "failed to activate venv."; exit 3; } + pip3 install -r "${prefix}"/requirements.txt || { log err "failed to install dependencies."; exit 4; } +} + +# Activate venv -- dot builtin should be called with command builtin: https://unix.stackexchange.com/a/740901/520093 +command . "${prefix}"/env/bin/activate || { log warn "failed to activate venv. Creating it."; create_env; } + +# Exec script with args +exec "${prefix}"/$(basename -- "${realpath}").py "$@" diff --git a/woxi.py b/woxi.py index eb1bcda..9462d79 100755 --- a/woxi.py +++ b/woxi.py @@ -1,12 +1,16 @@ #!/usr/bin/env python3 -from bs4 import BeautifulSoup -import requests import re -import sys import os +import sys import json +import requests +from bs4 import BeautifulSoup +from pygments import highlight +from pygments.lexers import JsonLexer +from pygments.formatters import TerminalFormatter + def main(query='hallo'): template_url = 'https://synonyme.woxikon.de/synonyme/%s.php' url = template_url % query @@ -46,7 +50,7 @@ def main(query='hallo'): card_list = card_list[::-1] out = json.dumps(card_list, indent=2, ensure_ascii=False) - print(out) + print(highlight(out, JsonLexer(), TerminalFormatter(style='monokai'))) if __name__ == '__main__':