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