summaryrefslogtreecommitdiff
path: root/plugins/yaml/python/node.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/yaml/python/node.c')
-rw-r--r--plugins/yaml/python/node.c168
1 files changed, 113 insertions, 55 deletions
diff --git a/plugins/yaml/python/node.c b/plugins/yaml/python/node.c
index d1aabee..f2984d1 100644
--- a/plugins/yaml/python/node.c
+++ b/plugins/yaml/python/node.c
@@ -31,6 +31,7 @@
#include <plugins/pychrysalide/helpers.h>
+#include "collection.h"
#include "line.h"
#include "../node.h"
@@ -39,14 +40,17 @@
/* Crée un nouvel objet Python de type 'YamlNode'. */
static PyObject *py_yaml_node_new(PyTypeObject *, PyObject *, PyObject *);
-/* Recherche le noeud correspondant à un chemin. */
-static PyObject *py_yaml_node_find_node_by_path(PyObject *, PyObject *);
+/* Recherche les noeuds correspondant à un chemin. */
+static PyObject *py_yaml_node_find_by_path(PyObject *, PyObject *);
/* Fournit la ligne principale associée à un noeud. */
static PyObject *py_yaml_node_get_yaml_line(PyObject *, void *);
-/* Fournit la liste des noeuds intégrés dans un noeud Yaml. */
-static PyObject *py_yaml_node_get_children(PyObject *, void *);
+/* Attache une collection de noeuds Yaml à un noeud. */
+static int py_yaml_node_set_collection(PyObject *, PyObject *, void *);
+
+/* Fournit une éventuelle collection rattachée à un noeud. */
+static PyObject *py_yaml_node_get_collection(PyObject *, void *);
@@ -99,27 +103,29 @@ static PyObject *py_yaml_node_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_node_find_node_by_path(PyObject *self, PyObject *args)
+static PyObject *py_yaml_node_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. */
- GYamlNode *node; /* Version GLib du type */
- GYamlNode *found; /* Création GLib à transmettre */
+ GYamlNode *node; /* Version GLib du noeud */
+ GYamlNode **found; /* Créations GLib à transmettre*/
+ size_t count; /* Quantité de trouvailles */
+ size_t i; /* Boucle de parcours */
#define YAML_NODE_FIND_BY_PATH PYTHON_METHOD_DEF \
( \
- find_node_by_path, "path", \
+ find_by_path, "path", \
METH_VARARGS, py_yaml_node, \
- "Find a child node in a Yaml node by its path.\n" \
+ "Find nodes from a Yaml node using a path.\n" \
"\n" \
"Paths are node keys separated by '/', such as '/my/path/to/node'." \
)
@@ -129,20 +135,26 @@ static PyObject *py_yaml_node_find_node_by_path(PyObject *self, PyObject *args)
node = G_YAML_NODE(pygobject_get(self));
- found = g_yaml_node_find_node_by_path(node, path);
+ g_yaml_node_find_by_path(node, 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;
}
@@ -164,7 +176,7 @@ static PyObject *py_yaml_node_find_node_by_path(PyObject *self, PyObject *args)
static PyObject *py_yaml_node_get_yaml_line(PyObject *self, void *closure)
{
PyObject *result; /* Résultat à retourner */
- GYamlNode *node; /* Version GLib du type */
+ GYamlNode *node; /* Version GLib du noeud */
GYamlLine *line; /* Line Yaml associée */
#define YAML_NODE_YAML_LINE_ATTRIB PYTHON_GET_DEF_FULL \
@@ -177,9 +189,65 @@ static PyObject *py_yaml_node_get_yaml_line(PyObject *self, void *closure)
line = g_yaml_node_get_yaml_line(node);
- result = pygobject_new(G_OBJECT(line));
+ if (line == NULL)
+ {
+ result = Py_None;
+ Py_INCREF(result);
+ }
+ else
+ {
+ result = pygobject_new(G_OBJECT(line));
+ g_object_unref(G_OBJECT(line));
+ }
+
+ return result;
+
+}
+
+
+
+
+
+/******************************************************************************
+* *
+* Paramètres : self = contenu binaire à manipuler. *
+* value = collection de noeuds Yaml. *
+* closure = adresse non utilisée ici. *
+* *
+* Description : Attache une collection de noeuds Yaml à un noeud. *
+* *
+* Retour : Jeu d'attributs liés au contenu courant. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static int py_yaml_node_set_collection(PyObject *self, PyObject *value, void *closure)
+{
+ int result; /* Bilan à renvoyer */
+ GYamlNode *node; /* Version GLib du noeud */
+ GYamlCollection *collec; /* Version GLib de la valeur */
+
+ node = G_YAML_NODE(pygobject_get(self));
- g_object_unref(G_OBJECT(line));
+ if (value == Py_None)
+ {
+ g_yaml_node_set_collection(node, NULL);
+ result = 0;
+ }
+
+ else
+ {
+ if (!convert_to_yaml_collection(value, &collec))
+ result = -1;
+
+ else
+ {
+ g_yaml_node_set_collection(node, collec);
+ result = 0;
+ }
+
+ }
return result;
@@ -188,54 +256,44 @@ static PyObject *py_yaml_node_get_yaml_line(PyObject *self, void *closure)
/******************************************************************************
* *
-* Paramètres : self = objet Python concerné par l'appel. *
-* closure = non utilisé ici. *
+* Paramètres : self = contenu binaire à manipuler. *
+* closure = adresse non utilisée ici. *
* *
-* Description : Fournit la liste des noeuds intégrés dans un noeud Yaml. *
+* Description : Fournit une éventuelle collection rattachée à un noeud. *
* *
-* Retour : Enfants d'un noeud issu d'une arborescence Yaml. *
+* Retour : Collection de noeuds Yaml ou None. *
* *
* Remarques : - *
* *
******************************************************************************/
-static PyObject *py_yaml_node_get_children(PyObject *self, void *closure)
+static PyObject *py_yaml_node_get_collection(PyObject *self, void *closure)
{
- PyObject *result; /* Résultat à retourner */
- GYamlNode *node; /* 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
+ PyObject *result; /* Instance à retourner */
+ GYamlNode *node; /* Version GLib du noeud */
+ GYamlCollection *collec; /* Collection à transmettre */
-#define YAML_NODE_CHILDREN_ATTRIB PYTHON_GET_DEF_FULL \
-( \
- children, py_yaml_node, \
- "List of nodes which are children of the current node." \
+#define YAML_NODE_COLLECTION_ATTRIB PYTHON_GETSET_DEF_FULL \
+( \
+ collection, py_yaml_node, \
+ "Provide or define the collection of nodes attached to another Yaml node." \
)
node = G_YAML_NODE(pygobject_get(self));
- nodes = g_yaml_node_get_children(node, &count);
+ collec = g_yaml_node_get_collection(node);
- result = PyTuple_New(count);
-
- for (i = 0; i < count; i++)
+ if (collec == NULL)
{
-#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]));
-
+ result = Py_None;
+ Py_INCREF(result);
}
- free(nodes);
+ else
+ {
+ result = pygobject_new(G_OBJECT(collec));
+ g_object_unref(collec);
+ }
return result;
@@ -263,7 +321,7 @@ PyTypeObject *get_python_yaml_node_type(void)
static PyGetSetDef py_yaml_node_getseters[] = {
YAML_NODE_YAML_LINE_ATTRIB,
- YAML_NODE_CHILDREN_ATTRIB,
+ YAML_NODE_COLLECTION_ATTRIB,
{ NULL }
};