From df8dba24f7b01a507acd03659c8d4d4868dea143 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Fri, 28 Dec 2018 18:25:45 +0100
Subject: Refactored the core module of the Python API.

---
 plugins/pychrysalide/core/demanglers.c |  70 ++++-------------------
 plugins/pychrysalide/core/demanglers.h |   7 +--
 plugins/pychrysalide/core/logs.c       |   4 +-
 plugins/pychrysalide/core/module.c     |   4 +-
 plugins/pychrysalide/core/params.c     | 100 +++++++++------------------------
 plugins/pychrysalide/core/params.h     |   7 +--
 6 files changed, 45 insertions(+), 147 deletions(-)

diff --git a/plugins/pychrysalide/core/demanglers.c b/plugins/pychrysalide/core/demanglers.c
index 2c98ee1..216b7e1 100644
--- a/plugins/pychrysalide/core/demanglers.c
+++ b/plugins/pychrysalide/core/demanglers.c
@@ -89,83 +89,33 @@ static PyObject *py_demanglers_get_for_type(PyObject *self, PyObject *args)
 *                                                                             *
 *  Paramètres  : -                                                            *
 *                                                                             *
-*  Description : Fournit un accès à une définition de type à diffuser.        *
+*  Description : Définit une extension du module 'core' à compléter.          *
 *                                                                             *
-*  Retour      : Définition d'objet pour Python.                              *
+*  Retour      : Bilan de l'opération.                                        *
 *                                                                             *
 *  Remarques   : -                                                            *
 *                                                                             *
 ******************************************************************************/
 
-PyTypeObject *get_python_demanglers_type(void)
+bool populate_core_module_with_demanglers(void)
 {
+    bool result;                            /* Bilan à retourner           */
+    PyObject *module;                       /* Module à recompléter        */
+
     static PyMethodDef py_demanglers_methods[] = {
 
         { "get_for_type", py_demanglers_get_for_type,
-          METH_VARARGS | METH_STATIC,
+          METH_VARARGS,
           "get_for_type(key, /)\n--\n\nCreate a new demangler for a given type of encoding."
         },
         { NULL }
 
     };
 
-    static PyTypeObject py_demanglers_type = {
-
-        PyVarObject_HEAD_INIT(NULL, 0)
-
-        .tp_name = "pychrysalide.core.demanglers",
-        .tp_basicsize = sizeof(PyObject),
-
-        .tp_flags = Py_TPFLAGS_DEFAULT,
-
-        .tp_doc = "Access to the code demanglers",
-
-        .tp_methods = py_demanglers_methods
-
-    };
-
-    return &py_demanglers_type;
-
-}
-
-
-/******************************************************************************
-*                                                                             *
-*  Paramètres  : module = module dont la définition est à compléter.          *
-*                                                                             *
-*  Description : Prend en charge l'objet 'pychrysalide.core.demanglers'.      *
-*                                                                             *
-*  Retour      : Bilan de l'opération.                                        *
-*                                                                             *
-*  Remarques   : -                                                            *
-*                                                                             *
-******************************************************************************/
-
-bool ensure_python_demanglers_is_registered(void)
-{
-    PyTypeObject *type;                     /* Type Python de 'demanglers' */
-    PyObject *module;                       /* Module à recompléter        */
-    int ret;                                /* Bilan d'un appel            */
-
-    type = get_python_demanglers_type();
-
-    if (!PyType_HasFeature(type, Py_TPFLAGS_READY))
-    {
-        type->tp_new = PyType_GenericNew;
-
-        if (PyType_Ready(type) != 0)
-            return false;
-
-        module = get_access_to_python_module("pychrysalide.core");
+    module = get_access_to_python_module("pychrysalide.core");
 
-        Py_INCREF(type);
-        ret = PyModule_AddObject(module, "demanglers", (PyObject *)type);
+    result = register_python_module_methods(module, py_demanglers_methods);
 
-        if (ret != 0)
-            return false;
-
-    }
-
-    return true;
+    return result;
 
 }
diff --git a/plugins/pychrysalide/core/demanglers.h b/plugins/pychrysalide/core/demanglers.h
index 5fc1cf0..75372ad 100644
--- a/plugins/pychrysalide/core/demanglers.h
+++ b/plugins/pychrysalide/core/demanglers.h
@@ -31,11 +31,8 @@
 
 
 
