/* Chrysalide - Outil d'analyse de fichiers binaires * reader.c - équivalent Python du fichier "plugins/yaml/reader.c" * * Copyright (C) 2019 Cyrille Bagard * * This file is part of Chrysalide. * * Chrysalide is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * Chrysalide is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "reader.h" #include #include #include #include #include #include "../reader-int.h" CREATE_DYN_CONSTRUCTOR(yaml_reader, G_TYPE_YAML_READER); /* 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 *); /* Fournit l'arborescence associée à la lecture de lignes Yaml. */ static PyObject *py_yaml_reader_get_tree(PyObject *, void *); /****************************************************************************** * * * Paramètres : self = objet à initialiser (théoriquement). * * args = arguments fournis à l'appel. * * kwds = arguments de type key=val fournis. * * * * Description : Initialise une instance sur la base du dérivé de GObject. * * * * Retour : 0. * * * * Remarques : - * * * ******************************************************************************/ static int py_yaml_reader_init(PyObject *self, PyObject *args, PyObject *kwds) { 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 */ static char *kwlist[] = { "text", "filename", NULL }; #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." /* Récupération des paramètres */ text = NULL; filename = NULL; ret = PyArg_ParseTupleAndKeywords(args, kwds, "|ss", kwlist, &text, &filename); if (!ret) return -1; /* Initialisation d'un objet GLib */ ret = forward_pygobjet_init(self); if (ret == -1) return -1; /* Eléments de base */ reader = G_YAML_READER(pygobject_get(self)); if (text != NULL) { if (!g_yaml_reader_create_from_text(reader, text)) { PyErr_SetString(PyExc_ValueError, _("Unable to create Yaml reader.")); return -1; } } else if (filename != NULL) { if (!g_yaml_reader_create_from_file(reader, filename)) { PyErr_SetString(PyExc_ValueError, _("Unable to create Yaml reader.")); return -1; } } else { PyErr_SetString(PyExc_ValueError, _("Unable to create empty Yaml reader.")); return -1; } return 0; } /****************************************************************************** * * * Paramètres : self = objet Python concerné par l'appel. * * closure = non utilisé ici. * * * * Description : Fournit la liste des lignes lues depuis un contenu Yaml. * * * * Retour : Liste de lignes correspondant au contenu Yaml lu. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_yaml_reader_get_lines(PyObject *self, void *closure) { PyObject *result; /* Résultat à retourner */ GYamlReader *reader; /* Version GLib du type */ size_t count; /* Quantité de lignes à traiter*/ GYamlLine **lines; /* Liste de lignes lues */ size_t i; /* Boucle de parcours */ #ifndef NDEBUG int ret; /* Bilan d'une insertion */ #endif #define YAML_READER_LINES_ATTRIB PYTHON_GET_DEF_FULL \ ( \ lines, py_yaml_reader, \ "List of Yaml lines processed by the reader." \ ) reader = G_YAML_READER(pygobject_get(self)); lines = g_yaml_reader_get_lines(reader, &count); result = PyTuple_New(count); for (i = 0; i < count; i++) { #ifndef NDEBUG ret = PyTuple_SetItem(result, i, pygobject_new(G_OBJECT(lines[i]))); assert(ret == 0); #else PyTuple_SetItem(result, i, pygobject_new(G_OBJECT(lines[i]))); #endif g_object_unref(G_OBJECT(lines[i])); } if (lines != NULL) free(lines); return result; } /****************************************************************************** * * * Paramètres : self = objet Python concerné par l'appel. * * closure = non utilisé ici. * * * * Description : Fournit l'arborescence associée à la lecture de lignes Yaml. * * * * Retour : Arborescence constituée par la lecture du contenu Yaml. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_yaml_reader_get_tree(PyObject *self, void *closure) { PyObject *result; /* Résultat à retourner */ GYamlReader *reader; /* Version GLib du type */ GYamlTree *tree; /* Arborescence associée */ #define YAML_READER_TREE_ATTRIB PYTHON_GET_DEF_FULL \ ( \ tree, py_yaml_reader, \ "Tree of all nodes built from the Yaml content." \ ) reader = G_YAML_READER(pygobject_get(self)); tree = g_yaml_reader_get_tree(reader); if (tree == NULL) { result = Py_None; Py_INCREF(result); } else { result = pygobject_new(G_OBJECT(tree)); g_object_unref(G_OBJECT(tree)); } return result; } /****************************************************************************** * * * Paramètres : - * * * * Description : Fournit un accès à une définition de type à diffuser. * * * * Retour : Définition d'objet pour Python. * * * * Remarques : - * * * ******************************************************************************/ PyTypeObject *get_python_yaml_reader_type(void) { static PyMethodDef py_yaml_reader_methods[] = { { NULL } }; static PyGetSetDef py_yaml_reader_getseters[] = { YAML_READER_LINES_ATTRIB, YAML_READER_TREE_ATTRIB, { NULL } }; static PyTypeObject py_yaml_reader_type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "pychrysalide.plugins.yaml.YamlReader", .tp_basicsize = sizeof(PyGObject), .tp_flags = Py_TPFLAGS_DEFAULT, .tp_doc = YAML_READER_DOC, .tp_methods = py_yaml_reader_methods, .tp_getset = py_yaml_reader_getseters, .tp_init = py_yaml_reader_init, .tp_new = py_yaml_reader_new, }; return &py_yaml_reader_type; } /****************************************************************************** * * * Paramètres : module = module dont la définition est à compléter. * * * * Description : Prend en charge l'objet 'pychrysalide.plugins.....YamlReader.* * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ bool register_python_yaml_reader(PyObject *module) { PyTypeObject *type; /* Type Python 'YamlReader' */ PyObject *dict; /* Dictionnaire du module */ type = get_python_yaml_reader_type(); dict = PyModule_GetDict(module); if (!register_class_for_pygobject(dict, G_TYPE_YAML_READER, type, &PyGObject_Type)) return false; return true; } /****************************************************************************** * * * 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 lecteur de données au format Yaml. * * * * Retour : Bilan de l'opération, voire indications supplémentaires. * * * * Remarques : - * * * ******************************************************************************/ int convert_to_yaml_reader(PyObject *arg, void *dst) { int result; /* Bilan à retourner */ result = PyObject_IsInstance(arg, (PyObject *)get_python_yaml_reader_type()); switch (result) { case -1: /* L'exception est déjà fixée par Python */ result = 0; break; case 0: PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to Yaml reader"); break; case 1: *((GYamlReader **)dst) = G_YAML_READER(pygobject_get(arg)); break; default: assert(false); break; } return result; }