From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-guru+bounces-152-garchives=archives.gentoo.org@lists.gentoo.org>
Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits))
	(No client certificate requested)
	by finch.gentoo.org (Postfix) with ESMTPS id 075B61581FB
	for <garchives@archives.gentoo.org>; Sun, 24 Nov 2024 22:47:46 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id 45FC8E092A
	for <garchives@archives.gentoo.org>; Sun, 24 Nov 2024 22:47:36 +0000 (UTC)
Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [IPv6:2a00:1450:4864:20::533])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(No client certificate requested)
	by pigeon.gentoo.org (Postfix) with ESMTPS id A749DE076B
	for <gentoo-guru@lists.gentoo.org>; Sun, 24 Nov 2024 13:01:10 +0000 (UTC)
Received: by mail-ed1-x533.google.com with SMTP id 4fb4d7f45d1cf-5cecbddb574so4948345a12.1
        for <gentoo-guru@lists.gentoo.org>; Sun, 24 Nov 2024 05:01:10 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20230601; t=1732453269; x=1733058069; darn=lists.gentoo.org;
        h=content-transfer-encoding:cc:to:subject:message-id:date:from
         :in-reply-to:references:mime-version:from:to:cc:subject:date
         :message-id:reply-to;
        bh=PsOcb3GyH8CqHdXQnMGY8KUutSgn5HzLTfpkR/vEygI=;
        b=ZXyFkJ0Qr/BEXhuH13k8G36Zm/JBQ8Sqg7p1M81qam0rYQvPvX2T9g1pUqSCCSKqig
         5r5EYsD2hsIo49lVMVVjA6ORpCLy0o+7Xo8AjQg4nrRfMDHq8tDJBydUtPSGsqh3xbjz
         DjgS/RTOuQ93egMSTgo6cft5GCUHG01iubz/jlho69yO1oGC0Vvxw/IZKUadrMdlWhW3
         /nX9xnL7Bt+VgdldFkAo/CbirpgU8deN7xkDKxOeIMOGeSpFrSPhqAoYINojdGSpFrOj
         RzmMnUPAbOxncULbexIFGUwdTZyPpWh4Zxf150DpNIBzLG5RjS2if9xel1uLLQOg1gVW
         5kUg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20230601; t=1732453269; x=1733058069;
        h=content-transfer-encoding:cc:to:subject:message-id:date:from
         :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc
         :subject:date:message-id:reply-to;
        bh=PsOcb3GyH8CqHdXQnMGY8KUutSgn5HzLTfpkR/vEygI=;
        b=H4H57oVcAQuOWaB2n/ve3UtnNdWifxqrXAnF4paCc12mOZtuMryEJxmEQInkC1fa1t
         XoGJe7EUqHEOEdO9y7ICeo3Mc9xTDdvPq42nybfUAsWY5py9xHi4G3gHfDm3R3WfbL7F
         rYW0A3Utc5xH0SxYsmArEYwUfgJlDkqFKqfwy03pI2UWBpj/djhMpvTksgxjfa48geZt
         +xeU3wtxJUrVWW7wyU1wC3vDkm0k207Z+8mU+nGCyYS6fSMG6A3YnmYwyYiEd3eUToho
         VKKiVizwCtzaRn8G1V12Dmul5O9UrpK0OVI0FdPyykvL4IaQDstMF0vCMsd8VyMmall3
         CdIw==
X-Gm-Message-State: AOJu0YzkYJ7EMIv4SOU96Ri5xIU/Kq7XnT4cdRl8275XzR512j0kBdLk
	7pnebXNJCfacP3J/2vF9JPGqs67yDleOOmbbGu8waolFOgmdC8jRIm7a0gGV3rdnpgAgRqHhtXd
	6k5gl549KH+/ZGwlBPVlnq3EtubkQtA==
X-Gm-Gg: ASbGncvnrNKtlIS/0Bz1TajYDGkVHaf31oWifhL5f48ij3ZfS9rD+KvzefScPQBgkEY
	CmPYu2aTbx+qE9vCwCsDfZnoX2ANBIcARWsm6CYCfRFpvFbd+a85U8nrMsXFjSS/Ixg==
X-Google-Smtp-Source: AGHT+IGOrwC+SugWJO9s99bvJaQG3yHjsBs73RlJLK4XdO5yL72V+qHmtqhm3XdHYPgTdscn6AzKT96LbXpd4KVb2d8=
X-Received: by 2002:a05:6402:5186:b0:5cf:4f4:95dc with SMTP id
 4fb4d7f45d1cf-5d0207b135bmr8119682a12.29.1732453269155; Sun, 24 Nov 2024
 05:01:09 -0800 (PST)
