| 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_config_Type; |
|---|
| 27 |
|
|---|
| 28 |
#define CONFIG_DEBUG(__name) \ |
|---|
| 29 |
CHECK_MGD; \ |
|---|
| 30 |
CLASS_METHOD_DEBUG(Pymidgard_config_Type.tp_name, __name); |
|---|
| 31 |
|
|---|
| 32 |
typedef struct { |
|---|
| 33 |
PyObject_HEAD |
|---|
| 34 |
MidgardConfig *mgdcfg; |
|---|
| 35 |
} PyMidgardConfig; |
|---|
| 36 |
|
|---|
| 37 |
static PyObject * |
|---|
| 38 |
pymidgard_config_read_file(PyGObject *self, PyObject *args) |
|---|
| 39 |
{ |
|---|
| 40 |
CONFIG_DEBUG("read_file"); |
|---|
| 41 |
gchar *name; |
|---|
| 42 |
gint user = 0; |
|---|
| 43 |
|
|---|
| 44 |
if(!PyArg_ParseTuple(args, "s|i", &name, &user)) |
|---|
| 45 |
return NULL; |
|---|
| 46 |
|
|---|
| 47 |
GError *error = NULL; |
|---|
| 48 |
|
|---|
| 49 |
if(midgard_config_read_file(MIDGARD_CONFIG(self->obj), |
|---|
| 50 |
(const gchar *)name, user, &error)) |
|---|
| 51 |
Py_RETURN_TRUE; |
|---|
| 52 |
|
|---|
| 53 |
if(error) { |
|---|
| 54 |
|
|---|
| 55 |
PyErr_SetString(PyExc_SystemError, error->message); |
|---|
| 56 |
g_error_free(error); |
|---|
| 57 |
|
|---|
| 58 |
return NULL; |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
PyErr_SetString(PyExc_SystemError, "Unhandled exception. FIXME!"); |
|---|
| 62 |
return NULL; |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
static PyObject * |
|---|
| 66 |
pymidgard_config_save_file(PyGObject *self, PyObject *args) |
|---|
| 67 |
{ |
|---|
| 68 |
CONFIG_DEBUG("save_file"); |
|---|
| 69 |
gchar *name; |
|---|
| 70 |
gint user = 1; |
|---|
| 71 |
|
|---|
| 72 |
if(!PyArg_ParseTuple(args, "s|i", &name, &user)) |
|---|
| 73 |
return NULL; |
|---|
| 74 |
|
|---|
| 75 |
GError *error = NULL; |
|---|
| 76 |
if(midgard_config_save_file(MIDGARD_CONFIG(self->obj), |
|---|
| 77 |
(const gchar *)name, user, &error)) |
|---|
| 78 |
Py_RETURN_TRUE; |
|---|
| 79 |
|
|---|
| 80 |
if(error) { |
|---|
| 81 |
|
|---|
| 82 |
PyErr_SetString(PyExc_SystemError, error->message); |
|---|
| 83 |
g_error_free(error); |
|---|
| 84 |
|
|---|
| 85 |
return NULL; |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
PyErr_SetString(PyExc_SystemError, "Unhandled exception. FIXME!"); |
|---|
| 89 |
return NULL; |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
static PyObject * |
|---|
| 93 |
pymidgard_config_list_files(PyGObject *self, PyObject *args) |
|---|
| 94 |
{ |
|---|
| 95 |
CONFIG_DEBUG("list_files"); |
|---|
| 96 |
|
|---|
| 97 |
gint user = 1; |
|---|
| 98 |
|
|---|
| 99 |
if(!PyArg_ParseTuple(args, "|i", &user)) |
|---|
| 100 |
return NULL; |
|---|
| 101 |
|
|---|
| 102 |
gchar **names = midgard_config_list_files(user); |
|---|
| 103 |
|
|---|
| 104 |
if(!names) |
|---|
| 105 |
return NULL; |
|---|
| 106 |
|
|---|
| 107 |
guint i = 0; |
|---|
| 108 |
while(names[i] != NULL) |
|---|
| 109 |
i++; |
|---|
| 110 |
|
|---|
| 111 |
PyObject *list = PyTuple_New(i); |
|---|
| 112 |
|
|---|
| 113 |
i = 0; |
|---|
| 114 |
while(names[i] != NULL) { |
|---|
| 115 |
|
|---|
| 116 |
PyObject *strname = PyString_FromString(names[i]); |
|---|
| 117 |
PyTuple_SetItem(list, i, strname); |
|---|
| 118 |
i++; |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
g_strfreev(names); |
|---|
| 122 |
|
|---|
| 123 |
return list; |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
static PyObject * |
|---|
| 127 |
pymidgard_config_create_midgard_tables(PyGObject *self, PyObject *args) |
|---|
| 128 |
{ |
|---|
| 129 |
CONFIG_DEBUG("create_midgard_tables"); |
|---|
| 130 |
|
|---|
| 131 |
MidgardConnection *mgd = |
|---|
| 132 |
_py_midgard_connection_singleton_get(); |
|---|
| 133 |
|
|---|
| 134 |
if(!mgd) |
|---|
| 135 |
return NULL; |
|---|
| 136 |
|
|---|
| 137 |
if(midgard_config_create_midgard_tables( |
|---|
| 138 |
MIDGARD_CONFIG(self->obj), mgd)) |
|---|
| 139 |
Py_RETURN_TRUE; |
|---|
| 140 |
|
|---|
| 141 |
Py_RETURN_FALSE; |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
static PyObject * |
|---|
| 145 |
pymidgard_config_create_class_table(PyGObject *self, PyObject *args) |
|---|
| 146 |
{ |
|---|
| 147 |
CONFIG_DEBUG("create_class_table"); |
|---|
| 148 |
|
|---|
| 149 |
gchar *cname; |
|---|
| 150 |
|
|---|
| 151 |
if(!PyArg_ParseTuple(args, "s", &cname)) |
|---|
| 152 |
return NULL; |
|---|
| 153 |
|
|---|
| 154 |
MidgardConnection *mgd = |
|---|
| 155 |
_py_midgard_connection_singleton_get(); |
|---|
| 156 |
|
|---|
| 157 |
if(!mgd) |
|---|
| 158 |
return NULL; |
|---|
| 159 |
|
|---|
| 160 |
MidgardObjectClass *klass = MIDGARD_OBJECT_GET_CLASS_BY_NAME(cname); |
|---|
| 161 |
|
|---|
| 162 |
if(!klass) |
|---|
| 163 |
return NULL; |
|---|
| 164 |
|
|---|
| 165 |
if(midgard_config_create_class_table( |
|---|
| 166 |
MIDGARD_CONFIG(self->obj), klass, mgd)) |
|---|
| 167 |
Py_RETURN_TRUE; |
|---|
| 168 |
|
|---|
| 169 |
Py_RETURN_FALSE; |
|---|
| 170 |
} |
|---|
| 171 |
|
|---|
| 172 |
static PyMethodDef pymidgard_config_methods[] = { |
|---|
| 173 |
{ "read_file", (PyCFunction)pymidgard_config_read_file, METH_VARARGS }, |
|---|
| 174 |
{ "list_files", (PyCFunction)pymidgard_config_list_files, METH_VARARGS }, |
|---|
| 175 |
{ "save_file", (PyCFunction)pymidgard_config_save_file, METH_VARARGS }, |
|---|
| 176 |
{ "create_midgard_tables", (PyCFunction)pymidgard_config_create_midgard_tables, METH_NOARGS }, |
|---|
| 177 |
{ "create_class_table", (PyCFunction)pymidgard_config_create_class_table, METH_VARARGS }, |
|---|
| 178 |
{ NULL, NULL, 0 } |
|---|
| 179 |
}; |
|---|
| 180 |
|
|---|
| 181 |
PyTypeObject G_GNUC_INTERNAL Pymidgard_config_Type = { |
|---|
| 182 |
PyObject_HEAD_INIT(NULL) |
|---|
| 183 |
0, |
|---|
| 184 |
"config", |
|---|
| 185 |
sizeof(PyGObject), |
|---|
| 186 |
0, |
|---|
| 187 |
|
|---|
| 188 |
(destructor)0, |
|---|
| 189 |
(printfunc)0, |
|---|
| 190 |
(getattrfunc)0, |
|---|
| 191 |
(setattrfunc)0, |
|---|
| 192 |
(cmpfunc)0, |
|---|
| 193 |
(reprfunc)0, |
|---|
| 194 |
(PyNumberMethods*)0, |
|---|
| 195 |
(PySequenceMethods*)0, |
|---|
| 196 |
(PyMappingMethods*)0, |
|---|
| 197 |
(hashfunc)0, |
|---|
| 198 |
(ternaryfunc)0, |
|---|
| 199 |
(reprfunc)0, |
|---|
| 200 |
_py_midgard_get_object_attribute, |
|---|
| 201 |
_py_midgard_set_object_attribute, |
|---|
| 202 |
(PyBufferProcs*)0, |
|---|
| 203 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
|---|
| 204 |
NULL, |
|---|
| 205 |
(traverseproc)0, |
|---|
| 206 |
(inquiry)0, |
|---|
| 207 |
(richcmpfunc)0, |
|---|
| 208 |
offsetof(PyGObject, weakreflist), |
|---|
| 209 |
(getiterfunc)0, |
|---|
| 210 |
(iternextfunc)0, |
|---|
| 211 |
pymidgard_config_methods, |
|---|
| 212 |
(struct PyMemberDef*)0, |
|---|
| 213 |
(struct PyGetSetDef*)0, |
|---|
| 214 |
NULL, |
|---|
| 215 |
NULL, |
|---|
| 216 |
(descrgetfunc)0, |
|---|
| 217 |
(descrsetfunc)0, |
|---|
| 218 |
offsetof(PyGObject, inst_dict), |
|---|
| 219 |
(initproc)0, |
|---|
| 220 |
(allocfunc)0, |
|---|
| 221 |
(newfunc)0, |
|---|
| 222 |
(freefunc)0, |
|---|
| 223 |
(inquiry)0 |
|---|
| 224 |
}; |
|---|
| 225 |
|
|---|
| 226 |
void py_midgard_config_register_class( |
|---|
| 227 |
PyObject *d, gpointer pygobject_type) |
|---|
| 228 |
{ |
|---|
| 229 |
pygobject_register_class(d, |
|---|
| 230 |
"config", |
|---|
| 231 |
MIDGARD_TYPE_CONFIG, |
|---|
| 232 |
&Pymidgard_config_Type, |
|---|
| 233 |
Py_BuildValue("(O)", pygobject_type)); |
|---|
| 234 |
|
|---|
| 235 |
pyg_set_object_has_new_constructor(MIDGARD_TYPE_CONFIG); |
|---|
| 236 |
} |
|---|