diff options
Diffstat (limited to 'plugins/yaml/python/collection.c')
-rw-r--r-- | plugins/yaml/python/collection.c | 81 |
1 files changed, 53 insertions, 28 deletions
diff --git a/plugins/yaml/python/collection.c b/plugins/yaml/python/collection.c index e21bb9e..fd8e08a 100644 --- a/plugins/yaml/python/collection.c +++ b/plugins/yaml/python/collection.c @@ -28,18 +28,22 @@ #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); -/* Indique la nature d'une collection Yaml. */ +/* 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 *); /* Fournit la liste des noeuds intégrés dans une collection. */ @@ -49,44 +53,56 @@ 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 */ #define YAML_COLLECTION_DOC \ - "YamlCollection handles a collection of Yaml nodes.\n" \ + "YamlCollection handles a collection of YAML nodes.\n" \ "\n" \ "Instances can be created using the following constructor:\n" \ "\n" \ " YamlCollection(seq=False)\n" \ "\n" \ - "Where seq defines if the collection will be a sequence or a mapping of nodes." + "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; } @@ -98,7 +114,7 @@ static PyObject *py_yaml_collection_new(PyTypeObject *type, PyObject *args, PyOb * * * Description : Fournit la liste des noeuds intégrés dans une collection. * * * -* Retour : Enfants d'un noeud issu d'une collection Yaml. * +* Retour : Enfants d'un noeud issu d'une collection YAML. * * * * Remarques : - * * * @@ -152,7 +168,7 @@ static PyObject *py_yaml_collection_get_nodes(PyObject *self, void *closure) * Paramètres : self = objet Python concerné par l'appel. * * closure = non utilisé ici. * * * -* Description : Indique la nature d'une collection Yaml. * +* Description : Indique la nature d'une collection YAML. * * * * Retour : Nature de la collection. * * * @@ -169,7 +185,7 @@ static PyObject *py_yaml_collection_is_sequence(PyObject *self, void *closure) #define YAML_COLLECTION_IS_SEQUENCE_ATTRIB PYTHON_IS_DEF_FULL \ ( \ sequence, py_yaml_collection, \ - "Nature of the collection: True is the collection is a sequence," \ + "Nature of the collection: True if the collection is a sequence," \ " False if it is a mapping of \"key: value\" nodes." \ ) @@ -222,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, }; @@ -233,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. * * * @@ -243,17 +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); - if (!register_class_for_pygobject(dict, G_TYPE_YAML_COLLEC, type, get_python_yaml_node_type())) - return false; + if (!register_class_for_pygobject(dict, G_TYPE_YAML_COLLEC, type)) + return false; + + } return true; @@ -265,7 +290,7 @@ bool register_python_yaml_collection(PyObject *module) * Paramètres : arg = argument quelconque à tenter de convertir. * * dst = destination des valeurs récupérées en cas de succès. * * * -* Description : Tente de convertir en collection de noeuds de format Yaml. * +* Description : Tente de convertir en collection de noeuds de format YAML. * * * * Retour : Bilan de l'opération, voire indications supplémentaires. * * * @@ -287,7 +312,7 @@ int convert_to_yaml_collection(PyObject *arg, void *dst) break; case 0: - PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to Yaml collection"); + PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to YAML collection"); break; case 1: |