]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/pythonsd.c
ebl fix debug message
[bacula/bacula] / bacula / src / stored / pythonsd.c
index 85ca46ed2e741a8e180b20a149847e4d6f98c18c..565588a0ae459220ccc8b292159f533455690c8c 100644 (file)
@@ -7,26 +7,34 @@
  *   Version $Id$
  *
  */
-
 /*
-   Copyright (C) 2005 Kern Sibbald
+   Bacula® - The Network Backup Solution
+
+   Copyright (C) 2005-2006 Free Software Foundation Europe e.V.
 
-   This program is free software; you can redistribute it and/or
-   modify it under the terms of the GNU General Public License as
-   published by the Free Software Foundation; either version 2 of
-   the License, or (at your option) any later version.
+   The main author of Bacula is Kern Sibbald, with contributions from
+   many others, a complete list can be found in the file AUTHORS.
+   This program is Free Software; you can redistribute it and/or
+   modify it under the terms of version two of the GNU General Public
+   License as published by the Free Software Foundation plus additions
+   that are listed in the file LICENSE.
 
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    General Public License for more details.
 
-   You should have received a copy of the GNU General Public
-   License along with this program; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
-   MA 02111-1307, USA.
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
+
+   Bacula® is a registered trademark of John Walker.
+   The licensor of Bacula is the Free Software Foundation Europe
+   (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
+   Switzerland, email:ftf@fsfeurope.org.
+*/
 
- */
 
 #include "bacula.h"
 #include "stored.h"
@@ -39,7 +47,14 @@ extern JCR *get_jcr_from_PyObject(PyObject *self);
 extern PyObject *find_method(PyObject *eventsObject, PyObject *method, 
          const char *name);
 
-static int set_job_events(PyObject *self, PyObject *arg);
+static PyObject *set_job_events(PyObject *self, PyObject *arg);
+static PyObject *job_write(PyObject *self, PyObject *arg);
+
+PyMethodDef JobMethods[] = {
+    {"set_events", set_job_events, METH_VARARGS, "Set Job events"},
+    {"write", job_write, METH_VARARGS, "Write to output"},
+    {NULL, NULL, 0, NULL}             /* last item */
+};
 
 
 struct s_vars {
@@ -48,18 +63,18 @@ struct s_vars {
 };
 
 static struct s_vars getvars[] = {
-   { N_("Job"),        "s"},          /* 0 */
-   { N_("SDName"),     "s"},          /* 1 */
-   { N_("Level"),      "s"},          /* 2 */
-   { N_("Type"),       "s"},          /* 3 */
-   { N_("JobId"),      "i"},          /* 4 */
-   { N_("Client"),     "s"},          /* 5 */
-   { N_("Pool"),       "s"},          /* 6 */
-   { N_("MediaType"),  "s"},          /* 7 */
-   { N_("JobName"),    "s"},          /* 8 */
-   { N_("JobStatus"),  "s"},          /* 9 */
-   { N_("VolumeName"), "s"},          /* 10 */
-   { N_("Device"),     "s"},          /* 11 */
+   { NT_("Job"),        "s"},          /* 0 */
+   { NT_("SDName"),     "s"},          /* 1 */
+   { NT_("Level"),      "s"},          /* 2 */
+   { NT_("Type"),       "s"},          /* 3 */
+   { NT_("JobId"),      "i"},          /* 4 */
+   { NT_("Client"),     "s"},          /* 5 */
+   { NT_("Pool"),       "s"},          /* 6 */
+   { NT_("MediaType"),  "s"},          /* 7 */
+   { NT_("JobName"),    "s"},          /* 8 */
+   { NT_("JobStatus"),  "s"},          /* 9 */
+   { NT_("VolumeName"), "s"},          /* 10 */
+   { NT_("Device"),     "s"},          /* 11 */
 
    { NULL,             NULL}
 };
@@ -67,9 +82,7 @@ static struct s_vars getvars[] = {
 
 /* Writable variables */
 static struct s_vars setvars[] = {
-   { N_("set_events"), NULL},
-   { N_("JobReport"),   "s"},
-   { N_("write"),       "s"},
+   { NT_("JobReport"),   "s"},
 
    { NULL,             NULL}
 };
@@ -87,7 +100,7 @@ PyObject *job_getattr(PyObject *self, char *attrname)
    Dmsg1(100, "In job_getattr=%s\n", attrname);
    jcr = get_jcr_from_PyObject(self);
    if (!jcr) {
-      bstrncpy(errmsg, "Job pointer not found.", sizeof(errmsg));
+      bstrncpy(errmsg, _("Job pointer not found."), sizeof(errmsg));
       goto bail_out;
    }
 
@@ -98,7 +111,8 @@ PyObject *job_getattr(PyObject *self, char *attrname)
       }
    }
    if (!found) {
-      goto not_found;
+      /* Try our methods */
+      return Py_FindMethod(JobMethods, self, attrname);
    }  
    switch (i) {
    case 0:                            /* Job */
@@ -128,8 +142,7 @@ PyObject *job_getattr(PyObject *self, char *attrname)
    case 11:
       return Py_BuildValue(getvars[i].fmt, jcr->dcr->dev_name);
    }
