1#
2# Copyright 2002-2006 Zuza Software Foundation
3#
4# This file is part of translate.
5#
6# translate is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# translate is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, see <http://www.gnu.org/licenses/>.
18
19"""Convert Gettext PO localization files to .ini files.
20
21See: http://docs.translatehouse.org/projects/translate-toolkit/en/latest/commands/ini2po.html
22for examples and usage instructions.
23"""
24
25import sys
26
27from translate.convert import convert
28from translate.storage import ini, po
29
30
31class po2ini:
32    """Convert a PO file and a template INI file to a INI file."""
33
34    SourceStoreClass = po.pofile
35    TargetStoreClass = ini.inifile
36    TargetUnitClass = ini.iniunit
37    MissingTemplateMessage = "A template INI file must be provided."
38
39    def __init__(
40        self,
41        input_file,
42        output_file,
43        template_file=None,
44        include_fuzzy=False,
45        output_threshold=None,
46        dialect="default",
47    ):
48        """Initialize the converter."""
49        if ini.INIConfig is None:
50            print("Missing iniparse library!")
51            sys.exit()
52
53        if template_file is None:
54            raise ValueError(self.MissingTemplateMessage)
55
56        self.source_store = self.SourceStoreClass(input_file)
57
58        self.should_output_store = convert.should_output_store(
59            self.source_store, output_threshold
60        )
61        if self.should_output_store:
62            self.include_fuzzy = include_fuzzy
63
64            self.output_file = output_file
65            self.template_store = self.TargetStoreClass(template_file, dialect=dialect)
66            self.target_store = self.TargetStoreClass(dialect=dialect)
67
68    def merge_stores(self):
69        """Convert a source file to a target file using a template file.
70
71        Source file is in source format, while target and template files use
72        target format.
73        """
74        self.source_store.makeindex()
75        for template_unit in self.template_store.units:
76            for location in template_unit.getlocations():
77                if location in self.source_store.locationindex:
78                    source_unit = self.source_store.locationindex[location]
79                    if source_unit.isfuzzy() and not self.include_fuzzy:
80                        template_unit.target = template_unit.source
81                    else:
82                        template_unit.target = source_unit.target
83                else:
84                    template_unit.target = template_unit.source
85
86    def run(self):
87        """Run the converter."""
88        if not self.should_output_store:
89            return 0
90
91        self.merge_stores()
92        self.template_store.serialize(self.output_file)
93        return 1
94
95
96def run_converter(
97    inputfile,
98    outputfile,
99    templatefile=None,
100    includefuzzy=False,
101    dialect="default",
102    outputthreshold=None,
103):
104    """Wrapper around converter."""
105    return po2ini(
106        inputfile, outputfile, templatefile, includefuzzy, outputthreshold, dialect
107    ).run()
108
109
110def convertisl(
111    inputfile,
112    outputfile,
113    templatefile=None,
114    includefuzzy=False,
115    dialect="inno",
116    outputthreshold=None,
117):
118    run_converter(
119        inputfile, outputfile, templatefile, includefuzzy, dialect, outputthreshold
120    )
121
122
123formats = {
124    ("po", "ini"): ("ini", run_converter),
125    ("po", "isl"): ("isl", convertisl),
126}
127
128
129def main(argv=None):
130    parser = convert.ConvertOptionParser(
131        formats, usetemplates=True, description=__doc__
132    )
133    parser.add_threshold_option()
134    parser.add_fuzzy_option()
135    parser.run(argv)
136
137
138if __name__ == "__main__":
139    main()
140