| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
#ifdef HAVE_CONFIG_H |
|---|
| 20 |
#include "config.h" |
|---|
| 21 |
#endif |
|---|
| 22 |
|
|---|
| 23 |
#define NO_IMPORT_PYGOBJECT |
|---|
| 24 |
#include "py_midgard.h" |
|---|
| 25 |
|
|---|
| 26 |
PyTypeObject G_GNUC_INTERNAL Pymidgard_connection_Type; |
|---|
| 27 |
|
|---|
| 28 |
#define CONNECTION_DEBUG(__name) \ |
|---|
| 29 |
CHECK_MGD; \ |
|---|
| 30 |
CLASS_METHOD_DEBUG(Pymidgard_connection_Type.tp_name, __name); |
|---|
| 31 |
|
|---|
| 32 |
static MidgardConnection *_global_singleton = NULL; |
|---|
| 33 |
|
|---|
| 34 |
void _py_midgard_connection_singleton_set(MidgardConnection *mgd) |
|---|
| 35 |
{ |
|---|
| 36 |
_global_singleton = mgd; |
|---|
| 37 |
|
|---|
| 38 |
midgard_connection_set_loghandler(mgd, get_global_log_handler()); |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
MidgardConnection *_py_midgard_connection_singleton_get(void) |
|---|
| 42 |
{ |
|---|
| 43 |
return _global_singleton; |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
static int |
|---|
| 51 |
__connection_singleton_init(PyGObject *self, PyObject *args, PyObject *kwargs) |
|---|
| 52 |
{ |
|---|
| 53 |
CONNECTION_DEBUG("__init__"); |
|---|
| 54 |
if(_py_midgard_connection_singleton_get()){ |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
PyErr_SetString(PyExc_TypeError, |
|---|
| 58 |
"MidgardConnection singleton already initialized"); |
|---|
| 59 |
return -1; |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
if (pygobject_constructv(self, 0, NULL)) |
|---|
| 63 |
PyErr_SetString(PyExc_RuntimeError, "could not create object"); |
|---|
| 64 |
|
|---|
| 65 |
if(self->obj != NULL) { |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
_py_midgard_connection_singleton_set(MIDGARD_CONNECTION(self->obj)); |
|---|
| 69 |
|
|---|
| 70 |
PyObject *module = PyImport_AddModule("_midgard"); |
|---|
| 71 |
PyObject *d = PyModule_GetDict(module); |
|---|
| 72 |
PyDict_DelItemString(d, "_connection"); |
|---|
| 73 |
PyDict_SetItemString(d, "_connection", (PyObject *)self); |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
return (self->obj) ? 0 : -1; |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
static PyObject * |
|---|
| 80 |
pymidgard_connection_open(PyGObject *self, PyObject *args) |
|---|
| 81 |
{ |
|---|
| 82 |
CONNECTION_DEBUG("open"); |
|---|
| 83 |
gchar *name; |
|---|
| 84 |
|
|---|
| 85 |
if(!PyArg_ParseTuple(args, "s", &name)) |
|---|
| 86 |
return NULL; |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
GError *error = NULL; |
|---|
| 90 |
|
|---|
| 91 |
gboolean connected = |
|---|
| 92 |
midgard_connection_open( |
|---|
| 93 |
MIDGARD_CONNECTION(self->obj), |
|---|
| 94 |
(const gchar *)name, &error); |
|---|
| 95 |
if(connected) |
|---|
| 96 |
Py_RETURN_TRUE; |
|---|
| 97 |
|
|---|
| 98 |
if(error) { |
|---|
| 99 |
|
|---|
| 100 |
PyErr_SetString(PyExc_SystemError, error->message); |
|---|
| 101 |
g_error_free(error); |
|---|
| 102 |
|
|---|
| 103 |
return NULL; |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
PyErr_SetString(PyExc_SystemError, "Unhandled midgard exception. FIXME!"); |
|---|
| 107 |
return NULL; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
static PyObject * |
|---|
| 111 |
pymidgard_connection_open_config(PyGObject *self, PyObject *args) |
|---|
| 112 |
{ |
|---|
| 113 |
CONNECTION_DEBUG("open_config"); |
|---|
| 114 |
PyObject *config; |
|---|
| 115 |
|
|---|
| 116 |
if(!PyArg_ParseTuple(args, "O", &config)) |
|---|
| 117 |
return NULL; |
|---|
| 118 |
|
|---|
| 119 |
if(config == NULL) |
|---|
| 120 |
g_warning("config is NULL"); |
|---|
| 121 |
|
|---|
| 122 |
PyGObject *_config = (PyGObject *)config; |
|---|
| 123 |
|
|---|
| 124 |
gboolean connected = |
|---|
| 125 |
midgard_connection_open_config( |
|---|
| 126 |
MIDGARD_CONNECTION(self->obj), |
|---|
| 127 |
MIDGARD_CONFIG(_config->obj)); |
|---|
| 128 |
if(connected) |
|---|
| 129 |
Py_RETURN_TRUE; |
|---|
| 130 |
|
|---|
| 131 |
Py_RETURN_FALSE; |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
static PyObject * |
|---|
| 135 |
pymidgard_connection_set_sitegroup(PyGObject *self, PyObject *args) |
|---|
| 136 |
{ |
|---|
| 137 |
CONNECTION_DEBUG("set_sitegroup"); |
|---|
| 138 |
gchar *name; |
|---|
| 139 |
|
|---|
| 140 |
if(!PyArg_ParseTuple(args, "s", &name)) |
|---|
| 141 |
return NULL; |
|---|
| 142 |
|
|---|
| 143 |
if(midgard_connection_set_sitegroup( |
|---|
| 144 |
MIDGARD_CONNECTION(self->obj), (const gchar *)name)) |
|---|
| 145 |
Py_RETURN_TRUE; |
|---|
| 146 |
|
|---|
| 147 |
Py_RETURN_FALSE; |
|---|
| 148 |
} |
|---|
| 149 |
|
|---|
| 150 |
static PyObject * |
|---|
| 151 |
pymidgard_connection_get_sitegroup(PyGObject *self, PyObject *args) |
|---|
| 152 |
{ |
|---|
| 153 |
CONNECTION_DEBUG("get_sitegroup"); |
|---|
| 154 |
if(!PyArg_ParseTuple(args, "")) |
|---|
| 155 |
return NULL; |
|---|
| 156 |
|
|---|
| 157 |
const gchar *name; |
|---|
| 158 |
name = midgard_connection_get_sitegroup(MIDGARD_CONNECTION(self->obj)); |
|---|
| 159 |
|
|---|
| 160 |
return Py_BuildValue("s", name); |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
static PyObject * |
|---|
| 164 |
pymidgard_connection_set_lang(PyGObject *self, PyObject *args) |
|---|
| 165 |
{ |
|---|
| 166 |
CONNECTION_DEBUG("set_lang"); |
|---|
| 167 |
gchar *lang; |
|---|
| 168 |
|
|---|
| 169 |
if(!PyArg_ParseTuple(args, "s", &lang)) |
|---|
| 170 |
return NULL; |
|---|
| 171 |
|
|---|
| 172 |
if(midgard_connection_set_lang( |
|---|
| 173 |
MIDGARD_CONNECTION(self->obj), (const gchar *)lang)) |
|---|
| 174 |
Py_RETURN_TRUE; |
|---|
| 175 |
|
|---|
| 176 |
Py_RETURN_FALSE; |
|---|
| 177 |
} |
|---|
| 178 |
|
|---|
| 179 |
static PyObject * |
|---|
| 180 |
pymidgard_connection_get_lang(PyGObject *self, PyObject *args) |
|---|
| 181 |
{ |
|---|
| 182 |
CONNECTION_DEBUG("get_lang"); |
|---|
| 183 |
if(!PyArg_ParseTuple(args, "")) |
|---|
| 184 |
return NULL; |
|---|
| 185 |
|
|---|
| 186 |
const gchar *lang; |
|---|
| 187 |
lang = midgard_connection_get_lang(MIDGARD_CONNECTION(self->obj)); |
|---|
| 188 |
|
|---|
| 189 |
return Py_BuildValue("s", lang); |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
static PyObject * |
|---|
| 193 |
pymidgard_connection_set_default_lang(PyGObject *self, PyObject *args) |
|---|
| 194 |
{ |
|---|
| 195 |
CONNECTION_DEBUG("set_default_lang"); |
|---|
| 196 |
gchar *lang; |
|---|
| 197 |
|
|---|
| 198 |
if(!PyArg_ParseTuple(args, "s", &lang)) |
|---|
| 199 |
return NULL; |
|---|
| 200 |
|
|---|
| 201 |
if(midgard_connection_set_default_lang( |
|---|
| 202 |
MIDGARD_CONNECTION(self->obj), (const gchar *)lang)) |
|---|
| 203 |
Py_RETURN_TRUE; |
|---|
| 204 |
|
|---|
| 205 |
Py_RETURN_FALSE; |
|---|
| 206 |
} |
|---|
| 207 |
|
|---|
| 208 |
static PyObject * |
|---|
| 209 |
pymidgard_connection_get_default_lang(PyGObject *self, PyObject *args) |
|---|
| 210 |
{ |
|---|
| 211 |
CONNECTION_DEBUG("get_default_lang"); |
|---|
| 212 |
if(!PyArg_ParseTuple(args, "")) |
|---|
| 213 |
return NULL; |
|---|
| 214 |
|
|---|
| 215 |
const gchar *lang; |
|---|
| 216 |
lang = midgard_connection_get_default_lang(MIDGARD_CONNECTION(self->obj)); |
|---|
| 217 |
|
|---|
| 218 |
return Py_BuildValue("s", lang); |
|---|
| 219 |
} |
|---|
| 220 |
|
|---|
| 221 |
static PyObject * |
|---|
| 222 |
pymidgard_connection_set_loglevel(PyGObject *self, PyObject *args) |
|---|
| 223 |
{ |
|---|
| 224 |
CONNECTION_DEBUG("set_loglevel"); |
|---|
| 225 |
const gchar *level; |
|---|
| 226 |
|
|---|
| 227 |
if(!PyArg_ParseTuple(args, "s", &level)) |
|---|
| 228 |
return NULL; |
|---|
| 229 |
|
|---|
| 230 |
midgard_connection_set_loglevel(MIDGARD_CONNECTION(self->obj), |
|---|
| 231 |
(const gchar *)level, NULL); |
|---|
| 232 |
|
|---|
| 233 |
Py_RETURN_NONE; |
|---|
| 234 |
} |
|---|
| 235 |
|
|---|
| 236 |
static PyObject * |
|---|
| 237 |
pymidgard_connection_get_error(PyGObject *self, PyObject *args) |
|---|
| 238 |
{ |
|---|
| 239 |
CONNECTION_DEBUG("get_error"); |
|---|
| 240 |
|
|---|
| 241 |
if(!PyArg_ParseTuple(args, "")) |
|---|
| 242 |
return NULL; |
|---|
| 243 |
|
|---|
| 244 |
gint errn = |
|---|
| 245 |
midgard_connection_get_error(MIDGARD_CONNECTION(self->obj)); |
|---|
| 246 |
|
|---|
| 247 |
return Py_BuildValue("i", (int)errn); |
|---|
| 248 |
} |
|---|
| 249 |
|
|---|
| 250 |
static PyObject * |
|---|
| 251 |
pymidgard_connection_get_error_string(PyGObject *self, PyObject *args) |
|---|
| 252 |
{ |
|---|
| 253 |
CONNECTION_DEBUG("get_error_string"); |
|---|
| 254 |
|
|---|
| 255 |
if(!PyArg_ParseTuple(args, "")) |
|---|
| 256 |
return NULL; |
|---|
| 257 |
|
|---|
| 258 |
const gchar *error_string = |
|---|
| 259 |
midgard_connection_get_error_string(MIDGARD_CONNECTION(self->obj)); |
|---|
| 260 |
|
|---|
| 261 |
return Py_BuildValue("s", error_string); |
|---|
| 262 |
} |
|---|
| 263 |
|
|---|
| 264 |
static PyObject * |
|---|
| 265 |
pymidgard_connection_get_user(PyGObject *self, PyObject *args) |
|---|
| 266 |
{ |
|---|
| 267 |
CONNECTION_DEBUG("get_user"); |
|---|
| 268 |
|
|---|
| 269 |
if(!PyArg_ParseTuple(args, "")) |
|---|
| 270 |
return NULL; |
|---|
| 271 |
|
|---|
| 272 |
MidgardUser *user = |
|---|
| 273 |
midgard_connection_get_user(MIDGARD_CONNECTION(self->obj)); |
|---|
| 274 |
|
|---|
| 275 |
if(user == NULL) |
|---|
| 276 |
Py_RETURN_NONE; |
|---|
| 277 |
|
|---|
| 278 |
GValue gval = {0, }; |
|---|
| 279 |
g_value_init(&gval, G_TYPE_OBJECT); |
|---|
| 280 |
g_value_set_object(&gval, G_OBJECT(user)); |
|---|
| 281 |
PyObject *pval = pyg_value_as_pyobject((const GValue *)&gval, FALSE); |
|---|
| 282 |
|
|---|
| 283 |
return pval; |
|---|
| 284 |
} |
|---|
| 285 |
|
|---|
| 286 |
static PyMethodDef pymidgard_connection_methods[] = { |
|---|
| 287 |
{ "open", (PyCFunction)pymidgard_connection_open, METH_VARARGS }, |
|---|
| 288 |
{ "open_config", (PyCFunction)pymidgard_connection_open_config, METH_VARARGS }, |
|---|
| 289 |
{ "set_sitegroup", (PyCFunction)pymidgard_connection_set_sitegroup, METH_VARARGS }, |
|---|
| 290 |
{ "get_sitegroup", (PyCFunction)pymidgard_connection_get_sitegroup, METH_VARARGS }, |
|---|
| 291 |
{ "set_lang", (PyCFunction)pymidgard_connection_set_lang, METH_VARARGS }, |
|---|
| 292 |
{ "get_lang", (PyCFunction)pymidgard_connection_get_lang, METH_VARARGS }, |
|---|
| 293 |
{ "set_default_lang", (PyCFunction)pymidgard_connection_set_default_lang, METH_VARARGS }, |
|---|
| 294 |
{ "get_default_lang", (PyCFunction)pymidgard_connection_get_default_lang, METH_VARARGS }, |
|---|
| 295 |
{ "set_loglevel", (PyCFunction)pymidgard_connection_set_loglevel, METH_VARARGS }, |
|---|
| 296 |
{ "get_error", (PyCFunction)pymidgard_connection_get_error, METH_VARARGS }, |
|---|
| 297 |
{ "get_error_string", (PyCFunction)pymidgard_connection_get_error_string, METH_VARARGS }, |
|---|
| 298 |
{ "get_user", (PyCFunction)pymidgard_connection_get_user, METH_VARARGS }, |
|---|
| 299 |
{ NULL, NULL, 0 } |
|---|
| 300 |
}; |
|---|
| 301 |
|
|---|
| 302 |
PyTypeObject G_GNUC_INTERNAL Pymidgard_connection_Type = { |
|---|
| 303 |
PyObject_HEAD_INIT(NULL) |
|---|
| 304 |
0, |
|---|
| 305 |
"connection", |
|---|
| 306 |
sizeof(PyGObject), |
|---|
| 307 |
0, |
|---|
| 308 |
|
|---|
| 309 |
(destructor)0, |
|---|
| 310 |
(printfunc)0, |
|---|
| 311 |
(getattrfunc)0, |
|---|
| 312 |
(setattrfunc)0, |
|---|
| 313 |
(cmpfunc)0, |
|---|
| 314 |
(reprfunc)0, |
|---|
| 315 |
(PyNumberMethods*)0, |
|---|
| 316 |
(PySequenceMethods*)0, |
|---|
| 317 |
(PyMappingMethods*)0, |
|---|
| 318 |
(hashfunc)0, |
|---|
| 319 |
(ternaryfunc)0, |
|---|
| 320 |
(reprfunc)0, |
|---|
| 321 |
_py_midgard_get_object_attribute, |
|---|
| 322 |
_py_midgard_set_object_attribute, |
|---|
| 323 |
(PyBufferProcs*)0, |
|---|
| 324 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
|---|
| 325 |
"MidgardConnection singleton class", |
|---|
| 326 |
(traverseproc)0, |
|---|
| 327 |
(inquiry)0, |
|---|
| 328 |
(richcmpfunc)0, |
|---|
| 329 |
offsetof(PyGObject, weakreflist), |
|---|
| 330 |
(getiterfunc)0, |
|---|
| 331 |
(iternextfunc)0, |
|---|
| 332 |
pymidgard_connection_methods, |
|---|
| 333 |
(struct PyMemberDef*)0, |
|---|
| 334 |
(struct PyGetSetDef*)0, |
|---|
| 335 |
NULL, |
|---|
| 336 |
NULL, |
|---|
| 337 |
(descrgetfunc)0, |
|---|
| 338 |
(descrsetfunc)0, |
|---|
| 339 |
offsetof(PyGObject, inst_dict), |
|---|
| 340 |
(initproc)__connection_singleton_init, |
|---|
| 341 |
(allocfunc)0, |
|---|
| 342 |
(newfunc)0, |
|---|
| 343 |
(freefunc)0, |
|---|
| 344 |
(inquiry)0 |
|---|
| 345 |
}; |
|---|
| 346 |
|
|---|
| 347 |
void py_midgard_connection_register_class( |
|---|
| 348 |
PyObject *d, gpointer pygobject_type) |
|---|
| 349 |
{ |
|---|
| 350 |
pygobject_register_class(d, |
|---|
| 351 |
"connection", |
|---|
| 352 |
MIDGARD_TYPE_CONNECTION, |
|---|
| 353 |
&Pymidgard_connection_Type, |
|---|
| 354 |
Py_BuildValue("(O)", pygobject_type)); |
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 |
} |
|---|