summaryrefslogtreecommitdiff
path: root/plugins/yaml/python
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2023-02-04 16:36:10 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2023-02-04 16:36:10 (GMT)
commite85f35454bf94b7414dd9d2f5e6609601951293c (patch)
treef8cf3afe5b2ed34949878552b663d4b037e16238 /plugins/yaml/python
parentdb3b204dd7a71b2f74a4e69b2159a96e3ab66614 (diff)
Provide constructor to load Yaml contents without external files.
Diffstat (limited to 'plugins/yaml/python')
-rw-r--r--plugins/yaml/python/line.c3
-rw-r--r--plugins/yaml/python/reader.c151
2 files changed, 66 insertions, 88 deletions
diff --git a/plugins/yaml/python/line.c b/plugins/yaml/python/line.c
index 11898d2..f098273 100644
--- a/plugins/yaml/python/line.c
+++ b/plugins/yaml/python/line.c
@@ -26,6 +26,7 @@
#include <pygobject.h>
+#include <string.h>
#include <plugins/pychrysalide/helpers.h>
@@ -92,7 +93,7 @@ static PyObject *py_yaml_line_new(PyTypeObject *type, PyObject *args, PyObject *
ret = PyArg_ParseTuple(args, "sn", &raw, &index);
if (!ret) return NULL;
- line = g_yaml_line_new(raw, index);
+ line = g_yaml_line_new(raw, strlen(raw), index);
if (line == NULL)
{
diff --git a/plugins/yaml/python/reader.c b/plugins/yaml/python/reader.c
index 74fd2b3..795b60c 100644
--- a/plugins/yaml/python/reader.c
+++ b/plugins/yaml/python/reader.c
@@ -28,25 +28,20 @@
#include <pygobject.h>
+#include <i18n.h>
+#include <plugins/pychrysalide/access.h>
#include <plugins/pychrysalide/helpers.h>
+#include <plugins/pychrysalide/analysis/content.h>
-#include "../reader.h"
+#include "../reader-int.h"
-#define YAML_READER_DOC \
- "YamlReader is the class which aims to provide a reader interface to Yaml content.\n" \
- "\n" \
- "When no input error, the Yaml content can be retrieved line by line or thanks to a tree."
+CREATE_DYN_CONSTRUCTOR(yaml_reader, G_TYPE_YAML_READER);
-
-
-/* Crée un lecteur pour contenu au format Yaml. */
-static PyObject *py_yaml_reader_new_from_content(PyObject *, PyObject *);
-
-/* Crée un lecteur pour contenu au format Yaml. */
-static PyObject *py_yaml_reader_new_from_path(PyObject *, PyObject *);
+/* Initialise une instance sur la base du dérivé de GObject. */
+static int py_yaml_reader_init(PyObject *, PyObject *, PyObject *);
/* Fournit la liste des lignes lues depuis un contenu Yaml. */
static PyObject *py_yaml_reader_get_lines(PyObject *, void *);
@@ -58,108 +53,90 @@ static PyObject *py_yaml_reader_get_tree(PyObject *, void *);
/******************************************************************************
* *
-* Paramètres : self = variable non utilisée ici. *
+* Paramètres : self = objet à initialiser (théoriquement). *
* args = arguments fournis à l'appel. *
+* kwds = arguments de type key=val fournis. *
* *
-* Description : Crée un lecteur pour contenu au format Yaml. *
+* Description : Initialise une instance sur la base du dérivé de GObject. *
* *
-* Retour : Instance mise en place ou None en cas d'échec. *
+* Retour : 0. *
* *
* Remarques : - *
* *
******************************************************************************/
-static PyObject *py_yaml_reader_new_from_content(PyObject *self, PyObject *args)
+static int py_yaml_reader_init(PyObject *self, PyObject *args, PyObject *kwds)
{
- PyObject *result; /* Instance à retourner */
- const char *content; /* Contenu brut au format Yaml */
- Py_ssize_t length; /* Taille de ce contenu */
+ const char *text; /* Contenu de règles à traiter */
+ const char *filename; /* Fichier de définitions */
int ret; /* Bilan de lecture des args. */
GYamlReader *reader; /* Création GLib à transmettre */
-#define YAML_READER_NEW_FROM_CONTENT_METHOD PYTHON_METHOD_DEF \
-( \
- new_from_content, "content", \
- METH_STATIC | METH_VARARGS, py_yaml_reader, \
- "Load a Yaml content." \
-)
+ static char *kwlist[] = { "text", "filename", NULL };
- /**
- * La taille doit être de type 'int' et non 'Py_ssize_t', sinon les 32 bits
- * de poids fort ne sont pas initialisés !
- */
+#define YAML_READER_DOC \
+ "YamlReader is the class which aims to provide a reader interface" \
+ " to Yaml content.\n" \
+ "\n" \
+ "Instances can be created using one of the following" \
+ " constructors:\n" \
+ "\n" \
+ " YamlReader(text=str)" \
+ "\n" \
+ " YamlReader(filename=str)" \
+ "\n" \
+ "Where *text* is a string containg a markup content to parse;" \
+ " the *filename* argument is an alternative string for a path" \
+ " pointing to the same kind of content. This path can be a real" \
+ " filename or a resource URI." \
+ "\n" \
+ "When parsing is successful, the Yaml content can be retrieved" \
+ " line by line or thanks to a tree."
- ret = PyArg_ParseTuple(args, "s#", &content, &length);
- if (!ret) return NULL;
+ /* Récupération des paramètres */
- reader = g_yaml_reader_new_from_content(content, length);
+ text = NULL;
+ filename = NULL;
- if (reader == NULL)
- {
- result = Py_None;
- Py_INCREF(result);
- }
+ ret = PyArg_ParseTupleAndKeywords(args, kwds, "|ss", kwlist, &text, &filename);
+ if (!ret) return -1;
- else
- {
- g_object_ref_sink(G_OBJECT(reader));
- result = pygobject_new(G_OBJECT(reader));
- g_object_unref(reader);
- }
+ /* Initialisation d'un objet GLib */
- return result;
+ ret = forward_pygobjet_init(self);
+ if (ret == -1) return -1;
-}
+ /* Eléments de base */
+ reader = G_YAML_READER(pygobject_get(self));
-/******************************************************************************
-* *
-* Paramètres : self = variable non utilisée ici. *
-* args = arguments fournis à l'appel. *
-* *
-* Description : Crée un lecteur pour contenu au format Yaml. *
-* *
-* Retour : Instance mise en place ou None en cas d'échec. *
-* *
-* Remarques : - *
-* *
-******************************************************************************/
-
-static PyObject *py_yaml_reader_new_from_path(PyObject *self, PyObject *args)
-{
- PyObject *result; /* Instance à retourner */
- const char *path; /* Chemin d'accès à un contenu */
- int ret; /* Bilan de lecture des args. */
- GYamlReader *reader; /* Création GLib à transmettre */
-
-#define YAML_READER_NEW_FROM_PATH_METHOD PYTHON_METHOD_DEF \
-( \
- new_from_path, "path", \
- METH_STATIC | METH_VARARGS, py_yaml_reader, \
- "Load a Yaml content from a path.\n" \
- "\n" \
- "The path can be a filename or a resource URI." \
-)
-
- ret = PyArg_ParseTuple(args, "s", &path);
- if (!ret) return NULL;
+ if (text != NULL)
+ {
+ if (!g_yaml_reader_create_from_text(reader, text))
+ {
+ PyErr_SetString(PyExc_ValueError, _("Unable to create Yaml reader."));
+ return -1;
+ }
- reader = g_yaml_reader_new_from_path(path);
+ }
- if (reader == NULL)
+ else if (filename != NULL)
{
- result = Py_None;
- Py_INCREF(result);
+ if (!g_yaml_reader_create_from_file(reader, filename))
+ {
+ PyErr_SetString(PyExc_ValueError, _("Unable to create Yaml reader."));
+ return -1;
+ }
+
}
else
{
- g_object_ref_sink(G_OBJECT(reader));
- result = pygobject_new(G_OBJECT(reader));
- g_object_unref(reader);
+ PyErr_SetString(PyExc_ValueError, _("Unable to create empty Yaml reader."));
+ return -1;
}
- return result;
+ return 0;
}
@@ -281,8 +258,6 @@ static PyObject *py_yaml_reader_get_tree(PyObject *self, void *closure)
PyTypeObject *get_python_yaml_reader_type(void)
{
static PyMethodDef py_yaml_reader_methods[] = {
- YAML_READER_NEW_FROM_CONTENT_METHOD,
- YAML_READER_NEW_FROM_PATH_METHOD,
{ NULL }
};
@@ -305,7 +280,9 @@ PyTypeObject *get_python_yaml_reader_type(void)
.tp_methods = py_yaml_reader_methods,
.tp_getset = py_yaml_reader_getseters,
- .tp_new = no_python_constructor_allowed
+
+ .tp_init = py_yaml_reader_init,
+ .tp_new = py_yaml_reader_new,
};