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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import os
  6. import tempfile
  7. from calibre.ebooks.rtf2xml import copy
  8.  
  9. class Pict:
  10.     
  11.     def __init__(self, in_file, bug_handler, out_file, copy = None, orig_file = None, run_level = 1):
  12.         self._Pict__file = in_file
  13.         self._Pict__bug_handler = bug_handler
  14.         self._Pict__copy = copy
  15.         self._Pict__run_level = run_level
  16.         self._Pict__write_to = tempfile.mktemp()
  17.         self._Pict__bracket_count = 0
  18.         self._Pict__ob_count = 0
  19.         self._Pict__cb_count = 0
  20.         self._Pict__pict_count = 0
  21.         self._Pict__in_pict = 0
  22.         self._Pict__already_found_pict = 0
  23.         self._Pict__orig_file = orig_file
  24.         self._Pict__initiate_pict_dict()
  25.         self._Pict__out_file = out_file
  26.         self._Pict__no_ask = 1
  27.  
  28.     
  29.     def __initiate_pict_dict(self):
  30.         self._Pict__pict_dict = {
  31.             'ob<nu<open-brack': self._Pict__open_br_func,
  32.             'cb<nu<clos-brack': self._Pict__close_br_func,
  33.             'tx<nu<__________': self._Pict__text_func }
  34.  
  35.     
  36.     def __open_br_func(self, line):
  37.         return '{\n'
  38.  
  39.     
  40.     def __close_br_func(self, line):
  41.         return '}\n'
  42.  
  43.     
  44.     def __text_func(self, line):
  45.         return line[17:]
  46.  
  47.     
  48.     def __make_dir(self):
  49.         base_name = os.path.basename(getattr(self._Pict__orig_file, 'name', self._Pict__orig_file))
  50.         base_name = os.path.splitext(base_name)[0]
  51.         if self._Pict__out_file:
  52.             dir_name = os.path.dirname(getattr(self._Pict__out_file, 'name', self._Pict__out_file))
  53.         else:
  54.             dir_name = os.path.dirname(self._Pict__orig_file)
  55.         self._Pict__dir_name = base_name + '_rtf_pict_dir/'
  56.         self._Pict__dir_name = os.path.join(dir_name, self._Pict__dir_name)
  57.         if not os.path.isdir(self._Pict__dir_name):
  58.             
  59.             try:
  60.                 os.mkdir(self._Pict__dir_name)
  61.             except OSError:
  62.                 msg = None
  63.                 msg = str(msg)
  64.                 msg += "Couldn't make directory '%s':\n" % self._Pict__dir_name
  65.                 raise self._Pict__bug_handler
  66.             except:
  67.                 None<EXCEPTION MATCH>OSError
  68.             
  69.  
  70.         None<EXCEPTION MATCH>OSError
  71.         if self._Pict__no_ask:
  72.             user_response = 'r'
  73.         else:
  74.             msg = 'Do you want to remove all files in %s?\n' % self._Pict__dir_name
  75.             msg += 'Type "r" to remove.\n'
  76.             msg += 'Type any other key to keep files in place.\n'
  77.             sys.stderr.write(msg)
  78.             user_response = raw_input()
  79.         if user_response == 'r':
  80.             if self._Pict__run_level > 1:
  81.                 sys.stderr.write('Removing files from old pict directory...\n')
  82.             
  83.             all_files = os.listdir(self._Pict__dir_name)
  84.             for the_file in all_files:
  85.                 the_file = os.path.join(self._Pict__dir_name, the_file)
  86.                 
  87.                 try:
  88.                     os.remove(the_file)
  89.                 continue
  90.                 except OSError:
  91.                     continue
  92.                 
  93.  
  94.             
  95.             if self._Pict__run_level > 1:
  96.                 sys.stderr.write('Files removed.\n')
  97.             
  98.         
  99.  
  100.     
  101.     def __create_pict_file(self):
  102.         self._Pict__pict_file = os.path.join(self._Pict__dir_name, 'picts.rtf')
  103.         write_pic_obj = open(self._Pict__pict_file, 'w')
  104.         write_pic_obj.close()
  105.         self._Pict__write_pic_obj = open(self._Pict__pict_file, 'a')
  106.  
  107.     
  108.     def __in_pict_func(self, line):
  109.         if self._Pict__cb_count == self._Pict__pict_br_count:
  110.             self._Pict__in_pict = 0
  111.             self._Pict__write_pic_obj.write('}\n')
  112.             return 1
  113.         action = self._Pict__pict_dict.get(self._Pict__token_info)
  114.         if action:
  115.             line = action(line)
  116.             self._Pict__write_pic_obj.write(line)
  117.         
  118.         return 0
  119.  
  120.     
  121.     def __default(self, line, write_obj):
  122.         if self._Pict__token_info == 'cw<gr<picture___':
  123.             self._Pict__pict_count += 1
  124.             write_obj.write('mi<mk<pict-start\n')
  125.             write_obj.write('mi<tg<empty-att_<pict<num>%03d\n' % self._Pict__pict_count)
  126.             write_obj.write('mi<mk<pict-end__\n')
  127.             if not self._Pict__already_found_pict:
  128.                 self._Pict__create_pict_file()
  129.                 self._Pict__already_found_pict = 1
  130.                 self._Pict__print_rtf_header()
  131.             
  132.             self._Pict__in_pict = 1
  133.             self._Pict__pict_br_count = self._Pict__ob_count
  134.             self._Pict__cb_count = 0
  135.             self._Pict__write_pic_obj.write('{\\pict\n')
  136.             return 0
  137.         return 1
  138.  
  139.     
  140.     def __print_rtf_header(self):
  141.         self._Pict__write_pic_obj.write('{\\rtf1 \n')
  142.         self._Pict__write_pic_obj.write('{\\fonttbl\\f0\\null;} \n')
  143.         self._Pict__write_pic_obj.write('{\\colortbl\\red255\\green255\\blue255;} \n')
  144.         self._Pict__write_pic_obj.write('\\pard \n')
  145.  
  146.     
  147.     def process_pict(self):
  148.         self._Pict__make_dir()
  149.         read_obj = open(self._Pict__file)
  150.         write_obj = open(self._Pict__write_to, 'w')
  151.         line_to_read = 'dummy'
  152.         while line_to_read:
  153.             line_to_read = read_obj.readline()
  154.             line = line_to_read
  155.             self._Pict__token_info = line[:16]
  156.             if self._Pict__token_info == 'ob<nu<open-brack':
  157.                 self._Pict__ob_count = line[-5:-1]
  158.             
  159.             if self._Pict__token_info == 'cb<nu<clos-brack':
  160.                 self._Pict__cb_count = line[-5:-1]
  161.             
  162.             if not self._Pict__in_pict:
  163.                 to_print = self._Pict__default(line, write_obj)
  164.                 if to_print:
  165.                     write_obj.write(line)
  166.                 
  167.             to_print
  168.             to_print = self._Pict__in_pict_func(line)
  169.             if to_print:
  170.                 write_obj.write(line)
  171.                 continue
  172.         if self._Pict__already_found_pict:
  173.             self._Pict__write_pic_obj.write('}\n')
  174.             self._Pict__write_pic_obj.close()
  175.         
  176.         read_obj.close()
  177.         write_obj.close()
  178.         copy_obj = copy.Copy(bug_handler = self._Pict__bug_handler)
  179.         if self._Pict__copy:
  180.             copy_obj.copy_file(self._Pict__write_to, 'pict.data')
  181.         
  182.         copy_obj.rename(self._Pict__write_to, self._Pict__file)
  183.         os.remove(self._Pict__write_to)
  184.         if self._Pict__pict_count == 0:
  185.             
  186.             try:
  187.                 os.rmdir(self._Pict__dir_name)
  188.             except OSError:
  189.                 pass
  190.             except:
  191.                 None<EXCEPTION MATCH>OSError
  192.             
  193.  
  194.         None<EXCEPTION MATCH>OSError
  195.  
  196.  
  197.