~ubuntu-branches/ubuntu/jaunty/calibre/jaunty-backports

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/rtf2xml/get_char_map.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-01-20 17:14:02 UTC
  • Revision ID: james.westby@ubuntu.com-20090120171402-8y3znf6nokwqe80k
Tags: upstream-0.4.125+dfsg
ImportĀ upstreamĀ versionĀ 0.4.125+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#########################################################################
 
2
#                                                                       #
 
3
#                                                                       #
 
4
#   copyright 2002 Paul Henry Tremblay                                  #
 
5
#                                                                       #
 
6
#   This program is distributed in the hope that it will be useful,     #
 
7
#   but WITHOUT ANY WARRANTY; without even the implied warranty of      #
 
8
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    #
 
9
#   General Public License for more details.                            #
 
10
#                                                                       #
 
11
#   You should have received a copy of the GNU General Public License   #
 
12
#   along with this program; if not, write to the Free Software         #
 
13
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA            #
 
14
#   02111-1307 USA                                                      #
 
15
#                                                                       #
 
16
#                                                                       #
 
17
#########################################################################
 
18
 
 
19
class GetCharMap:
 
20
    """
 
21
 
 
22
    Return the character map for the given value
 
23
 
 
24
    """
 
25
 
 
26
    def __init__(self, bug_handler, char_file):
 
27
        """
 
28
 
 
29
        Required:
 
30
 
 
31
            'char_file'--the file with the mappings
 
32
 
 
33
 
 
34
 
 
35
        Returns:
 
36
 
 
37
            nothing
 
38
 
 
39
            """
 
40
        self.__char_file = char_file
 
41
        self.__bug_handler = bug_handler
 
42
 
 
43
    def get_char_map(self, map):
 
44
        if map == 'ansicpg0':
 
45
            map = 'ansicpg1250'
 
46
        found_map = 0
 
47
        map_dict = {}
 
48
        self.__char_file.seek(0)
 
49
        for line in self.__char_file.readlines():
 
50
            if not line.strip(): continue
 
51
            begin_element = '<%s>' % map;
 
52
            end_element = '</%s>' % map
 
53
            if not found_map:
 
54
                if begin_element in line:
 
55
                    found_map = 1
 
56
            else:
 
57
                if end_element in line:
 
58
                    break
 
59
                fields = line.split(':')
 
60
                fields[1].replace('\\colon', ':')
 
61
                map_dict[fields[1]] = fields[3]
 
62
            
 
63
        
 
64
        if not found_map:
 
65
            msg = 'no map found\n'
 
66
            msg += 'map is "%s"\n'%(map,)
 
67
            raise self.__bug_handler, msg
 
68
        return map_dict
 
69