PyAPI: add bpy.utils.unescape_identifier

Utility to perform the reverse of `bpy.utils.escape_identifier`
This commit is contained in:
Campbell Barton 2020-12-10 14:09:29 +11:00
parent 2f86518ac0
commit b5bc9d80a1
2 changed files with 55 additions and 0 deletions

View File

@ -26,6 +26,7 @@ not associated with blenders internal data.
__all__ = (
"blend_paths",
"escape_identifier",
"unescape_identifier",
"keyconfig_init",
"keyconfig_set",
"load_scripts",
@ -60,6 +61,7 @@ from _bpy import (
_utils_units as units,
blend_paths,
escape_identifier,
unescape_identifier,
register_class,
resource_path,
script_paths as _bpy_script_paths,

View File

@ -292,6 +292,50 @@ static PyObject *bpy_escape_identifier(PyObject *UNUSED(self), PyObject *value)
return value_escape;
}
PyDoc_STRVAR(bpy_unescape_identifier_doc,
".. function:: unescape_identifier(string)\n"
"\n"
" Simple string un-escape function used for animation paths.\n"
" This performs the reverse of `escape_identifier`.\n"
"\n"
" :arg string: text\n"
" :type string: string\n"
" :return: The un-escaped string.\n"
" :rtype: string\n");
static PyObject *bpy_unescape_identifier(PyObject *UNUSED(self), PyObject *value)
{
const char *value_str;
Py_ssize_t value_str_len;
char *value_unescape_str;
Py_ssize_t value_unescape_str_len;
PyObject *value_unescape;
size_t size;
value_str = _PyUnicode_AsStringAndSize(value, &value_str_len);
if (value_str == NULL) {
PyErr_SetString(PyExc_TypeError, "expected a string");
return NULL;
}
size = value_str_len + 1;
value_unescape_str = PyMem_MALLOC(size);
value_unescape_str_len = BLI_str_unescape(value_unescape_str, value_str, size);
if (value_unescape_str_len == value_str_len) {
Py_INCREF(value);
value_unescape = value;
}
else {
value_unescape = PyUnicode_FromStringAndSize(value_unescape_str, value_unescape_str_len);
}
PyMem_FREE(value_unescape_str);
return value_unescape;
}
static PyMethodDef meth_bpy_script_paths = {
"script_paths",
(PyCFunction)bpy_script_paths,
@ -328,6 +372,12 @@ static PyMethodDef meth_bpy_escape_identifier = {
METH_O,
bpy_escape_identifier_doc,
};
static PyMethodDef meth_bpy_unescape_identifier = {
"unescape_identifier",
(PyCFunction)bpy_unescape_identifier,
METH_O,
bpy_unescape_identifier_doc,
};
static PyObject *bpy_import_test(const char *modname)
{
@ -429,6 +479,9 @@ void BPy_init_modules(struct bContext *C)
PyModule_AddObject(mod,
meth_bpy_escape_identifier.ml_name,
(PyObject *)PyCFunction_New(&meth_bpy_escape_identifier, NULL));
PyModule_AddObject(mod,
meth_bpy_unescape_identifier.ml_name,
(PyObject *)PyCFunction_New(&meth_bpy_unescape_identifier, NULL));
/* register funcs (bpy_rna.c) */
PyModule_AddObject(mod,