root/trunk/midgard/apis/python/py_midgard_config.c

Revision 16724, 6.4 kB (checked in by piotras, 5 months ago)

Accept GError argument while reading or saving configuration file

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2  * Copyright (C) 2007 Piotr Pokora <piotrek.pokora@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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); /* Free names array, we have copy in list's tuple */
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,                                 /* ob_size */
184     "config",                   /* tp_name */
185     sizeof(PyGObject),          /* tp_basicsize */
186     0,                                 /* tp_itemsize */
187     /* methods */
188     (destructor)0,        /* tp_dealloc */
189     (printfunc)0,                      /* tp_print */
190     (getattrfunc)0,       /* tp_getattr */
191     (setattrfunc)0,       /* tp_setattr */
192     (cmpfunc)0,           /* tp_compare */
193     (reprfunc)0,             /* tp_repr */
194     (PyNumberMethods*)0,     /* tp_as_number */
195     (PySequenceMethods*)0, /* tp_as_sequence */
196     (PyMappingMethods*)0,   /* tp_as_mapping */
197     (hashfunc)0,             /* tp_hash */
198     (ternaryfunc)0,          /* tp_call */
199     (reprfunc)0,              /* tp_str */
200     _py_midgard_get_object_attribute, /* tp_getattro */
201     _py_midgard_set_object_attribute, /* tp_setattro */
202     (PyBufferProcs*)0,  /* tp_as_buffer */
203     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,                      /* tp_flags */
204     NULL,                        /* Documentation string */
205     (traverseproc)0,     /* tp_traverse */
206     (inquiry)0,             /* tp_clear */
207     (richcmpfunc)0,   /* tp_richcompare */
208     offsetof(PyGObject, weakreflist),             /* tp_weaklistoffset */
209     (getiterfunc)0,          /* tp_iter */
210     (iternextfunc)0,     /* tp_iternext */
211     pymidgard_config_methods, /* tp_methods */
212     (struct PyMemberDef*)0,              /* tp_members */
213     (struct PyGetSetDef*)0,  /* tp_getset */
214     NULL,                              /* tp_base */
215     NULL,                              /* tp_dict */
216     (descrgetfunc)0,    /* tp_descr_get */
217     (descrsetfunc)0,    /* tp_descr_set */
218     offsetof(PyGObject, inst_dict),                 /* tp_dictoffset */
219     (initproc)0,             /* tp_init */
220     (allocfunc)0,           /* tp_alloc */
221     (newfunc)0,               /* tp_new */
222     (freefunc)0,             /* tp_free */
223     (inquiry)0              /* tp_is_gc */
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 }
Note: See TracBrowser for help on using the browser.