#! /usr/bin/env python
# Greenbone Security Assistant
# $Id$
# Description: Utility to generate po files from gsa xml i18n
#
# Authors:
#  Benoit Allard <benoit.allard@greenbone.net>
#
# Copyright:
#  Copyright (C) 2014-2015 Greenbone Networks GmbH
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2,
# or, at your option, any later version as published by the Free
# Software Foundation
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.

import os
import xml.parsers.expat

import polib

CHUNK_SIZE = 2048

in_context = False
ctxdata = ''
context = []
in_msgid = False
msgid = ''
in_msgstr = False
msgstr = ''
po = None

def start_element(name, attrs):
    if name == "ctxt":
        global in_context
        in_context = True
    elif name == "id":
        global in_msgid
        in_msgid = True
    elif name == "str":
        global in_msgstr
        in_msgstr = True

def end_element(name):
    global in_context, in_msgid, in_msgstr
    in_context = False
    in_msgid = False
    in_msgstr = False
    if name == "grp":
        """ reset the context at the end of a group """
        global context
        context = []
    elif name == 'ctxt':
        context.append(ctxdata)
        global ctxdata
        ctxdata = ''
    elif name == "msg":
        """ Add an entry """
        for ctx in context:
            e = po.find(msgid, msgctxt=ctx)
            if e is not None:
                print "Found duplicate string translation: %s (ctxt: %s)" % (msgid, ctx)
            else:
                po.append(polib.POEntry(msgid=msgid, msgstr=msgstr,
                                        msgctxt=ctx))
        global msgid, msgstr
        msgid = ''
        msgstr = ''


def char_data(data):
    if in_context:
        global ctxdata
        ctxdata += data
    elif in_msgid:
        global msgid
        msgid += data
    elif in_msgstr:
        global msgstr
        msgstr += data

def main(xmlfilepath, pofilepath=None):
    # Create a parser
    p = xml.parsers.expat.ParserCreate()

    p.StartElementHandler = start_element
    p.EndElementHandler = end_element
    p.CharacterDataHandler = char_data

    global po
    po = polib.POFile()
    po.metadata = {
	'MIME-Version': '1.0',
        'Content-Type': 'text/plain; charset=utf-8',
        'Content-Transfer-Encoding': '8bit',
    }

    with open(xmlfilepath, 'rt') as xml_file:
        while True:
            # Read a chunk
            chunk = xml_file.read(CHUNK_SIZE)
            if len(chunk) < CHUNK_SIZE:
                # End of file
                # tell the parser we're done
                p.Parse(chunk, 1)
                # exit the loop
                break
            # process the chunk
            p.Parse(chunk)
            
    # And save our file
    if pofilepath is None:
        pofilepath = os.path.join(os.path.dirname(xmlfilepath),
            os.path.splitext(os.path.basename(xmlfilepath))[0] + '.po')

    po.save(pofilepath)


if __name__ == "__main__":
    import sys
    if len(sys.argv) == 1:
        print "Usage: %s XMLFILE [POFILE]" % sys.argv[0]
        sys.exit(1)
    main(*sys.argv[1:])
