File: pyeventlog.c

package info (click to toggle)
lcm 1.3.1%2Brepack1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,784 kB
  • sloc: ansic: 16,184; java: 6,843; cs: 2,266; cpp: 1,594; python: 989; makefile: 348; xml: 252; sh: 62
file content (316 lines) | stat: -rw-r--r-- 8,545 bytes parent folder | download | duplicates (5)
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
#include <Python.h>

#include <lcm/eventlog.h>
#ifdef WIN32
#include <lcm/windows/WinPorting.h>
#endif

// to support python 2.5 and earlier
#ifndef Py_TYPE
    #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
#endif

// to support python 3 where strings are bytes
#if PY_MAJOR_VERSION < 3
    #define PyBytes_FromString PyString_FromString
#endif

typedef struct {
    PyObject_HEAD

    lcm_eventlog_t *eventlog;
    char mode;
} PyLogObject;

PyDoc_STRVAR(pylog_doc,
"Event Log parser\n\
");

//gives redefinition error in MSVC
//PyTypeObject pylcmeventlog_type; 

static PyObject *
pylog_close (PyLogObject *self)
{
    if (self->eventlog) {
        lcm_eventlog_destroy (self->eventlog);
        self->eventlog = NULL;
    }
    Py_INCREF (Py_None);
    return Py_None;
}

static PyObject *
pylog_read_next_event (PyLogObject *self)
{
    if (!self->eventlog) {
        PyErr_SetString (PyExc_ValueError, "event log already closed");
        return NULL;
    }

    if (self->mode != 'r') {
        PyErr_SetString (PyExc_RuntimeError, 
                "reading not allowed in write mode");
        return NULL;
    }

    lcm_eventlog_event_t *next_event = 
        lcm_eventlog_read_next_event (self->eventlog);
    if (!next_event) {
        Py_INCREF (Py_None);
        return Py_None;
    }

    #if PY_MAJOR_VERSION >= 3
PyObject *result = Py_BuildValue ("LLs#y#", // build from bytes
            next_event->eventnum,
            next_event->timestamp,
            next_event->channel, next_event->channellen,
            next_event->data, next_event->datalen);
    #else
    PyObject *result = Py_BuildValue ("LLs#s#", 
            next_event->eventnum,
            next_event->timestamp,
            next_event->channel, next_event->channellen,
            next_event->data, next_event->datalen);
    #endif
    lcm_eventlog_free_event (next_event);

    return result;
}

static PyObject *
pylog_seek (PyLogObject *self, PyObject *arg)
{
    int64_t offset = PyLong_AsLongLong (arg);
    if (PyErr_Occurred ()) return 0;

    if (!self->eventlog) {
        PyErr_SetString (PyExc_ValueError, "event log already closed");
        return NULL;
    }

    if (self->mode != 'r') {
        PyErr_SetString (PyExc_RuntimeError, 
                "seeking not allowed in write mode");
        return NULL;
    }

    fseeko (self->eventlog->f, offset, SEEK_SET);

    Py_INCREF (Py_None);
    return Py_None;
}

static PyObject *
pylog_seek_to_timestamp (PyLogObject *self, PyObject *arg)
{
    int64_t timestamp = PyLong_AsLongLong (arg);
    if (PyErr_Occurred ()) return 0;

    if (!self->eventlog) {
        PyErr_SetString (PyExc_ValueError, "event log already closed");
        return NULL;
    }

    if (self->mode != 'r') {
        PyErr_SetString (PyExc_RuntimeError, 
                "seeking not allowed in write mode");
        return NULL;
    }

    if (0 == lcm_eventlog_seek_to_timestamp(self->eventlog, timestamp)) {
        Py_INCREF (Py_None);
        return Py_None;
    } else {
        PyErr_SetFromErrno (PyExc_IOError);
        return Py_None;
    }
}

static PyObject *
pylog_write_next_event (PyLogObject *self, PyObject *args)
{
    int64_t utime = 0;
    char *channel = NULL;
    int channellen = 0;

    // TODO use a buffer object instead of a string
    uint8_t *data = NULL;
    int datalen = 0;

    if (!PyArg_ParseTuple(args, "Ls#s#", 
                &utime, &channel, &channellen, &data, &datalen)) {
        return NULL;
    }

    if (!self->eventlog) {
        PyErr_SetString (PyExc_ValueError, "event log already closed");
        return NULL;
    }

    if (self->mode != 'w') {
        PyErr_SetString (PyExc_RuntimeError, 
                "writing not allowed in read mode");
        return NULL;
    }

    lcm_eventlog_event_t le; //msvc needs init of all fields seperately
    le.eventnum = 0;
    le.timestamp = utime;
    le.channellen = channellen;
    le.datalen = datalen;
    le.channel = channel;
    le.data = data;
    

    if (0 != lcm_eventlog_write_event (self->eventlog, &le)) {
        PyErr_SetFromErrno (PyExc_IOError);
        return NULL;
    }

    Py_INCREF (Py_None);
    return Py_None;
}

