Python bindings: Make examples PEP8 compliant
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Sun, 12 Oct 2014 09:16:14 +0000 (05:16 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Sun, 12 Oct 2014 09:16:14 +0000 (05:16 -0400)
bindings/python/examples/babeltrace_and_lttng.py
bindings/python/examples/ctf_writer.py
bindings/python/examples/example-api-test.py
bindings/python/examples/sched_switch.py
bindings/python/examples/sequence_test.py

index 42ebc85eb3019c5e2579206b8ff36088dfc1a265..d837a53270e1e24a51c976c81d4a5292233d9f6d 100644 (file)
@@ -1,19 +1,19 @@
 #!/usr/bin/env python3
 # babeltrace_and_lttng.py
-# 
+#
 # Babeltrace and LTTng example script
-# 
+#
 # Copyright 2012 EfficiOS Inc.
-# 
+#
 # Author: Danny Serres <danny.serres@efficios.com>
-# 
+#
 # Permission is hereby granted, free of charge, to any person obtaining a copy
 # of this software and associated documentation files (the "Software"), to deal
 # in the Software without restriction, including without limitation the rights
 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 # copies of the Software, and to permit persons to whom the Software is
 # furnished to do so, subject to the following conditions:
-# 
+#
 # The above copyright notice and this permission notice shall be included in
 # all copies or substantial portions of the Software.
 
 
 
 # ------------------------------------------------------
-ses_name =     "babeltrace-lttng-test"
-trace_path =   "/lttng-traces/babeltrace-lttng-trace/"
-out_file =     "babeltrace-lttng-trace-text-output.txt"
+ses_name = "babeltrace-lttng-test"
+trace_path = "/lttng-traces/babeltrace-lttng-trace/"
+out_file = "babeltrace-lttng-trace-text-output.txt"
 # ------------------------------------------------------
 
 
 import time
 try:
-       import babeltrace, lttng
+    import babeltrace
+    import lttng
 except ImportError:
-       raise ImportError(      "both babeltrace and lttng-tools "
-                               "python modules must be installed" )
+    raise ImportError( "both babeltrace and lttng-tools "
+                       "python modules must be installed")
 
 
 # Errors to raise if something goes wrong
 class LTTngError(Exception):
-       pass
+    pass
+
+
 class BabeltraceError(Exception):
-       pass
+    pass
 
 
 # LTTNG-TOOLS
@@ -56,9 +59,9 @@ class BabeltraceError(Exception):
 lttng.destroy(ses_name)
 
 # Creating a new session and handle
-ret = lttng.create(ses_name,trace_path)
+ret = lttng.create(ses_name, trace_path)
 if ret < 0:
-       raise LTTngError(lttng.strerror(ret))
+    raise LTTngError(lttng.strerror(ret))
 
 domain = lttng.Domain()
 domain.type = lttng.DOMAIN_KERNEL
@@ -66,7 +69,7 @@ domain.type = lttng.DOMAIN_KERNEL
 han = None
 han = lttng.Handle(ses_name, domain)
 if han is None:
-       raise LTTngError("Handle not created")
+    raise LTTngError("Handle not created")
 
 
 # Enabling all events
@@ -75,24 +78,24 @@ event.type = lttng.EVENT_ALL
 event.loglevel_type = lttng.EVENT_LOGLEVEL_ALL
 ret = lttng.enable_event(han, event, None)
 if ret < 0:
-       raise LTTngError(lttng.strerror(ret))
+    raise LTTngError(lttng.strerror(ret))
 
 # Start, wait, stop
 ret = lttng.start(ses_name)
 if ret < 0:
-       raise LTTngError(lttng.strerror(ret))
+    raise LTTngError(lttng.strerror(ret))
 print("Tracing...")
 time.sleep(2)
 print("Stopped.")
 ret = lttng.stop(ses_name)
 if ret < 0:
-       raise LTTngError(lttng.strerror(ret))
+    raise LTTngError(lttng.strerror(ret))
 
 
 # Destroying tracing session
 ret = lttng.destroy(ses_name)
 if ret < 0:
-       raise LTTngError(lttng.strerror(ret))
+    raise LTTngError(lttng.strerror(ret))
 
 
 # BABELTRACE
@@ -101,7 +104,7 @@ if ret < 0:
 traces = babeltrace.TraceCollection()
 ret = traces.add_trace(trace_path + "/kernel", "ctf")
 if ret is None:
-       raise BabeltraceError("Error adding trace")
+    raise BabeltraceError("Error adding trace")
 
 # Reading events from trace
 # and outputting timestamps and event names
@@ -110,7 +113,8 @@ print("Writing trace file...")
 output = open(out_file, "wt")
 
 for event in traces.events:
-       output.write("TS: {}, {} : {}\n".format(event.timestamp, event.cycles, event.name))
+    output.write("TS: {}, {} : {}\n".format(
+        event.timestamp, event.cycles, event.name))
 
 # Closing file
 output.close()
index b33af931deebd8fd333348432dfe4708eae76997..178eed313c60ae6f3ec425f0eb52badee2127964 100644 (file)
@@ -58,7 +58,7 @@ uint16_type.signed = False
 packet_context_type = stream_class.packet_context_type
 print("\nFields in default packet context:")
 for field in packet_context_type.fields:
-               print(str(type(field[1])) + " " + field[0])
+    print(str(type(field[1])) + " " + field[0])
 packet_context_type.add_field(uint16_type, "a_custom_packet_context_field")
 stream_class.packet_context_type = packet_context_type
 
@@ -98,35 +98,35 @@ stream_class.add_event_class(event_class)
 stream = writer.create_stream(stream_class)
 
 for i in range(100):
-       event = CTFWriter.Event(event_class)
+    event = CTFWriter.Event(event_class)
 
-       clock.time = i * 1000
-       structure_field = event.payload("structure_field")
-       integer_field = structure_field.field("an_integer")
-       integer_field.value = i
+    clock.time = i * 1000
+    structure_field = event.payload("structure_field")
+    integer_field = structure_field.field("an_integer")
+    integer_field.value = i
 
-       string_field = structure_field.field("a_string_field")
-       string_field.value = "Test string."
+    string_field = structure_field.field("a_string_field")
+    string_field.value = "Test string."
 
-       float_field = event.payload("float_field")
-       float_field.value = float(i) + (float(i) / 100.0)
+    float_field = event.payload("float_field")
+    float_field.value = float(i) + (float(i) / 100.0)
 
-       array_field = event.payload("array_field")
-       for j in range(5):
-               element = array_field.field(j)
-               element.value = i + j
+    array_field = event.payload("array_field")
+    for j in range(5):
+        element = array_field.field(j)
+        element.value = i + j
 
-       event.payload("sequence_len").value = i % 10
-       sequence_field = event.payload("sequence_field")
-       sequence_field.length = event.payload("sequence_len")
-       for j in range(event.payload("sequence_len").value):
-               sequence_field.field(j).value = i + j
+    event.payload("sequence_len").value = i % 10
+    sequence_field = event.payload("sequence_field")
+    sequence_field.length = event.payload("sequence_len")
+    for j in range(event.payload("sequence_len").value):
+        sequence_field.field(j).value = i + j
 
-       enumeration_field = event.payload("enum_field")
-       integer_field = enumeration_field.container
-       enumeration_field.value = i % 10
+    enumeration_field = event.payload("enum_field")
+    integer_field = enumeration_field.container
+    enumeration_field.value = i % 10
 
-       stream.append_event(event)
+    stream.append_event(event)
 
 # Populate custom packet context field before flushing
 packet_context = stream.packet_context
index 56a8f88f4c84c1e58dad4a079eb47fca717b76b4..c1c6f42eac7ee1bd700de449ffbfdaf7c1663107 100644 (file)
@@ -25,36 +25,36 @@ from babeltrace import *
 
 # Check for path arg:
 if len(sys.argv) < 2:
-       raise TypeError("Usage: python example-api-test.py path/to/file")
+    raise TypeError("Usage: python example-api-test.py path/to/file")
 
 # Create TraceCollection and add trace:
 traces = TraceCollection()
 trace_handle = traces.add_trace(sys.argv[1], "ctf")
 if trace_handle is None:
-       raise IOError("Error adding trace")
+    raise IOError("Error adding trace")
 
 # Listing events
 print("--- Event list ---")
 for event_declaration in trace_handle.events:
-       print("event : {}".format(event_declaration.name))
-       if event_declaration.name == "sched_switch":
-               for field_declaration in event_declaration.fields:
-                       print(field_declaration)
+    print("event : {}".format(event_declaration.name))
+    if event_declaration.name == "sched_switch":
+        for field_declaration in event_declaration.fields:
+            print(field_declaration)
 print("--- Done ---")
 
 for event in traces.events:
-       print("TS: {}, {} : {}".format(event.timestamp, event.cycles, event.name))
-
-       if event.name == "sched_switch":
-               prev_comm = event["prev_comm"]
-               if prev_comm is None:
-                       print("ERROR: Missing prev_comm context info")
-               else:
-                       print("sched_switch prev_comm: {}".format(prev_comm))
-
-       if event.name == "exit_syscall":
-               ret_code = event["ret"]
-               if ret_code is None:
-                       print("ERROR: Unable to extract ret")
-               else:
-                       print("exit_syscall ret: {}".format(ret_code))
+    print("TS: {}, {} : {}".format(event.timestamp, event.cycles, event.name))
+
+    if event.name == "sched_switch":
+        prev_comm = event["prev_comm"]
+        if prev_comm is None:
+            print("ERROR: Missing prev_comm context info")
+        else:
+            print("sched_switch prev_comm: {}".format(prev_comm))
+
+    if event.name == "exit_syscall":
+        ret_code = event["ret"]
+        if ret_code is None:
+            print("ERROR: Unable to extract ret")
+        else:
+            print("exit_syscall ret: {}".format(ret_code))
index 8c475565ce7e56981036d20fe3e56d3db9fb38ad..99e818e290de8de98f1f30b23a85ef821ea52a60 100644 (file)
@@ -29,41 +29,41 @@ import sys
 from babeltrace import *
 
 if len(sys.argv) < 2 or len(sys.argv) > 3:
-       raise TypeError("Usage: python sched_switch.py [pid] path/to/trace")
+    raise TypeError("Usage: python sched_switch.py [pid] path/to/trace")
 elif len(sys.argv) == 3:
-       filterPID = True
+    filterPID = True
 else:
-       filterPID = False
+    filterPID = False
 
 traces = TraceCollection()
-ret = traces.add_trace(sys.argv[len(sys.argv)-1], "ctf")
+ret = traces.add_trace(sys.argv[len(sys.argv) - 1], "ctf")
 if ret is None:
-       raise IOError("Error adding trace")
+    raise IOError("Error adding trace")
 
 for event in traces.events:
-       if event.name != "sched_switch":
-               continue
+    if event.name != "sched_switch":
+        continue
 
-       # Getting PID
-       pid = event.field_with_scope("pid", CTFScope.STREAM_EVENT_CONTEXT)
-       if pid is None:
-               print("ERROR: Missing PID info for sched_switch")
-               continue # Next event
+    # Getting PID
+    pid = event.field_with_scope("pid", CTFScope.STREAM_EVENT_CONTEXT)
+    if pid is None:
+        print("ERROR: Missing PID info for sched_switch")
+        continue  # Next event
 
-       if filterPID and (pid != long(sys.argv[1])):
-               continue # Next event
+    if filterPID and (pid != long(sys.argv[1])):
+        continue  # Next event
 
-       prev_comm = event["prev_comm"]
-       prev_tid = event["prev_tid"]
-       prev_prio = event["prev_prio"]
-       prev_state = event["prev_state"]
-       next_comm = event["next_comm"]
-       next_tid = event["next_tid"]
-       next_prio = event["next_prio"]
+    prev_comm = event["prev_comm"]
+    prev_tid = event["prev_tid"]
+    prev_prio = event["prev_prio"]
+    prev_state = event["prev_state"]
+    next_comm = event["next_comm"]
+    next_tid = event["next_tid"]
+    next_prio = event["next_prio"]
 
-       # Output
-       print("sched_switch, pid = {}, TS = {}, prev_comm = {},\n\t"
-               "prev_tid = {}, prev_prio = {}, prev_state = {},\n\t"
-               "next_comm = {}, next_tid = {}, next_prio = {}".format(
-               pid, event.timestamp, prev_comm, prev_tid,
-               prev_prio, prev_state, next_comm, next_tid, next_prio))
+    # Output
+    print("sched_switch, pid = {}, TS = {}, prev_comm = {},\n\t"
+          "prev_tid = {}, prev_prio = {}, prev_state = {},\n\t"
+          "next_comm = {}, next_tid = {}, next_prio = {}".format(
+              pid, event.timestamp, prev_comm, prev_tid,
+              prev_prio, prev_state, next_comm, next_tid, next_prio))
index c6313b7e83ad0f943b089f4f81dc884689553e81..38cf0aa1a47084e1cd069dacac667fd055a38145 100644 (file)
@@ -27,32 +27,32 @@ from babeltrace import *
 
 # Check for path arg:
 if len(sys.argv) < 2:
-       raise TypeError("Usage: sequence_test.py path/to/file")
+    raise TypeError("Usage: sequence_test.py path/to/file")
 
 # Create TraceCollection and add trace:
 traces = TraceCollection()
 trace_handle = traces.add_trace(sys.argv[1], "ctf")
 if trace_handle is None:
-       raise IOError("Error adding trace")
+    raise IOError("Error adding trace")
 
 # Listing events
 print("--- Event list ---")
 for event_declaration in trace_handle.events:
-       print("event : {}".format(event_declaration.name))
+    print("event : {}".format(event_declaration.name))
 print("--- Done ---")
 
 for event in traces.events:
-       print("TS: {}, {} : {}".format(event.timestamp,
-               event.cycles, event.name))
-
-       try:
-               sequence = event["seq_int_field"]
-               print("int sequence values: {}". format(sequence))
-       except KeyError:
-               pass
-
-       try:
-               sequence = event["seq_long_field"]
-               print("long sequence values: {}". format(sequence))
-       except KeyError:
-               pass
+    print("TS: {}, {} : {}".format(event.timestamp,
+                                   event.cycles, event.name))
+
+    try:
+        sequence = event["seq_int_field"]
+        print("int sequence values: {}". format(sequence))
+    except KeyError:
+        pass
+
+    try:
+        sequence = event["seq_long_field"]
+        print("long sequence values: {}". format(sequence))
+    except KeyError:
+        pass
This page took 0.0318 seconds and 4 git commands to generate.