From da64442fc289332c688ea4a7a4bbd445b0eb54bf Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Fri, 10 Oct 2014 21:45:49 -0400 Subject: [PATCH] Python bindings: Add UUID accessors to the Clock class MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérémie Galarneau --- bindings/python/babeltrace.i.in | 31 +++++++++++++++++++++ bindings/python/examples/ctf_writer.py | 1 + bindings/python/python-complements.c | 37 ++++++++++++++++++++++++++ bindings/python/python-complements.h | 5 ++++ 4 files changed, 74 insertions(+) diff --git a/bindings/python/babeltrace.i.in b/bindings/python/babeltrace.i.in index 724ed45d..cdb687d5 100644 --- a/bindings/python/babeltrace.i.in +++ b/bindings/python/babeltrace.i.in @@ -114,6 +114,10 @@ const char *_bt_python_ctf_event_class_get_field_name( struct bt_ctf_event_class *event_class, size_t index); struct bt_ctf_field_type *_bt_python_ctf_event_class_get_field_type( struct bt_ctf_event_class *event_class, size_t index); +int _bt_python_ctf_clock_get_uuid_index(struct bt_ctf_clock *clock, + size_t index, unsigned char *OUTPUT); +int _bt_python_ctf_clock_set_uuid_index(struct bt_ctf_clock *clock, + size_t index, unsigned char value); /* ================================================================= CONTEXT.H, CONTEXT-INTERNAL.H @@ -1586,6 +1590,7 @@ void bt_ctf_writer_get(struct bt_ctf_writer *writer); void bt_ctf_writer_put(struct bt_ctf_writer *writer); %pythoncode %{ +import uuid class CTFWriter: # Used to compare to -1ULL in error checks @@ -1731,6 +1736,32 @@ class CTFWriter: if ret < 0: raise ValueError("Could not set the clock's absolute attribute.") + """ + Get a clock's UUID (an object of type UUID). + """ + @property + def uuid(self): + uuid_list = [] + for i in range(16): + ret, value = _bt_python_ctf_clock_get_uuid_index(self._c, i) + if ret < 0: + raise ValueError("Invalid clock instance") + uuid_list.append(value) + return uuid.UUID(bytes=bytes(uuid_list)) + + """ + Set a clock's UUID (an object of type UUID). + """ + @uuid.setter + def uuid(self, uuid): + uuid_bytes = uuid.bytes + if len(uuid_bytes) != 16: + raise ValueError("Invalid UUID provided. UUID length must be 16 bytes") + for i in range(len(uuid_bytes)): + ret = _bt_python_ctf_clock_set_uuid_index(self._c, i, uuid_bytes[i]) + if ret < 0: + raise ValueError("Invalid clock instance") + """ Get the current time in nanoseconds since the clock's origin (offset and offset_s attributes). diff --git a/bindings/python/examples/ctf_writer.py b/bindings/python/examples/ctf_writer.py index fd9ce159..b7bd73f2 100644 --- a/bindings/python/examples/ctf_writer.py +++ b/bindings/python/examples/ctf_writer.py @@ -36,6 +36,7 @@ print("Clock offset_seconds is {}".format(clock.offset_seconds)) print("Clock offset is {}".format(clock.offset)) print("Clock is absolute: {}".format(clock.absolute)) print("Clock time is {}".format(clock.time)) +print("Clock UUID is {}".format(clock.uuid)) writer.add_clock(clock) writer.add_environment_field("Python_version", str(sys.version_info)) diff --git a/bindings/python/python-complements.c b/bindings/python/python-complements.c index 55312fff..872512d6 100644 --- a/bindings/python/python-complements.c +++ b/bindings/python/python-complements.c @@ -23,6 +23,7 @@ #include #include #include +#include /* List-related functions ---------------------------------------------------- @@ -342,3 +343,39 @@ struct bt_ctf_field_type *_bt_python_ctf_event_class_get_field_type( return !ret ? type : NULL; } +int _bt_python_ctf_clock_get_uuid_index(struct bt_ctf_clock *clock, + size_t index, unsigned char *value) +{ + int ret = 0; + const unsigned char *uuid; + + if (index >= 16) { + ret = -1; + goto end; + } + + uuid = bt_ctf_clock_get_uuid(clock); + if (!uuid) { + ret = -1; + goto end; + } + + *value = uuid[index]; +end: + return ret; +} + +int _bt_python_ctf_clock_set_uuid_index(struct bt_ctf_clock *clock, + size_t index, unsigned char value) +{ + int ret = 0; + + if (index >= 16) { + ret = -1; + goto end; + } + + clock->uuid[index] = value; +end: + return ret; +} diff --git a/bindings/python/python-complements.h b/bindings/python/python-complements.h index 0132b45e..e2f12374 100644 --- a/bindings/python/python-complements.h +++ b/bindings/python/python-complements.h @@ -27,6 +27,7 @@ #include #include #include +#include /* ctf-field-list */ struct bt_definition **_bt_python_field_listcaller( @@ -85,3 +86,7 @@ const char *_bt_python_ctf_event_class_get_field_name( struct bt_ctf_event_class *event_class, size_t index); struct bt_ctf_field_type *_bt_python_ctf_event_class_get_field_type( struct bt_ctf_event_class *event_class, size_t index); +int _bt_python_ctf_clock_get_uuid_index(struct bt_ctf_clock *clock, + size_t index, unsigned char *value); +int _bt_python_ctf_clock_set_uuid_index(struct bt_ctf_clock *clock, + size_t index, unsigned char value); -- 2.34.1