static PyObject *
pylog_size (PyLogObject *self)
{
    struct stat sbuf;
    if (0 != fstat (fileno (self->eventlog->f), &sbuf)) {
        PyErr_SetFromErrno (PyExc_IOError);
        return NULL;
    }
    return PyLong_FromLongLong (sbuf.st_size);
}

static PyObject *
pylog_ftell (PyLogObject *self)
{
    return PyLong_FromLongLong (ftello(self->eventlog->f));
}

static PyMethodDef pylog_methods[] = {
    { "close", (PyCFunction)pylog_close, METH_NOARGS, "" },
    { "seek", (PyCFunction)pylog_seek, METH_O, "" },
    { "seek_to_timestamp", (PyCFunction)pylog_seek_to_timestamp, METH_O, "" },
    { "read_next_event", (PyCFunction)pylog_read_next_event, METH_NOARGS, "" },
    { "write_event", (PyCFunction)pylog_write_next_event, METH_VARARGS, "" },
    { "size", (PyCFunction)pylog_size, METH_NOARGS, "" },
    { "ftell", (PyCFunction)pylog_ftell, METH_NOARGS, "" },
    { NULL, NULL }
};

// ==================== class administrative methods ====================

static PyObject *
pylog_repr(PyLogObject *s)
{
    char buf[512];
    PyOS_snprintf(buf, sizeof(buf),
                "<Log object ... TODO>");
    return PyBytes_FromString(buf);
}

static PyObject *
pylog_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
	//msvc does not allow usage of new as variable name
	PyObject *newobj;

	newobj = type->tp_alloc(type, 0);
	if (newobj != NULL) {
		((PyLogObject *)newobj)->eventlog = NULL;
        ((PyLogObject *)newobj)->mode = 0;
    }
	return newobj;
}

static void
pylog_dealloc(PyLogObject *self)
{
    if (self->eventlog) {
        lcm_eventlog_destroy (self->eventlog);
    }
    Py_TYPE(self)->tp_free((PyObject*)self);
}

static int
pylog_initobj(PyObject *s, PyObject *args, PyObject *kwds)
{
    PyLogObject *self = (PyLogObject *)s;
    static char *keywords[] = { "filename", "mode", 0 };
    char *filename = NULL;
    char *mode = "r";

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|s", keywords, &filename,
                &mode))
        return -1;

    if (!strcmp (mode, "r")) {
        self->mode = 'r';
    } else if (!strcmp (mode, "w")) {
        self->mode = 'w';
    } else {
        PyErr_SetString (PyExc_ValueError, "invalid mode");
        return -1;
    }

    if (self->eventlog) { lcm_eventlog_destroy (self->eventlog); }

    self->eventlog = lcm_eventlog_create (filename, mode);
    if (!self->eventlog) {
        PyErr_SetFromErrno (PyExc_IOError);
        return -1;
    }

    return 0;
}

/* Type object */
PyTypeObject pylcmeventlog_type = {
#if PY_MAJOR_VERSION >= 3
    PyVarObject_HEAD_INIT(0, 0) /* size is now part of macro */
#else
    PyObject_HEAD_INIT(0)   /* Must fill in type value later */
    0,                  /* ob_size */
#endif
    "EventLog",            /* tp_name */
    sizeof(PyLogObject),     /* tp_basicsize */
    0,                  /* tp_itemsize */
    (destructor)pylog_dealloc,     /* tp_dealloc */
    0,                  /* tp_print */
    0,                  /* tp_getattr */
    0,                  /* tp_setattr */
    0,                  /* tp_compare */
    (reprfunc)pylog_repr,          /* tp_repr */
    0,                  /* tp_as_number */
    0,                  /* tp_as_sequence */
    0,                  /* tp_as_mapping */
    0,                  /* tp_hash */
    0,                  /* tp_call */
    0,                  /* tp_str */
    PyObject_GenericGetAttr,        /* tp_getattro */
    0,                  /* tp_setattro */
    0,                  /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
    pylog_doc,               /* tp_doc */
    0,                  /* tp_traverse */
    0,                  /* tp_clear */
    0,                  /* tp_richcompare */
    0,                  /* tp_weaklistoffset */
    0,                  /* tp_iter */
    0,                  /* tp_iternext */
    pylog_methods,               /* tp_methods */
    0,                  /* tp_members */
    0,                  /* tp_getset */
    0,                  /* tp_base */
    0,                  /* tp_dict */
    0,                  /* tp_descr_get */
    0,                  /* tp_descr_set */
    0,                  /* tp_dictoffset */
    pylog_initobj,             /* tp_init */
    PyType_GenericAlloc,            /* tp_alloc */
    pylog_new,             /* tp_new */
    PyObject_Del,               /* tp_free */
};