#!/usr/bin/python

# google_page_count_to_tcgmsg.py
#       --copyright--                   Copyright 2007 (C) Tranzoa, Co. All rights reserved.    Warranty: You're free and on your own here. This code is not necessarily up-to-date or of public quality.
#       --url--                         http://www.tranzoa.net/tzpython/
#       --email--                       pycode is the name to send to. tranzoa.com is the place to send to.
#       --bodstamps--
#       February 14, 2005       bar
#       November 18, 2007       bar     turn on doxygen
#       November 27, 2007       bar     insert boilerplate copyright
#       May 17, 2008            bar     email adr
#       --eodstamps--
##      \file
#
#
#       Convert a file of Google page counts, as printed by GoogleSearch.py in to a file of tcg msgs.
#
#


if  __name__ == '__main__' :
    import  re
    import  sys

    import  tgcmsg
    import  TZCommandLineAtFile

    del(sys.argv[0])

    TZCommandLineAtFile.expand_at_sign_command_line_files(sys.argv)


    fld_re = re.compile(r"\+(?:\"([^\"]+)\"|(\S+))\s+(.+)")

    fi     = sys.stdin
    if  len(sys.argv) >= 1 :
        fi = open(sys.argv[0], "r")

    fo     = sys.stdout
    if  len(sys.argv) >= 2 :
        fo = open(sys.argv[1], "w")

    lnum   = 0
    while True :
        li  = fi.readline()
        if  not li :    break

        lnum += 1

        li    = li.strip()

        if  len(li) > 0 :

            if  not li.startswith("+") :
                print >> sys.stderr, "No starting plus sign character on line", lnum
                sys.exit(101)

            msg         = []
            have_count  = False
            while len(li) > 0 :

                gi  = 1
                g   = fld_re.match(li)
                if  g :
                    if  g.group(gi) == None :   gi = 2
                    li = g.group(3)
                else :
                    g = re.match(r"(\d+)$", li)
                    if  not g :
                        print >> sys.stderr, "Invalid count on line", lnum
                        sys.exit(102)
                    li         = ""
                    have_count = True

                msg.append(g.group(gi))

            if  not have_count :
                print >> sys.stderr, "No count on line", lnum
                sys.exit(103)

            if  len(msg) <= 0 :
                print >> sys.stderr, "Unparsable input on line", lnum
                sys.exit(104)

            print >> fo, tgcmsg.tgc_msg(msg)

        pass

    if  fi != sys.stdin :
        fi.close()

    if  fo != sys.stdout :
        fo.close()


    pass

#
#
#
# eof

