Skip to content

Commit

Permalink
autopep8
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Lautman committed Oct 20, 2018
1 parent 6c5acfd commit bda7764
Show file tree
Hide file tree
Showing 22 changed files with 249 additions and 136 deletions.
6 changes: 3 additions & 3 deletions rqt_bag/src/rqt_bag/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
# POSSIBILITY OF SUCH DAMAGE.

from rqt_bag import bag_helper
from rqt_bag.plugins.message_view import MessageView
from rqt_bag.plugins.message_view import MessageView
from rqt_bag.plugins.topic_message_view import TopicMessageView
from rqt_bag.plugins.timeline_renderer import TimelineRenderer
from rqt_bag.timeline_cache import TimelineCache
from rqt_bag.plugins.timeline_renderer import TimelineRenderer
from rqt_bag.timeline_cache import TimelineCache
12 changes: 8 additions & 4 deletions rqt_bag/src/rqt_bag/bag.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@

from .bag_widget import BagWidget


class Bag(Plugin):

"""
Subclass of Plugin to provide interactive bag visualization, playing(publishing) and recording
"""

def __init__(self, context):
"""
:param context: plugin context hook to enable adding widgets as a ROS_GUI pane, ''PluginContext''
Expand All @@ -53,13 +56,14 @@ def __init__(self, context):

self._widget = BagWidget(context, args.clock)
if context.serial_number() > 1:
self._widget.setWindowTitle(self._widget.windowTitle() + (' (%d)' % context.serial_number()))
self._widget.setWindowTitle(
self._widget.windowTitle() + (' (%d)' % context.serial_number()))
context.add_widget(self._widget)

def load_bags():
for bagfile in args.bagfiles:
self._widget.load_bag(bagfile)

load_thread = threading.Thread(target=load_bags)
load_thread.start()

Expand All @@ -73,7 +77,7 @@ def _isfile(parser, arg):
if os.path.isfile(arg):
return arg
else:
parser.error("Bag file %s does not exist" % ( arg ))
parser.error("Bag file %s does not exist" % (arg))

@staticmethod
def add_arguments(parser):
Expand All @@ -95,5 +99,5 @@ def restore_settings(self, plugin_settings, instance_settings):
# v = instance_settings.value(k)
pass

#def trigger_configuration(self):
# def trigger_configuration(self):
# TODO move some of the button functionality to config button if it is "more configy"
1 change: 1 addition & 0 deletions rqt_bag/src/rqt_bag/bag_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import time
import rospy


def stamp_to_str(t):
"""
Convert a rospy.Time to a human-readable string.
Expand Down
68 changes: 43 additions & 25 deletions rqt_bag/src/rqt_bag/bag_timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,19 @@


class BagTimeline(QGraphicsScene):

"""
BagTimeline contains bag files, all information required to display the bag data visualization on the screen
Also handles events
BagTimeline contains bag files, all information required to display the bag data visualization
on the screen Also handles events
"""
status_bar_changed_signal = Signal()
selected_region_changed = Signal(rospy.Time, rospy.Time)

def __init__(self, context, publish_clock):
"""
:param context: plugin context hook to enable adding rqt_bag plugin widgets as ROS_GUI snapin panes, ''PluginContext''
:param context:
plugin context hook to enable adding rqt_bag plugin widgets as ROS_GUI snapin panes,
''PluginContext''
"""
super(BagTimeline, self).__init__()
self._bags = []
Expand Down Expand Up @@ -99,8 +102,9 @@ def __init__(self, context, publish_clock):
self._listeners = {}

# Initialize scene
# the timeline renderer fixes use of black pens and fills, so ensure we fix white here for contrast.
# otherwise a dark qt theme will default it to black and the frame render pen will be unreadable
# the timeline renderer fixes use of black pens and fills, so ensure we fix white here for
# contrast. Otherwise a dark qt theme will default it to black and the frame render
# pen will be unreadable
self.setBackgroundBrush(Qt.white)
self._timeline_frame = TimelineFrame(self)
self._timeline_frame.setPos(0, 0)
Expand Down Expand Up @@ -179,7 +183,7 @@ def file_size(self):
with self._bag_lock:
return sum(b.size for b in self._bags)

