xref: /aoo42x/main/l10ntools/scripts/tool/sdf.py (revision a0428e9e)
1*a0428e9eSAndrew Rist#**************************************************************
2*a0428e9eSAndrew Rist#
3*a0428e9eSAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
4*a0428e9eSAndrew Rist#  or more contributor license agreements.  See the NOTICE file
5*a0428e9eSAndrew Rist#  distributed with this work for additional information
6*a0428e9eSAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
7*a0428e9eSAndrew Rist#  to you under the Apache License, Version 2.0 (the
8*a0428e9eSAndrew Rist#  "License"); you may not use this file except in compliance
9*a0428e9eSAndrew Rist#  with the License.  You may obtain a copy of the License at
10*a0428e9eSAndrew Rist#
11*a0428e9eSAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
12*a0428e9eSAndrew Rist#
13*a0428e9eSAndrew Rist#  Unless required by applicable law or agreed to in writing,
14*a0428e9eSAndrew Rist#  software distributed under the License is distributed on an
15*a0428e9eSAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a0428e9eSAndrew Rist#  KIND, either express or implied.  See the License for the
17*a0428e9eSAndrew Rist#  specific language governing permissions and limitations
18*a0428e9eSAndrew Rist#  under the License.
19*a0428e9eSAndrew Rist#
20*a0428e9eSAndrew Rist#**************************************************************
21cdf0e10cSrcweir
22cdf0e10cSrcweirfrom pseudo import PseudoSet,PseudoOrderedDict
23cdf0e10cSrcweirfrom time import gmtime, strftime
24cdf0e10cSrcweir
25cdf0e10cSrcweirclass SdfData:
26cdf0e10cSrcweir    _filename        = "";
27cdf0e10cSrcweir    _dict            = PseudoOrderedDict()
28cdf0e10cSrcweir    _languages_found = [];
29cdf0e10cSrcweir
30cdf0e10cSrcweir    def __init__ (self, filename=""):
31cdf0e10cSrcweir        self._filename = filename
32cdf0e10cSrcweir
33cdf0e10cSrcweir    def __getitem__(self, key):
34cdf0e10cSrcweir        if self._dict.has_key(key):
35cdf0e10cSrcweir            return self._dict[key]
36cdf0e10cSrcweir        else:
37cdf0e10cSrcweir            return None
38cdf0e10cSrcweir
39cdf0e10cSrcweir    def has_key(self, key):
40cdf0e10cSrcweir        return self._dict.has_key(key)
41cdf0e10cSrcweir
42cdf0e10cSrcweir    def __setitem__(self, key, value):
43cdf0e10cSrcweir        self._dict[key] = value
44cdf0e10cSrcweir
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")
51cdf0e10cSrcweir            lines = [line.rstrip('\n') for line in f.readlines()]
52cdf0e10cSrcweir        except IOError:
53cdf0e10cSrcweir            print "ERROR: Trying to read "+ self._filename
54cdf0e10cSrcweir            raise
55cdf0e10cSrcweir        else:
56cdf0e10cSrcweir            f.close()
57cdf0e10cSrcweir        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:
65cdf0e10cSrcweir            f = open(filename, "w+")
66cdf0e10cSrcweir            for value in self._dict.itervalues():
67cdf0e10cSrcweir                #f.write( repr(value)+"\n" )
68cdf0e10cSrcweir                f.write(value + "\n")
69cdf0e10cSrcweir        except IOError:
70cdf0e10cSrcweir            print "ERROR: Trying to write " + filename
71cdf0e10cSrcweir            raise
72cdf0e10cSrcweir        else:
73cdf0e10cSrcweir            f.close()
74cdf0e10cSrcweir
75cdf0e10cSrcweirimport sys
76cdf0e10cSrcweirclass 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            = ""
93cdf0e10cSrcweir
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
110cdf0e10cSrcweir
111cdf0e10cSrcweir    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:
136cdf0e10cSrcweir            self.project        = splitted[ self.const._PROJECT_POS ]
137cdf0e10cSrcweir            self.source_file    = splitted[ self.const._SOURCE_FILE_POS ]
138cdf0e10cSrcweir            self.dummy1         = splitted[ self.const._DUMMY1_POS ]
139cdf0e10cSrcweir            self.resource_type  = splitted[ self.const._RESOURCE_TYPE_POS ]
140cdf0e10cSrcweir            self.gid            = splitted[ self.const._GID_POS ]
141cdf0e10cSrcweir            self.lid            = splitted[ self.const._LID_POS ]
142cdf0e10cSrcweir            self.helpid         = splitted[ self.const._HELPID_POS ]
143cdf0e10cSrcweir            self.platform       = splitted[ self.const._PLATFORM_POS ]
144cdf0e10cSrcweir            self.dummy2         = splitted[ self.const._DUMMY2_POS ]
145cdf0e10cSrcweir            self.langid         = splitted[ self.const._LANGID_POS ]
146cdf0e10cSrcweir            self.text           = splitted[ self.const._TEXT_POS ]
147cdf0e10cSrcweir            self.helptext       = splitted[ self.const._HELPTEXT_POS ]
148cdf0e10cSrcweir            self.quickhelptext  = splitted[ self.const._QUICKHELPTEXT_POS ]
149cdf0e10cSrcweir            self.title          = splitted[ self.const._TITLE_POS ]
150cdf0e10cSrcweir            self.date           = splitted[ self.const._DATE_POS ]
151cdf0e10cSrcweir
152cdf0e10cSrcweir    def get_file_id(self):
153cdf0e10cSrcweir        return self.project + "\\" + self.source_file
154cdf0e10cSrcweir
155cdf0e10cSrcweir    def get_resource_path(self):
156cdf0e10cSrcweir            return self.source_file[0:self.source_file.rfind( "\\" )-1]
157cdf0e10cSrcweir
158cdf0e10cSrcweir    def __str__(self):
159cdf0e10cSrcweir        return ''.join([self.project, "\t", self.source_file, "\t", self.dummy1, "\t", self.resource_type, "\t" ,
160cdf0e10cSrcweir            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 ])
162cdf0e10cSrcweir
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