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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import os
  6. from calibre.ebooks.rtf2xml import headings_to_sections, line_endings, footnote, fields_small, default_encoding, make_lists, preamble_div, header, colors, group_borders, check_encoding, add_brackets, table, combine_borders, fields_large, process_tokens, hex_2_utf8, tokenize, delete_info, sections, check_brackets, styles, paragraph_def, convert_to_tags, output, copy, list_numbers, info, pict, table_info, fonts, paragraphs, body_styles, preamble_rest, group_styles, inline
  7. from calibre.ebooks.rtf2xml.old_rtf import OldRtf
  8.  
  9. class InvalidRtfException(Exception):
  10.     pass
  11.  
  12.  
  13. class RtfInvalidCodeException(Exception):
  14.     pass
  15.  
  16.  
  17. class ParseRtf:
  18.     
  19.     def __init__(self, in_file, out_file = '', out_dir = None, dtd = '', deb_dir = None, convert_symbol = None, convert_wingdings = None, convert_zapf = None, convert_caps = None, run_level = 1, indent = None, replace_illegals = 1, form_lists = 1, headings_to_sections = 1, group_styles = 1, group_borders = 1, empty_paragraphs = 1, no_dtd = 0, char_data = ''):
  20.         self._ParseRtf__file = in_file
  21.         self._ParseRtf__out_file = out_file
  22.         self._ParseRtf__out_dir = out_dir
  23.         self._ParseRtf__temp_dir = out_dir
  24.         self._ParseRtf__dtd_path = dtd
  25.         self._ParseRtf__check_file(in_file, 'file_to_parse')
  26.         self._ParseRtf__char_data = char_data
  27.         self._ParseRtf__debug_dir = deb_dir
  28.         self._ParseRtf__check_dir(self._ParseRtf__temp_dir)
  29.         self._ParseRtf__copy = self._ParseRtf__check_dir(self._ParseRtf__debug_dir)
  30.         self._ParseRtf__convert_caps = convert_caps
  31.         self._ParseRtf__convert_symbol = convert_symbol
  32.         self._ParseRtf__convert_wingdings = convert_wingdings
  33.         self._ParseRtf__convert_zapf = convert_zapf
  34.         self._ParseRtf__run_level = run_level
  35.         self._ParseRtf__exit_level = 0
  36.         self._ParseRtf__indent = indent
  37.         self._ParseRtf__replace_illegals = replace_illegals
  38.         self._ParseRtf__form_lists = form_lists
  39.         self._ParseRtf__headings_to_sections = headings_to_sections
  40.         self._ParseRtf__group_styles = group_styles
  41.         self._ParseRtf__group_borders = group_borders
  42.         self._ParseRtf__empty_paragraphs = empty_paragraphs
  43.         self._ParseRtf__no_dtd = no_dtd
  44.  
  45.     
  46.     def __check_file(self, the_file, type):
  47.         if hasattr(the_file, 'read'):
  48.             return None
  49.         if the_file == None:
  50.             if type == 'file_to_parse':
  51.                 message = 'You must provide a file for the script to work'
  52.             
  53.             msg = message
  54.             raise RtfInvalidCodeException, msg
  55.         the_file == None
  56.         if os.path.exists(the_file):
  57.             pass
  58.         else:
  59.             message = "The file '%s' cannot be found" % the_file
  60.             msg = message
  61.             raise RtfInvalidCodeException, msg
  62.         return hasattr(the_file, 'read')
  63.  
  64.     
  65.     def __check_dir(self, the_dir):
  66.         if not the_dir:
  67.             return None
  68.         dir_exists = os.path.isdir(the_dir)
  69.         if not dir_exists:
  70.             message = '%s is not a directory' % the_dir
  71.             msg = message
  72.             raise RtfInvalidCodeException, msg
  73.         dir_exists
  74.         return 1
  75.  
  76.     
  77.     def parse_rtf(self):
  78.         self._ParseRtf__temp_file = self._ParseRtf__make_temp_file(self._ParseRtf__file)
  79.         if self._ParseRtf__debug_dir:
  80.             copy_obj = copy.Copy(bug_handler = RtfInvalidCodeException)
  81.             copy_obj.set_dir(self._ParseRtf__debug_dir)
  82.             copy_obj.remove_files()
  83.             copy_obj.copy_file(self._ParseRtf__temp_file, 'original_file')
  84.         
  85.         if self._ParseRtf__debug_dir or self._ParseRtf__run_level > 2:
  86.             self._ParseRtf__check_brack_obj = check_brackets.CheckBrackets(file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException)
  87.         
  88.         line_obj = line_endings.FixLineEndings(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level, replace_illegals = self._ParseRtf__replace_illegals)
  89.         return_value = line_obj.fix_endings()
  90.         self._ParseRtf__return_code(return_value)
  91.         tokenize_obj = tokenize.Tokenize(bug_handler = RtfInvalidCodeException, in_file = self._ParseRtf__temp_file, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level)
  92.         tokenize_obj.tokenize()
  93.         process_tokens_obj = process_tokens.ProcessTokens(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level, exception_handler = InvalidRtfException)
  94.         
  95.         try:
  96.             return_value = process_tokens_obj.process_tokens()
  97.         except InvalidRtfException:
  98.             msg = None
  99.             
  100.             try:
  101.                 os.remove(self._ParseRtf__temp_file)
  102.             except OSError:
  103.                 pass
  104.  
  105.             check_encoding_obj = check_encoding.CheckEncoding(bug_handler = RtfInvalidCodeException)
  106.             check_encoding_obj.check_encoding(self._ParseRtf__file)
  107.             None(sys.stderr.write if isinstance(self._ParseRtf__file, str) else self._ParseRtf__file.encode('utf-8'))
  108.             raise InvalidRtfException, msg
  109.  
  110.         delete_info_obj = delete_info.DeleteInfo(in_file = self._ParseRtf__temp_file, copy = self._ParseRtf__copy, bug_handler = RtfInvalidCodeException, run_level = self._ParseRtf__run_level)
  111.         found_destination = delete_info_obj.delete_info()
  112.         self._ParseRtf__bracket_match('delete_data_info')
  113.         pict_obj = pict.Pict(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, orig_file = self._ParseRtf__file, out_file = self._ParseRtf__out_file, run_level = self._ParseRtf__run_level)
  114.         pict_obj.process_pict()
  115.         self._ParseRtf__bracket_match('pict_data_info')
  116.         combine_obj = combine_borders.CombineBorders(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level)
  117.         combine_obj.combine_borders()
  118.         self._ParseRtf__bracket_match('combine_borders_info')
  119.         footnote_obj = footnote.Footnote(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level)
  120.         footnote_obj.separate_footnotes()
  121.         self._ParseRtf__bracket_match('separate_footnotes_info')
  122.         header_obj = header.Header(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level)
  123.         header_obj.separate_headers()
  124.         self._ParseRtf__bracket_match('separate_headers_info')
  125.         list_numbers_obj = list_numbers.ListNumbers(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level)
  126.         list_numbers_obj.fix_list_numbers()
  127.         self._ParseRtf__bracket_match('list_number_info')
  128.         preamble_div_obj = preamble_div.PreambleDiv(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level)
  129.         list_of_lists = preamble_div_obj.make_preamble_divisions()
  130.         self._ParseRtf__bracket_match('make_preamble_divisions')
  131.         encode_obj = default_encoding.DefaultEncoding(in_file = self._ParseRtf__temp_file, run_level = self._ParseRtf__run_level, bug_handler = RtfInvalidCodeException)
  132.         (platform, code_page, default_font_num) = encode_obj.find_default_encoding()
  133.         hex2utf_obj = hex_2_utf8.Hex2Utf8(in_file = self._ParseRtf__temp_file, copy = self._ParseRtf__copy, area_to_convert = 'preamble', char_file = self._ParseRtf__char_data, default_char_map = code_page, run_level = self._ParseRtf__run_level, bug_handler = RtfInvalidCodeException, invalid_rtf_handler = InvalidRtfException)
  134.         hex2utf_obj.convert_hex_2_utf8()
  135.         self._ParseRtf__bracket_match('hex_2_utf_preamble')
  136.         fonts_obj = fonts.Fonts(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, default_font_num = default_font_num, run_level = self._ParseRtf__run_level)
  137.         special_font_dict = fonts_obj.convert_fonts()
  138.         self._ParseRtf__bracket_match('fonts_info')
  139.         color_obj = colors.Colors(in_file = self._ParseRtf__temp_file, copy = self._ParseRtf__copy, bug_handler = RtfInvalidCodeException, run_level = self._ParseRtf__run_level)
  140.         color_obj.convert_colors()
  141.         self._ParseRtf__bracket_match('colors_info')
  142.         style_obj = styles.Styles(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level)
  143.         style_obj.convert_styles()
  144.         self._ParseRtf__bracket_match('styles_info')
  145.         info_obj = info.Info(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level)
  146.         info_obj.fix_info()
  147.         default_font = special_font_dict.get('default-font')
  148.         preamble_rest_obj = preamble_rest.Preamble(file = self._ParseRtf__temp_file, copy = self._ParseRtf__copy, bug_handler = RtfInvalidCodeException, platform = platform, default_font = default_font, code_page = code_page)
  149.         preamble_rest_obj.fix_preamble()
  150.         self._ParseRtf__bracket_match('preamble_rest_info')
  151.         old_rtf_obj = OldRtf(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, run_level = self._ParseRtf__run_level)
  152.         old_rtf = old_rtf_obj.check_if_old_rtf()
  153.         if old_rtf:
  154.             if self._ParseRtf__run_level > 5:
  155.                 msg = 'older RTF\n'
  156.                 msg += 'self.__run_level is "%s"\n' % self._ParseRtf__run_level
  157.                 raise RtfInvalidCodeException, msg
  158.             self._ParseRtf__run_level > 5
  159.             if self._ParseRtf__run_level > 1:
  160.                 sys.stderr.write('File could be older RTF...\n')
  161.             
  162.             if found_destination:
  163.                 if self._ParseRtf__run_level > 1:
  164.                     sys.stderr.write('File also has newer RTF.\nWill do the best to convert.\n')
  165.                 
  166.             
  167.             add_brackets_obj = add_brackets.AddBrackets(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level)
  168.             add_brackets_obj.add_brackets()
  169.         
  170.         fields_small_obj = fields_small.FieldsSmall(in_file = self._ParseRtf__temp_file, copy = self._ParseRtf__copy, bug_handler = RtfInvalidCodeException, run_level = self._ParseRtf__run_level)
  171.         fields_small_obj.fix_fields()
  172.         self._ParseRtf__bracket_match('fix_small_fields_info')
  173.         fields_large_obj = fields_large.FieldsLarge(in_file = self._ParseRtf__temp_file, copy = self._ParseRtf__copy, bug_handler = RtfInvalidCodeException, run_level = self._ParseRtf__run_level)
  174.         fields_large_obj.fix_fields()
  175.         self._ParseRtf__bracket_match('fix_large_fields_info')
  176.         sections_obj = sections.Sections(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level)
  177.         sections_obj.make_sections()
  178.         self._ParseRtf__bracket_match('sections_info')
  179.         paragraphs_obj = paragraphs.Paragraphs(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, write_empty_para = self._ParseRtf__empty_paragraphs, run_level = self._ParseRtf__run_level)
  180.         paragraphs_obj.make_paragraphs()
  181.         self._ParseRtf__bracket_match('paragraphs_info')
  182.         default_font = special_font_dict['default-font']
  183.         paragraph_def_obj = paragraph_def.ParagraphDef(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, default_font = default_font, run_level = self._ParseRtf__run_level)
  184.         list_of_styles = paragraph_def_obj.make_paragraph_def()
  185.         body_styles_obj = body_styles.BodyStyles(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, list_of_styles = list_of_styles, run_level = self._ParseRtf__run_level)
  186.         body_styles_obj.insert_info()
  187.         self._ParseRtf__bracket_match('body_styles_info')
  188.         self._ParseRtf__bracket_match('paragraph_def_info')
  189.         table_obj = table.Table(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level)
  190.         table_data = table_obj.make_table()
  191.         self._ParseRtf__bracket_match('table_info')
  192.         table_info_obj = table_info.TableInfo(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, table_data = table_data, run_level = self._ParseRtf__run_level)
  193.         table_info_obj.insert_info()
  194.         self._ParseRtf__bracket_match('table__data_info')
  195.         if self._ParseRtf__form_lists:
  196.             make_list_obj = make_lists.MakeLists(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, headings_to_sections = self._ParseRtf__headings_to_sections, run_level = self._ParseRtf__run_level, list_of_lists = list_of_lists)
  197.             make_list_obj.make_lists()
  198.             self._ParseRtf__bracket_match('form_lists_info')
  199.         
  200.         if self._ParseRtf__headings_to_sections:
  201.             headings_to_sections_obj = headings_to_sections.HeadingsToSections(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level)
  202.             headings_to_sections_obj.make_sections()
  203.             self._ParseRtf__bracket_match('headings_to_sections_info')
  204.         
  205.         if self._ParseRtf__group_styles:
  206.             group_styles_obj = group_styles.GroupStyles(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, wrap = 1, run_level = self._ParseRtf__run_level)
  207.             group_styles_obj.group_styles()
  208.             self._ParseRtf__bracket_match('group_styles_info')
  209.         
  210.         if self._ParseRtf__group_borders:
  211.             group_borders_obj = group_borders.GroupBorders(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, wrap = 1, run_level = self._ParseRtf__run_level)
  212.             group_borders_obj.group_borders()
  213.             self._ParseRtf__bracket_match('group_borders_info')
  214.         
  215.         inline_obj = inline.Inline(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level)
  216.         inline_obj.form_tags()
  217.         self._ParseRtf__bracket_match('inline_info')
  218.         hex2utf_obj.update_values(file = self._ParseRtf__temp_file, area_to_convert = 'body', copy = self._ParseRtf__copy, char_file = self._ParseRtf__char_data, convert_caps = self._ParseRtf__convert_caps, convert_symbol = self._ParseRtf__convert_symbol, convert_wingdings = self._ParseRtf__convert_wingdings, convert_zapf = self._ParseRtf__convert_zapf, symbol = 1, wingdings = 1, dingbats = 1)
  219.         hex2utf_obj.convert_hex_2_utf8()
  220.         header_obj.join_headers()
  221.         footnote_obj.join_footnotes()
  222.         tags_obj = convert_to_tags.ConvertToTags(in_file = self._ParseRtf__temp_file, copy = self._ParseRtf__copy, dtd_path = self._ParseRtf__dtd_path, indent = self._ParseRtf__indent, run_level = self._ParseRtf__run_level, no_dtd = self._ParseRtf__no_dtd, bug_handler = RtfInvalidCodeException)
  223.         tags_obj.convert_to_tags()
  224.         output_obj = output.Output(file = self._ParseRtf__temp_file, orig_file = self._ParseRtf__file, output_dir = self._ParseRtf__out_dir, out_file = self._ParseRtf__out_file)
  225.         output_obj.output()
  226.         os.remove(self._ParseRtf__temp_file)
  227.         return self._ParseRtf__exit_level
  228.  
  229.     
  230.     def __bracket_match(self, file_name):
  231.         if self._ParseRtf__run_level > 2:
  232.             (good_br, msg) = self._ParseRtf__check_brack_obj.check_brackets()
  233.             if good_br:
  234.                 pass
  235.             else:
  236.                 msg += msg + " in file '" + file_name + "'\n"
  237.                 raise RtfInvalidCodeException, msg
  238.         good_br
  239.  
  240.     
  241.     def __return_code(self, num):
  242.         if num == None:
  243.             return None
  244.         if int(num) > self._ParseRtf__exit_level:
  245.             self._ParseRtf__exit_level = num
  246.         
  247.  
  248.     
  249.     def __make_temp_file(self, file):
  250.         write_file = 'rtf_write_file'
  251.         read_obj = None if hasattr(file, 'read') else open(file, 'r')
  252.         write_obj = open(write_file, 'w')
  253.         line = 'dummy'
  254.         while line:
  255.             line = read_obj.read(1000)
  256.             write_obj.write(line)
  257.         write_obj.close()
  258.         return write_file
  259.  
  260.  
  261.