home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import sys
- import os
- import tempfile
- import re
- from calibre.ebooks.rtf2xml import copy
-
- class Colors:
-
- def __init__(self, in_file, bug_handler, copy = None, run_level = 1):
- self._Colors__file = in_file
- self._Colors__copy = copy
- self._Colors__bug_handler = bug_handler
- self._Colors__write_to = tempfile.mktemp()
- self._Colors__run_level = run_level
-
-
- def __initiate_values(self):
- self._Colors__color_dict = { }
- self._Colors__state = 'before_color_table'
- self._Colors__state_dict = {
- 'before_color_table': self._Colors__before_color_func,
- 'in_color_table': self._Colors__in_color_func,
- 'after_color_table': self._Colors__after_color_func,
- 'cw<ci<red_______': self._Colors__default_color_func,
- 'cw<ci<green_____': self._Colors__default_color_func,
- 'cw<ci<blue______': self._Colors__blue_func,
- 'tx<nu<__________': self._Colors__do_nothing_func }
- self._Colors__color_string = '#'
- self._Colors__color_num = 1
- self._Colors__line_color_exp = re.compile('bdr-color_:(\\d+)')
-
-
- def __before_color_func(self, line):
- if self._Colors__token_info == 'mi<mk<clrtbl-beg':
- self._Colors__state = 'in_color_table'
-
- self._Colors__write_obj.write(line)
-
-
- def __default_color_func(self, line):
- hex_num = line[-3:-1]
- self._Colors__color_string += hex_num
-
-
- def __blue_func(self, line):
- hex_num = line[-3:-1]
- self._Colors__color_string += hex_num
- self._Colors__color_dict[self._Colors__color_num] = self._Colors__color_string
- self._Colors__write_obj.write('mi<tg<empty-att_<color-in-table<num>%s<value>%s\n' % (self._Colors__color_num, self._Colors__color_string))
- self._Colors__color_num += 1
- self._Colors__color_string = '#'
-
-
- def __in_color_func(self, line):
- if self._Colors__token_info == 'mi<mk<clrtbl-end':
- self._Colors__state = 'after_color_table'
- else:
- action = self._Colors__state_dict.get(self._Colors__token_info)
- if action == None:
- sys.stderr.write('in module colors.py\nfunction is self.__in_color_func\nno action for %s' % self._Colors__token_info)
-
- action(line)
-
-
- def __after_color_func(self, line):
- if self._Colors__token_info == 'cw<ci<font-color':
- hex_num = int(line[20:-1])
- hex_num = self._Colors__figure_num(hex_num)
- if hex_num:
- self._Colors__write_obj.write('cw<ci<font-color<nu<%s\n' % hex_num)
-
- elif line[0:5] == 'cw<bd':
- the_index = line.find('bdr-color_')
- if the_index > -1:
- line = re.sub(self._Colors__line_color_exp, self._Colors__sub_from_line_color, line)
-
- self._Colors__write_obj.write(line)
- else:
- self._Colors__write_obj.write(line)
-
-
- def __sub_from_line_color(self, match_obj):
- num = match_obj.group(1)
-
- try:
- num = int(num)
- except ValueError:
- if self._Colors__run_level > 3:
- msg = "can't make integer from string\n"
- raise self._Colors__bug_handler, msg
- self._Colors__run_level > 3
- return 'bdr-color_:no-value'
-
- hex_num = self._Colors__figure_num(num)
- return_value = 'bdr-color_:%s' % hex_num
- return return_value
-
-
- def __figure_num(self, num):
- if num == 0:
- hex_num = 'false'
- else:
- hex_num = self._Colors__color_dict.get(num)
- if hex_num == None:
- if self._Colors__run_level > 3:
- msg = 'no value in self.__color_dict for key %s\n' % num
- raise self._Colors__bug_hanlder, msg
- self._Colors__run_level > 3
-
- if hex_num == None:
- hex_num = '0'
-
- return hex_num
-
-
- def __do_nothing_func(self, line):
- pass
-
-
- def convert_colors(self):
- self._Colors__initiate_values()
- read_obj = open(self._Colors__file, 'r')
- self._Colors__write_obj = open(self._Colors__write_to, 'w')
- line_to_read = 1
- while line_to_read:
- line_to_read = read_obj.readline()
- line = line_to_read
- self._Colors__token_info = line[:16]
- action = self._Colors__state_dict.get(self._Colors__state)
- if action == None:
- sys.stderr.write('no no matching state in module fonts.py\n')
- sys.stderr.write(self._Colors__state + '\n')
-
- action(line)
- read_obj.close()
- self._Colors__write_obj.close()
- copy_obj = copy.Copy(bug_handler = self._Colors__bug_handler)
- if self._Colors__copy:
- copy_obj.copy_file(self._Colors__write_to, 'color.data')
-
- copy_obj.rename(self._Colors__write_to, self._Colors__file)
- os.remove(self._Colors__write_to)
-
-
-