1   
  2   
  3  from translate.convert import po2csv 
  4  from translate.convert import csv2po 
  5  from translate.convert import test_convert 
  6  from translate.misc import wStringIO 
  7  from translate.storage import po 
  8  from translate.storage import csvl10n 
  9   
 12          """helper that converts po source to csv source without requiring files""" 
 13          inputfile = wStringIO.StringIO(posource) 
 14          inputpo = po.pofile(inputfile) 
 15          convertor = po2csv.po2csv() 
 16          outputcsv = convertor.convertstore(inputpo) 
 17          return outputcsv 
  18   
 19 -    def csv2po(self, csvsource, template=None): 
  20          """helper that converts csv source to po source without requiring files""" 
 21          inputfile = wStringIO.StringIO(csvsource) 
 22          inputcsv = csvl10n.csvfile(inputfile) 
 23          if template: 
 24              templatefile = wStringIO.StringIO(template) 
 25              inputpot = po.pofile(templatefile) 
 26          else: 
 27              inputpot = None 
 28          convertor = csv2po.csv2po(templatepo=inputpot) 
 29          outputpo = convertor.convertstore(inputcsv) 
 30          return outputpo 
  31   
 33          """checks that the pofile contains a single non-header element, and returns it""" 
 34          assert len(storage.units) == 1 
 35          return storage.units[0] 
  36   
 38          """checks that a simple csv entry definition converts properly to a po entry""" 
 39          minipo = r'''#: term.cpp 
 40  msgid "Term" 
 41  msgstr "asdf"''' 
 42          csvfile = self.po2csv(minipo) 
 43          unit = self.singleelement(csvfile) 
 44          assert unit.comment == "term.cpp" 
 45          assert unit.source == "Term" 
 46          assert unit.target == "asdf" 
  47   
 49          """tests multiline po entries""" 
 50          minipo = r'''msgid "First part " 
 51  "and extra" 
 52  msgstr "Eerste deel " 
 53  "en ekstra"''' 
 54          csvfile = self.po2csv(minipo) 
 55          unit = self.singleelement(csvfile) 
 56          assert unit.source == "First part and extra" 
 57          assert unit.target == "Eerste deel en ekstra" 
  58   
 60          """Test the escaping of newlines""" 
 61          minipo = r'''msgid "First line\nSecond line" 
 62  msgstr "Eerste lyn\nTweede lyn" 
 63  ''' 
 64          csvfile = self.po2csv(minipo) 
 65          unit = self.singleelement(csvfile) 
 66          assert unit.source == "First line\nSecond line" 
 67          assert unit.target == "Eerste lyn\nTweede lyn" 
 68          pofile = self.csv2po(str(csvfile)) 
 69          unit = self.singleelement(pofile) 
 70          assert unit.source == "First line\nSecond line" 
 71          assert unit.target == "Eerste lyn\nTweede lyn" 
  72   
 74          """Test the escaping of tabs""" 
 75          minipo = r'''msgid "First column\tSecond column" 
 76  msgstr "Eerste kolom\tTweede kolom" 
 77  ''' 
 78          csvfile = self.po2csv(minipo) 
 79          unit = self.singleelement(csvfile) 
 80          assert unit.source == "First column\tSecond column" 
 81          assert unit.target == "Eerste kolom\tTweede kolom" 
 82          assert csvfile.findunit("First column\tSecond column").target == "Eerste kolom\tTweede kolom" 
  83   
 85          """Test the escaping of quotes (and slash)""" 
 86          minipo = r'''msgid "Hello \"Everyone\"" 
 87  msgstr "Good day \"All\"" 
 88   
 89  msgid "Use \\\"." 
 90  msgstr "Gebruik \\\"." 
 91  ''' 
 92          csvfile = self.po2csv(minipo) 
 93          assert csvfile.findunit('Hello "Everyone"').target == 'Good day "All"' 
 94          assert csvfile.findunit('Use \\".').target == 'Gebruik \\".' 
  95   
 97          """Test the escaping of pure escapes is unaffected""" 
 98          minipo = r'''msgid "Find\\Options" 
 99  msgstr "Vind\\Opsies" 
100  ''' 
101          csvfile = self.po2csv(minipo) 
102          print minipo 
103          print csvfile 
104          assert csvfile.findunit(r'Find\Options').target == r'Vind\Opsies' 
 105   
107          """Tests that single quotes are preserved correctly""" 
108          minipo = '''msgid "source 'source'"\nmsgstr "target 'target'"\n''' 
109          csvfile = self.po2csv(minipo) 
110          print str(csvfile) 
111          assert csvfile.findunit("source 'source'").target == "target 'target'" 
112           
113          minipo = '''msgid "'source'"\nmsgstr "'target'"\n''' 
114          csvfile = self.po2csv(minipo) 
115          print str(csvfile) 
116          assert csvfile.findunit(r"'source'").target == r"'target'" 
 117           
118   
126   
136      """Tests running actual po2csv commands on files""" 
137      convertmodule = po2csv 
138   
 144