def main(argv):
    parser = argparse.ArgumentParser()
    parser.add_argument("--replay", help="Replay in 'real time'",
                        action="store_true")
    parser.add_argument("--current",
                        help="Use date adjusted from script start time.",
                        action="store_true")
    parser.add_argument("--incidents",
                        help="Whether to generate and publish (fake) " +
                        "traffic incidents. Requires a second PubSub topic " +
                        "to be specified.",
                        action="store_true")
    parser.add_argument("--random_delays",
                        help="Whether to randomly alter the data to " +
                        "sometimes introduce delays between log date and " +
                        "publish timestamp.",
                        action="store_true")
    parser.add_argument("--filename", help="input filename")
    parser.add_argument("--num_lines", type=int, default=0,
                        help="The number of lines to process. " +
                        "0 indicates all.")
    parser.add_argument("--topic", default=TRAFFIC_TOPIC,
                        help="The pubsub 'traffic' topic to publish to. " +
                        "Should already exist.")
    parser.add_argument("--incident_topic", default=INCIDENT_TOPIC,
                        help="The pubsub 'incident' topic to publish to. " +
                        "Only used if the --incidents flag is set. " +
                        "If so, should already exist.")
    args = parser.parse_args()

    pubsub_topic = args.topic
    print "Publishing to pubsub 'traffic' topic: %s" % pubsub_topic
    incidents = args.incidents
    random_delays = args.random_delays
    if incidents:
        incident_topic = args.incident_topic
        print "Publishing to pubsub 'incident' topic: %s" % incident_topic
    filename = args.filename
    print "filename: %s" % filename
    replay = args.replay
    print "replay mode: %s" % replay
    current = args.current
    print "current date mode: %s" % current
    num_lines = args.num_lines
    if num_lines:
        print "processing %s lines" % num_lines

    client = create_pubsub_client()
    dt = parse('01/01/2010 00:00:00')  # earliest date in the traffic files
    now = datetime.datetime.utcnow()
    # used if altering date to replay from start time
    diff = now - dt
    # used if running in 'replay' mode, reflecting pauses in the data
    prev_date = dt
    restart_time = now
    line_count = 0
    incident_count = 0

    print "processing %s" % filename  # process the traffic data file
    with open(filename) as data_file:
        reader = csv.reader(data_file)
        for line in reader:
            line_count += 1
            if num_lines:  # if terminating after num_lines processed
                if line_count >= num_lines:
                    print "Have processed %s lines" % num_lines
                    break
            if (line_count % LINE_BATCHES) == 0:
                print "%s lines processed" % line_count
            ts = ""
            try:
                timestring = line[0]
                orig_date = parse(timestring)
                if current:  # if using --current flag
                    (line, ts) = process_current_mode(
                        orig_date, diff, line, replay, random_delays)
                else:  # not using --current flag
                    (line, ts) = process_noncurrent_mode(
                        orig_date, line, random_delays)

                if replay and orig_date != prev_date:
                    date_delta = orig_date - prev_date
                    print "date delta: %s" % date_delta.total_seconds()
                    current_time = datetime.datetime.utcnow()
                    timelapse = current_time - restart_time
                    print "timelapse: %s" % timelapse.total_seconds()
                    d2 = date_delta - timelapse
                    sleeptime = d2.total_seconds()
                    print "sleeping %s" % sleeptime
                    time.sleep(sleeptime)
                    restart_time = datetime.datetime.utcnow()
                    print "restart_time is set to: %s" % restart_time
                prev_date = orig_date
                msg_attributes = {'timestamp': ts}
                publish(client, pubsub_topic, ",".join(line), msg_attributes)
                if incidents:  # if generating traffic 'incidents' as well
                    # randomly determine whether we'll generate an incident
                    # associated with this reading.
                    if random.random() < INCIDENT_THRESH:
                        print "Generating a traffic incident for %s." % line
                        # grab the timestring, station id, freeway, and
                        # direction of travel.
                        # Then generate some 'incident' data and publish it to
                        # the incident topic.  Use the incident count as a
                        # simplistic id.
                        incident_count += 1
                        publish_random_incident(client, incident_topic,
                                                incident_count,
                                                line[0], line[1], line[2],
                                                line[3], msg_attributes)
            except __HOLE__, e:
                sys.stderr.write("---Error: %s for %s\n" % (e, line))
dataset/ETHPy150Open GoogleCloudPlatform/cloud-pubsub-samples-python/gce-cmdline-publisher/traffic_pubsub_generator.py/main