Precedence: bulk
List-Post: <mailto:gentoo-guru@lists.gentoo.org>
List-Help: <mailto:gentoo-guru+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-guru+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-guru+subscribe@lists.gentoo.org>
List-Id: Discussion on the GURU project and repository <gentoo-guru.gentoo.org>
X-BeenThere: gentoo-guru@gentoo.org
X-BeenThere: gentoo-guru@lists.gentoo.org
X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply
MIME-Version: 1.0
References: <20241123073302.24038-1-cyber+gentoo@sysrq.in> <20241123073302.24038-2-cyber+gentoo@sysrq.in>
In-Reply-To: <20241123073302.24038-2-cyber+gentoo@sysrq.in>
From: Takuya Wakazono <pastalian46@gmail.com>
Date: Sun, 24 Nov 2024 22:00:33 +0900
Message-ID: <CAAaAyq_1O9twBoq9hWaA8BOm6B_jJUQ8zqVsL=48T89mCBLuqQ@mail.gmail.com>
Subject: Re: [gentoo-guru] [PATCH 1/2] click-app.eclass: new eclass
To: "Anna (cybertailor) Vyalkova" <cyber+gentoo@sysrq.in>
Cc: gentoo-guru@lists.gentoo.org
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Archives-Salt: e8cca2fa-b867-4bb9-bd61-33993d07af70
X-Archives-Hash: 82c8ef050d65610685faef8c73e97a74

