Skip to content

Commit cd87e7e

Browse files
authored
Merge pull request #227 from CESNET/pytrap_copyMessage
pytrap: add UnirecTemplate.copyMessage()
2 parents a8d19d1 + 86ea4dd commit cd87e7e

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

pytrap/src/unirecmodule.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,30 @@ UnirecTemplate_copy(pytrap_unirectemplate *self)
16871687
return (PyObject *) n;
16881688
}
16891689

1690+
static PyObject *
1691+
UnirecTemplate_copyMessage(pytrap_unirectemplate *self, PyObject *args, PyObject *keywds)
1692+
{
1693+
pytrap_unirectemplate *src = NULL;
1694+
1695+
static char *kwlist[] = {"src", NULL};
1696+
if (!PyArg_ParseTupleAndKeywords(args, keywds, "O!", kwlist, &pytrap_UnirecTemplate, &src)) {
1697+
return NULL;
1698+
}
1699+
1700+
if (self->data_obj == NULL || self->data == NULL) {
1701+
PyErr_SetString(PyExc_TypeError, "Data not allocated by createMessage(), cannot copy content.");
1702+
return NULL;
1703+
}
1704+
if (src->data_obj == NULL) {
1705+
PyErr_SetString(PyExc_TypeError, "UnirecTemplate src does not have any data to copy.");
1706+
return NULL;
1707+
}
1708+
1709+
ur_copy_fields(self->urtmplt, self->data, src->urtmplt, src->data);
1710+
1711+
Py_RETURN_NONE;
1712+
}
1713+
16901714
static PyObject *
16911715
UnirecTemplate_strRecord(pytrap_unirectemplate *self)
16921716
{
@@ -2008,6 +2032,15 @@ static PyMethodDef pytrap_unirectemplate_methods[] = {
20082032
" UnirecTemplate: New copy of object (not just reference).\n"
20092033
},
20102034

2035+
{"copyMessage", (PyCFunction) UnirecTemplate_copyMessage, METH_VARARGS | METH_KEYWORDS,
2036+
"Set the content of the record using src UnirecTemplate argument.\n\n"
2037+
"The current object must have enough allocated memory using createMessage()!!!\n\n"
2038+
"Args:\n"
2039+
" src (UnirecTemplate): UniRec message with data (previously set by setData)"
2040+
"Raises:\n"
2041+
" TypeError: self or src has no allocated data.\n"
2042+
},
2043+
20112044
{"strRecord", (PyCFunction) UnirecTemplate_strRecord, METH_NOARGS,
20122045
"Get values of record in readable format.\n\n"
20132046
"Returns:\n"

pytrap/test/unirectemplate_unittest.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,82 @@ def runTest(self):
858858
self.assertEqual(astr, bstr)
859859
self.assertEqual(astr, '(ipaddr SRC_IP,time TIME_FIRST,uint32 ABC,uint32 BCD,bytes STREAMBYTES,string TEXT)')
860860

861+
class CopyMessageTest(unittest.TestCase):
862+
def runTest(self):
863+
import pytrap
864+
a = pytrap.UnirecTemplate("ipaddr DST_IP,ipaddr SRC_IP,time TIME_FIRST,time TIME_LAST,uint32 ABC,uint32 BCD,string TEXT,string TEXT2,bytes STREAMBYTES,bytes STREAMBYTES2")
865+
a.createMessage(100)
866+
a.setFromDict({ "SRC_IP": pytrap.UnirecIPAddr("10.0.0.1"), "DST_IP": pytrap.UnirecIPAddr("10.0.0.1"),
867+
"TIME_FIRST": pytrap.UnirecTime(1669885132, 853),
868+
"TIME_LAST": pytrap.UnirecTime(1669885132, 853),
869+
"ABC": 123, "BCD": 321, "TEXT": "some_text", "TEXT2": "some_text2",
870+
"STREAMBYTES": b"abc\x01\x02\x00\x03", "STREAMBYTES2": b"abc\x02\x03\x00\x04"})
871+
astr = str(a)
872+
b = pytrap.UnirecTemplate("ipaddr SRC_IP,time TIME_FIRST,uint32 ABC,string TEXT2,bytes STREAMBYTES2,string UNKNOWN")
873+
b.createMessage(100)
874+
b.copyMessage(a)
875+
# print(str(b), b.strRecord())
876+
# print(b.getData())
877+
self.assertEqual(b.getDict(), {"SRC_IP":
878+
pytrap.UnirecIPAddr("10.0.0.1"), "TIME_FIRST": pytrap.UnirecTime(1669885132, 853),
879+
"ABC": 123, "TEXT2": "some_text2", "STREAMBYTES2": b"abc\x02\x03\x00\x04", "UNKNOWN": ""})
880+
a.STREAMBYTES2 = b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
881+
a.TEXT2 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
882+
b.UNKNOWN = "unknown text"
883+
b.copyMessage(a)
884+
self.assertEqual(b.STREAMBYTES2, a.STREAMBYTES2)
885+
self.assertEqual(b.TEXT2, a.TEXT2)
886+
del(a)
887+
# print(str(b), b.strRecord())
888+
# print(b.getData())
889+
self.assertEqual(b.getDict(), {"SRC_IP":
890+
pytrap.UnirecIPAddr("10.0.0.1"), "TIME_FIRST": pytrap.UnirecTime(1669885132, 853),
891+
"ABC": 123, "TEXT2": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "STREAMBYTES2": b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "UNKNOWN": "unknown text"})
892+
893+
class CopyMessageEdgeCasesTest(unittest.TestCase):
894+
def runTest(self):
895+
import pytrap
896+
data = { "SRC_IP": pytrap.UnirecIPAddr("10.0.0.1"), "DST_IP": pytrap.UnirecIPAddr("10.0.0.1"),
897+
"TIME_FIRST": pytrap.UnirecTime(1669885132, 853),
898+
"TIME_LAST": pytrap.UnirecTime(1669885132, 853),
899+
"ABC": 123, "BCD": 321, "TEXT": "some_text", "TEXT2": "some_text2",
900+
"STREAMBYTES": b"abc\x01\x02\x00\x03", "STREAMBYTES2": b"abc\x02\x03\x00\x04"}
901+
a = pytrap.UnirecTemplate("ipaddr DST_IP,ipaddr SRC_IP,time TIME_FIRST,time TIME_LAST,uint32 ABC,uint32 BCD,string TEXT,string TEXT2,bytes STREAMBYTES,bytes STREAMBYTES2")
902+
b = pytrap.UnirecTemplate("ipaddr SRC_IP,time TIME_FIRST,uint32 ABC,string TEXT2,bytes STREAMBYTES2,string UNKNOWN")
903+
with self.assertRaises(TypeError):
904+
# both a and b have no allocated data
905+
b.copyMessage(a)
906+
a.createMessage(100)
907+
a.setFromDict(data)
908+
with self.assertRaises(TypeError):
909+
# b has no allocated data
910+
b.copyMessage(a)
911+
912+
b.createMessage(100)
913+
# recreate a
914+
a = pytrap.UnirecTemplate("ipaddr DST_IP,ipaddr SRC_IP,time TIME_FIRST,time TIME_LAST,uint32 ABC,uint32 BCD,string TEXT,string TEXT2,bytes STREAMBYTES,bytes STREAMBYTES2")
915+
with self.assertRaises(TypeError):
916+
# a has no allocated data
917+
b.copyMessage(a)
918+
919+
a.createMessage(100)
920+
a.setFromDict(data)
921+
b = pytrap.UnirecTemplate("")
922+
b.createMessage()
923+
b.copyMessage(a)
924+
self.assertEqual(b.getDict(), {})
925+
926+
del(a)
927+
del(b)
928+
929+
a = pytrap.UnirecTemplate("")
930+
a.createMessage()
931+
b = pytrap.UnirecTemplate("ipaddr DST_IP,time TIME_FIRST,string TEXT")
932+
b.createMessage(100)
933+
b.copyMessage(a)
934+
self.assertEqual(b.getDict(), {"DST_IP": pytrap.UnirecIPAddr("::"), "TIME_FIRST": pytrap.UnirecTime(0, 0), "TEXT": ""})
935+
936+
861937
class AllocateBigMessage(unittest.TestCase):
862938
def runTest(self):
863939
import pytrap

0 commit comments

Comments
 (0)