#TODO Rethink API and if these need to be visible
# TODO Rethink API and if these need to be visible
def _get_start_stamp(self):
"""
:return: first stamp in the bags, ''rospy.Time''
Expand All @@ -188,7 +192,8 @@ def _get_start_stamp(self):
start_stamp = None
for bag in self._bags:
bag_start_stamp = bag_helper.get_start_stamp(bag)
if bag_start_stamp is not None and (start_stamp is None or bag_start_stamp < start_stamp):
if bag_start_stamp is not None and \
(start_stamp is None or bag_start_stamp < start_stamp):
start_stamp = bag_start_stamp
return start_stamp

Expand Down Expand Up @@ -236,7 +241,8 @@ def get_datatype(self, topic):
for bag in self._bags:
bag_datatype = bag_helper.get_datatype(bag, topic)
if datatype and bag_datatype and (bag_datatype != datatype):
raise Exception('topic %s has multiple datatypes: %s and %s' % (topic, datatype, bag_datatype))
raise Exception('topic %s has multiple datatypes: %s and %s' %
(topic, datatype, bag_datatype))
if bag_datatype:
datatype = bag_datatype
return datatype
Expand Down Expand Up @@ -322,7 +328,7 @@ def get_entry_before(self, t):
with self._bag_lock:
entry_bag, entry = None, None
for bag in self._bags:
bag_entry = bag._get_entry(t-rospy.Duration(0,1), bag._get_connections())
bag_entry = bag._get_entry(t - rospy.Duration(0, 1), bag._get_connections())
if bag_entry and (not entry or bag_entry.time < entry.time):
entry_bag, entry = bag, bag_entry

Expand Down Expand Up @@ -373,15 +379,17 @@ def resume(self):
if (self._player):
self._player.resume()

### Copy messages to...
# Copy messages to...

def start_background_task(self, background_task):
"""
Verify that a background task is not currently running before starting a new one
:param background_task: name of the background task, ''str''
"""
if self.background_task is not None:
QMessageBox(QMessageBox.Warning, 'Exclamation', 'Background operation already running:\n\n%s' % self.background_task, QMessageBox.Ok).exec_()
QMessageBox(
QMessageBox.Warning, 'Exclamation', 'Background operation already running:\n\n%s' %
self.background_task, QMessageBox.Ok).exec_()
return False

self.background_task = background_task
Expand All @@ -393,7 +401,9 @@ def stop_background_task(self):

def copy_region_to_bag(self, filename):
if len(self._bags) > 0:
self._export_region(filename, self._timeline_frame.topics, self._timeline_frame.play_region[0], self._timeline_frame.play_region[1])
self._export_region(filename, self._timeline_frame.topics,
self._timeline_frame.play_region[0],
self._timeline_frame.play_region[1])

def _export_region(self, path, topics, start_stamp, end_stamp):
"""
Expand Down Expand Up @@ -424,12 +434,15 @@ def _export_region(self, path, topics, start_stamp, end_stamp):
try:
export_bag = rosbag.Bag(path, 'w')
except Exception:
QMessageBox(QMessageBox.Warning, 'rqt_bag', 'Error opening bag file [%s] for writing' % path, QMessageBox.Ok).exec_()
QMessageBox(QMessageBox.Warning, 'rqt_bag',
'Error opening bag file [%s] for writing' % path, QMessageBox.Ok).exec_()
self.stop_background_task()
return

# Run copying in a background thread
self._export_thread = threading.Thread(target=self._run_export_region, args=(export_bag, topics, start_stamp, end_stamp, bag_entries))
self._export_thread = threading.Thread(
target=self._run_export_region,
args=(export_bag, topics, start_stamp, end_stamp, bag_entries))
self._export_thread.start()

