xref: /trunk/main/l10ntools/scripts/tool/xtxex.py (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir#*************************************************************************
2*cdf0e10cSrcweir#
3*cdf0e10cSrcweir# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir#
5*cdf0e10cSrcweir# Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir#
7*cdf0e10cSrcweir# OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir#
9*cdf0e10cSrcweir# This file is part of OpenOffice.org.
10*cdf0e10cSrcweir#
11*cdf0e10cSrcweir# OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir# it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir# only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir#
15*cdf0e10cSrcweir# OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir# but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir# GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir# (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir#
21*cdf0e10cSrcweir# You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir# version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir# <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir# for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir#
26*cdf0e10cSrcweir#*************************************************************************
27*cdf0e10cSrcweir
28*cdf0e10cSrcweirfrom l10ntool import AbstractL10nTool
29*cdf0e10cSrcweirfrom sdf import SdfEntity
30*cdf0e10cSrcweirimport sys
31*cdf0e10cSrcweirimport shutil
32*cdf0e10cSrcweir
33*cdf0e10cSrcweirclass Xtxex(AbstractL10nTool):
34*cdf0e10cSrcweir    _resource_type       = "xtx"
35*cdf0e10cSrcweir
36*cdf0e10cSrcweir    def __init__(self):
37*cdf0e10cSrcweir        AbstractL10nTool.__init__(self)
38*cdf0e10cSrcweir
39*cdf0e10cSrcweir    def merge_file(self, inputfilename, outputfilename, parsed_file_ref, lang, is_forced_lang, sdfdata):
40*cdf0e10cSrcweir        # Special handling for en-US files
41*cdf0e10cSrcweir        if lang == "en-US":
42*cdf0e10cSrcweir            mod_outputfilename = outputfilename
43*cdf0e10cSrcweir            # mod here if needed
44*cdf0e10cSrcweir            self.copy_file(inputfilename, mod_outputfilename)
45*cdf0e10cSrcweir            return
46*cdf0e10cSrcweir        # merge usual lang
47*cdf0e10cSrcweir        sdfline = self.prepare_sdf_line(inputfilename,lang)
48*cdf0e10cSrcweir        if sdfdata.has_key(sdfline.get_id()):
49*cdf0e10cSrcweir            line = sdfdata[sdfline.get_id()].text.replace("\\n", '\n')
50*cdf0e10cSrcweir            self.make_dirs(outputfilename)
51*cdf0e10cSrcweir            try:
52*cdf0e10cSrcweir                f = open(outputfilename, "w+")
53*cdf0e10cSrcweir                f.write(line)
54*cdf0e10cSrcweir            except IOError:
55*cdf0e10cSrcweir                print "ERROR: Can not write file " + outputfilename
56*cdf0e10cSrcweir                sys.exit(-1)
57*cdf0e10cSrcweir            else:
58*cdf0e10cSrcweir                f.close()
59*cdf0e10cSrcweir            return
60*cdf0e10cSrcweir        # no sdf data found then copy en-US source file
61*cdf0e10cSrcweir        if is_forced_lang:
62*cdf0e10cSrcweir            self.copy_file(inputfilename, outputfilename)
63*cdf0e10cSrcweir
64*cdf0e10cSrcweir    ##### Extract a single File
65*cdf0e10cSrcweir    def extract_file(self, inputfile):
66*cdf0e10cSrcweir        lines = []
67*cdf0e10cSrcweir        try:
68*cdf0e10cSrcweir            f = open(inputfile, "r")
69*cdf0e10cSrcweir            lines = f.readlines()
70*cdf0e10cSrcweir        except IOError:
71*cdf0e10cSrcweir            print "ERROR: Can not open file " + inputfile
72*cdf0e10cSrcweir            sys.exit(-1)
73*cdf0e10cSrcweir        else:
74*cdf0e10cSrcweir            f.close()
75*cdf0e10cSrcweir        # remove legal header
76*cdf0e10cSrcweir        lines = [line for line in lines if len(line) > 0 and not line[0] == '#']
77*cdf0e10cSrcweir        # escape all returns
78*cdf0e10cSrcweir        lines = [line.replace('\n', "\\n") for line in lines]
79*cdf0e10cSrcweir        line = ''.join(lines)
80*cdf0e10cSrcweir        test = str(line)
81*cdf0e10cSrcweir        if len(test.strip()):
82*cdf0e10cSrcweir            sdf_entity = self.prepare_sdf_line(inputfile);
83*cdf0e10cSrcweir            sdf_entity.text = line
84*cdf0e10cSrcweir            return str(sdf_entity)
85*cdf0e10cSrcweir        else:
86*cdf0e10cSrcweir            return ""
87*cdf0e10cSrcweir
88*cdf0e10cSrcweir    def prepare_sdf_line(self, inputfile="", lang=""):
89*cdf0e10cSrcweir        if lang == "":
90*cdf0e10cSrcweir            lang = self._source_language
91*cdf0e10cSrcweir        return SdfEntity(project=self._options.project_name, source_file=self.get_filename_string(inputfile),
92*cdf0e10cSrcweir                          resource_type=self._resource_type, gid="none", lid="none", langid=lang,text="")
93*cdf0e10cSrcweir
94*cdf0e10cSrcweirrun = Xtxex()
95