diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2020-05-17 18:05:48 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2020-05-17 18:05:48 (GMT) |
commit | d110791309783e6e30df837a81cf8e14e1ac9641 (patch) | |
tree | 75e65909e080cc3ace578c7beb6f0bdae6dc18c1 /plugins/pychrysalide/core | |
parent | f3e136eab9fd6adcb51988c9f70ca7f35552abc4 (diff) |
Updated the plugin system and its Python documentation.
Diffstat (limited to 'plugins/pychrysalide/core')
-rw-r--r-- | plugins/pychrysalide/core/constants.c | 56 | ||||
-rw-r--r-- | plugins/pychrysalide/core/constants.h | 3 | ||||
-rw-r--r-- | plugins/pychrysalide/core/logs.c | 32 |
3 files changed, 67 insertions, 24 deletions
diff --git a/plugins/pychrysalide/core/constants.c b/plugins/pychrysalide/core/constants.c index e61f451..f3ec530 100644 --- a/plugins/pychrysalide/core/constants.c +++ b/plugins/pychrysalide/core/constants.c @@ -73,3 +73,59 @@ bool define_core_logs_constants(PyObject *module) return result; } + + +/****************************************************************************** +* * +* 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 constante LogMessageType. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_log_message_type(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + unsigned long value; /* Valeur transcrite */ + + result = PyObject_IsInstance(arg, (PyObject *)&PyLong_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 LogMessageType"); + break; + + case 1: + value = PyLong_AsUnsignedLong(arg); + + if (value > LMT_COUNT) + { + PyErr_SetString(PyExc_TypeError, "invalid value for LogMessageType"); + result = 0; + } + + else + *((LogMessageType *)dst) = value; + + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/pychrysalide/core/constants.h b/plugins/pychrysalide/core/constants.h index 38a8ebc..6ed6fbb 100644 --- a/plugins/pychrysalide/core/constants.h +++ b/plugins/pychrysalide/core/constants.h @@ -34,6 +34,9 @@ /* Définit les constantes pour les types de messages. */ bool define_core_logs_constants(PyObject *); +/* Tente de convertir en constante LogMessageType. */ +int convert_to_log_message_type(PyObject *, void *); + #endif /* _PLUGINS_PYCHRYSALIDE_CORE_CONSTANTS_H */ diff --git a/plugins/pychrysalide/core/logs.c b/plugins/pychrysalide/core/logs.c index 0965b4b..4e0bc7b 100644 --- a/plugins/pychrysalide/core/logs.c +++ b/plugins/pychrysalide/core/logs.c @@ -103,7 +103,7 @@ static PyObject *py_logs_get_verbosity(PyObject *self, PyObject *args) static PyObject *py_logs_set_verbosity(PyObject *self, PyObject *args) { PyObject *result; /* Bilan à retourner */ - unsigned long verbosity; /* Niveau de filtre de message */ + LogMessageType verbosity; /* Niveau de filtre de message */ #define LOGS_SET_VERBOSITY_METHOD PYTHON_METHOD_DEF \ ( \ @@ -116,7 +116,7 @@ static PyObject *py_logs_set_verbosity(PyObject *self, PyObject *args) " all kinds of logs get printed." \ ) - if (!PyArg_ParseTuple(args, "k", &verbosity)) + if (!PyArg_ParseTuple(args, "O&", convert_to_log_message_type, &verbosity)) return NULL; set_log_verbosity(verbosity); @@ -145,7 +145,7 @@ static PyObject *py_logs_set_verbosity(PyObject *self, PyObject *args) static PyObject *py_logs_log_message(PyObject *self, PyObject *args) { PyObject *result; /* Bilan à retourner */ - unsigned long type; /* Espèce du message */ + LogMessageType type; /* Espèce du message */ const char *msg; /* Contenu du message */ #define LOGS_LOG_MESSAGE_METHOD PYTHON_METHOD_DEF \ @@ -159,29 +159,13 @@ static PyObject *py_logs_log_message(PyObject *self, PyObject *args) " value." \ ) - if (!PyArg_ParseTuple(args, "ks", &type, &msg)) + if (!PyArg_ParseTuple(args, "O&s", convert_to_log_message_type, &type, &msg)) return NULL; - switch (type) - { - case LMT_INFO: - case LMT_PROCESS: - case LMT_WARNING: - case LMT_BAD_BINARY: - case LMT_ERROR: - case LMT_EXT_ERROR: - log_plugin_simple_message(type, msg); - result = Py_None; - Py_INCREF(result); - break; - - default: - PyErr_SetString(PyExc_ValueError, - _("Invalid type of message")); - result = NULL; - break; - - } + log_plugin_simple_message(type, msg); + + result = Py_None; + Py_INCREF(result); return result; |