-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython_api.c
More file actions
67 lines (56 loc) · 1.94 KB
/
Copy pathpython_api.c
File metadata and controls
67 lines (56 loc) · 1.94 KB
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
61
62
63
64
65
66
67
#define PY_SSIZE_T_CLEAN
#include <Python.h>
// BTMHA external declarations
extern void mha_init(char quiet);
extern void mha_cleanup(void);
extern void parse_line(char *line, char quiet);
// Python wrapper for mha_init
static PyObject* py_btmha_init(PyObject *self, PyObject *args) {
int quiet = 1;
if (!PyArg_ParseTuple(args, "|p", &quiet)) {
return NULL;
}
mha_init((char)quiet);
Py_RETURN_NONE;
}
// Python wrapper for mha_cleanup
static PyObject* py_btmha_cleanup(PyObject *self, PyObject *args) {
mha_cleanup();
Py_RETURN_NONE;
}
// Python wrapper for parse_line
static PyObject* py_btmha_parse(PyObject *self, PyObject *args) {
const char *command;
int quiet = 1;
// Parse the string argument and optional quiet boolean
if (!PyArg_ParseTuple(args, "s|p", &command, &quiet)) {
return NULL;
}
// Create a mutable copy since parse_line might modify the string
char *cmd_copy = strdup(command);
if (!cmd_copy) {
return PyErr_NoMemory();
}
parse_line(cmd_copy, (char)quiet);
free(cmd_copy);
Py_RETURN_NONE;
}
// Method definition array
static PyMethodDef BtmhaMethods[] = {
{"init", py_btmha_init, METH_VARARGS, "Initialize the BTMHA engine. Optional arg: quiet (bool)"},
{"cleanup", py_btmha_cleanup, METH_NOARGS, "Cleanup BTMHA resources."},
{"parse", py_btmha_parse, METH_VARARGS, "Parse and evaluate a BTMHA command string."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
// Module definition structure
static struct PyModuleDef btmhamodule = {
PyModuleDef_HEAD_INIT,
"btmha", /* name of module */
"BTMHA Python API", /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
BtmhaMethods
};
// Initialization function for the module
PyMODINIT_FUNC PyInit_btmha(void) {
return PyModule_Create(&btmhamodule);
}