Add the ability for Python to pass download progress to Project Manager and to cancel downloads.

Signed-off-by: AMZN-Phil <pconroy@amazon.com>
This commit is contained in:
AMZN-Phil
2021-10-21 16:07:07 -07:00
parent 50068bcae7
commit e3cfcd4cc7
8 changed files with 142 additions and 11 deletions
@@ -223,6 +223,50 @@ namespace RedirectOutput
}
} // namespace RedirectOutput
namespace O3DEProjectManagerPy
{
using DownloadProgressFunc = AZStd::function<void(int)>;
DownloadProgressFunc currentProgressCallback;
bool requestCancelDownload = false;
static PyObject* CLICancelDownload(PyObject* /*self*/, PyObject* /*args*/)
{
if (requestCancelDownload)
{
return Py_True;
}
else
{
return Py_False;
}
}
static PyObject* CLIDownloadProgress(PyObject* /*self*/, PyObject* args)
{
int progress_percentage = 0;
if (!PyArg_ParseTuple(args, "i", &progress_percentage))
return NULL;
if (currentProgressCallback)
{
currentProgressCallback(progress_percentage);
}
Py_RETURN_NONE;
}
static PyMethodDef O3DEPMPyMethods[] = { { "download_progress", CLIDownloadProgress, METH_VARARGS, "Used to call back to the UI to inform of download progress." },
{ "request_cancel_download", CLICancelDownload, METH_NOARGS, "Returns that the UI is requesting that the current download be cancelled." },
{ NULL, NULL, 0, NULL } };
static PyModuleDef O3DEPMPyModule = { PyModuleDef_HEAD_INIT, "o3de_projectmanager", NULL, -1, O3DEPMPyMethods, NULL, NULL, NULL, NULL };
static PyObject* PyInit_O3DEPMPy(void)
{
return PyModule_Create(&O3DEPMPyModule);
}
} // namespace O3DEProjectManagerPy
namespace O3DE::ProjectManager
{
PythonBindings::PythonBindings(const AZ::IO::PathView& enginePath)
@@ -270,6 +314,7 @@ namespace O3DE::ProjectManager
AZ_TracePrintf("python", "Py_GetProgramFullPath=%ls \n", Py_GetProgramFullPath());
PyImport_AppendInittab("azlmbr_redirect", RedirectOutput::PyInit_RedirectOutput);
PyImport_AppendInittab("o3de_projectmanager", &O3DEProjectManagerPy::PyInit_O3DEPMPy);
try
{
@@ -1080,6 +1125,8 @@ namespace O3DE::ProjectManager
AZ::Outcome<void, AZStd::string> PythonBindings::DownloadGem(const QString& gemName, std::function<void(int)> gemProgressCallback)
{
bool downloadSucceeded = false;
O3DEProjectManagerPy::currentProgressCallback = gemProgressCallback;
O3DEProjectManagerPy::requestCancelDownload = false;
auto result = ExecuteWithLockErrorHandling(
[&]
{
@@ -1091,6 +1138,8 @@ namespace O3DE::ProjectManager
downloadSucceeded = (downloadResult.cast<int>() == 0);
});
O3DEProjectManagerPy::currentProgressCallback = nullptr;
if (!result.IsSuccess())
{
return result;
@@ -1102,4 +1151,9 @@ namespace O3DE::ProjectManager
return AZ::Success();
}
void PythonBindings::CancelDownload()
{
O3DEProjectManagerPy::requestCancelDownload = true;
}
}