-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathschedule_py2.cc
60 lines (44 loc) · 1.43 KB
/
schedule_py2.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "schedule_lib.h"
#include "Python.h"
using namespace std;
using namespace schedule;
PyMODINIT_FUNC initschedule_pylib(void); /* Forward */
int main(int argc, char **argv) {
/* Pass argv[0] to the Python interpreter */
Py_SetProgramName(argv[0]);
/* Initialize the Python interpreter. Required. */
Py_Initialize();
/* Add a static module */
initschedule_pylib();
/* Exit, cleaning up the interpreter */
Py_Exit(0);
/*NOTREACHED*/
return 0;
}
/* 'self' is not used */
static PyObject * schedule_pylib_make_schedule(PyObject *self, PyObject* args) {
const char *tasks_string;
int task_str_len = 0;
if (!PyArg_ParseTuple(args, "s#", &tasks_string, &task_str_len))
return NULL;
string tasks_str;
tasks_str.assign(tasks_string, task_str_len);
Tasks tasks;
Schedules schedules;
string schedules_str;
tasks.ParseFromString(tasks_str);
if (make_schedule(tasks, &schedules)) {
schedules.SerializeToString(&schedules_str);
}
return Py_BuildValue("s#", schedules_str.c_str(), schedules_str.size());
}
static PyMethodDef schedule_pylib_methods[] = {
{"MakeSchedule", schedule_pylib_make_schedule, METH_VARARGS,
"Make a schedule for you."},
{NULL, NULL, 0, NULL} /* sentinel */
};
PyMODINIT_FUNC initschedule_pylib(void)
{
PyImport_AddModule("schedule_pylib");
Py_InitModule("schedule_pylib", schedule_pylib_methods);
}