nicer measurement of failures and collisions
[samba.git] / source3 / python / py_common.c
1 /* 
2    Python wrappers for DCERPC/SMB client routines.
3
4    Copyright (C) Tim Potter, 2002
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "Python.h"
23
24 /* Return a tuple of (error code, error string) from a WERROR */
25
26 PyObject *py_werror_tuple(WERROR werror)
27 {
28         return Py_BuildValue("is", W_ERROR_V(werror), 
29                              dos_errstr(werror));
30 }
31
32 /* Return a tuple of (error code, error string) from a WERROR */
33
34 PyObject *py_ntstatus_tuple(NTSTATUS ntstatus)
35 {
36         return Py_BuildValue("is", NT_STATUS_V(ntstatus), 
37                              nt_errstr(ntstatus));
38 }
39
40 /* Initialise samba client routines */
41
42 static BOOL initialised;
43
44 void py_samba_init(void)
45 {
46         if (initialised)
47                 return;
48
49         /* Load configuration file */
50
51         if (!lp_load(dyn_CONFIGFILE, True, False, False))
52                 fprintf(stderr, "Can't load %s\n", dyn_CONFIGFILE);
53
54         /* Misc other stuff */
55
56         load_interfaces();
57         
58         initialised = True;
59 }
60
61 /* Debuglevel routines */
62
63 PyObject *get_debuglevel(PyObject *self, PyObject *args)
64 {
65         PyObject *debuglevel;
66
67         if (!PyArg_ParseTuple(args, ""))
68                 return NULL;
69
70         debuglevel = PyInt_FromLong(DEBUGLEVEL);
71
72         return debuglevel;
73 }
74
75 PyObject *set_debuglevel(PyObject *self, PyObject *args)
76 {
77         int debuglevel;
78
79         if (!PyArg_ParseTuple(args, "i", &debuglevel))
80                 return NULL;
81
82         DEBUGLEVEL = debuglevel;
83
84         Py_INCREF(Py_None);
85         return Py_None;
86 }
87
88 /* Initialise logging */
89
90 PyObject *py_setup_logging(PyObject *self, PyObject *args, PyObject *kw)
91 {
92         BOOL interactive = False;
93         char *logfilename = NULL;
94         static char *kwlist[] = {"interactive", "logfilename", NULL};
95
96         if (!PyArg_ParseTupleAndKeywords(args, kw, "|is", kwlist,
97                                          &interactive, &logfilename))
98                 return NULL;
99         
100         if (interactive && logfilename) {
101                 PyErr_SetString(PyExc_RuntimeError,
102                                 "can't be interactive and set log file name");
103                 return NULL;
104         }
105
106         if (interactive)
107                 setup_logging("spoolss", True);
108
109         if (logfilename) {
110                 lp_set_logfile(logfilename);
111                 setup_logging(logfilename, False);
112                 reopen_logs();
113         }
114
115         Py_INCREF(Py_None);
116         return Py_None;
117 }