-/* Fournit un accès à une définition de type à diffuser. */
-PyTypeObject *get_python_demanglers_type(void);
-
-/* Prend en charge l'objet 'pychrysalide.core.demanglers'. */
-bool ensure_python_demanglers_is_registered(void);
+/* Définit une extension du module 'core' à compléter. */
+bool populate_core_module_with_demanglers(void);
 
 
 
diff --git a/plugins/pychrysalide/core/logs.c b/plugins/pychrysalide/core/logs.c
index f2158f9..be1f57a 100644
--- a/plugins/pychrysalide/core/logs.c
+++ b/plugins/pychrysalide/core/logs.c
@@ -206,7 +206,7 @@ bool populate_core_module_with_logs(void)
     PyObject *module;                       /* Module à recompléter        */
     PyObject *dict;                         /* Dictionnaire dudit module   */
 
-    static PyMethodDef py_queue_methods[] = {
+    static PyMethodDef py_logs_methods[] = {
 
         {
             "get_verbosity", py_logs_get_verbosity,
@@ -229,7 +229,7 @@ bool populate_core_module_with_logs(void)
 
     module = get_access_to_python_module("pychrysalide.core");
 
-    result = register_python_module_methods(module, py_queue_methods);
+    result = register_python_module_methods(module, py_logs_methods);
 
     if (result)
     {
diff --git a/plugins/pychrysalide/core/module.c b/plugins/pychrysalide/core/module.c
index c786ad6..3d117a5 100644
--- a/plugins/pychrysalide/core/module.c
+++ b/plugins/pychrysalide/core/module.c
@@ -92,10 +92,10 @@ bool populate_core_module(void)
 
     result = true;
 
-    if (result) result = ensure_python_demanglers_is_registered();
+    if (result) result = populate_core_module_with_demanglers();
     if (result) result = populate_core_module_with_global();
     if (result) result = populate_core_module_with_logs();
-    if (result) result = ensure_python_params_is_registered();
+    if (result) result = populate_core_module_with_params();
     if (result) result = populate_core_module_with_queue();
 
     assert(result);
diff --git a/plugins/pychrysalide/core/params.c b/plugins/pychrysalide/core/params.c
index e31c129..b9d8741 100644
--- a/plugins/pychrysalide/core/params.c
+++ b/plugins/pychrysalide/core/params.c
@@ -40,7 +40,7 @@
 static PyObject *py_params_get_main_configuration(PyObject *, PyObject *);
 
 /* Définit les constantes pour les paramètres. */
-static bool py_params_define_constants(PyTypeObject *);
+static bool py_params_define_constants(PyObject *);
 
 
 
@@ -74,51 +74,7 @@ static PyObject *py_params_get_main_configuration(PyObject *self, PyObject *args
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : -                                                            *
-*                                                                             *
-*  Description : Fournit un accès à une définition de type à diffuser.        *
-*                                                                             *
-*  Retour      : Définition d'objet pour Python.                              *
-*                                                                             *
-*  Remarques   : -                                                            *
-*                                                                             *
-******************************************************************************/
-
-PyTypeObject *get_python_params_type(void)
-{
-    static PyMethodDef py_params_methods[] = {
-
-        { "get_main_configuration", py_params_get_main_configuration,
-          METH_NOARGS | METH_STATIC,
-          "get_main_configuration(, /)\n--\n\nGive access to the main configuration of Chrysalide."
-        },
-        { NULL }
-
-    };
-
-    static PyTypeObject py_params_type = {
-
-        PyVarObject_HEAD_INIT(NULL, 0)
-
-        .tp_name = "pychrysalide.core.params",
-        .tp_basicsize = sizeof(PyObject),
-
-        .tp_flags = Py_TPFLAGS_DEFAULT,
-
-        .tp_doc = "Python object for parameters",
-
-        .tp_methods = py_params_methods
-
-    };
-
-    return &py_params_type;
-
-}
-
-
-/******************************************************************************
-*                                                                             *
-*  Paramètres  : obj_type = type dont le dictionnaire est à compléter.        *
+*  Paramètres  : dict = dictionnaire de module à compléter.                   *
 *                                                                             *
 *  Description : Définit les constantes pour les paramètres.                  *
 *                                                                             *
@@ -128,17 +84,17 @@ PyTypeObject *get_python_params_type(void)
 *                                                                             *
 ******************************************************************************/
 
-static bool py_params_define_constants(PyTypeObject *obj_type)
+static bool py_params_define_constants(PyObject *dict)
 {
     bool result;                            /* Bilan à retourner           */
 
     result = true;
 
-    result &= PyDict_AddStringMacro(obj_type, MPK_LAST_PROJECT);
-    result &= PyDict_AddStringMacro(obj_type, MPK_ELLIPSIS_HEADER);
-    result &= PyDict_AddStringMacro(obj_type, MPK_ELLIPSIS_TAB);
-    result &= PyDict_AddStringMacro(obj_type, MPK_KEYBINDINGS_EDIT);
-    result &= PyDict_AddStringMacro(obj_type, MPK_AUTO_SAVE);
+    result &= PyModDict_AddStringMacro(dict, MPK_LAST_PROJECT);
+    result &= PyModDict_AddStringMacro(dict, MPK_ELLIPSIS_HEADER);
+    result &= PyModDict_AddStringMacro(dict, MPK_ELLIPSIS_TAB);
+    result &= PyModDict_AddStringMacro(dict, MPK_KEYBINDINGS_EDIT);
+    result &= PyModDict_AddStringMacro(dict, MPK_AUTO_SAVE);
 
     return result;
 
@@ -147,9 +103,9 @@ static bool py_params_define_constants(PyTypeObject *obj_type)
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : module = module dont la définition est à compléter.          *
+*  Paramètres  : -                                                            *
 *                                                                             *
-*  Description : Prend en charge l'objet 'pychrysalide.core.params'.          *
+*  Description : Définit une extension du module 'core' à compléter.          *
 *                                                                             *
 *  Retour      : Bilan de l'opération.                                        *
 *                                                                             *
@@ -157,34 +113,32 @@ static bool py_params_define_constants(PyTypeObject *obj_type)
 *                                                                             *
 ******************************************************************************/
 
-bool ensure_python_params_is_registered(void)
+bool populate_core_module_with_params(void)
 {
-    PyTypeObject *type;                     /* Type Python pour 'params'   */
+    bool result;                            /* Bilan à retourner           */
     PyObject *module;                       /* Module à recompléter        */
-    int ret;                                /* Bilan d'un appel            */
+    PyObject *dict;                         /* Dictionnaire dudit module   */
 
-    type = get_python_params_type();
-
-    if (!PyType_HasFeature(type, Py_TPFLAGS_READY))
-    {
-        type->tp_new = PyType_GenericNew;
-
-        if (PyType_Ready(type) != 0)
-            return false;
+    static PyMethodDef py_params_methods[] = {
 
-        if (!py_params_define_constants(type))
-            return false;
+        { "get_main_configuration", py_params_get_main_configuration,
+          METH_NOARGS,
+          "get_main_configuration(, /)\n--\n\nGive access to the main configuration of Chrysalide."
+        },
+        { NULL }
 
-        module = get_access_to_python_module("pychrysalide.core");
+    };
 
-        Py_INCREF(type);
-        ret = PyModule_AddObject(module, "params", (PyObject *)type);
+    module = get_access_to_python_module("pychrysalide.core");
 
-        if (ret != 0)
-            return false;
+    result = register_python_module_methods(module, py_params_methods);
 
+    if (result)
+    {
+        dict = PyModule_GetDict(module);
+        result = py_params_define_constants(dict);
     }
 
-    return true;
+    return result;
 
 }
diff --git a/plugins/pychrysalide/core/params.h b/plugins/pychrysalide/core/params.h
index b3bcece..1c397af 100644
--- a/plugins/pychrysalide/core/params.h
+++ b/plugins/pychrysalide/core/params.h
@@ -31,11 +31,8 @@
 
 
 
-/* Fournit un accès à une définition de type à diffuser. */
-PyTypeObject *get_python_params_type(void);
-
-/* Prend en charge l'objet 'pychrysalide.core.params'. */
-bool ensure_python_params_is_registered(void);
+/* Définit une extension du module 'core' à compléter. */
+bool populate_core_module_with_params(void);
 
 
 
-- 
cgit v0.11.2-87-g4458