xref: /aoo41x/main/l10ntools/scripts/tool/sdf.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 pseudo import PseudoSet,PseudoOrderedDict
29from time import gmtime, strftime
30
31class SdfData:
32    _filename        = "";
33    _dict            = PseudoOrderedDict()
34    _languages_found = [];
35
36    def __init__ (self, filename=""):
37        self._filename = filename
38
39    def __getitem__(self, key):
40        if self._dict.has_key(key):
41            return self._dict[key]
42        else:
43            return None
44
45    def has_key(self, key):
46        return self._dict.has_key(key)
47
48    def __setitem__(self, key, value):
49        self._dict[key] = value
50
51    def get_languages_found_in_sdf(self):
52        return PseudoSet(self._languages_found)
53
54    def read(self):
55        try:
56            f = open(self._filename, "r")
57            lines = [line.rstrip('\n') for line in f.readlines()]
58        except IOError:
59            print "ERROR: Trying to read "+ self._filename
60            raise
61        else:
62            f.close()
63        for line in lines:
64            entity = SdfEntity()
65            entity.set_properties(line)
66            self._dict[entity.get_id()] = entity
67            self._languages_found.append(entity.langid)
68
69    def write(self, filename):
70        try:
71            f = open(filename, "w+")
72            for value in self._dict.itervalues():
73                #f.write( repr(value)+"\n" )
74                f.write(value + "\n")
75        except IOError:
76            print "ERROR: Trying to write " + filename
77            raise
78        else:
79            f.close()
80
81import sys
82class SdfEntity:
83    # Sdf format columns
84    project         = ""
85    source_file     = ""
86    dummy1          = ""
87    resource_type   = ""
88    gid             = ""
89    lid             = ""
90    helpid          = ""
91    platform        = ""
92    dummy2          = ""
93    langid          = ""
94    text            = ""
95    helptext        = ""
96    quickhelptext   = ""
97    title           = ""
98    date            = ""
99
100    import const
101    const._PROJECT_POS         = 0
102    const._SOURCE_FILE_POS     = 1
103    const._DUMMY1_POS          = 2
104    const._RESOURCE_TYPE_POS   = 3
105    const._GID_POS             = 4
106    const._LID_POS             = 5
107    const._HELPID_POS          = 6
108    const._PLATFORM_POS        = 7
109    const._DUMMY2_POS          = 8
110    const._LANGID_POS          = 9
111    const._TEXT_POS            = 10
112    const._HELPTEXT_POS        = 11
113    const._QUICKHELPTEXT_POS   = 12
114    const._TITLE_POS           = 13
115    const._DATE_POS            = 14
116
117    def __init__(self, project="", source_file="", dummy1="0", resource_type="", gid="", lid="", helpid="", platform="", dummy2="0", langid="",
118                       text="", helptext="", quickhelptext="", title="", date=""):
119        self.project        = project;
120        self.source_file    = source_file;
121        self.dummy1         = dummy1;
122        self.resource_type  = resource_type;
123        self.gid            = gid;
124        self.lid            = lid;
125        self.helpid         = helpid;
126        self.platform       = platform;
127        self.dummy2         = dummy2;
128        self.langid         = langid;
129        self.text           = text;
130        self.helptext       = helptext;
131        self.quickhelptext  = quickhelptext;
132        self.title          = title;
133        if date != "":
134            self.date = date;
135        else:
136            self.date = strftime("%Y-%m-%d %H:%M:%S",gmtime())
137
138
139    def set_properties(self, line):
140        splitted = line.split("\t")
141        if len(splitted) == 15:
142            self.project        = splitted[ self.const._PROJECT_POS ]
143            self.source_file    = splitted[ self.const._SOURCE_FILE_POS ]
144            self.dummy1         = splitted[ self.const._DUMMY1_POS ]
145            self.resource_type  = splitted[ self.const._RESOURCE_TYPE_POS ]
146            self.gid            = splitted[ self.const._GID_POS ]
147            self.lid            = splitted[ self.const._LID_POS ]
148            self.helpid         = splitted[ self.const._HELPID_POS ]
149            self.platform       = splitted[ self.const._PLATFORM_POS ]
150            self.dummy2         = splitted[ self.const._DUMMY2_POS ]
151            self.langid         = splitted[ self.const._LANGID_POS ]
152            self.text           = splitted[ self.const._TEXT_POS ]
153            self.helptext       = splitted[ self.const._HELPTEXT_POS ]
154            self.quickhelptext  = splitted[ self.const._QUICKHELPTEXT_POS ]
155            self.title          = splitted[ self.const._TITLE_POS ]
156            self.date           = splitted[ self.const._DATE_POS ]
157
158    def get_file_id(self):
159        return self.project + "\\" + self.source_file
160
161    def get_resource_path(self):
162            return self.source_file[0:self.source_file.rfind( "\\" )-1]
163
164    def __str__(self):
165        return ''.join([self.project, "\t", self.source_file, "\t", self.dummy1, "\t", self.resource_type, "\t" ,
166            self.gid, "\t", self.lid, "\t", self.helpid, "\t", self.platform, "\t", self.dummy2, "\t" , self.langid,
167            "\t", self.text, "\t", self.helptext, "\t", self.quickhelptext, "\t" , self.title, "\t", self.date ])
168
169    def get_id(self):
170        return ''.join([self.project, self.gid, self.lid, self.source_file, self.resource_type, self.platform, self.helpid, self.langid])
171