1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """convert Comma-Separated Value (.csv) files to a TermBase eXchange (.tbx) glossary file"""
23
24 from translate.storage import tbx
25 from translate.storage import csvl10n
26
28 """a class that takes translations from a .csv file and puts them in a .tbx file"""
30 """construct the converter..."""
31 self.charset = charset
32
34 """converts a csvfile to a tbxfile, and returns it. uses templatepo if given at construction"""
35 mightbeheader = True
36 self.tbxfile = tbx.tbxfile()
37 for thecsv in thecsvfile.units:
38 if mightbeheader:
39
40 mightbeheader = False
41 if [item.strip().lower() for item in thecsv.comment, thecsv.source, thecsv.target] == \
42 ["comment", "original", "translation"]:
43 continue
44 if len(thecsv.comment.strip()) == 0 and thecsv.source.find("Content-Type:") != -1:
45 continue
46 term = tbx.tbxunit.buildfromunit(thecsv)
47
48 self.tbxfile.addunit(term)
49 return self.tbxfile
50
51 -def convertcsv(inputfile, outputfile, templatefile, charset=None, columnorder=None):
52 """reads in inputfile using csvl10n, converts using csv2tbx, writes to outputfile"""
53 inputstore = csvl10n.csvfile(inputfile, fieldnames=columnorder)
54 convertor = csv2tbx(charset=charset)
55 outputstore = convertor.convertfile(inputstore)
56 if len(outputstore.units) == 0:
57 return 0
58 outputfile.write(str(outputstore))
59 return 1
60
62 from translate.convert import convert
63 formats = {("csv", "tbx"): ("tbx", convertcsv), ("csv", None): ("tbx", convertcsv)}
64 parser = convert.ConvertOptionParser(formats, usetemplates=False, description=__doc__)
65 parser.add_option("", "--charset", dest="charset", default=None,
66 help="set charset to decode from csv files", metavar="CHARSET")
67 parser.add_option("", "--columnorder", dest="columnorder", default=None,
68 help="specify the order and position of columns (comment,source,target)")
69 parser.passthrough.append("charset")
70 parser.passthrough.append("columnorder")
71 parser.run()
72
73
74
75 if __name__ == '__main__':
76 main()
77