On Sat, Nov 23, 2024 at 4:33=E2=80=AFPM Anna (cybertailor) Vyalkova
<cyber+gentoo@sysrq.in> wrote:
>
> Signed-off-by: Anna (cybertailor) Vyalkova <cyber+gentoo@sysrq.in>
> ---
>  eclass/click-app.eclass | 162 ++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 162 insertions(+)
>  create mode 100644 eclass/click-app.eclass
>
> diff --git a/eclass/click-app.eclass b/eclass/click-app.eclass
> new file mode 100644
> index 000000000..0a75b9ea5
> --- /dev/null
> +++ b/eclass/click-app.eclass
> @@ -0,0 +1,162 @@
> +# Copyright 2024 Gentoo Authors
> +# Distributed under the terms of the GNU General Public License v2
> +
> +# @ECLASS: click-app.eclass
> +# @MAINTAINER:
> +# Anna <cyber+gentoo@sysrq.in>
> +# @AUTHOR:
> +# Anna <cyber+gentoo@sysrq.in>
> +# @SUPPORTED_EAPIS: 8
> +# @BLURB: eclass for Click-based Python applications
> +# @DESCRIPTION:
> +# This eclass provides a streamlined way to generate and install shell
> +# completions for Python applications based on the Click library
> +# (dev-python/click package).
> +
> +case ${EAPI} in
> +       8) ;;
> +       *) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
> +esac
> +
> +if [[ ! ${_CLICK_APP_ECLASS} ]]; then
> +_CLICK_APP_ECLASS=3D1
> +
> +inherit distutils-r1 shell-completion
> +
> +readonly _CLICK_COMPLETIONS_BUILD_DIR=3D"${WORKDIR}/${P}_click-shell-com=
pletions"
> +
> +# @FUNCTION: click-app_enable_completions
> +# @USAGE: <script...>
> +# @DESCRIPTION:
> +# Set up IUSE, BDEPEND, python_compile_all() and python_install_all() to
> +# generate and install shell completions for the given scripts.
> +#
> +# This function does not overwrite python_compile_all() or python_instal=
l_all()
> +# if they are already defined (e.g. by distutils_enable_sphinx).  You sh=
ould call
> +# click-app_python_compile_all or click-app_python_install_all in the
> +# corresponding common phase functions in such cases.
> +#
> +# This function must be called in global scope.
> +#
> +# See also: https://click.palletsprojects.com/en/stable/shell-completion=
/
> +click-app_enable_completions() {
> +       debug-print-function "${FUNCNAME}" "${@}"
> +       (( $# >=3D 1 )) ||
> +               die "${FUNCNAME} takes at least one argument"
> +
> +       IUSE+=3D" bash-completion"
> +       BDEPEND+=3D" bash-completion? ( ${RDEPEND} )"
How about 'shell-completion'? 'bash-completion' controlling fish and zsh
completion files feels unintuitive.

> +
> +       _CLICK_SCRIPTS=3D()
> +       for script in "${@}"; do
> +               _CLICK_SCRIPTS+=3D( "${script}" )
> +       done
> +       readonly -a _CLICK_SCRIPTS
> +
> +       if ! declare -f python_compile_all; then
> +               python_compile_all() { click-app_python_compile_all; }
> +       fi
> +
> +       if ! declare -f python_install_all; then
> +               python_install_all() {
> +                       click-app_python_install_all
> +                       distutils-r1_python_install_all
> +               }
> +       fi
> +
> +       # we need to ensure successful return in case we're called last,
> +       # otherwise Portage may wrongly assume sourcing failed
> +       return 0
> +}
> +
> +# @FUNCTION: click-app_pkg_setup
> +# @DESCRIPTION:
> +# Ensure that only enabled Python implementations are used in python_...=
_all
> +# pseudo-phases when shell completions are to be built.
> +click-app_pkg_setup() {
> +       debug-print-function "${FUNCNAME}" "${@}"
> +       use bash-completion || return 0
> +
> +       # If the package isn't built for a Python implementation that run=
s
> +       # python_compile_all(), we won't be able to call Python scripts f=
rom
> +       # its install tree.
> +       DISTUTILS_ALL_SUBPHASE_IMPLS=3D()
> +       for impl in "${PYTHON_COMPAT[@]}"; do
> +               use "python_targets_${impl}" &&
> +                       DISTUTILS_ALL_SUBPHASE_IMPLS+=3D( "${impl}" )
> +       done
> +}
> +
> +# @FUNCTION: click-app_python_compile_all
> +# @DESCRIPTION:
> +# Build shell completions for all scripts for which shell completions
> +# were requested with click-app_enable_completions.
> +click-app_python_compile_all() {
> +       debug-print-function "${FUNCNAME}" "${@}"
> +       use bash-completion || return 0
> +
> +       for script in "${_CLICK_SCRIPTS[@]}"; do
> +               build_click_completions "${script}"
> +       done
> +}
> +
> +# @FUNCTION: click-app_python_install_all
> +# @DESCRIPTION:
> +# Install generated shell completions for all scripts for which shell
> +# completions were requested with click-app_enable_completions.
> +click-app_python_install_all() {
> +       debug-print-function "${FUNCNAME}" "${@}"
> +       use bash-completion || return 0
> +
> +       for script in "${_CLICK_SCRIPTS[@]}"; do
> +               install_click_completions "${script}"
> +       done
> +}
> +
> +# @FUNCTION: build_click_completions
> +# @USAGE: <script>
> +# @DESCRIPTION:
> +# Build shell completions for a script.
> +build_click_completions() {
> +       debug-print-function "${FUNCNAME}" "${@}"
> +       (( $# =3D=3D 1 )) ||
> +               die "${FUNCNAME} takes exactly one argument"
> +
> +       local env_var_name out_path script_path t
> +
> +       script_path=3D"${BUILD_DIR}/install${EPREFIX}/usr/bin/${1}"
> +       [[ -f "${script_path}" ]] ||
> +               die "${script_path} not found, build_click_completions ca=
ll wrong"
> +
> +       # convert to screaming snake case
> +       t=3D${1^^}
> +       t=3D${t//-/_}
> +       env_var_name=3D"_${t}_COMPLETE"
> +
> +       mkdir -p "${_CLICK_COMPLETIONS_BUILD_DIR}" || die
> +       for shell in bash fish zsh; do
> +               out_path=3D"${_CLICK_COMPLETIONS_BUILD_DIR}/${1}.${shell}=
"
> +
> +               echo "${env_var_name}=3D${shell}_source ${script_path} > =
${out_path}" >&2
> +               local -x "${env_var_name}"=3D"${shell}_source" || die
> +               "${script_path}" > "${out_path}" || die
> +       done
> +}
> +
> +# @FUNCTION: install_click_completions
> +# @USAGE: <script>
> +# @DESCRIPTION:
> +# Install generated shell completions for a script.
> +install_click_completions() {
> +       debug-print-function "${FUNCNAME}" "${@}"
> +       (( $# =3D=3D 1 )) ||
> +               die "${FUNCNAME} takes exactly one argument"
> +
> +       newbashcomp "${_CLICK_COMPLETIONS_BUILD_DIR}/${1}.bash" "${1}"
> +       newfishcomp "${_CLICK_COMPLETIONS_BUILD_DIR}/${1}.fish" "${1}.fis=
h"
> +       newzshcomp "${_CLICK_COMPLETIONS_BUILD_DIR}/${1}.zsh" "_${1}"
> +}
> +
> +fi
> +
> +EXPORT_FUNCTIONS pkg_setup
> --
> 2.47.0
>
>