diff options
Diffstat (limited to 'plugins/yaml/python/tree.c')
-rw-r--r-- | plugins/yaml/python/tree.c | 99 |
1 files changed, 43 insertions, 56 deletions
diff --git a/plugins/yaml/python/tree.c b/plugins/yaml/python/tree.c index 9e1a2a3..eeab3de 100644 --- a/plugins/yaml/python/tree.c +++ b/plugins/yaml/python/tree.c @@ -40,11 +40,11 @@ /* Crée un nouvel objet Python de type 'YamlTree'. */ static PyObject *py_yaml_tree_new(PyTypeObject *, PyObject *, PyObject *); -/* Recherche le noeud correspondant à un chemin. */ -static PyObject *py_yaml_tree_find_node_by_path(PyObject *, PyObject *); +/* Recherche les noeuds correspondant à un chemin. */ +static PyObject *py_yaml_tree_find_by_path(PyObject *, PyObject *); -/* Fournit la liste des premiers noeuds de l'arborescence Yaml. */ -static PyObject *py_yaml_tree_get_root_nodes(PyObject *, void *); +/* Fournit le noeud constituant la racine d'arborescence Yaml. */ +static PyObject *py_yaml_tree_get_root(PyObject *, void *); @@ -65,7 +65,6 @@ static PyObject *py_yaml_tree_get_root_nodes(PyObject *, void *); static PyObject *py_yaml_tree_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyObject *result; /* Instance à retourner */ - Py_ssize_t indent; /* Indice de ligne associée */ PyObject *tuple; /* Liste de lignes Yaml */ int ret; /* Bilan de lecture des args. */ size_t count; /* Nombre d'éléments présents */ @@ -80,12 +79,11 @@ static PyObject *py_yaml_tree_new(PyTypeObject *type, PyObject *args, PyObject * "\n" \ "Instances can be created using the following constructor:\n" \ "\n" \ - " YamlTree(indent, lines)" \ + " YamlTree(lines)" \ "\n" \ - "Where indent provides the length of a single level of indentation" \ - " and lines are a tuple of Yaml lines used to built the tree." + "Where lines are a tuple of Yaml lines used to built the tree." - ret = PyArg_ParseTuple(args, "nO!", &indent, &PyTuple_Type, &tuple); + ret = PyArg_ParseTuple(args, "O!", &PyTuple_Type, &tuple); if (!ret) return NULL; count = PyTuple_Size(tuple); @@ -106,7 +104,7 @@ static PyObject *py_yaml_tree_new(PyTypeObject *type, PyObject *args, PyObject * } - tree = g_yaml_tree_new(indent, lines, count); + tree = g_yaml_tree_new(lines, count); arg_error: @@ -141,27 +139,29 @@ static PyObject *py_yaml_tree_new(PyTypeObject *type, PyObject *args, PyObject * * Paramètres : self = variable non utilisée ici. * * args = arguments fournis à l'appel. * * * -* Description : Recherche le noeud correspondant à un chemin. * +* Description : Recherche les noeuds correspondant à un chemin. * * * -* Retour : Eventuel noeud trouvé ou NULL si aucun. * +* Retour : Liste de noeuds trouvés, éventuellement vide. * * * * Remarques : - * * * ******************************************************************************/ -static PyObject *py_yaml_tree_find_node_by_path(PyObject *self, PyObject *args) +static PyObject *py_yaml_tree_find_by_path(PyObject *self, PyObject *args) { PyObject *result; /* Instance à retourner */ const char *path; /* Chemin d'accès à traiter */ int ret; /* Bilan de lecture des args. */ GYamlTree *tree; /* Version GLib du type */ - GYamlNode *found; /* Création GLib à transmettre */ + GYamlNode **found; /* Créations GLib à transmettre*/ + size_t count; /* Quantité de trouvailles */ + size_t i; /* Boucle de parcours */ #define YAML_TREE_FIND_BY_PATH PYTHON_METHOD_DEF \ ( \ - find_node_by_path, "path", \ + find_by_path, "path", \ METH_VARARGS, py_yaml_tree, \ - "Find a node in a Yaml tree by its path.\n" \ + "Find nodes in a Yaml tree using a path.\n" \ "\n" \ "Paths are node keys separated by '/', such as '/my/path/to/node'." \ ) @@ -171,20 +171,26 @@ static PyObject *py_yaml_tree_find_node_by_path(PyObject *self, PyObject *args) tree = G_YAML_TREE(pygobject_get(self)); - found = g_yaml_tree_find_node_by_path(tree, path); + g_yaml_tree_find_by_path(tree, path, &found, &count); - if (found == NULL) - { - result = Py_None; - Py_INCREF(result); - } + result = PyTuple_New(count); - else + for (i = 0; i < count; i++) { - result = pygobject_new(G_OBJECT(found)); - g_object_unref(found); +#ifndef NDEBUG + ret = PyTuple_SetItem(result, i, pygobject_new(G_OBJECT(found[i]))); + assert(ret == 0); +#else + PyTuple_SetItem(result, i, pygobject_new(G_OBJECT(found[i]))); +#endif + + g_object_unref(G_OBJECT(found[i])); + } + if (found != NULL) + free(found); + return result; } @@ -195,51 +201,32 @@ static PyObject *py_yaml_tree_find_node_by_path(PyObject *self, PyObject *args) * Paramètres : self = objet Python concerné par l'appel. * * closure = non utilisé ici. * * * -* Description : Fournit la liste des premiers noeuds de l'arborescence Yaml. * +* Description : Fournit le noeud constituant la racine d'arborescence Yaml. * * * -* Retour : Noeuds constituant les racines de l'arborescence. * +* Retour : Noeud constituant la racine de l'arborescence. * * * * Remarques : - * * * ******************************************************************************/ -static PyObject *py_yaml_tree_get_root_nodes(PyObject *self, void *closure) +static PyObject *py_yaml_tree_get_root(PyObject *self, void *closure) { PyObject *result; /* Résultat à retourner */ GYamlTree *tree; /* Version GLib du type */ - size_t count; /* Quantité de noeuds à traiter*/ - GYamlNode **nodes; /* Liste des noeuds à la racine*/ - size_t i; /* Boucle de parcours */ -#ifndef NDEBUG - int ret; /* Bilan d'une insertion */ -#endif + GYamlNode *root; /* Noeud racine d'arborescence */ -#define YAML_TREE_ROOT_NODES_ATTRIB PYTHON_GET_DEF_FULL \ -( \ - root_nodes, py_yaml_tree, \ - "List of Yaml nodes which are the roots of all tree nodes." \ +#define YAML_TREE_ROOT_ATTRIB PYTHON_GET_DEF_FULL \ +( \ + root, py_yaml_tree, \ + "Yaml node which is the root of the whole tree nodes." \ ) tree = G_YAML_TREE(pygobject_get(self)); - nodes = g_yaml_tree_get_root_nodes(tree, &count); - - result = PyTuple_New(count); - - for (i = 0; i < count; i++) - { -#ifndef NDEBUG - ret = PyTuple_SetItem(result, i, pygobject_new(G_OBJECT(nodes[i]))); - assert(ret == 0); -#else - PyTuple_SetItem(result, i, pygobject_new(G_OBJECT(nodes[i]))); -#endif - - g_object_unref(G_OBJECT(nodes[i])); - - } + root = g_yaml_tree_get_root(tree); - free(nodes); + result = pygobject_new(G_OBJECT(root)); + g_object_unref(G_OBJECT(root)); return result; @@ -266,7 +253,7 @@ PyTypeObject *get_python_yaml_tree_type(void) }; static PyGetSetDef py_yaml_tree_getseters[] = { - YAML_TREE_ROOT_NODES_ATTRIB, + YAML_TREE_ROOT_ATTRIB, { NULL } }; |