#!/usr/bin/env python

###############################################################################
# Copyright 2017 The Apollo Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###############################################################################
"""
This program can replay a planning output pb file via ros
"""
import os.path
import sys
import argparse
import rospy
from std_msgs.msg import String
from google.protobuf import text_format

import common.message_manager as message_manager


def generate_message(filename, pb_type):
    f_handle = file(filename, 'r')
    message = pb_type()
    text_format.Merge(f_handle.read(), message)
    f_handle.close()
    return message


def seq_publisher(seq_num, period):
    """publisher"""
    rospy.init_node('replay_node', anonymous=True)
    messages = {}
    for msg in message_manager.topic_pb_list:
        topic = msg.topic
        name = msg.name
        msg_type = msg.msg_type
        messages[topic] = {}
        filename = str(seq_num) + "_" + name + ".pb.txt"
        print "trying to load pb file:", filename
        messages[topic]["publisher"] = rospy.Publisher(
            topic, msg_type, queue_size=1)
        pb_msg = msg.parse_file(filename)
        if not pb_msg:
            print topic, " pb is none"
            # continue
        messages[topic]["value"] = pb_msg

    rate = rospy.Rate(int(1.0 / period))  # 10hz
    while not rospy.is_shutdown():
        for topic in messages:
            if messages[topic]["value"] is not None:
                print "publish: ", topic
                messages[topic]["publisher"].publish(messages[topic]["value"])
        rate.sleep()


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description="replay a set of pb files with the same sequence number")
    parser.add_argument(
        "seq",
        action="store",
        type=int,
        default=-1,
        help="set sequence number to replay")
    parser.add_argument(
        "--period",
        action="store",
        type=float,
        default=1,
        help="set the topic publish time duration")
    args = parser.parse_args()
    try:
        seq_publisher(args.seq, args.period)

    except rospy.ROSInterruptException:
        print "failed to replay message"

@@ -1070,23 +1070,17 @@
     
-f_handle = file
+with open
 (fil
@@ -1090,17 +1090,28 @@
 me, 'r')
-%0A
+ as fp:%0A    
     mess
@@ -1130,16 +1130,20 @@
 e()%0A    
+  
+  
 text_for
@@ -1153,23 +1153,17 @@
 .Merge(f
-_handle
+p
 .read(),
@@ -1176,29 +1176,8 @@
 ge)%0A
-    f_handle.close()%0A
     
@@ -1192,17 +1192,16 @@
 essage%0A%0A
-%0A
 def seq_
@@ -1248,16 +1248,17 @@
 sher%22%22%22%0A
+%0A
     rosp
@@ -1545,18 +1545,18 @@
    print
- %22
+('
 trying t
@@ -1570,18 +1570,22 @@
 pb file:
-%22,
+ %25s' %25
  filenam
@@ -1581,24 +1581,25 @@
 ' %25 filename
+)
 %0A        mes
@@ -1772,17 +1772,12 @@
 rint
- topic, %22
+('%25s
  pb 
@@ -1783,17 +1783,26 @@
  is none
-%22
+' %25 topic)
 %0A       
@@ -2049,18 +2049,18 @@
    print
- %22
+('
 publish:
@@ -2064,16 +2064,20 @@
 sh: 
-%22,
+%25s' %25
  topic
+)
 %0A   
@@ -2174,17 +2174,16 @@
 leep()%0A%0A
-%0A
 if __nam
@@ -2768,11 +2768,11 @@
 rint
- %22f
+('F
 aile
@@ -2790,10 +2790,12 @@
  message
-%22
+.')
 %0A

rewrite generate_message