1 | |
---|
2 | /* |
---|
3 | * Rappture Utils Python Interface |
---|
4 | * |
---|
5 | * ====================================================================== |
---|
6 | * Derrick S. Kearney, Purdue University |
---|
7 | * Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
8 | * |
---|
9 | * See the file "license.terms" for information on usage and |
---|
10 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
11 | * ====================================================================== |
---|
12 | */ |
---|
13 | #include <Python.h> |
---|
14 | #include <rappture.h> |
---|
15 | |
---|
16 | static PyObject *ErrorObject; |
---|
17 | |
---|
18 | PyDoc_STRVAR(RpUtils_progress_doc, |
---|
19 | "progress (percent,message)\n\ |
---|
20 | \n\ |
---|
21 | Send progress messages to Rappture for updating the\n\ |
---|
22 | graphical user interface's progress bar."); |
---|
23 | |
---|
24 | static PyObject* |
---|
25 | RpUtils_progress(PyObject *self, PyObject *args, PyObject *keywds) |
---|
26 | { |
---|
27 | double pct; |
---|
28 | char *msg; |
---|
29 | static char *kwlist[] = { |
---|
30 | (char *)"percent", (char *)"message", NULL |
---|
31 | }; |
---|
32 | if (PyTuple_Size(args) != 2) { |
---|
33 | PyErr_Format(PyExc_ValueError, |
---|
34 | "progress() takes exactly 2 arguments, got %ld", |
---|
35 | PyTuple_Size(args)); |
---|
36 | return NULL; |
---|
37 | } |
---|
38 | if (!PyArg_ParseTupleAndKeywords(args, keywds, "ds", kwlist, |
---|
39 | &pct, &msg)) { |
---|
40 | return NULL; |
---|
41 | } |
---|
42 | if (Rappture::Utils::progress((int)pct, msg) != 0) { |
---|
43 | PyErr_SetString(PyExc_RuntimeError, "Error while writing to stdout"); |
---|
44 | return NULL; |
---|
45 | } |
---|
46 | Py_INCREF(Py_None); |
---|
47 | return Py_None; |
---|
48 | } |
---|
49 | |
---|
50 | /* ---------- */ |
---|
51 | |
---|
52 | |
---|
53 | /* List of functions defined in the module */ |
---|
54 | |
---|
55 | static PyMethodDef RpUtils_Methods[] = { |
---|
56 | |
---|
57 | {"progress", (PyCFunction)RpUtils_progress, METH_VARARGS|METH_KEYWORDS, |
---|
58 | RpUtils_progress_doc}, |
---|
59 | |
---|
60 | {NULL, NULL} /* sentinel */ |
---|
61 | }; |
---|
62 | |
---|
63 | PyDoc_STRVAR(module_doc, "Rappture Utils Module for Python."); |
---|
64 | |
---|
65 | /* Initialization function for the module */ |
---|
66 | |
---|
67 | PyMODINIT_FUNC |
---|
68 | initUtils(void) |
---|
69 | { |
---|
70 | PyObject *m; |
---|
71 | |
---|
72 | /* Create the module and add the functions */ |
---|
73 | m = Py_InitModule3("Utils", RpUtils_Methods, module_doc); |
---|
74 | |
---|
75 | if (ErrorObject == NULL) { |
---|
76 | ErrorObject = PyErr_NewException((char *)"Rappture.Utils.error", |
---|
77 | NULL, NULL); |
---|
78 | if (ErrorObject == NULL) |
---|
79 | return; |
---|
80 | } |
---|
81 | Py_INCREF(ErrorObject); |
---|
82 | PyModule_AddObject(m, "error", ErrorObject); |
---|
83 | |
---|
84 | return; |
---|
85 | } |
---|