-not_found:
-   bsnprintf(errmsg, sizeof(errmsg), "Attribute %s not found.", attrname);
+   bsnprintf(errmsg, sizeof(errmsg), _("Attribute %s not found."), attrname);
 bail_out:
    PyErr_SetString(PyExc_AttributeError, errmsg);
    return NULL;
@@ -146,13 +159,13 @@ int job_setattr(PyObject *self, char *attrname, PyObject *value)
 
    Dmsg2(100, "In job_setattr=%s val=%p.\n", attrname, value);
    if (value == NULL) {                /* Cannot delete variables */
-      bsnprintf(buf, sizeof(buf), "Cannot delete attribute %s", attrname);
+      bsnprintf(buf, sizeof(buf), _("Cannot delete attribute %s"), attrname);
       errmsg = buf;
       goto bail_out;
    }
    jcr = get_jcr_from_PyObject(self);
    if (!jcr) {
-      errmsg = "Job pointer not found.";
+      errmsg = _("Job pointer not found.");
       goto bail_out;
    }
 
@@ -169,20 +182,17 @@ int job_setattr(PyObject *self, char *attrname, PyObject *value)
    /* Get argument value ***FIXME*** handle other formats */
    if (setvars[i].fmt != NULL) {
       if (!PyArg_Parse(value, setvars[i].fmt, &strval)) {
-         PyErr_SetString(PyExc_TypeError, "Read-only attribute");
+         PyErr_SetString(PyExc_TypeError, _("Read-only attribute"));
          return -1;
       }
    }   
    switch (i) {
-   case 0:                            /* set_events */
-      return set_job_events(self, value);
-   case 1:                            /* JobReport */
-   case 2:                            /* write */
+   case 0:                            /* JobReport */
       Jmsg(jcr, M_INFO, 0, "%s", strval);
       return 0;
    }
 not_found:
-   bsnprintf(buf, sizeof(buf), "Cannot find attribute %s", attrname);
+   bsnprintf(buf, sizeof(buf), _("Cannot find attribute %s"), attrname);
    errmsg = buf;
 bail_out:
    PyErr_SetString(PyExc_AttributeError, errmsg);
@@ -190,26 +200,37 @@ bail_out:
 }
 
 
-static int set_job_events(PyObject *self, PyObject *arg)
+static PyObject *set_job_events(PyObject *self, PyObject *arg)
 {
    PyObject *eObject;
    JCR *jcr;
 
    Dmsg0(100, "In set_job_events.\n");
-   if (!PyArg_Parse(arg, "O", &eObject)) {
-      Dmsg0(000, "Parse error looking for Object argument\n");
-      return -1;
+   if (!PyArg_ParseTuple(arg, "O:set_events", &eObject)) {
+      Pmsg0(000, _("Error in ParseTuple\n"));
+      return NULL;
    }
    jcr = get_jcr_from_PyObject(self);
-   if (!jcr) {
-      PyErr_SetString(PyExc_AttributeError, "Job pointer not found.");
-      return -1;
-   }
-   Py_XDECREF((PyObject *)jcr->Python_events);  /* release any old events Object */
+   Py_XDECREF((PyObject *)jcr->Python_events);
    Py_INCREF(eObject);
-   jcr->Python_events = (void *)eObject;        /* set new events */
+   jcr->Python_events = (void *)eObject;
+   Py_INCREF(Py_None);
+   return Py_None;
+}
+
+static PyObject *job_write(PyObject *self, PyObject *args)
+{
+   char *text = NULL;
 
-   return 0;
+   if (!PyArg_ParseTuple(args, "s:write", &text)) {
+      Pmsg0(000, _("Parse tuple error in job_write\n"));
+      return NULL;
+   }
+   if (text) {
+      Jmsg(NULL, M_INFO, 0, "%s", text);
+   }
+   Py_INCREF(Py_None);
+   return Py_None;
 }
 
 
@@ -217,16 +238,17 @@ int generate_job_event(JCR *jcr, const char *event)
 {
    PyObject *method = NULL;
    PyObject *Job = (PyObject *)jcr->Python_job;
+   PyObject *events = (PyObject *)jcr->Python_events;
    PyObject *result = NULL;
    int stat = 0;
 
-   if (!Job) {
+   if (!Job || !events) {
       return 0;
    }
 
-   PyEval_AcquireLock();
+   lock_python();
+// PyEval_AcquireLock();
 
-   PyObject *events = (PyObject *)jcr->Python_events;
    method = find_method(events, method, event);
    if (!method) {
       goto bail_out;
@@ -238,7 +260,7 @@ int generate_job_event(JCR *jcr, const char *event)
    if (result == NULL) {
       if (PyErr_Occurred()) {
          PyErr_Print();
-         Dmsg1(000, "Error in Python method %s\n", event);
+         Dmsg1(000, _("Error in Python method %s\n"), event);
       }
    } else {
       stat = 1;
@@ -246,7 +268,8 @@ int generate_job_event(JCR *jcr, const char *event)
    Py_XDECREF(result);
 
 bail_out:
-   PyEval_ReleaseLock();
+   unlock_python();
+// PyEval_ReleaseLock();
    return stat;
 }