]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/python.c
Add Python.c
[bacula/bacula] / bacula / src / stored / python.c
1 /*
2  *
3  * Bacula interface to Python for the Storage Daemon
4  *
5  * Kern Sibbald, January MMV
6  *
7  *   Version $Id$
8  *
9  */
10
11 /*
12
13    Copyright (C) 2005 Kern Sibbald
14
15    This program is free software; you can redistribute it and/or
16    modify it under the terms of the GNU General Public License as
17    published by the Free Software Foundation; either version 2 of
18    the License, or (at your option) any later version.
19
20    This program is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23    General Public License for more details.
24
25    You should have received a copy of the GNU General Public
26    License along with this program; if not, write to the Free
27    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28    MA 02111-1307, USA.
29
30  */
31
32 #include "bacula.h"
33 #include "stored.h"
34
35 #ifdef HAVE_PYTHON
36 #undef _POSIX_C_SOURCE
37 #include <Python.h>
38
39 PyObject *bacula_get(PyObject *self, PyObject *args);
40 PyObject *bacula_set(PyObject *self, PyObject *args, PyObject *keyw);
41
42 /* Define Bacula entry points */
43 PyMethodDef BaculaMethods[] = {
44     {"get", bacula_get, METH_VARARGS, "Get Bacula variables."},
45     {"set", (PyCFunction)bacula_set, METH_VARARGS|METH_KEYWORDS,
46         "Set Bacula variables."},
47     {NULL, NULL, 0, NULL}             /* last item */
48 };
49
50
51 struct s_vars {
52    const char *name;
53    char *fmt;
54 };
55
56 static struct s_vars vars[] = {
57    { N_("Job"),        "s"},          /* 0 */
58    { N_("SDName"),     "s"},          /* 1 */
59    { N_("Level"),      "s"},          /* 2 */
60    { N_("Type"),       "s"},          /* 3 */
61    { N_("JobId"),      "i"},          /* 4 */
62    { N_("Client"),     "s"},          /* 5 */
63    { N_("Pool"),       "s"},          /* 6 */
64    { N_("MediaType"),  "s"},          /* 7 */
65    { N_("JobName"),    "s"},          /* 8 */
66    { N_("JobStatus"),  "s"},          /* 9 */
67    { N_("VolumeName"), "s"},          /* 10 */
68    { N_("Device"),     "s"},          /* 11 */
69
70    { NULL,             NULL}
71 };
72
73 /* Return Bacula variables */
74 PyObject *bacula_get(PyObject *self, PyObject *args)
75 {
76    PyObject *CObject;
77    JCR *jcr;
78    char *item;
79    bool found = false;
80    int i;
81    char buf[10];
82
83    if (!PyArg_ParseTuple(args, "Os:get", &CObject, &item)) {
84       return NULL;
85    }
86    jcr = (JCR *)PyCObject_AsVoidPtr(CObject);
87    for (i=0; vars[i].name; i++) {
88       if (strcmp(vars[i].name, item) == 0) {
89          found = true;
90          break;
91       }
92    }
93    if (!found) {
94       return NULL;
95    }
96    switch (i) {
97    case 0:                            /* Job */
98       return Py_BuildValue(vars[i].fmt, jcr->job_name);    /* Non-unique name */
99    case 1:                            /* SD's name */
100       return Py_BuildValue(vars[i].fmt, my_name);
101    case 2:                            /* level */
102       return Py_BuildValue(vars[i].fmt, job_level_to_str(jcr->JobLevel));
103    case 3:                            /* type */
104       return Py_BuildValue(vars[i].fmt, job_type_to_str(jcr->JobType));
105    case 4:                            /* JobId */
106       return Py_BuildValue(vars[i].fmt, jcr->JobId);
107    case 5:                            /* Client */
108       return Py_BuildValue(vars[i].fmt, jcr->client_name);
109    case 6:                            /* Pool */
110       return Py_BuildValue(vars[i].fmt, jcr->dcr->pool_name);
111    case 7:                            /* MediaType */
112       return Py_BuildValue(vars[i].fmt, jcr->dcr->media_type);
113    case 8:                            /* JobName */
114       return Py_BuildValue(vars[i].fmt, jcr->Job);
115    case 9:                            /* JobStatus */
116       buf[1] = 0;
117       buf[0] = jcr->JobStatus;
118       return Py_BuildValue(vars[i].fmt, buf);
119    case 10:
120       return Py_BuildValue(vars[i].fmt, jcr->dcr->VolumeName);
121    case 11:
122       return Py_BuildValue(vars[i].fmt, jcr->dcr->dev_name);
123    }
124    return NULL;
125 }
126
127 /* Set Bacula variables */
128 PyObject *bacula_set(PyObject *self, PyObject *args, PyObject *keyw)
129 {
130    PyObject *CObject;
131    JCR *jcr;
132    char *msg = NULL;
133    static char *kwlist[] = {"jcr", "JobReport", NULL};
134    if (!PyArg_ParseTupleAndKeywords(args, keyw, "O|ss:set", kwlist,
135         &CObject, &msg)) {
136       return NULL;
137    }
138    jcr = (JCR *)PyCObject_AsVoidPtr(CObject);
139
140    if (msg) {
141       Jmsg(jcr, M_INFO, 0, "%s", msg);
142    }
143    return Py_BuildValue("i", 1);
144 }
145
146 #endif /* HAVE_PYTHON */