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
27
29 """a class that takes translations from a .csv file and puts them in a .tbx file"""
30
32 """construct the converter..."""
33 self.charset = charset
34
36 """converts a csvfile to a tbxfile, and returns it. uses templatepo if given at construction"""
37 mightbeheader = True
38 self.tbxfile = tbx.tbxfile()
39 for thecsv in thecsvfile.units:
40 if mightbeheader:
41
42 mightbeheader = False
43 if [item.strip().lower() for item in thecsv.comment, thecsv.source, thecsv.target] == \
44 ["comment", "original", "translation"]:
45 continue
46 if len(thecsv.comment.strip()) == 0 and thecsv.source.find("Content-Type:") != -1:
47 continue
48 term = tbx.tbxunit.buildfromunit(thecsv)
49
50 self.tbxfile.addunit(term)
51 return self.tbxfile
52
53
54 -def convertcsv(inputfile, outputfile, templatefile, charset=None, columnorder=None):
55 """reads in inputfile using csvl10n, converts using csv2tbx, writes to outputfile"""
56 inputstore = csvl10n.csvfile(inputfile, fieldnames=columnorder)
57 convertor = csv2tbx(charset=charset)
58 outputstore = convertor.convertfile(inputstore)
59 if len(outputstore.units) == 0:
60 return 0
61 outputfile.write(str(outputstore))
62 return 1
63
64
66 from translate.convert import convert
67 formats = {("csv", "tbx"): ("tbx", convertcsv), ("csv", None): ("tbx", convertcsv)}
68 parser = convert.ConvertOptionParser(formats, usetemplates=False, description=__doc__)
69 parser.add_option("", "--charset", dest="charset", default=None,
70 help="set charset to decode from csv files", metavar="CHARSET")
71 parser.add_option("", "--columnorder", dest="columnorder", default=None,
72 help="specify the order and position of columns (comment,source,target)")
73 parser.passthrough.append("charset")
74 parser.passthrough.append("columnorder")
75 parser.run()
76
77
78 if __name__ == '__main__':
79 main()
80