File: wreport.cc

package info (click to toggle)
wreport 3.36-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,572 kB
  • sloc: cpp: 18,927; python: 583; sh: 78; makefile: 13
file content (85 lines) | stat: -rw-r--r-- 2,479 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "config.h"
#include "common.h"
#include "vartable.h"
#include "varinfo.h"
#include "var.h"
#include "utils/methods.h"
#include "utils/values.h"
#include "wreport/conv.h"

using namespace std;
using namespace wreport;
using namespace wreport::python;

namespace {

struct convert_units : public MethKwargs<convert_units, PyObject>
{
    constexpr static const char* name = "convert_units";
    constexpr static const char* signature = "from_unit: str, to_unit: str, value: float";
    constexpr static const char* returns = "float";
    constexpr static const char* summary = "convert a value from a unit to another, as understood by wreport";
    constexpr static const char* doc = nullptr;

    static PyObject* run(Impl* self, PyObject* args, PyObject* kw)
    {
        static const char* kwlist[] = { "from_unit", "to_unit", "value", nullptr };
        const char* from_unit = nullptr;
        const char* to_unit = nullptr;
        double value;
        if (!PyArg_ParseTupleAndKeywords(args, kw, "ssd", const_cast<char**>(kwlist),
                    &from_unit, &to_unit, &value))
            return nullptr;

        try {
            return to_python(wreport::convert_units(from_unit, to_unit, value));
        } WREPORT_CATCH_RETURN_PYO;
    }
};

Methods<convert_units> methods;

}

extern "C" {

static PyModuleDef wreport_module = {
    PyModuleDef_HEAD_INIT,
    "_wreport",       /* m_name */
    "wreport Python library",  /* m_doc */
    -1,             /* m_size */
    methods.as_py(), /* m_methods */
    NULL,           /* m_reload */
    NULL,           /* m_traverse */
    NULL,           /* m_clear */
    NULL,           /* m_free */

};

PyMODINIT_FUNC PyInit__wreport(void)
{
    static wrpy_c_api c_api;

    try {
        memset(&c_api, 0, sizeof(wrpy_c_api));
        c_api.version_major = 1;
        c_api.version_minor = 1;

        pyo_unique_ptr m(throw_ifnull(PyModule_Create(&wreport_module)));
        PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION);

        register_varinfo(m, c_api);
        register_vartable(m, c_api);
        register_var(m, c_api);

        // Create a Capsule containing the API struct's address
        pyo_unique_ptr c_api_object(throw_ifnull(PyCapsule_New((void *)&c_api, "_wreport._C_API", nullptr)));
        int res = PyModule_AddObject(m, "_C_API", c_api_object.release());
        if (res)
            return nullptr;

        return m.release();
    } WREPORT_CATCH_RETURN_PYO;
}

}