Add scripting to libmu_py.
* python/libmu_py/script.c: New file.
Showing
5 changed files
with
134 additions
and
5 deletions
... | @@ -156,6 +156,7 @@ extern PyFolder * PyFolder_NEW (); | ... | @@ -156,6 +156,7 @@ extern PyFolder * PyFolder_NEW (); |
156 | extern PyHeader * PyHeader_NEW (); | 156 | extern PyHeader * PyHeader_NEW (); |
157 | extern PyMailcap * PyMailcap_NEW (); | 157 | extern PyMailcap * PyMailcap_NEW (); |
158 | extern PyMailcapEntry * PyMailcapEntry_NEW (); | 158 | extern PyMailcapEntry * PyMailcapEntry_NEW (); |
159 | extern PyMailbox * PyMailbox_NEW (); | ||
159 | extern PyMailer * PyMailer_NEW (); | 160 | extern PyMailer * PyMailer_NEW (); |
160 | extern PyMessage * PyMessage_NEW (); | 161 | extern PyMessage * PyMessage_NEW (); |
161 | extern PyMime * PyMime_NEW (); | 162 | extern PyMime * PyMime_NEW (); |
... | @@ -170,6 +171,26 @@ extern int PyAuthData_Check (PyObject *x); | ... | @@ -170,6 +171,26 @@ extern int PyAuthData_Check (PyObject *x); |
170 | extern int PyMessage_Check (PyObject *x); | 171 | extern int PyMessage_Check (PyObject *x); |
171 | extern int PyStream_Check (PyObject *x); | 172 | extern int PyStream_Check (PyObject *x); |
172 | 173 | ||
174 | typedef struct { | ||
175 | char *name; | ||
176 | PyObject *obj; | ||
177 | } mu_py_dict; | ||
178 | |||
179 | typedef struct { | ||
180 | char *module_name; | ||
181 | mu_py_dict *attrs; | ||
182 | } mu_py_script_data; | ||
183 | |||
184 | extern void mu_py_script_init (int argc, char *argv[]); | ||
185 | extern void mu_py_script_finish (void); | ||
186 | extern int mu_py_script_run (const char *filename, | ||
187 | mu_py_script_data *data); | ||
188 | |||
189 | extern int mu_py_script_process_mailbox (int argc, char *argv[], | ||
190 | const char *python_filename, | ||
191 | const char *module_name, | ||
192 | mu_mailbox_t mbox); | ||
193 | |||
173 | #ifdef __cplusplus | 194 | #ifdef __cplusplus |
174 | } | 195 | } |
175 | #endif | 196 | #endif | ... | ... |
... | @@ -73,6 +73,12 @@ static PyTypeObject PyMailboxType = { | ... | @@ -73,6 +73,12 @@ static PyTypeObject PyMailboxType = { |
73 | 0, /* tp_new */ | 73 | 0, /* tp_new */ |
74 | }; | 74 | }; |
75 | 75 | ||
76 | PyMailbox * | ||
77 | PyMailbox_NEW () | ||
78 | { | ||
79 | return (PyMailbox *)PyObject_NEW (PyMailbox, &PyMailboxType); | ||
80 | } | ||
81 | |||
76 | static PyObject * | 82 | static PyObject * |
77 | api_mailbox_create (PyObject *self, PyObject *args) | 83 | api_mailbox_create (PyObject *self, PyObject *args) |
78 | { | 84 | { | ... | ... |
python/libmu_py/script.c
0 → 100644
1 | /* | ||
2 | GNU Mailutils -- a suite of utilities for electronic mail | ||
3 | Copyright (C) 2009 Free Software Foundation, Inc. | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Lesser General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 3 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Lesser General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Lesser General | ||
16 | Public License along with this library; if not, write to the | ||
17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301 USA | ||
19 | */ | ||
20 | |||
21 | #include "libmu_py.h" | ||
22 | |||
23 | void | ||
24 | mu_py_script_init (int argc, char *argv[]) | ||
25 | { | ||
26 | if (!Py_IsInitialized ()) | ||
27 | Py_Initialize (); | ||
28 | PySys_SetArgv (argc, argv); | ||
29 | } | ||
30 | |||
31 | void | ||
32 | mu_py_script_finish (void) | ||
33 | { | ||
34 | Py_Finalize (); | ||
35 | } | ||
36 | |||
37 | static PyMethodDef nomethods[] = { | ||
38 | { NULL, NULL } | ||
39 | }; | ||
40 | |||
41 | int | ||
42 | mu_py_script_run (const char *python_filename, mu_py_script_data *data) | ||
43 | { | ||
44 | FILE *fp; | ||
45 | PyObject *py_module; | ||
46 | |||
47 | if (!python_filename) | ||
48 | return MU_ERR_OUT_PTR_NULL; | ||
49 | |||
50 | fp = fopen (python_filename, "r"); | ||
51 | if (!fp) | ||
52 | return errno; | ||
53 | |||
54 | py_module = Py_InitModule (data->module_name, nomethods); | ||
55 | if (!py_module) | ||
56 | return MU_ERR_FAILURE; | ||
57 | |||
58 | for (; data->attrs->name; data->attrs++) | ||
59 | PyObject_SetAttrString (py_module, data->attrs->name, data->attrs->obj); | ||
60 | |||
61 | if (PyRun_SimpleFile (fp, python_filename)) | ||
62 | return MU_ERR_FAILURE; | ||
63 | |||
64 | fclose (fp); | ||
65 | return 0; | ||
66 | } | ||
67 | |||
68 | int | ||
69 | mu_py_script_process_mailbox (int argc, char *argv[], | ||
70 | const char *python_filename, | ||
71 | const char *module_name, | ||
72 | mu_mailbox_t mbox) | ||
73 | { | ||
74 | int status; | ||
75 | PyMailbox *py_mbox; | ||
76 | mu_py_dict dict[2]; | ||
77 | mu_py_script_data data[1]; | ||
78 | |||
79 | mu_py_script_init (argc, argv); | ||
80 | |||
81 | py_mbox = PyMailbox_NEW (); | ||
82 | py_mbox->mbox = mbox; | ||
83 | Py_INCREF (py_mbox); | ||
84 | |||
85 | dict[0].name = "mailbox"; | ||
86 | dict[0].obj = (PyObject *)py_mbox; | ||
87 | dict[1].name = NULL; | ||
88 | |||
89 | data[0].module_name = module_name; | ||
90 | data[0].attrs = dict; | ||
91 | |||
92 | status = mu_py_script_run (python_filename, data); | ||
93 | mu_py_script_finish (); | ||
94 | return status; | ||
95 | } |
... | @@ -161,14 +161,20 @@ class MailboxBase: | ... | @@ -161,14 +161,20 @@ class MailboxBase: |
161 | 161 | ||
162 | 162 | ||
163 | class Mailbox (MailboxBase): | 163 | class Mailbox (MailboxBase): |
164 | __owner = False | ||
164 | def __init__ (self, name): | 165 | def __init__ (self, name): |
165 | self.mbox = mailbox.MailboxType () | 166 | if isinstance (name, mailbox.MailboxType): |
166 | status = mailbox.create (self.mbox, name) | 167 | self.mbox = name |
167 | if status: | 168 | else: |
168 | raise MailboxError (status) | 169 | self.mbox = mailbox.MailboxType () |
170 | self.__owner = True | ||
171 | status = mailbox.create (self.mbox, name) | ||
172 | if status: | ||
173 | raise MailboxError (status) | ||
169 | 174 | ||
170 | def __del__ (self): | 175 | def __del__ (self): |
171 | mailbox.destroy (self.mbox) | 176 | if self.__owner: |
177 | mailbox.destroy (self.mbox) | ||
172 | del self.mbox | 178 | del self.mbox |
173 | 179 | ||
174 | class MailboxDefault (MailboxBase): | 180 | class MailboxDefault (MailboxBase): | ... | ... |
-
Please register or sign in to post a comment