-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_pymovex.c
354 lines (290 loc) · 10.3 KB
/
_pymovex.c
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/**
* This is an example program that show how to use the Movex API Client Interface in
* the Linux environment.
*
* When you compile/create your own programs and modules use the following commands:
*
* gcc -o TestSock TestSock.c -L. -lMvxSock
*
* Note:
*
*/
#include <Python.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <locale.h>
#include "MvxSock.h"
#define LOBYTE(w) ((unsigned char)(w))
#define HIBYTE(w) ((unsigned char)(((unsigned short)(w) >> 8) & 0xFF))
static SERVER_ID comStruct;
static unsigned long result;
static unsigned long len;
static PyObject *PyMovexError;
static char errstr[1024];
static PyObject*OrderedDict;
static int DEBUG = 0;
PyObject*reportError(char*method, unsigned long result) {
PyObject* unicodeError;
snprintf(errstr, sizeof(errstr), "%s returned error %lu: %s", method, result, comStruct.Buff);
unicodeError = PyUnicode_FromString(errstr);
PyErr_SetObject(PyMovexError, unicodeError);
return NULL;
}
static PyObject *
pymovex_connect(PyObject *self, PyObject *args, PyObject*kwargs)
{
char* host;
int port;
char* user;
char* passwd;
char*conoDivi = "";
char *program;
static char *kwlist[] = {"program", "host", "port", "user", "passwd"};
if (! PyArg_ParseTupleAndKeywords(args, kwargs, "ssiss", kwlist, &program, &host, &port, &user, &passwd))
return NULL;
/*
* If FPW is running remote or locally does not matter if you use
* the host name by which it is known in your network.
* If only locally on this server you can as well use 127.0.0.1.
* It might as well be on another AS/400 in which case we write that IP-address
* or its host name.
* We name this test-program "MvxSock C-test"
* We do NOT want encryption (0) so the last argument (CryptKey) is ignored.
*/
memset(&comStruct, '\0', sizeof(SERVER_ID));
if (DEBUG)
fprintf(stderr, "Connecting with program %s, host %s, port %d, user %s\n", program, host, port, user);
if((result=MvxSockSetup(&comStruct, host, port, "MvxSock C-test", 0, "NotUsed")))
return reportError("MvxSockSetup", result);
if((result=MvxSockInit(&comStruct, conoDivi, user, passwd, program)))
return reportError("MvxSockInit", result);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject * pymovex_query(PyObject*self, PyObject*args) {
char recvBuff[1000];
char *sendBuff;
int count;
if (! PyArg_ParseTuple(args, "s", &sendBuff))
return NULL;
if (DEBUG)
fprintf(stderr, "Query: |%s|\n", sendBuff);
len=sizeof(recvBuff);
count=0;
if((result=MvxSockTrans(&comStruct, sendBuff, recvBuff, &len)))
return reportError("MvxSockTrans", result);
if (DEBUG) {
fprintf(stderr, "Sent %zu bytes: %s\n", strlen(sendBuff), sendBuff);
fprintf(stderr, "Got %zu bytes: %s\n", strlen(recvBuff), recvBuff);
}
while(strncmp(recvBuff, "REP ", 5)==0) {
count++;
len=1000;
if((result=MvxSockReceive(&comStruct, recvBuff, &len)))
return reportError("MvxSockReceive", result);
if (DEBUG)
fprintf(stderr, "Got %zu bytes: %s\n", strlen(recvBuff), recvBuff);
}
if (DEBUG)
fprintf(stderr, "Got %d REP-lines.\n", count);
Py_INCREF(Py_None);
return Py_None;
}
typedef struct {
PyObject_HEAD
char* cmd;
PyObject * outputFields;
int firstResult;
} pymovex_fquery_MyIter;
PyObject* pymovex_fquery_MyIter_iter(PyObject *self)
{
Py_INCREF(self);
return self;
}
PyObject* pymovex_next_result(PyObject* outputFields) {
PyObject* d = PyObject_CallFunctionObjArgs(OrderedDict, NULL);
int pos;
PyObject*key;
char*c_key;
char*value;
for (pos=0; pos<PyTuple_GET_SIZE(outputFields); pos++) {
key = PyTuple_GetItem(outputFields, pos);
c_key = PyString_AsString(PyObject_Str(key));
value = MvxSockGetField(&comStruct, c_key);
if (! value) {
snprintf(errstr, sizeof(errstr), "No such field: %s", c_key);
PyErr_SetString(PyMovexError, errstr);
return NULL;
}
PyObject*pyvalue = PyString_FromString(value);
Py_INCREF(pyvalue);
PyObject_SetItem(d, key, pyvalue);
}
Py_INCREF(d);
return d;
}
PyObject* pymovex_fquery_MyIter_iternext(PyObject *self)
{
char * cmd;
pymovex_fquery_MyIter *p = (pymovex_fquery_MyIter *)self;
if (p->firstResult)
cmd = p->cmd;
else
cmd = NULL;
if ((result=MvxSockAccess(&comStruct, cmd)))
return reportError("MvxSockAccess", result);
p->firstResult = 0;
if (! MvxSockMore(&comStruct)) {
/* Raising of standard StopIteration exception with empty value. */
PyErr_SetNone(PyExc_StopIteration);
if (DEBUG)
fprintf(stderr, "No more results\n");
return NULL;
}
return pymovex_next_result(p->outputFields);
}
static PyTypeObject pymovex_fquery_MyIterType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"_pymovex._fquery_MyIter", /*tp_name*/
sizeof(pymovex_fquery_MyIter), /*tp_basicsize*/
0, /*tp_itemsize*/
0, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER,
/* tp_flags: Py_TPFLAGS_HAVE_ITER tells python to
use tp_iter and tp_iternext fields. */
"Internal myiter iterator object.", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
pymovex_fquery_MyIter_iter, /* tp_iter: __iter__() method */
pymovex_fquery_MyIter_iternext /* tp_iternext: next() method */
};
int pymovex_set_fields(PyObject* fieldMap) {
PyObject *key, *value, *string_value, *string_key;
Py_ssize_t pos = 0;
while (PyDict_Next(fieldMap, &pos, &key, &value)) {
char* skey;
char* svalue;
// Make sure key is a string
if (! PyString_Check(key)) {
string_key = PyObject_Str(key);
if (string_key == NULL) {
snprintf(errstr, sizeof(errstr), "Invalid parameter");
PyErr_SetString(PyMovexError, errstr);
return 0;
}
skey = PyString_AsString(string_key);
snprintf(errstr, sizeof(errstr), "Bad parameter name: %s", skey);
PyErr_SetString(PyMovexError, errstr);
return 0;
}
skey = PyString_AsString(key);
// Note: value could be an integer, so we have to call str(value) first
if (! PyString_Check(value)) {
string_value = PyObject_Str(value);
if (string_value == NULL) {
snprintf(errstr, sizeof(errstr), "Cannot convert parameter %s to string", skey);
PyErr_SetString(PyMovexError, errstr);
return 0;
}
} else {
string_value = value;
}
svalue = PyString_AsString(string_value);
MvxSockSetField(&comStruct, skey, svalue);
if (DEBUG)
fprintf(stderr, " Field: (%s, %s)\n", skey, svalue);
}
return 1;
}
static PyObject * pymovex_fquery(PyObject*self, PyObject*args) {
PyObject * fieldMap;
PyObject * outputFields;
char*cmd;
pymovex_fquery_MyIter *p;
if (! PyArg_ParseTuple(args, "sOO", &cmd, &fieldMap, &outputFields))
return NULL;
if (DEBUG)
fprintf(stderr, "Command: %s\n", cmd);
if (! pymovex_set_fields(fieldMap))
return NULL;
p = PyObject_New(pymovex_fquery_MyIter, &pymovex_fquery_MyIterType);
if (!p) return NULL;
p->firstResult = 1;
p->cmd = cmd;
p->outputFields = outputFields;
Py_INCREF(p);
return (PyObject*)p;
}
static PyObject * pymovex_fquery_single(PyObject*self, PyObject*args) {
PyObject * fieldMap;
PyObject * outputFields;
char*cmd;
if (! PyArg_ParseTuple(args, "sOO", &cmd, &fieldMap, &outputFields))
return NULL;
if (DEBUG)
fprintf(stderr, "Command: %s\n", cmd);
if (! pymovex_set_fields(fieldMap))
return NULL;
if ((result=MvxSockAccess(&comStruct, cmd)))
return reportError("MvxSockAccess", result);
return pymovex_next_result(outputFields);
}
static PyObject* pymovex_close(PyObject *self, PyObject* args) {
if((result=MvxSockClose(&comStruct)))
return reportError("MvxSockClose", result);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject* pymovex_debug(PyObject *self, PyObject* args) {
if (!PyArg_ParseTuple(args, "i", &DEBUG))
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef PyMovexMethods[] = {
{"connect", (PyCFunction)pymovex_connect, METH_VARARGS|METH_KEYWORDS, "Connect"},
{"close", pymovex_close, METH_VARARGS, "Close"},
{"query", pymovex_query, METH_VARARGS, "Query"},
{"fquery", pymovex_fquery, METH_VARARGS, "Field-based query"},
{"fquery_single", pymovex_fquery_single, METH_VARARGS, "Field-based query with only one expected result"},
{"debug", pymovex_debug, METH_VARARGS, "Set debug option"},
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyMODINIT_FUNC init_pymovex(void)
{
PyObject *m;
m = Py_InitModule("_pymovex", PyMovexMethods);
if (m == NULL)
return;
/*
* Create a new _pymovex.Error exception type
*/
PyMovexError = PyErr_NewException("_pymovex.Error", NULL, NULL);
PyModule_AddObject(m, "Error", PyMovexError);
/*
* Import OrderedDict
*/
PyObject*collectionsMod = PyImport_ImportModule("collections");
if (collectionsMod == NULL)
return;
OrderedDict=PyObject_GetAttrString(collectionsMod, "OrderedDict");
}