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