Back to index

plone3  3.1.7
fillmsgstr.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # encoding: utf-8
00003 
00004 #  fillmsgstr.py
00005 #  Created by Russ Ferriday 2006.03.28
00006 #  http://topia.com
00007 #  Copyright (c) 2006 Russ Ferriday - russf@topia.com 
00008 #
00009 #  This program is dedicated to Rhoslyn Prys and Emyr Thomas...
00010 #     Welcome to Plone! And thank you for Plone in Welsh!
00011 #
00012 #  Licenced under the FreeBSD licence:
00013 #  http://www.freebsd.org/copyright/freebsd-license.html
00014 #  (i.e. do with it what you want but keep this notice.)
00015 
00016 import os
00017 import sys
00018 import getopt
00019 import fileinput
00020 
00021 help_message = '''
00022 fillmsgstr [-h] [-r] [-v] [-o] <inputFile1> [ <inputFile2> [<inputFile3> ... ] ]
00023   -h : print this text
00024   -r : reverse the process
00025   -v : verbose -- a good way to get just the messages
00026   -o : output file -- defaults to <inputFile>.filled - Only works with single input file.
00027   <inputFile> : the file(s) you want to process or reverse process
00028   
00029   'Processing' takes the contents of all Default lines, and copies them
00030   into msgid lines, as a starting point for Translation Memory enabled tools.
00031   The previous contents of msgid being saved as #savedmsgid
00032   
00033   'Reverse Processing' renames #savedmsgid to msgid, and deletes the next msgid.
00034  
00035   So an example from the end of plone.po looks like this:
00036   
00037 #. Default: "You are here:"
00038 #: ./skins/plone_templates/global_pathbar.pt
00039 msgid "you_are_here"
00040 msgstr ""
00041 
00042   After running fillmsgstr.py on the plone.po file, the last entry will look like this in
00043   the newly created plone.po.filled.  
00044   
00045 #. Default: "You are here:"
00046 #: ./skins/plone_templates/global_pathbar.pt
00047 #savedmsgid "you_are_here"
00048 msgid "You are here:"
00049 msgstr ""
00050 
00051   Notice that the msgid, "you_are_here", has been relabeled #savedmsgid and
00052   the Default: value has been put in its place.
00053 
00054   Then the file is run through a translation tool. It's probably useful to
00055   rename the plone.po.filled file to plone.po before this, so the extension
00056   meets expectations. If the tool has a translation memory the english value
00057   of the msgid, "You are here:" is used as the lookup key. You can see how a
00058   msgid of "you_are_here" might not occur much in literature and would not be
00059   automatically translated by a TM tool.
00060 
00061 #. Default: "You are here:"
00062 #: ./skins/plone_templates/global_pathbar.pt
00063 #savedmsgid "you_are_here"
00064 msgid "You are here:"
00065 msgstr "Sie sind hier:"
00066 
00067   So finally we run fillmsgstr.py with the -r option, giving the plone.po file
00068   as the input file. The last entry in the resulting plone.po.unfilled file
00069   will look like this:
00070 
00071 #. Default: "You are here:"
00072 #: ./skins/plone_templates/global_pathbar.pt
00073 msgid "you_are_here"
00074 msgstr "Sie sind hier:"
00075 
00076   Notice that the original msgid has been replaced, and the savedmsgid has
00077   been removed.
00078 
00079   Copyright (c) 2006 Russ Ferriday - russf@topia.com 
00080 '''
00081 
00082 
00083 class Usage(Exception):
00084     def __init__(self, msg):
00085         self.msg = msg
00086 
00087 def dump(s):
00088     print '>>> %s' % s
00089 
00090 def main(argv=None):
00091     if argv is None:
00092         argv = sys.argv
00093     try:
00094         try:
00095             opts, args = getopt.getopt(argv[1:], "hro:v", 
00096                                        ["help", "output="])
00097         except getopt.error, msg:
00098              raise Usage(msg)
00099     
00100         # option processing
00101         verbose, output, infile, infilepath, reverse = \
00102           False, None, None, None, False
00103         for option, value in opts:
00104             if option == "-v":
00105                 verbose = True
00106             if option in ("-h", "--help"):
00107                 raise Usage(help_message)
00108             if option in ("-o", "--output"):
00109                 output = value
00110             if option in ("-r"):
00111                 reverse = True
00112         if len(args) < 1:
00113             print >> sys.stderr, "You forgot your input file..."
00114             raise Usage(help_message)
00115 
00116         if len(args) > 1 and output:
00117             print >> sys.stderr, "If you specify an output file, then you may only provide a single input file..."
00118             raise Usage(help_message)
00119                         
00120          
00121         for infilepath in args:
00122             if os.path.isfile(infilepath):
00123                 infile=fileinput.input(infilepath)
00124             else:
00125                 print >> sys.stderr, "Check your input file. %s Is it a text file?" % infilepath
00126                 raise Usage(help_message)
00127 
00128     except Usage, err:
00129         print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
00130         print >> sys.stderr, "\t for help use --help"
00131         return 2
00132     
00133     for infilepath in args:
00134         infile=fileinput.input(infilepath)        
00135 
00136         if output:
00137             outfilepath=output
00138         else:
00139             if reverse:
00140                 outfilepath=infilepath + '.unfilled'
00141             else:
00142                 outfilepath=infilepath + '.filled'
00143     
00144         outfile=open(outfilepath, 'w')
00145     
00146         defMark='#. Default:'
00147         msgidMark='msgid '
00148         msgstrMark='msgstr '
00149         savedMark='#saved'
00150         buff = None
00151         if reverse:
00152             for line in infile:
00153                 # convert the #savedmsgid lines back to msgid lines
00154                 # and swallow the next msgid, i.e. the fake one we created earlier
00155                 if line.startswith(savedMark):
00156                     buff=1          
00157                     # save this line, less the savedMark, until we see the msgstr line
00158                     delayedLine=line[len(savedMark):]
00159                     continue
00160                 if not buff:
00161                     outfile.write(line)
00162                     continue
00163                 # ok, so we're buffering, which means...
00164                 #   Delete next msgid, by omission, and drop the delayedLine in its place.
00165                 #   This ensures it appears after any comments that it may been 
00166                 #   moved ahead of by some translation memory tools.
00167                 if line.startswith(msgidMark): 
00168                     buff=None
00169                     outfile.write(delayedLine)
00170                     continue
00171                 # but meanwhile other lines remain unchanged
00172                 outfile.write(line)
00173         else:
00174             for line in infile:
00175                 if line.startswith(defMark):
00176                     buff=line[len(defMark):]
00177                     outfile.write(line)
00178                     continue
00179                 if not buff:
00180                     outfile.write(line)
00181                     continue
00182                 # ok, so we're buffering, which means...
00183         
00184                 # we rename msgid to savedmsgid
00185                 # and if the msgid is cryptic (different from buff) write buff 
00186                 # as msgid, else write line
00187                 if line.startswith(msgidMark):
00188                     outfile.write('#saved%s' % line)
00189                     if line[len(msgidMark):] <> buff:
00190                         oline='msgid%s' % buff
00191                     else:
00192                         oline=line
00193                     outfile.write(oline)
00194                     buff=None
00195                     if verbose:
00196                         dump(oline)            
00197                     continue
00198                 outfile.write(line)
00199         
00200         outfile.close()
00201         infile.close()
00202     
00203 
00204 if __name__ == "__main__":
00205     sys.exit(main())