Changeset 104

Show
Ignore:
Timestamp:
Thu Aug 18 04:01:26 2005
Author:
fredrikc
Message:

Made all end of line chars Unix style.

Files:

Legend:

Unmodified
Added
Removed
Modified
  • trunk/transolution/tmx/xliff2tmx.py

    r73 r104  
    3 3 import sys  
    4 4 import codecs  
    5   from transolution.path_mod import path  
    6   from transolution.xmlutils import createNodeAttr,NodeCreator,createDocument,EMPTY_NAMESPACE,XML_NAMESPACE  
    7   from transolution.xliffeditor.document.segments import TuSegment,External  
    8   from transolution.xliffeditor.xliffparser_ng import XliffDocument,XliffException  
    9    
      5 from transolution.path_mod import path  
      6 from transolution.xmlutils import createNodeAttr,NodeCreator,createDocument,EMPTY_NAMESPACE,XML_NAMESPACE  
      7 from transolution.xliffeditor.document.segments import TuSegment,External  
      8 from transolution.xliffeditor.xliffparser_ng import XliffDocument,XliffException  
      9  
    10 10 src_lang = u"en"  
    11 11 trg_lang = u"sv-se"  
     
    13 13 def addTMXHeader(doc,parent,entry):  
    14 14     header = doc.createElementNS(EMPTY_NAMESPACE,"header")  
    15   ##    if len(entry.xlatComment) > 0:  
    16   ##        note = doc.createElementNS(EMPTY_NAMESPACE,"note")  
    17   ##        str_note = u"".join(entry.xlatComment).strip()  
    18   ##        note_txt = doc.createTextNode(str_note)  
    19   ##        note.appendChild(note_txt)  
    20   ##        header.appendChild(note)  
    21   ##        prop = doc.createElementNS(EMPTY_NAMESPACE,"prop")  
    22   ##        prop.setAttributeNS(EMPTY_NAMESPACE,"type","x-gettext-pi")  
    23   ##        str_prop = u"".join(entry.procComment).strip()  
    24   ##        prop_txt = doc.createTextNode(str_prop)  
    25   ##        prop.appendChild(prop_txt)  
    26   ##        header.appendChild(prop)  
      15 ##    if len(entry.xlatComment) > 0:  
      16 ##        note = doc.createElementNS(EMPTY_NAMESPACE,"note")  
      17 ##        str_note = u"".join(entry.xlatComment).strip()  
      18 ##        note_txt = doc.createTextNode(str_note)  
      19 ##        note.appendChild(note_txt)  
      20 ##        header.appendChild(note)  
      21 ##        prop = doc.createElementNS(EMPTY_NAMESPACE,"prop")  
      22 ##        prop.setAttributeNS(EMPTY_NAMESPACE,"type","x-gettext-pi")  
      23 ##        str_prop = u"".join(entry.procComment).strip()  
      24 ##        prop_txt = doc.createTextNode(str_prop)  
      25 ##        prop.appendChild(prop_txt)  
      26 ##        header.appendChild(prop)  
    27 27     parent.appendChild(header)  
    28 28      
     
    32 32 def  addTMXTu(doc,parent,entry):  
    33 33     tu = doc.createElementNS(EMPTY_NAMESPACE,"tu")  
    34   ##    if len(entry.xlatComment) > 0:  
    35   ##        note = doc.createElementNS(EMPTY_NAMESPACE,"note")  
    36   ##        str_note = u"".join(entry.xlatComment).strip()  
    37   ##        note_txt = doc.createTextNode(str_note)  
    38   ##        note.appendChild(note_txt)  
    39   ##        tu.appendChild(note)  
    40   ##    if len(entry.autoComment) > 0:  
    41   ##        note = doc.createElementNS(EMPTY_NAMESPACE,"note")  
    42   ##        str_note = u"".join(entry.autoComment).strip()  
    43   ##        note_txt = doc.createTextNode(str_note)  
    44   ##        note.appendChild(note_txt)  
    45   ##        tu.appendChild(note)  
    46   ##    if len(entry.refComment) > 0:  
    47   ##        prop = doc.createElementNS(EMPTY_NAMESPACE,"prop")  
    48   ##        prop.setAttributeNS(EMPTY_NAMESPACE,"type","x-gettext-ref")  
    49   ##        str_prop = u"".join(entry.refComment).strip()  
    50   ##        prop_txt = doc.createTextNode(str_prop)  
    51   ##        prop.appendChild(prop_txt)  
    52   ##        tu.appendChild(prop)  
    53   ##    if len(entry.procComment) > 0:  
    54   ##        prop = doc.createElementNS(EMPTY_NAMESPACE,"prop")  
    55   ##        prop.setAttributeNS(EMPTY_NAMESPACE,"type","x-gettext-pi")  
    56   ##        str_prop = u"".join(entry.procComment).strip()  
    57   ##        prop_txt = doc.createTextNode(str_prop)  
    58   ##        prop.appendChild(prop_txt)  
    59   ##        tu.appendChild(prop)  
      34 ##    if len(entry.xlatComment) > 0:  
      35 ##        note = doc.createElementNS(EMPTY_NAMESPACE,"note")  
      36 ##        str_note = u"".join(entry.xlatComment).strip()  
      37 ##        note_txt = doc.createTextNode(str_note)  
      38 ##        note.appendChild(note_txt)  
      39 ##        tu.appendChild(note)  
      40 ##    if len(entry.autoComment) > 0:  
      41 ##        note = doc.createElementNS(EMPTY_NAMESPACE,"note")  
      42 ##        str_note = u"".join(entry.autoComment).strip()  
      43 ##        note_txt = doc.createTextNode(str_note)  
      44 ##        note.appendChild(note_txt)  
      45 ##        tu.appendChild(note)  
      46 ##    if len(entry.refComment) > 0:  
      47 ##        prop = doc.createElementNS(EMPTY_NAMESPACE,"prop")  
      48 ##        prop.setAttributeNS(EMPTY_NAMESPACE,"type","x-gettext-ref")  
      49 ##        str_prop = u"".join(entry.refComment).strip()  
      50 ##        prop_txt = doc.createTextNode(str_prop)  
      51 ##        prop.appendChild(prop_txt)  
      52 ##        tu.appendChild(prop)  
      53 ##    if len(entry.procComment) > 0:  
      54 ##        prop = doc.createElementNS(EMPTY_NAMESPACE,"prop")  
      55 ##        prop.setAttributeNS(EMPTY_NAMESPACE,"type","x-gettext-pi")  
      56 ##        str_prop = u"".join(entry.procComment).strip()  
      57 ##        prop_txt = doc.createTextNode(str_prop)  
      58 ##        prop.appendChild(prop_txt)  
      59 ##        tu.appendChild(prop)  
    60 60     tuv = doc.createElementNS(EMPTY_NAMESPACE,"tuv")  
    61 61     tuv.setAttributeNS(XML_NAMESPACE,'xml:lang',src_lang)  
    62       seg = doc.createElementNS(EMPTY_NAMESPACE,"seg")  
    63       for node in entry.tu.getSourceNode().childNodes:  
    64           seg.appendChild(node.cloneNode(True))  
      62     seg = doc.createElementNS(EMPTY_NAMESPACE,"seg")  
      63     for node in entry.tu.getSourceNode().childNodes:  
      64         seg.appendChild(node.cloneNode(True))  
    65 65  
    66 66     tuv.appendChild(seg)  
     
    69 69     tuv.setAttributeNS(XML_NAMESPACE,'xml:lang',trg_lang)  
    70 70     seg = doc.createElementNS(EMPTY_NAMESPACE,"seg")  
    71       for node in entry.tu.getTargetNode().childNodes:  
    72           seg.appendChild(node.cloneNode(True))  
    73    
      71     for node in entry.tu.getTargetNode().childNodes:  
      72         seg.appendChild(node.cloneNode(True))  
      73  
    74 74     tuv.appendChild(seg)  
    75 75     tu.appendChild(tuv)  
    76 76      
    77 77     parent.appendChild(tu)  
    78    
    79    
    80   def parseArgs():  
    81       import sys  
    82       from optparse import OptionParser  
    83    
    84       usage = "usage: %prog [options] path out_tmx"  
    85    
    86       op = OptionParser(usage=usage)  
    87       op.add_option('-r', '--recursive',  
    88                     action='store_true', dest='recursive',  
    89                     help='Process files recursive.')  
    90       op.add_option('-f', '--fmask',  
    91                     action='store', type='string', dest='fmask',  
    92                     help='File mask to use when running recursive.')  
    93    
    94    
    95       options, args = op.parse_args(sys.argv[1:])  
    96       try:  
    97           pname = path(args[0])  
    98           outfile = path(args[1])  
    99       except IndexError,e:  
    100           op.error("Invalid number if arguments, use -h for help")  
    101    
    102            
    103       if options.recursive:  
    104           if not options.fmask:  
    105               op.error("must give filemask option to when recurse")  
    106           else:  
    107               fnames = pname.walkfiles(options.fmask)  
    108       else:  
    109           if options.fmask:  
    110               if not pname.isdir():  
    111                   op.error("must give a directory path when option fmask is given")  
    112               else:  
    113                   fnames = pname.listdir(options.fmask)  
    114           else:  
    115               fnames = [pname,]  
    116    
    117    
    118       options.fnames = fnames  
    119       options.outfile = outfile  
    120       return options  
    121    
    122    
    123   def main():  
    124       import sys  
    125       import codecs  
    126       import transolution  
    127       global src_lang,trg_lang  
    128        
    129       options = parseArgs()  
    130    
      78  
      79  
      80 def parseArgs():  
      81     import sys  
      82     from optparse import OptionParser  
      83  
      84     usage = "usage: %prog [options] path out_tmx"  
      85  
      86     op = OptionParser(usage=usage)  
      87     op.add_option('-r', '--recursive',  
      88                   action='store_true', dest='recursive',  
      89                   help='Process files recursive.')  
      90     op.add_option('-f', '--fmask',  
      91                   action='store', type='string', dest='fmask',  
      92                   help='File mask to use when running recursive.')  
      93  
      94  
      95     options, args = op.parse_args(sys.argv[1:])  
      96     try:  
      97         pname = path(args[0])  
      98         outfile = path(args[1])  
      99     except IndexError,e:  
      100         op.error("Invalid number if arguments, use -h for help")  
      101  
      102          
      103     if options.recursive:  
      104         if not options.fmask:  
      105             op.error("must give filemask option to when recurse")  
      106         else:  
      107             fnames = pname.walkfiles(options.fmask)  
      108     else:  
      109         if options.fmask:  
      110             if not pname.isdir():  
      111                 op.error("must give a directory path when option fmask is given")  
      112             else:  
      113                 fnames = pname.listdir(options.fmask)  
      114         else:  
      115             fnames = [pname,]  
      116  
      117  
      118     options.fnames = fnames  
      119     options.outfile = outfile  
      120     return options  
      121  
      122  
      123 def main():  
      124     import sys  
      125     import codecs  
      126     import transolution  
      127     global src_lang,trg_lang  
      128      
      129     options = parseArgs()  
      130  
    131 131     dom = createDocument("tmx",ns=EMPTY_NAMESPACE)  
    132 132     doc = dom.documentElement  
    133 133     doc.setAttributeNS(EMPTY_NAMESPACE,"version","1.4")  
    134       body = dom.createElementNS(EMPTY_NAMESPACE,"body")  
    135       doc.appendChild(body)  
    136       print "Parsing Xliff"  
    137       for fname in options.fnames:  
    138            
    139           print 'Processing:%s -->'%(fname,)  
    140           xliff = XliffDocument(fname)  
    141           xliff.parse()  
    142           for filenode in xliff.files:  
    143               src_lang = filenode.getSourceLang()  
    144               trg_lang = filenode.getTargetLang()  
    145               assert src_lang and trg_lang,'Source or Target language not set.'  
    146    
    147               filenode.parseSkeleton()  
    148               for seg in filenode.generateSegments():  
    149                   if seg.__class__ is TuSegment and seg.tu.hasTarget():  
    150                       addTMXTu(dom,body,seg)  
    151    
    152       print "Writing TMX"  
    153       transolution.xmlutils.toxml(doc,file(options.outfile,'w'))  
    154       print "Done"  
    155   if __name__ == "__main__":  
    156       main()  
      134     body = dom.createElementNS(EMPTY_NAMESPACE,"body")  
      135     doc.appendChild(body)  
      136     print "Parsing Xliff"  
      137     for fname in options.fnames:  
      138          
      139         print 'Processing:%s -->'%(fname,)  
      140         xliff = XliffDocument(fname)  
      141         xliff.parse()  
      142         for filenode in xliff.files:  
      143             src_lang = filenode.getSourceLang()  
      144             trg_lang = filenode.getTargetLang()  
      145             assert src_lang and trg_lang,'Source or Target language not set.'  
      146  
      147             filenode.parseSkeleton()  
      148             for seg in filenode.generateSegments():  
      149                 if seg.__class__ is TuSegment and seg.tu.hasTarget():  
      150                     addTMXTu(dom,body,seg)  
      151  
      152     print "Writing TMX"  
      153     transolution.xmlutils.toxml(doc,file(options.outfile,'w'))  
      154     print "Done"  
      155 if __name__ == "__main__":  
      156     main()