-
Notifications
You must be signed in to change notification settings - Fork 6
/
xpa.pyx
181 lines (131 loc) · 4.16 KB
/
xpa.pyx
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
cdef extern from "stdio.h":
pass
cdef extern from "stdlib.h":
void free(void *)
cdef extern from "string.h":
void *memcpy(void *s1, void *s2, int n)
cdef extern from "Python.h":
object PyBytes_FromStringAndSize(char *s, int len)
object PyBytes_FromString(char *s)
int PyBytes_Size(object s)
ctypedef int size_t
void* PyMem_Malloc(size_t n)
void PyMem_Free(void* buf)
cdef extern from "xpa.h":
ctypedef struct XPARec
XPARec *XPAOpen(char *mode)
void XPAClose(XPARec *xpa)
int XPANSLookup(XPARec *xpa,
char *template, char *type,
char ***classes, char ***names,
char ***methods, char ***infos)
int XPAGet(XPARec *xpa,
char *template, char *paramlist, char *mode,
char **bufs, int *lens, char **names, char **messages,
int n)
int XPASet(XPARec *xpa,
char *template, char *paramlist, char *mode,
char *buf, int len, char **names, char **messages,
int n)
def nslookup(template="*"):
cdef char **classes
cdef char **names
cdef char **methods
cdef char **infos
cdef int i, n
cdef int iter
#first run
n = XPANSLookup(NULL, template, "g", &classes, &names, &methods, &infos)
l = []
for i from 0 <= i < n:
#print "%s %s %s %s" % (classes[i], names[i], methods[i], infos[i])
s = PyBytes_FromString(methods[i])
l.append(s)
free(classes[i])
free(names[i])
free(methods[i])
free(infos[i])
if n > 0:
free(classes)
free(names)
free(methods)
free(infos)
return l
class XpaException(Exception):
pass
#class _pyXPA:
# XPARec *xpa
# def __init__(self):
# self.xpa = NULL
cdef _get(XPARec *xpa, char *template, char *param):
cdef int i, got
cdef int lens[1]
cdef char *bufs[1]
cdef char *names[1]
cdef char *messages[1]
got = XPAGet(NULL, template, param, NULL, bufs, lens, names, messages, 1)
if got == 1 and messages[0] == NULL:
buf = PyBytes_FromStringAndSize( bufs[0], lens[0] )
#print buf
free(bufs[0])
free(names[0])
else:
if messages[0] != NULL:
mesg = PyBytes_FromString( messages[0] )
free(messages[0]);
else:
mesg = "Unknown XPA Error : XPAGet returned 0!"
if ( names[0] ):
free(names[0])
if( bufs[0] ):
free(bufs[0])
raise XpaException(mesg)
return buf
def get(template="*", param=""):
return _get(NULL, template, param)
cdef _set(XPARec *xpa, char *template, char *param, buf):
cdef int got
cdef int length
#cdef char *bufs[1]
cdef char *names[1]
cdef char *messages[1]
if buf:
length = PyBytes_Size(buf)
else:
buf = b""
length = 0
got = XPASet(xpa, template, param, NULL, buf, length, names, messages, 1)
if got == 1 and messages[0] == NULL:
#buf = PyBytes_FromStringAndSize( bufs[0], lens[0] )
##print buf
#free(bufs[0])
free(names[0])
else:
if messages[0] != NULL:
mesg = PyBytes_FromString( messages[0] )
free(messages[0]);
else:
mesg = "Unknown XPA Error (XPASet returned 0)!"
if ( names[0] ):
free(names[0])
raise XpaException(mesg)
def set(template="*", param="", buf=None):
_set(NULL, template, param, buf)
cdef class xpa:
cdef XPARec *_xpa
cdef char *_template
cdef _set_template(self, template):
cdef int n
n = PyBytes_Size(template)
self._template = <char *>PyMem_Malloc(n+1)
memcpy(self._template, <char *>template, n+1)
def __init__(self, template):
self._set_template(template)
self._xpa = XPAOpen("")
def __del__(self):
XPAClose(self._xpa)
PyMem_Free(self._template)
def get(self, param=b""):
return _get(self._xpa, self._template, param)
def set(self, param=b"", buf=None):
_set(self._xpa, self._template, param, buf)