home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1089 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  5.4 KB  |  148 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import os
  6. import tempfile
  7. import re
  8. from calibre.ebooks.rtf2xml import copy
  9.  
  10. class Colors:
  11.     
  12.     def __init__(self, in_file, bug_handler, copy = None, run_level = 1):
  13.         self._Colors__file = in_file
  14.         self._Colors__copy = copy
  15.         self._Colors__bug_handler = bug_handler
  16.         self._Colors__write_to = tempfile.mktemp()
  17.         self._Colors__run_level = run_level
  18.  
  19.     
  20.     def __initiate_values(self):
  21.         self._Colors__color_dict = { }
  22.         self._Colors__state = 'before_color_table'
  23.         self._Colors__state_dict = {
  24.             'before_color_table': self._Colors__before_color_func,
  25.             'in_color_table': self._Colors__in_color_func,
  26.             'after_color_table': self._Colors__after_color_func,
  27.             'cw<ci<red_______': self._Colors__default_color_func,
  28.             'cw<ci<green_____': self._Colors__default_color_func,
  29.             'cw<ci<blue______': self._Colors__blue_func,
  30.             'tx<nu<__________': self._Colors__do_nothing_func }
  31.         self._Colors__color_string = '#'
  32.         self._Colors__color_num = 1
  33.         self._Colors__line_color_exp = re.compile('bdr-color_:(\\d+)')
  34.  
  35.     
  36.     def __before_color_func(self, line):
  37.         if self._Colors__token_info == 'mi<mk<clrtbl-beg':
  38.             self._Colors__state = 'in_color_table'
  39.         
  40.         self._Colors__write_obj.write(line)
  41.  
  42.     
  43.     def __default_color_func(self, line):
  44.         hex_num = line[-3:-1]
  45.         self._Colors__color_string += hex_num
  46.  
  47.     
  48.     def __blue_func(self, line):
  49.         hex_num = line[-3:-1]
  50.         self._Colors__color_string += hex_num
  51.         self._Colors__color_dict[self._Colors__color_num] = self._Colors__color_string
  52.         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))
  53.         self._Colors__color_num += 1
  54.         self._Colors__color_string = '#'
  55.  
  56.     
  57.     def __in_color_func(self, line):
  58.         if self._Colors__token_info == 'mi<mk<clrtbl-end':
  59.             self._Colors__state = 'after_color_table'
  60.         else:
  61.             action = self._Colors__state_dict.get(self._Colors__token_info)
  62.             if action == None:
  63.                 sys.stderr.write('in module colors.py\nfunction is self.__in_color_func\nno action for %s' % self._Colors__token_info)
  64.             
  65.             action(line)
  66.  
  67.     
  68.     def __after_color_func(self, line):
  69.         if self._Colors__token_info == 'cw<ci<font-color':
  70.             hex_num = int(line[20:-1])
  71.             hex_num = self._Colors__figure_num(hex_num)
  72.             if hex_num:
  73.                 self._Colors__write_obj.write('cw<ci<font-color<nu<%s\n' % hex_num)
  74.             
  75.         elif line[0:5] == 'cw<bd':
  76.             the_index = line.find('bdr-color_')
  77.             if the_index > -1:
  78.                 line = re.sub(self._Colors__line_color_exp, self._Colors__sub_from_line_color, line)
  79.             
  80.             self._Colors__write_obj.write(line)
  81.         else:
  82.             self._Colors__write_obj.write(line)
  83.  
  84.     
  85.     def __sub_from_line_color(self, match_obj):
  86.         num = match_obj.group(1)
  87.         
  88.         try:
  89.             num = int(num)
  90.         except ValueError:
  91.             if self._Colors__run_level > 3:
  92.                 msg = "can't make integer from string\n"
  93.                 raise self._Colors__bug_handler, msg
  94.             self._Colors__run_level > 3
  95.             return 'bdr-color_:no-value'
  96.  
  97.         hex_num = self._Colors__figure_num(num)
  98.         return_value = 'bdr-color_:%s' % hex_num
  99.         return return_value
  100.  
  101.     
  102.     def __figure_num(self, num):
  103.         if num == 0:
  104.             hex_num = 'false'
  105.         else:
  106.             hex_num = self._Colors__color_dict.get(num)
  107.         if hex_num == None:
  108.             if self._Colors__run_level > 3:
  109.                 msg = 'no value in self.__color_dict for key %s\n' % num
  110.                 raise self._Colors__bug_hanlder, msg
  111.             self._Colors__run_level > 3
  112.         
  113.         if hex_num == None:
  114.             hex_num = '0'
  115.         
  116.         return hex_num
  117.  
  118.     
  119.     def __do_nothing_func(self, line):
  120.         pass
  121.  
  122.     
  123.     def convert_colors(self):
  124.         self._Colors__initiate_values()
  125.         read_obj = open(self._Colors__file, 'r')
  126.         self._Colors__write_obj = open(self._Colors__write_to, 'w')
  127.         line_to_read = 1
  128.         while line_to_read:
  129.             line_to_read = read_obj.readline()
  130.             line = line_to_read
  131.             self._Colors__token_info = line[:16]
  132.             action = self._Colors__state_dict.get(self._Colors__state)
  133.             if action == None:
  134.                 sys.stderr.write('no no matching state in module fonts.py\n')
  135.                 sys.stderr.write(self._Colors__state + '\n')
  136.             
  137.             action(line)
  138.         read_obj.close()
  139.         self._Colors__write_obj.close()
  140.         copy_obj = copy.Copy(bug_handler = self._Colors__bug_handler)
  141.         if self._Colors__copy:
  142.             copy_obj.copy_file(self._Colors__write_to, 'color.data')
  143.         
  144.         copy_obj.rename(self._Colors__write_to, self._Colors__file)
  145.         os.remove(self._Colors__write_to)
  146.  
  147.  
  148.