
import codecs, polib

from fileutil import ensure_dir
from flowutil import makeRandomTextFlows

outputDir = 'output/po/'


def generatePoFiles(names, locales, numFlows, contentLength):
    """Generates a mock pot file for each name with associated mock po files for each locale"""
    
    ensure_dir(outputDir)
    
    for name in names:
        flows = makeRandomTextFlows(locales, name, numFlows, contentLength)
        
        #write the source file
        f = polib.POFile()
        #TODO write some sort of header (in targets too)
        # see here for header info http://translate.sourceforge.net/wiki/guide/project/howto
        addTextFlows(toFile=f, textFlows=flows)
        savePoFile(f, outputDir, name, None)
        
        #write the targets
        for locale in locales:
            f = polib.POFile()
            f.encoding='utf-8'
            addTextFlows(toFile=f, textFlows=flows, locale=locale)
            savePoFile(f, outputDir, name, locale)


def addTextFlows(toFile, textFlows, locale=None):
    """Writes text flows. If no locale is specified, source is assumed."""
    
    isSource = (locale is None)
    
    for flow in textFlows:
        poentry = polib.POEntry()
        poentry.msgid = flow.content
        poentry.occurrences = flow.occurrences
        poentry.comment = flow.extracted_comment


        if isSource:
            poentry.msgstr = ''
        else:
            target = flow.getTarget(locale)
            poentry.msgstr = target.content
            poentry.tcomment = target.comment
            poentry.flags = target.flags
            
        toFile.append(poentry)
    



#TODO add options for subdirs and for whether to append locale to name
#TODO put the dir and locale appending stuff in fileutil
#TODO add encoding option here - aim is to support all encodings listed here:
# http://www.gnu.org/software/gettext/manual/gettext.html#index-charset-of-PO-files-268
def savePoFile(pofile, path, name, locale):
    """Writes a po file with the given name, in a directory
    for the locale and with a po or pot extension added."""
    
    if locale is not None:
        path += locale + '/'
        name = name + '.po'
    else:
        name = name + '.pot'

    ensure_dir(path)
    
    #TODO this currently lacks an encoding header, could be an issue
    pofile.save(path+name)



@@ -16,16 +16,46 @@
  polib%0A%0A
+from datetime import datetime%0A
 from fil
@@ -519,149 +519,125 @@
     
-#TODO write some sort of header (in targets too)%0A        # see here for header info http://translate.sourceforge.net/wiki/guide/project/howto
+header = makePoHeader(name)%0A        f.header = header%0A        meta = makePoMetadata()%0A        f.metadata.update(meta)
 %0A   
@@ -854,16 +854,117 @@
 ='utf-8'
+ #TODO is this needed?%0A            f.header = header%0A            f.metadata.update(meta)%0A            
 %0A       
@@ -1076,16 +1076,1283 @@
 cale)%0A%0A%0A
+# see here for header info http://translate.sourceforge.net/wiki/guide/project/howto%0A%0Adef makePoHeader(name):%0A    header = %22Generated title for %25(name)s%5Cn%22%0A    header += %22Copyright (C) %25(name)s %25(year)s.%5Cn%22%0A    header += %22This file is distributed under the same license as the %25(name)s package.%5Cn%22%0A    header += %22Gibberer Gibberer gibberer@example.com, %25(year)s%5Cn%22%0A    return header %25 %7B'name': name, 'year': datetime.now().year%7D%0A    %0ATIME_FORMAT = '%25Y-%25m-%25dT%25H:%25M:%25S'%0A%0Adef makePoMetadata():%0A    %22%22%22Pass the po file's metadata to this function to have the values set%22%22%22%0A    %0A    meta = %7B%0A        u'Project-Id-Version' : u'0',%0A        u'Report-Msgid-Bugs-To' : u'gibberer@example.com',%0A        u'POT-Creation-Date' : unicode(datetime.now().strftime(TIME_FORMAT)),%0A        u'PO-Revision-Date' : unicode(datetime.now().strftime(TIME_FORMAT)),%0A        u'Last-Translator' : u'The Gibberer',%0A        u'Language-Team' : u'Gibberish Team',%0A        u'MIME-Version' : u'1.0',%0A        u'Content-Type' : u'application/x-publican; charset=UTF-8',%0A        u'Content-Transfer-Encoding' : u'8bit',%0A        u'Plural-Forms' : u'nplurals=2; plural=n != 1;'%0A    %7D%0A    %0A    #could make plural forms correct for different locales. Not worth the bother at the moment%0A    %0A    return meta%0A%0A%0A%0A
 def addT

add headers and metadata to po format output