#define PY_SSIZE_T_CLEAN #include #include "tree_structurer.hpp" static PyObject* TreeStructurerError; static TreeStructurer* g_tree_structurer = nullptr; static PyObject* create_tree_structurer(PyObject* self, PyObject* args) { if (g_tree_structurer != nullptr) { delete g_tree_structurer; } g_tree_structurer = new TreeStructurer(); Py_RETURN_NONE; } static PyObject* get_structure(PyObject* self, PyObject* args) { const char* path = nullptr; if (!PyArg_ParseTuple(args, "|s", &path)) { return NULL; } if (g_tree_structurer == nullptr) { PyErr_SetString(TreeStructurerError, "TreeStructurer not initialized"); return NULL; } try { std::string path_str = path ? path : ""; std::vector structure = g_tree_structurer->get_directory_structure(path_str); PyObject* list = PyList_New(structure.size()); for (size_t i = 0; i < structure.size(); i++) { PyList_SET_ITEM(list, i, PyUnicode_FromString(structure[i].c_str())); } return list; } catch (const std::exception& e) { PyErr_SetString(TreeStructurerError, e.what()); return NULL; } } static PyMethodDef TreeStructurerMethods[] = { {"create_tree_structurer", create_tree_structurer, METH_NOARGS, "Create a new TreeStructurer instance"}, {"get_structure", get_structure, METH_VARARGS, "Get the directory structure for the given path"}, {NULL, NULL, 0, NULL} }; static struct PyModuleDef tree_structurer_module = { PyModuleDef_HEAD_INIT, "_tree_structurer", // Changed module name to match Python import "Module for analyzing directory structures", -1, TreeStructurerMethods }; PyMODINIT_FUNC PyInit__tree_structurer(void) { // Changed function name to match module name PyObject* m = PyModule_Create(&tree_structurer_module); if (m == NULL) return NULL; TreeStructurerError = PyErr_NewException("tree_structurer.error", NULL, NULL); Py_XINCREF(TreeStructurerError); if (PyModule_AddObject(m, "error", TreeStructurerError) < 0) { Py_XDECREF(TreeStructurerError); Py_CLEAR(TreeStructurerError); Py_DECREF(m); return NULL; } return m; }