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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
/*
* Copyright (c) 2023 Attila Szakacs
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation, or (at your option) any later version.
*
* This program 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "python-options.h"
#include "python-types.h"
#include "python-helpers.h"
#include "python-logtemplate.h"
#include "str-utils.h"
#include "string-list.h"
#include "atomic.h"
/* Python Option */
struct _PythonOption
{
GAtomicCounter ref_cnt;
gchar *name;
PyObject *(*create_value_py_object)(const PythonOption *s);
void (*free_fn)(PythonOption *s);
};
static void
python_option_init_instance(PythonOption *s, const gchar *name)
{
g_atomic_counter_set(&s->ref_cnt, 1);
s->name = __normalize_key(name);
}
const gchar *
python_option_get_name(const PythonOption *s)
{
return s->name;
}
PyObject *
python_option_create_value_py_object(const PythonOption *s)
{
g_assert(s->create_value_py_object);
PyObject *value;
PyGILState_STATE gstate = PyGILState_Ensure();
{
value = s->create_value_py_object(s);
if (!value)
{
gchar buf[256];
msg_error("python-options: error converting option to Python object",
evt_tag_str("option", python_option_get_name(s)),
evt_tag_str("exception", _py_format_exception_text(buf, sizeof(buf))));
_py_finish_exception_handling();
}
}
PyGILState_Release(gstate);
return value;
}
PythonOption *
python_option_ref(PythonOption *s)
{
g_assert(!s || g_atomic_counter_get(&s->ref_cnt) > 0);
if (s)
{
g_atomic_counter_inc(&s->ref_cnt);
}
return s;
}
void
python_option_unref(PythonOption *s)
{
g_assert(!s || g_atomic_counter_get(&s->ref_cnt));
if (s && (g_atomic_counter_dec_and_test(&s->ref_cnt)))
{
if (s->free_fn)
s->free_fn(s);
g_free(s->name);
g_free(s);
}
}
/* String */
typedef struct _PythonOptionString
{
PythonOption super;
gchar *value;
} PythonOptionString;
static PyObject *
_string_create_value_py_object(const PythonOption *s)
{
PythonOptionString *self = (PythonOptionString *) s;
return py_string_from_string(self->value, -1);
}
static void
_string_free_fn(PythonOption *s)
{
PythonOptionString *self = (PythonOptionString *) s;
g_free(self->value);
}
PythonOption *
python_option_string_new(const gchar *name, const gchar *value)
{
PythonOptionString *self = g_new0(PythonOptionString, 1);
python_option_init_instance(&self->super, name);
self->super.create_value_py_object = _string_create_value_py_object;
self->super.free_fn = _string_free_fn;
self->value = g_strdup(value);
return &self->super;
}
/* Long */
typedef struct _PythonOptionLong
{
PythonOption super;
gint64 value;
} PythonOptionLong;
static PyObject *
_long_create_value_py_object(const PythonOption *s)
{
PythonOptionLong *self = (PythonOptionLong *) s;
return py_long_from_long(self->value);
}
PythonOption *
python_option_long_new(const gchar *name, gint64 value)
{
PythonOptionLong *self = g_new0(PythonOptionLong, 1);
python_option_init_instance(&self->super, name);
self->super.create_value_py_object = _long_create_value_py_object;
self->value = value;
return &self->super;
}
/* Double */
typedef struct _PythonOptionDouble
{
PythonOption super;
gdouble value;
} PythonOptionDouble;
static PyObject *
_double_create_value_py_object(const PythonOption *s)
{
PythonOptionDouble *self = (PythonOptionDouble *) s;
return py_double_from_double(self->value);
}
PythonOption *
python_option_double_new(const gchar *name, gdouble value)
{
PythonOptionDouble *self = g_new0(PythonOptionDouble, 1);
python_option_init_instance(&self->super, name);
self->super.create_value_py_object = _double_create_value_py_object;
self->value = value;
return &self->super;
}
/* Boolean */
typedef struct _PythonOptionBoolean
{
PythonOption super;
gboolean value;
} PythonOptionBoolean;
static PyObject *
_boolean_create_value_py_object(const PythonOption *s)
{
PythonOptionBoolean *self = (PythonOptionBoolean *) s;
return py_boolean_from_boolean(self->value);
}
PythonOption *
python_option_boolean_new(const gchar *name, gboolean value)
{
PythonOptionBoolean *self = g_new0(PythonOptionBoolean, 1);
python_option_init_instance(&self->super, name);
self->super.create_value_py_object = _boolean_create_value_py_object;
self->value = value;
return &self->super;
}
/* String List */
typedef struct _PythonOptionStringList
{
PythonOption super;
GList *value;
} PythonOptionStringList;
static PyObject *
_string_list_create_value_py_object(const PythonOption *s)
{
PythonOptionStringList *self = (PythonOptionStringList *) s;
return py_string_list_from_string_list(self->value);
}
static void
_string_list_free_fn(PythonOption *s)
{
PythonOptionStringList *self = (PythonOptionStringList *) s;
string_list_free(self->value);
}
PythonOption *
python_option_string_list_new(const gchar *name, const GList *value)
{
PythonOptionStringList *self = g_new0(PythonOptionStringList, 1);
python_option_init_instance(&self->super, name);
self->super.create_value_py_object = _string_list_create_value_py_object;
self->super.free_fn = _string_list_free_fn;
self->value = string_list_clone(value);
return &self->super;
}
/* Template */
typedef struct _PythonOptionTemplate
{
PythonOption super;
gchar *value;
} PythonOptionTemplate;
static PyObject *
_template_create_value_py_object(const PythonOption *s)
{
PythonOptionTemplate *self = (PythonOptionTemplate *) s;
PyObject *template_str = py_string_from_string(self->value, -1);
if (!template_str)
return NULL;
PyObject *args = PyTuple_Pack(1, template_str);
PyObject *py_template = PyObject_Call((PyObject *) &py_log_template_type, args, NULL);
Py_DECREF(template_str);
Py_DECREF(args);
return py_template;
}
static void
_template_free_fn(PythonOption *s)
{
PythonOptionTemplate *self = (PythonOptionTemplate *) s;
g_free(self->value);
}
PythonOption *
python_option_template_new(const gchar *name, const gchar *value)
{
PythonOptionTemplate *self = g_new0(PythonOptionTemplate, 1);
python_option_init_instance(&self->super, name);
self->super.create_value_py_object = _template_create_value_py_object;
self->super.free_fn = _template_free_fn;
self->value = g_strdup(value);
return &self->super;
}
/* Python Options */
struct _PythonOptions
{
GList *options;
};
PythonOptions *
python_options_new(void)
{
PythonOptions *self = g_new0(PythonOptions, 1);
return self;
}
PythonOptions *
python_options_clone(const PythonOptions *self)
{
PythonOptions *cloned = python_options_new();
for (GList *elem = self->options; elem; elem = elem->next)
python_options_add_option(cloned, (PythonOption *) elem->data);
return cloned;
}
void
python_options_add_option(PythonOptions *self, PythonOption *option)
{
self->options = g_list_append(self->options, python_option_ref(option));
}
PyObject *
python_options_create_py_dict(const PythonOptions *self)
{
PyObject *py_dict;
PyGILState_STATE gstate = PyGILState_Ensure();
{
py_dict = PyDict_New();
if (!py_dict)
{
PyGILState_Release(gstate);
return NULL;
}
for (GList *elem = self->options; elem; elem = elem->next)
{
const PythonOption *option = (const PythonOption *) elem->data;
const gchar *name = python_option_get_name(option);
PyObject *value = python_option_create_value_py_object(option);
if (!value)
continue;
if (PyDict_SetItemString(py_dict, name, value) < 0)
msg_error("python-options: Failed to add option to dict", evt_tag_str("name", name));
Py_DECREF(value);
}
}
PyGILState_Release(gstate);
return py_dict;
}
void
python_options_free(PythonOptions *self)
{
if (!self)
return;
g_list_free_full(self->options, (GDestroyNotify) python_option_unref);
g_free(self);
}
|