File: varinfo.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 (202 lines) | stat: -rw-r--r-- 5,384 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "varinfo.h"
#include "wreport/var.h"
#include "common.h"
#include "utils/type.h"
#include "utils/methods.h"
#include "utils/values.h"

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

extern "C" {

PyTypeObject* wrpy_Varinfo_Type = nullptr;

}

namespace {

struct type : public Getter<type, wrpy_Varinfo>
{
    constexpr static const char* name = "type";
    constexpr static const char* doc = "return a string describing the type of the variable (string, binary, integer, decimal)";
    constexpr static void* closure = nullptr;

    static PyObject* get(Impl* self, void* closure)
    {
        try {
            return to_python(vartype_format(self->info->type));
        } WREPORT_CATCH_RETURN_PYO;
    }
};

struct code : public Getter<code, wrpy_Varinfo>
{
    constexpr static const char* name = "code";
    constexpr static const char* doc = "variable code";
    constexpr static void* closure = nullptr;

    static PyObject* get(Impl* self, void* closure)
    {
        try {
            return wrpy_varcode_format(self->info->code);
        } WREPORT_CATCH_RETURN_PYO;
    }
};

struct len : public Getter<len, wrpy_Varinfo>
{
    constexpr static const char* name = "len";
    constexpr static const char* doc = "number of significant digits";
    constexpr static void* closure = nullptr;

    static PyObject* get(Impl* self, void* closure)
    {
        try {
            return to_python(self->info->len);
        } WREPORT_CATCH_RETURN_PYO;
    }
};

struct unit : public Getter<unit, wrpy_Varinfo>
{
    constexpr static const char* name = "unit";
    constexpr static const char* doc = "measurement unit";
    constexpr static void* closure = nullptr;

    static PyObject* get(Impl* self, void* closure)
    {
        try {
            return to_python(self->info->unit);
        } WREPORT_CATCH_RETURN_PYO;
    }
};

struct desc : public Getter<desc, wrpy_Varinfo>
{
    constexpr static const char* name = "desc";
    constexpr static const char* doc = "description";
    constexpr static void* closure = nullptr;

    static PyObject* get(Impl* self, void* closure)
    {
        try {
            return to_python(self->info->desc);
        } WREPORT_CATCH_RETURN_PYO;
    }
};

struct scale : public Getter<scale, wrpy_Varinfo>
{
    constexpr static const char* name = "scale";
    constexpr static const char* doc = "scale of the value as a power of 10";
    constexpr static void* closure = nullptr;

    static PyObject* get(Impl* self, void* closure)
    {
        try {
            return to_python(self->info->scale);
        } WREPORT_CATCH_RETURN_PYO;
    }
};

struct bit_ref : public Getter<bit_ref, wrpy_Varinfo>
{
    constexpr static const char* name = "bit_ref";
    constexpr static const char* doc = "reference value added after scaling, for BUFR decoding";
    constexpr static void* closure = nullptr;

    static PyObject* get(Impl* self, void* closure)
    {
        try {
            return to_python(self->info->bit_ref);
        } WREPORT_CATCH_RETURN_PYO;
    }
};

struct bit_len : public Getter<bit_len, wrpy_Varinfo>
{
    constexpr static const char* name = "bit_len";
    constexpr static const char* doc = "number of bits used to encode the value in BUFR";
    constexpr static void* closure = nullptr;

    static PyObject* get(Impl* self, void* closure)
    {
        try {
            return to_python(self->info->bit_len);
        } WREPORT_CATCH_RETURN_PYO;
    }
};


struct VarinfoDef : public Type<VarinfoDef, wrpy_Varinfo>
{
    constexpr static const char* name = "Varinfo";
    constexpr static const char* qual_name = "wreport.Varinfo";
    constexpr static const char* doc = R"(
Varinfo object holds all possible information about a variable, such as its
measurement unit, description and number of significant digits.

Varinfo objects cannot be instantiated directly, and are created by
querying :class:`Vartable` objects.
)";
    GetSetters<type, code, len, unit, desc, scale, bit_ref, bit_len> getsetters;
    Methods<> methods;

    static void _dealloc(Impl* self)
    {
        Py_TYPE(self)->tp_free(self);
    }

    static PyObject* _str(Impl* self)
    {
        try {
            return wrpy_varcode_format(self->info->code);
        } WREPORT_CATCH_RETURN_PYO;
    }

    static PyObject* _repr(Impl* self)
    {
        std::string res = "Varinfo('";
        res += varcode_format(self->info->code);
        res += "')";
        return PyUnicode_FromString(res.c_str());
    }

    static int _init(Impl* self, PyObject* args, PyObject* kw)
    {
        // People should not invoke Varinfo() as a constructor, but if they do,
        // this is better than a segfault later on
        PyErr_SetString(PyExc_NotImplementedError, "Varinfo objects cannot be constructed explicitly");
        return -1;
    }
};

VarinfoDef* varinfo_def = nullptr;

}

namespace wreport {
namespace python {

PyObject* varinfo_create(Varinfo v)
{
    wrpy_Varinfo* result = PyObject_New(wrpy_Varinfo, wrpy_Varinfo_Type);
    if (!result) return nullptr;
    result->info = v;
    return (PyObject*)result;
}

void register_varinfo(PyObject* m, wrpy_c_api& c_api)
{
    varinfo_def = new VarinfoDef;
    varinfo_def->define(wrpy_Varinfo_Type, m);

    // Initialize the C api struct
    c_api.varinfo_create = varinfo_create;
    c_api.varinfo_type = wrpy_Varinfo_Type;
}

}
}