Blender V5.3
bpy_path.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <Python.h>
12
13#include "bpy_path.hh"
14
16
17#include "BKE_autoexec.hh"
18
19/* #include "IMB_imbuf_types.hh" */
20
21namespace blender {
22
23extern const char *imb_ext_image[];
24extern const char *imb_ext_movie[];
25extern const char *imb_ext_audio[];
26
28 /* Wrap. */
29 bpy_path_is_autoexec_doc,
30 ".. function:: is_autoexec(path, *, skip_overrides=False, canonicalize=False, "
31 "strip_filename=False)\n"
32 "\n"
33 " Return false when a directory is excluded from running scripts automatically,\n"
34 " see :class:`bpy.types.PreferencesFilePaths.autoexec_paths`.\n"
35 "\n"
36 " .. note:: The preference to enable automatic script execution & any command line\n"
37 " override are checked unless ``skip_overrides`` is set,\n"
38 " see :class:`bpy.types.PreferencesFilePaths.use_scripts_auto_execute`.\n"
39 "\n"
40 " :param path: The directory to check, expected to end with a path separator,\n"
41 " must not be empty.\n"
42 " :type path: str | bytes\n"
43 " :param skip_overrides: Only check the excluded paths, skipping the preference to enable\n"
44 " automatic script execution & any command line override.\n"
45 " When false, the result is the default for \"Trusted Source\" when opening the path.\n"
46 " :type skip_overrides: bool\n"
47 " :param canonicalize: Resolve the path first,\n"
48 " disable when it's known to be resolved.\n"
49 " :type canonicalize: bool\n"
50 " :param strip_filename: Use the directory of `path`, otherwise it is a directory already.\n"
51 " :type strip_filename: bool\n"
52 " :return: True when blend-files in the directory are trusted to run scripts.\n"
53 " :rtype: bool\n");
54static PyObject *bpy_path_is_autoexec(PyObject * /*self*/, PyObject *args, PyObject *kw)
55{
56 PyC_UnicodeAsBytesAndSize_Data path_data = {nullptr};
57 bool skip_overrides = false;
58 bool canonicalize = false;
59 bool strip_filename = false;
60
61 static const char *_keywords[] = {
62 "path", "skip_overrides", "canonicalize", "strip_filename", nullptr};
63 static _PyArg_Parser _parser = {
64 "O&" /* `path` */
65 "|$" /* Optional, keyword only arguments. */
66 "O&" /* `skip_overrides` */
67 "O&" /* `canonicalize` */
68 "O&" /* `strip_filename` */
69 ":is_autoexec",
70 _keywords,
71 nullptr,
72 };
73 if (!_PyArg_ParseTupleAndKeywordsFast(args,
74 kw,
75 &_parser,
77 &path_data,
79 &skip_overrides,
81 &canonicalize,
83 &strip_filename))
84 {
85 return nullptr;
86 }
87
88 PyObject *result = nullptr;
89 if (path_data.value_len == 0) [[unlikely]] {
90 PyErr_SetString(PyExc_ValueError, "is_autoexec: path must not be empty");
91 }
92 else {
93 const bool is_autoexec = BKE_autoexec_default_trust_source(
94 path_data.value,
95 {
96 .skip_overrides = skip_overrides,
97 .canonicalize = canonicalize,
98 .strip_filename = strip_filename,
99 });
100 result = PyBool_FromLong(is_autoexec);
101 }
102
103 Py_XDECREF(path_data.value_coerce);
104 return result;
105}
106
107static PyMethodDef _bpy_path_methods[] = {
108 {"is_autoexec",
109 reinterpret_cast<PyCFunction>(bpy_path_is_autoexec),
110 METH_VARARGS | METH_KEYWORDS,
111 bpy_path_is_autoexec_doc},
112 {nullptr, nullptr, 0, nullptr},
113};
114
115/*----------------------------MODULE INIT-------------------------*/
116static PyModuleDef _bpy_path_module_def = {
117 /*m_base*/ PyModuleDef_HEAD_INIT,
118 /*m_name*/ "_bpy_path",
119 /*m_doc*/ nullptr,
120 /*m_size*/ 0,
121 /*m_methods*/ _bpy_path_methods,
122 /*m_slots*/ nullptr,
123 /*m_traverse*/ nullptr,
124 /*m_clear*/ nullptr,
125 /*m_free*/ nullptr,
126};
127
129{
130 PyObject *submodule;
131
132 submodule = PyModule_Create(&_bpy_path_module_def);
133
134 PyModule_AddObject(submodule, "extensions_image", PyC_FrozenSetFromStrings(imb_ext_image));
135 PyModule_AddObject(submodule, "extensions_movie", PyC_FrozenSetFromStrings(imb_ext_movie));
136 PyModule_AddObject(submodule, "extensions_audio", PyC_FrozenSetFromStrings(imb_ext_audio));
137
138 return submodule;
139}
140
141} // namespace blender
BMesh Iterators.
PyObject * PyC_FrozenSetFromStrings(const char **strings)
int PyC_ParseBool(PyObject *o, void *p)
static PyObject * bpy_path_is_autoexec(PyObject *, PyObject *args, PyObject *kw)
Definition bpy_path.cc:54
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
PyObject * BPyInit__bpy_path()
Definition bpy_path.cc:128
int PyC_ParseUnicodeAsBytesAndSize(PyObject *o, void *p)
bool BKE_autoexec_default_trust_source(const char *path, const AutoExec_Params &params)
Definition autoexec.cc:103
static PyMethodDef _bpy_path_methods[]
Definition bpy_path.cc:107
static PyModuleDef _bpy_path_module_def
Definition bpy_path.cc:116