summaryrefslogtreecommitdiff
path: root/plugins/yaml/python/collection.c
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2023-05-24 00:24:00 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2023-05-24 00:26:23 (GMT)
commit9f4abb8a20871c64b33f88ad5538bbbe111c1d4c (patch)
tree6fede4c409ba09151b79e88ee3ffe1831be6727e /plugins/yaml/python/collection.c
parent62f073f9a5eb039d8bc5b6d304fca484d4198a9f (diff)
Update the YAML Python bindings code.
Diffstat (limited to 'plugins/yaml/python/collection.c')
-rw-r--r--plugins/yaml/python/collection.c64
1 files changed, 43 insertions, 21 deletions
diff --git a/plugins/yaml/python/collection.c b/plugins/yaml/python/collection.c
index a3ea76c..fd8e08a 100644
--- a/plugins/yaml/python/collection.c
+++ b/plugins/yaml/python/collection.c
@@ -28,16 +28,20 @@
#include <pygobject.h>
+#include <i18n.h>
+#include <plugins/pychrysalide/access.h>
#include <plugins/pychrysalide/helpers.h>
#include "node.h"
-#include "../collection.h"
+#include "../collection-int.h"
-/* Crée un nouvel objet Python de type 'YamlCollection'. */
-static PyObject *py_yaml_collection_new(PyTypeObject *, PyObject *, PyObject *);
+CREATE_DYN_CONSTRUCTOR(yaml_collection, G_TYPE_YAML_COLLEC);
+
+/* Initialise une instance sur la base du dérivé de GObject. */
+static int py_yaml_collection_init(PyObject *, PyObject *, PyObject *);
/* Indique la nature d'une collection YAML. */
static PyObject *py_yaml_collection_is_sequence(PyObject *, void *);
@@ -49,21 +53,20 @@ static PyObject *py_yaml_collection_get_nodes(PyObject *, void *);
/******************************************************************************
* *
-* Paramètres : type = type de l'objet à instancier. *
+* Paramètres : self = objet à initialiser (théoriquement). *
* args = arguments fournis à l'appel. *
* kwds = arguments de type key=val fournis. *
* *
-* Description : Crée un nouvel objet Python de type 'YamlCollection'. *
+* Description : Initialise une instance sur la base du dérivé de GObject. *
* *
-* Retour : Instance Python mise en place. *
+* Retour : 0. *
* *
* Remarques : - *
* *
******************************************************************************/
-static PyObject *py_yaml_collection_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+static int py_yaml_collection_init(PyObject *self, PyObject *args, PyObject *kwds)
{
- PyObject *result; /* Instance à retourner */
int seq; /* Indicateur de type */
int ret; /* Bilan de lecture des args. */
GYamlCollection *collec; /* Création GLib à transmettre */
@@ -78,16 +81,28 @@ static PyObject *py_yaml_collection_new(PyTypeObject *type, PyObject *args, PyOb
"Where *seq* is a boolean value which defines if the collection will be a" \
" sequence or a mapping of nodes."
+ /* Récupération des paramètres */
+
ret = PyArg_ParseTuple(args, "p", &seq);
- if (!ret) return NULL;
+ if (!ret) return -1;
- collec = g_yaml_collection_new(seq);
+ /* Initialisation d'un objet GLib */
- g_object_ref_sink(G_OBJECT(collec));
- result = pygobject_new(G_OBJECT(collec));
- g_object_unref(collec);
+ ret = forward_pygobjet_init(self);
+ if (ret == -1) return -1;
- return result;
+ /* Eléments de base */
+
+ collec = G_YAML_COLLEC(pygobject_get(self));
+
+ if (!g_yaml_collection_create(collec, seq))
+ {
+ PyErr_SetString(PyExc_ValueError, _("Unable to create YAML collection."));
+ return -1;
+
+ }
+
+ return 0;
}
@@ -223,7 +238,9 @@ PyTypeObject *get_python_yaml_collection_type(void)
.tp_methods = py_yaml_collection_methods,
.tp_getset = py_yaml_collection_getseters,
- .tp_new = py_yaml_collection_new
+
+ .tp_init = py_yaml_collection_init,
+ .tp_new = py_yaml_collection_new,
};
@@ -234,7 +251,7 @@ PyTypeObject *get_python_yaml_collection_type(void)
/******************************************************************************
* *
-* Paramètres : module = module dont la définition est à compléter. *
+* Paramètres : - *
* *
* Description : Prend en charge l'objet 'pychrysalide.....YamlCollection. *
* *
@@ -244,19 +261,24 @@ PyTypeObject *get_python_yaml_collection_type(void)
* *
******************************************************************************/
-bool register_python_yaml_collection(PyObject *module)
+bool ensure_python_yaml_collection_is_registered(void)
{
PyTypeObject *type; /* Type Python 'YamlCollection'*/
+ PyObject *module; /* Module à recompléter */
PyObject *dict; /* Dictionnaire du module */
type = get_python_yaml_collection_type();
- dict = PyModule_GetDict(module);
+ if (!PyType_HasFeature(type, Py_TPFLAGS_READY))
+ {
+ module = get_access_to_python_module("pychrysalide.plugins.yaml");
+
+ dict = PyModule_GetDict(module);
- /* TODO : ensure get_python_yaml_node_type() */
+ if (!register_class_for_pygobject(dict, G_TYPE_YAML_COLLEC, type))
+ return false;
- if (!register_class_for_pygobject(dict, G_TYPE_YAML_COLLEC, type))
- return false;
+ }
return true;