xref: /aoo41x/main/l10ntools/scripts/tool/sdf.py (revision 7d9fa7c3)
1a0428e9eSAndrew Rist#**************************************************************
2a0428e9eSAndrew Rist#
3a0428e9eSAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
4a0428e9eSAndrew Rist#  or more contributor license agreements.  See the NOTICE file
5a0428e9eSAndrew Rist#  distributed with this work for additional information
6a0428e9eSAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
7a0428e9eSAndrew Rist#  to you under the Apache License, Version 2.0 (the
8a0428e9eSAndrew Rist#  "License"); you may not use this file except in compliance
9a0428e9eSAndrew Rist#  with the License.  You may obtain a copy of the License at
10a0428e9eSAndrew Rist#
11a0428e9eSAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
12a0428e9eSAndrew Rist#
13a0428e9eSAndrew Rist#  Unless required by applicable law or agreed to in writing,
14a0428e9eSAndrew Rist#  software distributed under the License is distributed on an
15a0428e9eSAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16a0428e9eSAndrew Rist#  KIND, either express or implied.  See the License for the
17a0428e9eSAndrew Rist#  specific language governing permissions and limitations
18a0428e9eSAndrew Rist#  under the License.
19a0428e9eSAndrew Rist#
20a0428e9eSAndrew Rist#**************************************************************
21cdf0e10cSrcweir
22cdf0e10cSrcweirfrom pseudo import PseudoSet,PseudoOrderedDict
23cdf0e10cSrcweirfrom time import gmtime, strftime
24cdf0e10cSrcweir
25cdf0e10cSrcweirclass SdfData:
26cdf0e10cSrcweir    _filename        = "";
27cdf0e10cSrcweir    _dict            = PseudoOrderedDict()
28cdf0e10cSrcweir    _languages_found = [];
29ae54856bSPedro Giffuni
30cdf0e10cSrcweir    def __init__ (self, filename=""):
31cdf0e10cSrcweir        self._filename = filename
32ae54856bSPedro Giffuni
33cdf0e10cSrcweir    def __getitem__(self, key):
34*7d9fa7c3SPedro Giffuni        if key in self._dict:
35cdf0e10cSrcweir            return self._dict[key]
36cdf0e10cSrcweir        else:
37cdf0e10cSrcweir            return None
38ae54856bSPedro Giffuni
39cdf0e10cSrcweir    def has_key(self, key):
40*7d9fa7c3SPedro Giffuni        return key in self._dict
41ae54856bSPedro Giffuni
42cdf0e10cSrcweir    def __setitem__(self, key, value):
43cdf0e10cSrcweir        self._dict[key] = value
44ae54856bSPedro Giffuni
45cdf0e10cSrcweir    def get_languages_found_in_sdf(self):
46cdf0e10cSrcweir        return PseudoSet(self._languages_found)
47cdf0e10cSrcweir
48cdf0e10cSrcweir    def read(self):
49cdf0e10cSrcweir        try:
50cdf0e10cSrcweir            f = open(self._filename, "r")
51ae54856bSPedro Giffuni            lines = [line.rstrip('\n') for line in f.readlines()]
52cdf0e10cSrcweir        except IOError:
53*7d9fa7c3SPedro Giffuni            print("ERROR: Trying to read "+ self._filename)
54cdf0e10cSrcweir            raise
55cdf0e10cSrcweir        else:
56cdf0e10cSrcweir            f.close()
57ae54856bSPedro Giffuni        for line in lines:
58cdf0e10cSrcweir            entity = SdfEntity()
59cdf0e10cSrcweir            entity.set_properties(line)
60cdf0e10cSrcweir            self._dict[entity.get_id()] = entity
61cdf0e10cSrcweir            self._languages_found.append(entity.langid)
62cdf0e10cSrcweir
63cdf0e10cSrcweir    def write(self, filename):
64cdf0e10cSrcweir        try:
65ae54856bSPedro Giffuni            f = open(filename, "w+")
66*7d9fa7c3SPedro Giffuni            for value in self._dict.values():
67cdf0e10cSrcweir                #f.write( repr(value)+"\n" )
68cdf0e10cSrcweir                f.write(value + "\n")
69cdf0e10cSrcweir        except IOError:
70*7d9fa7c3SPedro Giffuni            print("ERROR: Trying to write " + filename)
71cdf0e10cSrcweir            raise
72cdf0e10cSrcweir        else:
73cdf0e10cSrcweir            f.close()
74cdf0e10cSrcweir
75cdf0e10cSrcweirimport sys
76ae54856bSPedro Giffuniclass SdfEntity:
77cdf0e10cSrcweir    # Sdf format columns
78cdf0e10cSrcweir    project         = ""
79cdf0e10cSrcweir    source_file     = ""
80cdf0e10cSrcweir    dummy1          = ""
81cdf0e10cSrcweir    resource_type   = ""
82cdf0e10cSrcweir    gid             = ""
83cdf0e10cSrcweir    lid             = ""
84cdf0e10cSrcweir    helpid          = ""
85cdf0e10cSrcweir    platform        = ""
86cdf0e10cSrcweir    dummy2          = ""
87cdf0e10cSrcweir    langid          = ""
88cdf0e10cSrcweir    text            = ""
89cdf0e10cSrcweir    helptext        = ""
90cdf0e10cSrcweir    quickhelptext   = ""
91cdf0e10cSrcweir    title           = ""
92cdf0e10cSrcweir    date            = ""
93ae54856bSPedro Giffuni
94cdf0e10cSrcweir    import const
95cdf0e10cSrcweir    const._PROJECT_POS         = 0
96cdf0e10cSrcweir    const._SOURCE_FILE_POS     = 1
97cdf0e10cSrcweir    const._DUMMY1_POS          = 2
98cdf0e10cSrcweir    const._RESOURCE_TYPE_POS   = 3
99cdf0e10cSrcweir    const._GID_POS             = 4
100cdf0e10cSrcweir    const._LID_POS             = 5
101cdf0e10cSrcweir    const._HELPID_POS          = 6
102cdf0e10cSrcweir    const._PLATFORM_POS        = 7
103cdf0e10cSrcweir    const._DUMMY2_POS          = 8
104cdf0e10cSrcweir    const._LANGID_POS          = 9
105cdf0e10cSrcweir    const._TEXT_POS            = 10
106cdf0e10cSrcweir    const._HELPTEXT_POS        = 11
107cdf0e10cSrcweir    const._QUICKHELPTEXT_POS   = 12
108cdf0e10cSrcweir    const._TITLE_POS           = 13
109cdf0e10cSrcweir    const._DATE_POS            = 14
110ae54856bSPedro Giffuni
111ae54856bSPedro Giffuni    def __init__(self, project="", source_file="", dummy1="0", resource_type="", gid="", lid="", helpid="", platform="", dummy2="0", langid="",
112cdf0e10cSrcweir                       text="", helptext="", quickhelptext="", title="", date=""):
113cdf0e10cSrcweir        self.project        = project;
114cdf0e10cSrcweir        self.source_file    = source_file;
115cdf0e10cSrcweir        self.dummy1         = dummy1;
116cdf0e10cSrcweir        self.resource_type  = resource_type;
117cdf0e10cSrcweir        self.gid            = gid;
118cdf0e10cSrcweir        self.lid            = lid;
119cdf0e10cSrcweir        self.helpid         = helpid;
120cdf0e10cSrcweir        self.platform       = platform;
121cdf0e10cSrcweir        self.dummy2         = dummy2;
122cdf0e10cSrcweir        self.langid         = langid;
123cdf0e10cSrcweir        self.text           = text;
124cdf0e10cSrcweir        self.helptext       = helptext;
125cdf0e10cSrcweir        self.quickhelptext  = quickhelptext;
126cdf0e10cSrcweir        self.title          = title;
127cdf0e10cSrcweir        if date != "":
128cdf0e10cSrcweir            self.date = date;
129cdf0e10cSrcweir        else:
130cdf0e10cSrcweir            self.date = strftime("%Y-%m-%d %H:%M:%S",gmtime())
131cdf0e10cSrcweir
132cdf0e10cSrcweir
133cdf0e10cSrcweir    def set_properties(self, line):
134cdf0e10cSrcweir        splitted = line.split("\t")
135cdf0e10cSrcweir        if len(splitted) == 15:
136ae54856bSPedro Giffuni            self.project        = splitted[ self.const._PROJECT_POS ]
137ae54856bSPedro Giffuni            self.source_file    = splitted[ self.const._SOURCE_FILE_POS ]
138ae54856bSPedro Giffuni            self.dummy1         = splitted[ self.const._DUMMY1_POS ]
139ae54856bSPedro Giffuni            self.resource_type  = splitted[ self.const._RESOURCE_TYPE_POS ]
140ae54856bSPedro Giffuni            self.gid            = splitted[ self.const._GID_POS ]
141ae54856bSPedro Giffuni            self.lid            = splitted[ self.const._LID_POS ]
142ae54856bSPedro Giffuni            self.helpid         = splitted[ self.const._HELPID_POS ]
143ae54856bSPedro Giffuni            self.platform       = splitted[ self.const._PLATFORM_POS ]
144ae54856bSPedro Giffuni            self.dummy2         = splitted[ self.const._DUMMY2_POS ]
145ae54856bSPedro Giffuni            self.langid         = splitted[ self.const._LANGID_POS ]
146ae54856bSPedro Giffuni            self.text           = splitted[ self.const._TEXT_POS ]
147ae54856bSPedro Giffuni            self.helptext       = splitted[ self.const._HELPTEXT_POS ]
148ae54856bSPedro Giffuni            self.quickhelptext  = splitted[ self.const._QUICKHELPTEXT_POS ]
149ae54856bSPedro Giffuni            self.title          = splitted[ self.const._TITLE_POS ]
150ae54856bSPedro Giffuni            self.date           = splitted[ self.const._DATE_POS ]
151cdf0e10cSrcweir
152cdf0e10cSrcweir    def get_file_id(self):
153cdf0e10cSrcweir        return self.project + "\\" + self.source_file
154ae54856bSPedro Giffuni
155cdf0e10cSrcweir    def get_resource_path(self):
156ae54856bSPedro Giffuni        return self.source_file[0:self.source_file.rfind( "\\" )-1]
157ae54856bSPedro Giffuni
158cdf0e10cSrcweir    def __str__(self):
159ae54856bSPedro Giffuni        return ''.join([self.project, "\t", self.source_file, "\t", self.dummy1, "\t", self.resource_type, "\t" ,
160ae54856bSPedro Giffuni            self.gid, "\t", self.lid, "\t", self.helpid, "\t", self.platform, "\t", self.dummy2, "\t" , self.langid,
161cdf0e10cSrcweir            "\t", self.text, "\t", self.helptext, "\t", self.quickhelptext, "\t" , self.title, "\t", self.date ])
162ae54856bSPedro Giffuni
163cdf0e10cSrcweir    def get_id(self):
164cdf0e10cSrcweir        return ''.join([self.project, self.gid, self.lid, self.source_file, self.resource_type, self.platform, self.helpid, self.langid])
165