# Copyright 2022 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

DISTUTILS_USE_PEP517=setuptools
PYTHON_COMPAT=( python3_{8..10} )

DATA_COM="5a3db8447d3e13ed402545662f20f5ff191a6d42"
DATA_DATE="20190506"

inherit distutils-r1 virtualx xdg-utils

DESCRIPTION="Interactive visualization in Python"
HOMEPAGE="http://vispy.org"
SRC_URI="mirror://pypi/${PN:0:1}/${PN}/${P}.tar.gz
	doc? (
		https://github.com/vispy/demo-data/raw/${DATA_COM}/mona_lisa/mona_lisa_sm.png -> ${PN}-${DATA_DATE}-d-mona_lisa_sm.png
		https://github.com/vispy/demo-data/raw/${DATA_COM}/spot/spot.obj.gz -> ${PN}-${DATA_DATE}-d-spot.obj.gz
		https://github.com/vispy/demo-data/raw/${DATA_COM}/spot/spot.png -> ${PN}-${DATA_DATE}-d-spot.png
		https://github.com/vispy/demo-data/raw/${DATA_COM}/volume/stent.npz -> ${PN}-${DATA_DATE}-d-stent.npz
		https://github.com/vispy/demo-data/raw/${DATA_COM}/orig/triceratops.obj.gz -> ${PN}-${DATA_DATE}-d-triceratops.obj.gz
		https://github.com/vispy/demo-data/raw/${DATA_COM}/orig/crate.npz -> ${PN}-${DATA_DATE}-d-crate.npz
		https://github.com/vispy/demo-data/raw/${DATA_COM}/brain/mri.npz -> ${PN}-${DATA_DATE}-d-mri.npz
	)
"

LICENSE="BSD"
SLOT="0"
KEYWORDS="~amd64"	# no x86 KEYWORD for meshio PyQt6 pyside6
IUSE="examples io ipython-static pyglet +pyqt5 pyqt6 pyside2 pyside6 sdl2 wx"
PROPERTIES="test_network"
RESTRICT="test"
REQUIRED_USE="|| ( pyglet pyqt5 pyqt6 pyside2 pyside6 sdl2 wx )"

DEPEND="dev-python/numpy[${PYTHON_USEDEP}]"
RDEPEND="${DEPEND}
	dev-python/freetype-py[${PYTHON_USEDEP}]
	dev-python/hsluv[${PYTHON_USEDEP}]
	dev-python/kiwisolver[${PYTHON_USEDEP}]
	dev-python/packaging[${PYTHON_USEDEP}]
	io? (
		dev-python/meshio[${PYTHON_USEDEP}]
		dev-python/pillow[${PYTHON_USEDEP}]
	)
	ipython-static? ( dev-python/ipython[${PYTHON_USEDEP}] )
	pyglet? ( >=dev-python/pyglet-1.2[${PYTHON_USEDEP}] )
	pyqt5? ( dev-python/PyQt5[${PYTHON_USEDEP},gui,testlib,widgets] )
	pyqt6? ( dev-python/PyQt6[${PYTHON_USEDEP},gui,testlib,widgets] )
	pyside2? ( dev-python/pyside2[${PYTHON_USEDEP},gui,testlib,widgets] )
	pyside6? ( dev-python/pyside6[${PYTHON_USEDEP},gui,testlib,widgets] )
	sdl2? ( dev-python/PySDL2[${PYTHON_USEDEP}] )
	wx? ( dev-python/wxpython[${PYTHON_USEDEP}] )
"
BDEPEND="dev-python/setuptools_scm_git_archive[${PYTHON_USEDEP}]
	>=dev-python/cython-0.29.2[${PYTHON_USEDEP}]
	doc? (
		media-libs/fontconfig
		virtual/opengl
	)
	test? (
		dev-python/imageio[${PYTHON_USEDEP}]
		dev-python/meshio[${PYTHON_USEDEP}]
		dev-python/networkx[${PYTHON_USEDEP}]
		dev-python/numpydoc[${PYTHON_USEDEP}]
		dev-python/pyopengl[${PYTHON_USEDEP}]
		dev-python/sphinx-gallery[${PYTHON_USEDEP}]
		media-libs/fontconfig
		virtual/opengl
	)
"

distutils_enable_tests pytest
# QThread: Destroyed while thread is still running
distutils_enable_sphinx doc dev-python/sphinx-gallery \
	dev-python/sphinxcontrib-apidoc \
	dev-python/pydata-sphinx-theme \
	dev-python/imageio \
	dev-python/myst_parser \
	dev-python/networkx \
	dev-python/numpydoc \
	dev-python/pyopengl \
	dev-python/pytest

python_prepare_all() {
	use doc && { eapply "${FILESDIR}"/${PN}-0.12.0-use-local-demo-data.patch ; \
		for dat in "${DISTDIR}"/*-d-*; do { cp ${dat} "${S}"/examples/scene/${dat##*-d-} || die ; } ; done ; \
		cp {"${DISTDIR}"/${PN}-${DATA_DATE}-d-,"${S}"/examples/plotting/}mri.npz || die ; }
	xdg_environment_reset

	distutils-r1_python_prepare_all
}

python_compile_all() {
	if use doc; then
		cp "${BUILD_DIR}"/install/$(python_get_sitedir)/${PN}/visuals/text/*.cpython*so ${PN}/visuals/text || die
		virtx sphinx_compile_all
	fi
}

python_install_all() {
	if use examples; then
		docompress -x "/usr/share/doc/${PF}/examples"
		docinto examples
		dodoc -r examples/.
	fi

	distutils-r1_python_install_all
}

python_test() {
	virtx epytest "${BUILD_DIR}"
}