xref: /aoo42x/main/l10ntools/scripts/tool/xtxex.py (revision a0428e9e)
1*a0428e9eSAndrew Rist#**************************************************************
2*a0428e9eSAndrew Rist#
3*a0428e9eSAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
4*a0428e9eSAndrew Rist#  or more contributor license agreements.  See the NOTICE file
5*a0428e9eSAndrew Rist#  distributed with this work for additional information
6*a0428e9eSAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
7*a0428e9eSAndrew Rist#  to you under the Apache License, Version 2.0 (the
8*a0428e9eSAndrew Rist#  "License"); you may not use this file except in compliance
9*a0428e9eSAndrew Rist#  with the License.  You may obtain a copy of the License at
10*a0428e9eSAndrew Rist#
11*a0428e9eSAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
12*a0428e9eSAndrew Rist#
13*a0428e9eSAndrew Rist#  Unless required by applicable law or agreed to in writing,
14*a0428e9eSAndrew Rist#  software distributed under the License is distributed on an
15*a0428e9eSAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a0428e9eSAndrew Rist#  KIND, either express or implied.  See the License for the
17*a0428e9eSAndrew Rist#  specific language governing permissions and limitations
18*a0428e9eSAndrew Rist#  under the License.
19*a0428e9eSAndrew Rist#
20*a0428e9eSAndrew Rist#**************************************************************
21cdf0e10cSrcweir
22cdf0e10cSrcweirfrom l10ntool import AbstractL10nTool
23cdf0e10cSrcweirfrom sdf import SdfEntity
24cdf0e10cSrcweirimport sys
25cdf0e10cSrcweirimport shutil
26cdf0e10cSrcweir
27cdf0e10cSrcweirclass Xtxex(AbstractL10nTool):
28cdf0e10cSrcweir    _resource_type       = "xtx"
29cdf0e10cSrcweir
30cdf0e10cSrcweir    def __init__(self):
31cdf0e10cSrcweir        AbstractL10nTool.__init__(self)
32cdf0e10cSrcweir
33cdf0e10cSrcweir    def merge_file(self, inputfilename, outputfilename, parsed_file_ref, lang, is_forced_lang, sdfdata):
34cdf0e10cSrcweir        # Special handling for en-US files
35cdf0e10cSrcweir        if lang == "en-US":
36cdf0e10cSrcweir            mod_outputfilename = outputfilename
37cdf0e10cSrcweir            # mod here if needed
38cdf0e10cSrcweir            self.copy_file(inputfilename, mod_outputfilename)
39cdf0e10cSrcweir            return
40cdf0e10cSrcweir        # merge usual lang
41cdf0e10cSrcweir        sdfline = self.prepare_sdf_line(inputfilename,lang)
42cdf0e10cSrcweir        if sdfdata.has_key(sdfline.get_id()):
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:
49cdf0e10cSrcweir                print "ERROR: Can not write file " + outputfilename
50cdf0e10cSrcweir                sys.exit(-1)
51cdf0e10cSrcweir            else:
52cdf0e10cSrcweir                f.close()
53cdf0e10cSrcweir            return
54cdf0e10cSrcweir        # no sdf data found then copy en-US source file
55cdf0e10cSrcweir        if is_forced_lang:
56cdf0e10cSrcweir            self.copy_file(inputfilename, outputfilename)
57cdf0e10cSrcweir
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:
65cdf0e10cSrcweir            print "ERROR: Can not open file " + inputfile
66cdf0e10cSrcweir            sys.exit(-1)
67cdf0e10cSrcweir        else:
68cdf0e10cSrcweir            f.close()
69cdf0e10cSrcweir        # remove legal header
70cdf0e10cSrcweir        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="")
87cdf0e10cSrcweir
88cdf0e10cSrcweirrun = Xtxex()
89