xref: /aoo42x/main/l10ntools/scripts/tool/xtxex.py (revision 7d9fa7c3)
1a0428e9eSAndrew Rist#**************************************************************
2a0428e9eSAndrew Rist#
3a0428e9eSAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
4a0428e9eSAndrew Rist#  or more contributor license agreements.  See the NOTICE file
5a0428e9eSAndrew Rist#  distributed with this work for additional information
6a0428e9eSAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
7a0428e9eSAndrew Rist#  to you under the Apache License, Version 2.0 (the
8a0428e9eSAndrew Rist#  "License"); you may not use this file except in compliance
9a0428e9eSAndrew Rist#  with the License.  You may obtain a copy of the License at
10a0428e9eSAndrew Rist#
11a0428e9eSAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
12a0428e9eSAndrew Rist#
13a0428e9eSAndrew Rist#  Unless required by applicable law or agreed to in writing,
14a0428e9eSAndrew Rist#  software distributed under the License is distributed on an
15a0428e9eSAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16a0428e9eSAndrew Rist#  KIND, either express or implied.  See the License for the
17a0428e9eSAndrew Rist#  specific language governing permissions and limitations
18a0428e9eSAndrew Rist#  under the License.
19a0428e9eSAndrew Rist#
20a0428e9eSAndrew Rist#**************************************************************
21cdf0e10cSrcweir
22cdf0e10cSrcweirfrom l10ntool import AbstractL10nTool
23cdf0e10cSrcweirfrom sdf import SdfEntity
24cdf0e10cSrcweirimport sys
25ae54856bSPedro Giffuniimport shutil
26cdf0e10cSrcweir
27cdf0e10cSrcweirclass Xtxex(AbstractL10nTool):
28ae54856bSPedro Giffuni    _resource_type       = "xtx"
29ae54856bSPedro Giffuni
30cdf0e10cSrcweir    def __init__(self):
31cdf0e10cSrcweir        AbstractL10nTool.__init__(self)
32ae54856bSPedro Giffuni
33ae54856bSPedro Giffuni    def merge_file(self, inputfilename, outputfilename, parsed_file_ref, lang, is_forced_lang, sdfdata):
34cdf0e10cSrcweir        # Special handling for en-US files
35ae54856bSPedro Giffuni        if lang == "en-US":
36cdf0e10cSrcweir            mod_outputfilename = outputfilename
37cdf0e10cSrcweir            # mod here if needed
38cdf0e10cSrcweir            self.copy_file(inputfilename, mod_outputfilename)
39ae54856bSPedro Giffuni            return
40cdf0e10cSrcweir        # merge usual lang
41cdf0e10cSrcweir        sdfline = self.prepare_sdf_line(inputfilename,lang)
42*7d9fa7c3SPedro Giffuni        if sdfline.get_id() in sdfdata:
43cdf0e10cSrcweir            line = sdfdata[sdfline.get_id()].text.replace("\\n", '\n')
44cdf0e10cSrcweir            self.make_dirs(outputfilename)
45cdf0e10cSrcweir            try:
46cdf0e10cSrcweir                f = open(outputfilename, "w+")
47cdf0e10cSrcweir                f.write(line)
48cdf0e10cSrcweir            except IOError:
49*7d9fa7c3SPedro Giffuni                print("ERROR: Can not write file " + outputfilename)
50cdf0e10cSrcweir                sys.exit(-1)
51cdf0e10cSrcweir            else:
52cdf0e10cSrcweir                f.close()
53ae54856bSPedro Giffuni            return
54cdf0e10cSrcweir        # no sdf data found then copy en-US source file
55cdf0e10cSrcweir        if is_forced_lang:
56cdf0e10cSrcweir            self.copy_file(inputfilename, outputfilename)
57ae54856bSPedro Giffuni
58cdf0e10cSrcweir    ##### Extract a single File
59cdf0e10cSrcweir    def extract_file(self, inputfile):
60cdf0e10cSrcweir        lines = []
61cdf0e10cSrcweir        try:
62cdf0e10cSrcweir            f = open(inputfile, "r")
63cdf0e10cSrcweir            lines = f.readlines()
64cdf0e10cSrcweir        except IOError:
65*7d9fa7c3SPedro Giffuni            print("ERROR: Can not open file " + inputfile)
66cdf0e10cSrcweir            sys.exit(-1)
67cdf0e10cSrcweir        else:
68cdf0e10cSrcweir            f.close()
69cdf0e10cSrcweir        # remove legal header
70ae54856bSPedro Giffuni        lines = [line for line in lines if len(line) > 0 and not line[0] == '#']
71cdf0e10cSrcweir        # escape all returns
72cdf0e10cSrcweir        lines = [line.replace('\n', "\\n") for line in lines]
73cdf0e10cSrcweir        line = ''.join(lines)
74cdf0e10cSrcweir        test = str(line)
75cdf0e10cSrcweir        if len(test.strip()):
76cdf0e10cSrcweir            sdf_entity = self.prepare_sdf_line(inputfile);
77cdf0e10cSrcweir            sdf_entity.text = line
78cdf0e10cSrcweir            return str(sdf_entity)
79cdf0e10cSrcweir        else:
80cdf0e10cSrcweir            return ""
81cdf0e10cSrcweir
82cdf0e10cSrcweir    def prepare_sdf_line(self, inputfile="", lang=""):
83cdf0e10cSrcweir        if lang == "":
84cdf0e10cSrcweir            lang = self._source_language
85cdf0e10cSrcweir        return SdfEntity(project=self._options.project_name, source_file=self.get_filename_string(inputfile),
86cdf0e10cSrcweir                          resource_type=self._resource_type, gid="none", lid="none", langid=lang,text="")
87ae54856bSPedro Giffuni
88cdf0e10cSrcweirrun = Xtxex()
89