3 from translate
.convert
import csv2po
4 from translate
.convert
import test_convert
5 from translate
.misc
import wStringIO
6 from translate
.storage
import po
7 from translate
.storage
import csvl10n
10 def csv2po(self
, csvsource
, template
=None):
11 """helper that converts csv source to po source without requiring files"""
12 inputfile
= wStringIO
.StringIO(csvsource
)
13 inputcsv
= csvl10n
.csvfile(inputfile
)
15 templatefile
= wStringIO
.StringIO(template
)
16 inputpot
= po
.pofile(templatefile
)
19 convertor
= csv2po
.csv2po(templatepo
=inputpot
)
20 outputpo
= convertor
.convertstore(inputcsv
)
23 def singleelement(self
, storage
):
24 """checks that the pofile contains a single non-header element, and returns it"""
26 assert len(storage
.units
) == 1
27 return storage
.units
[0]
29 def test_simpleentity(self
):
30 """checks that a simple csv entry definition converts properly to a po entry"""
31 csvheader
= 'comment,original,translation\n'
32 csvsource
= 'intl.charset.default,ISO-8859-1,UTF-16'
34 pofile
= self
.csv2po(csvsource
)
35 pounit
= self
.singleelement(pofile
)
37 pofile
= self
.csv2po(csvheader
+ csvsource
)
38 pounit
= self
.singleelement(pofile
)
39 assert pounit
.getlocations() == ['intl.charset.default']
40 assert pounit
.source
== "ISO-8859-1"
41 assert pounit
.target
== "UTF-16"
43 def test_simpleentity_with_template(self
):
44 """checks that a simple csv entry definition converts properly to a po entry"""
45 csvsource
= '''source,original,translation
46 intl.charset.default,ISO-8859-1,UTF-16'''
47 potsource
= '''#: intl.charset.default
51 pofile
= self
.csv2po(csvsource
, potsource
)
52 pounit
= self
.singleelement(pofile
)
53 assert pounit
.getlocations() == ['intl.charset.default']
54 assert pounit
.source
== "ISO-8859-1"
55 assert pounit
.target
== "UTF-16"
57 def test_newlines(self
):
58 """tests multiline po entries"""
59 minicsv
= r
'''"Random comment
60 with continuation","Original text","Langdradige teks
63 pofile
= self
.csv2po(minicsv
)
64 unit
= self
.singleelement(pofile
)
65 assert unit
.getlocations() == ['Random', 'comment', 'with', 'continuation']
66 assert unit
.source
== "Original text"
68 assert unit
.target
== "Langdradige teks\nwat lank aanhou"
71 """Test the escaping of tabs"""
72 minicsv
= ',"First column\tSecond column","Twee kolomme gesky met \t"'
73 pofile
= self
.csv2po(minicsv
)
74 unit
= self
.singleelement(pofile
)
76 assert unit
.source
== "First column\tSecond column"
77 assert not pofile
.findunit("First column\tSecond column").target
== "Twee kolomme gesky met \\t"
79 def test_quotes(self
):
80 """Test the escaping of quotes (and slash)"""
81 minicsv
= r
''',"Hello ""Everyone""","Good day ""All"""
82 ,"Use \"".","Gebruik \""."'''
84 csvfile
= csvl10n
.csvfile(wStringIO
.StringIO(minicsv
))
86 pofile
= self
.csv2po(minicsv
)
87 unit
= pofile
.units
[0]
88 assert unit
.source
== 'Hello "Everyone"'
89 assert pofile
.findunit('Hello "Everyone"').target
== 'Good day "All"'
91 for unit
in pofile
.units
:
95 # assert pofile.findunit('Use \\".').target == 'Gebruik \\".'
97 def test_empties(self
):
98 """Tests that things keep working with empty entries"""
100 pofile
= self
.csv2po(minicsv
)
101 assert pofile
.findunit("Source") is not None
102 assert pofile
.findunit("Source").target
== ""
103 assert len(pofile
.units
) == 1
105 def test_kdecomment(self
):
106 """checks that we can merge into KDE comment entries"""
107 csvsource
= '''comment,original,translation
108 simple.c,Source,Target'''
109 potsource
= r
'''#: simple.c
110 msgid "_: KDE comment\n"
114 pofile
= self
.csv2po(csvsource
, potsource
)
115 pounit
= self
.singleelement(pofile
)
116 assert pounit
._extract
_msgidcomments
() == 'KDE comment'
117 assert pounit
.source
== "Source"
118 assert pounit
.target
== "Target"
120 class TestCSV2POCommand(test_convert
.TestConvertCommand
, TestCSV2PO
):
121 """Tests running actual csv2po commands on files"""
122 convertmodule
= csv2po
125 """tests getting help"""
126 options
= test_convert
.TestConvertCommand
.test_help(self
)
127 options
= self
.help_check(options
, "-t TEMPLATE, --template=TEMPLATE")
128 options
= self
.help_check(options
, "--charset=CHARSET")
129 options
= self
.help_check(options
, "--columnorder=COLUMNORDER")
130 options
= self
.help_check(options
, "--duplicates=DUPLICATESTYLE", last
=True)