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