Cleanup: simplify argument parsing for preview load enum

Use PyC_StringEnumItems to handle matching a value & raising an error
for invalid arguments.
This commit is contained in:
Campbell Barton
2023-08-11 12:28:28 +10:00
parent 3af7ed0e8e
commit d2a2d06691

View File

@@ -24,6 +24,8 @@
#include "bpy_rna.h"
#include "bpy_utils_previews.h"
#include "../generic/py_capi_utils.h"
#include "MEM_guardedalloc.h"
#include "IMB_imbuf.h"
@@ -90,43 +92,42 @@ PyDoc_STRVAR(
" :raises KeyError: if ``name`` already exists.");
static PyObject *bpy_utils_previews_load(PyObject * /*self*/, PyObject *args)
{
char *name, *path, *path_type_s;
int path_type, force_reload = false;
char *name, *filepath;
const PyC_StringEnumItems path_type_items[] = {
{THB_SOURCE_IMAGE, "IMAGE"},
{THB_SOURCE_MOVIE, "MOVIE"},
{THB_SOURCE_BLEND, "BLEND"},
{THB_SOURCE_FONT, "FONT"},
{THB_SOURCE_OBJECT_IO, "OBJECT_IO"},
{0, nullptr},
};
PyC_StringEnum path_type = {
path_type_items,
/* The default isn't used. */
0,
};
int force_reload = false;
PreviewImage *prv;
if (!PyArg_ParseTuple(args,
"s" /* `name` */
"s" /* `filepath` */
"O&" /* `filetype` */
"|" /* Optional arguments. */
"p" /* `force_reload` */
":load",
&name,
&filepath,
PyC_ParseStringEnum,
&path_type,
&force_reload))
{
return nullptr;
}
PreviewImage *prv = BKE_previewimg_cached_thumbnail_read(
name, filepath, path_type.value_found, force_reload);
PointerRNA ptr;
if (!PyArg_ParseTuple(args, "sss|p:load", &name, &path, &path_type_s, &force_reload)) {
return nullptr;
}
if (STREQ(path_type_s, "IMAGE")) {
path_type = THB_SOURCE_IMAGE;
}
else if (STREQ(path_type_s, "MOVIE")) {
path_type = THB_SOURCE_MOVIE;
}
else if (STREQ(path_type_s, "BLEND")) {
path_type = THB_SOURCE_BLEND;
}
else if (STREQ(path_type_s, "FONT")) {
path_type = THB_SOURCE_FONT;
}
else if (STREQ(path_type_s, "OBJECT_IO")) {
path_type = THB_SOURCE_OBJECT_IO;
}
else {
PyErr_Format(PyExc_ValueError,
"load: invalid '%s' filetype, only [" STR_SOURCE_TYPES
"] "
"are supported",
path_type_s);
return nullptr;
}
prv = BKE_previewimg_cached_thumbnail_read(name, path, path_type, force_reload);
RNA_pointer_create(nullptr, &RNA_ImagePreview, prv, &ptr);
return pyrna_struct_CreatePyObject(&ptr);
}