def _run_export_region(self, export_bag, topics, start_stamp, end_stamp, bag_entries):
Expand All @@ -452,7 +465,8 @@ def _run_export_region(self, export_bag, topics, start_stamp, end_stamp, bag_ent
topic, msg, t = self.read_message(bag, entry.position)
export_bag.write(topic, msg, t)
except Exception as ex:
qWarning('Error exporting message at position %s: %s' % (str(entry.position), str(ex)))
qWarning('Error exporting message at position %s: %s' %
(str(entry.position), str(ex)))
export_bag.close()
self.stop_background_task()
return
Expand All @@ -473,14 +487,15 @@ def _run_export_region(self, export_bag, topics, start_stamp, end_stamp, bag_ent
self.status_bar_changed_signal.emit()
export_bag.close()
except Exception as ex:
QMessageBox(QMessageBox.Warning, 'rqt_bag', 'Error closing bag file [%s]: %s' % (export_bag.filename, str(ex)), QMessageBox.Ok).exec_()
QMessageBox(QMessageBox.Warning, 'rqt_bag', 'Error closing bag file [%s]: %s' % (
export_bag.filename, str(ex)), QMessageBox.Ok).exec_()
self.stop_background_task()

def read_message(self, bag, position):
with self._bag_lock:
return bag._read_message(position)

### Mouse events
# Mouse events
def on_mouse_down(self, event):
if event.buttons() == Qt.LeftButton:
self._timeline_frame.on_left_down(event)
Expand Down Expand Up @@ -516,7 +531,7 @@ def translate_timeline_left(self):
def translate_timeline_right(self):
self._timeline_frame.translate_timeline_right()

### Publishing
# Publishing
def is_publishing(self, topic):
return self._player and self._player.is_publishing(topic)

Expand Down Expand Up @@ -574,7 +589,7 @@ def _set_play_all(self, play_all):
def toggle_play_all(self):
self.play_all = not self.play_all

### Playing
# Playing
def on_idle(self):
self._step_playhead()

Expand Down Expand Up @@ -608,7 +623,8 @@ def step_fixed(self):
if self.stick_to_end:
new_playhead = self.end_stamp
else:
new_playhead = self._timeline_frame.playhead + rospy.Duration.from_sec((now - self.last_frame).to_sec() * self.play_speed)
new_playhead = self._timeline_frame.playhead + \
rospy.Duration.from_sec((now - self.last_frame).to_sec() * self.play_speed)

start_stamp, end_stamp = self._timeline_frame.play_region

Expand Down Expand Up @@ -668,11 +684,12 @@ def step_next_message(self):
self.last_frame = rospy.Time.from_sec(time.time())
self.last_playhead = self._timeline_frame.playhead

### Recording
# Recording

def record_bag(self, filename, all=True, topics=[], regex=False, limit=0):
try:
self._recorder = Recorder(filename, bag_lock=self._bag_lock, all=all, topics=topics, regex=regex, limit=limit)
self._recorder = Recorder(
filename, bag_lock=self._bag_lock, all=all, topics=topics, regex=regex, limit=limit)
except Exception as ex:
qWarning('Error opening bag for recording [%s]: %s' % (filename, str(ex)))
return
Expand Down Expand Up @@ -724,7 +741,7 @@ def _message_recorded(self, topic, msg, t):
except Exception as ex:
qWarning('Error calling timeline_changed on %s: %s' % (type(listener), str(ex)))

### Views / listeners
# Views / listeners
def add_view(self, topic, frame):
self._views.append(frame)

Expand All @@ -734,7 +751,8 @@ def has_listeners(self, topic):
def add_listener(self, topic, listener):
self._listeners.setdefault(topic, []).append(listener)

self._message_listener_threads[(topic, listener)] = MessageListenerThread(self, topic, listener)
self._message_listener_threads[(topic, listener)] = \
MessageListenerThread(self, topic, listener)
# Notify the message listeners
self._message_loaders[topic].reset()
with self._playhead_positions_cvs[topic]:
Expand All @@ -756,7 +774,7 @@ def remove_listener(self, topic, listener):
del self._message_listener_threads[(topic, listener)]
self.update()

### Playhead
# Playhead

# property: play_speed
def _get_play_speed(self):
Expand Down
Loading

0 comments on commit bda7764

Please sign in to comment.