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