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