home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import sys
- import os
- 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
- from calibre.ebooks.rtf2xml.old_rtf import OldRtf
-
- class InvalidRtfException(Exception):
- pass
-
-
- class RtfInvalidCodeException(Exception):
- pass
-
-
- class ParseRtf:
-
- 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 = ''):
- self._ParseRtf__file = in_file
- self._ParseRtf__out_file = out_file
- self._ParseRtf__out_dir = out_dir
- self._ParseRtf__temp_dir = out_dir
- self._ParseRtf__dtd_path = dtd
- self._ParseRtf__check_file(in_file, 'file_to_parse')
- self._ParseRtf__char_data = char_data
- self._ParseRtf__debug_dir = deb_dir
- self._ParseRtf__check_dir(self._ParseRtf__temp_dir)
- self._ParseRtf__copy = self._ParseRtf__check_dir(self._ParseRtf__debug_dir)
- self._ParseRtf__convert_caps = convert_caps
- self._ParseRtf__convert_symbol = convert_symbol
- self._ParseRtf__convert_wingdings = convert_wingdings
- self._ParseRtf__convert_zapf = convert_zapf
- self._ParseRtf__run_level = run_level
- self._ParseRtf__exit_level = 0
- self._ParseRtf__indent = indent
- self._ParseRtf__replace_illegals = replace_illegals
- self._ParseRtf__form_lists = form_lists
- self._ParseRtf__headings_to_sections = headings_to_sections
- self._ParseRtf__group_styles = group_styles
- self._ParseRtf__group_borders = group_borders
- self._ParseRtf__empty_paragraphs = empty_paragraphs
- self._ParseRtf__no_dtd = no_dtd
-
-
- def __check_file(self, the_file, type):
- if hasattr(the_file, 'read'):
- return None
- if the_file == None:
- if type == 'file_to_parse':
- message = 'You must provide a file for the script to work'
-
- msg = message
- raise RtfInvalidCodeException, msg
- the_file == None
- if os.path.exists(the_file):
- pass
- else:
- message = "The file '%s' cannot be found" % the_file
- msg = message
- raise RtfInvalidCodeException, msg
- return hasattr(the_file, 'read')
-
-
- def __check_dir(self, the_dir):
- if not the_dir:
- return None
- dir_exists = os.path.isdir(the_dir)
- if not dir_exists:
- message = '%s is not a directory' % the_dir
- msg = message
- raise RtfInvalidCodeException, msg
- dir_exists
- return 1
-
-
- def parse_rtf(self):
- self._ParseRtf__temp_file = self._ParseRtf__make_temp_file(self._ParseRtf__file)
- if self._ParseRtf__debug_dir:
- copy_obj = copy.Copy(bug_handler = RtfInvalidCodeException)
- copy_obj.set_dir(self._ParseRtf__debug_dir)
- copy_obj.remove_files()
- copy_obj.copy_file(self._ParseRtf__temp_file, 'original_file')
-
- if self._ParseRtf__debug_dir or self._ParseRtf__run_level > 2:
- self._ParseRtf__check_brack_obj = check_brackets.CheckBrackets(file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException)
-
- 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)
- return_value = line_obj.fix_endings()
- self._ParseRtf__return_code(return_value)
- tokenize_obj = tokenize.Tokenize(bug_handler = RtfInvalidCodeException, in_file = self._ParseRtf__temp_file, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level)
- tokenize_obj.tokenize()
- 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)
-
- try:
- return_value = process_tokens_obj.process_tokens()
- except InvalidRtfException:
- msg = None
-
- try:
- os.remove(self._ParseRtf__temp_file)
- except OSError:
- pass
-
- check_encoding_obj = check_encoding.CheckEncoding(bug_handler = RtfInvalidCodeException)
- check_encoding_obj.check_encoding(self._ParseRtf__file)
- None(sys.stderr.write if isinstance(self._ParseRtf__file, str) else self._ParseRtf__file.encode('utf-8'))
- raise InvalidRtfException, msg
-
- 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)
- found_destination = delete_info_obj.delete_info()
- self._ParseRtf__bracket_match('delete_data_info')
- 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)
- pict_obj.process_pict()
- self._ParseRtf__bracket_match('pict_data_info')
- combine_obj = combine_borders.CombineBorders(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level)
- combine_obj.combine_borders()
- self._ParseRtf__bracket_match('combine_borders_info')
- footnote_obj = footnote.Footnote(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level)
- footnote_obj.separate_footnotes()
- self._ParseRtf__bracket_match('separate_footnotes_info')
- header_obj = header.Header(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level)
- header_obj.separate_headers()
- self._ParseRtf__bracket_match('separate_headers_info')
- 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)
- list_numbers_obj.fix_list_numbers()
- self._ParseRtf__bracket_match('list_number_info')
- 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)
- list_of_lists = preamble_div_obj.make_preamble_divisions()
- self._ParseRtf__bracket_match('make_preamble_divisions')
- encode_obj = default_encoding.DefaultEncoding(in_file = self._ParseRtf__temp_file, run_level = self._ParseRtf__run_level, bug_handler = RtfInvalidCodeException)
- (platform, code_page, default_font_num) = encode_obj.find_default_encoding()
- 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)
- hex2utf_obj.convert_hex_2_utf8()
- self._ParseRtf__bracket_match('hex_2_utf_preamble')
- 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)
- special_font_dict = fonts_obj.convert_fonts()
- self._ParseRtf__bracket_match('fonts_info')
- color_obj = colors.Colors(in_file = self._ParseRtf__temp_file, copy = self._ParseRtf__copy, bug_handler = RtfInvalidCodeException, run_level = self._ParseRtf__run_level)
- color_obj.convert_colors()
- self._ParseRtf__bracket_match('colors_info')
- style_obj = styles.Styles(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level)
- style_obj.convert_styles()
- self._ParseRtf__bracket_match('styles_info')
- info_obj = info.Info(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level)
- info_obj.fix_info()
- default_font = special_font_dict.get('default-font')
- 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)
- preamble_rest_obj.fix_preamble()
- self._ParseRtf__bracket_match('preamble_rest_info')
- old_rtf_obj = OldRtf(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, run_level = self._ParseRtf__run_level)
- old_rtf = old_rtf_obj.check_if_old_rtf()
- if old_rtf:
- if self._ParseRtf__run_level > 5:
- msg = 'older RTF\n'
- msg += 'self.__run_level is "%s"\n' % self._ParseRtf__run_level
- raise RtfInvalidCodeException, msg
- self._ParseRtf__run_level > 5
- if self._ParseRtf__run_level > 1:
- sys.stderr.write('File could be older RTF...\n')
-
- if found_destination:
- if self._ParseRtf__run_level > 1:
- sys.stderr.write('File also has newer RTF.\nWill do the best to convert.\n')
-
-
- 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)
- add_brackets_obj.add_brackets()
-
- 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)
- fields_small_obj.fix_fields()
- self._ParseRtf__bracket_match('fix_small_fields_info')
- 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)
- fields_large_obj.fix_fields()
- self._ParseRtf__bracket_match('fix_large_fields_info')
- sections_obj = sections.Sections(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level)
- sections_obj.make_sections()
- self._ParseRtf__bracket_match('sections_info')
- 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)
- paragraphs_obj.make_paragraphs()
- self._ParseRtf__bracket_match('paragraphs_info')
- default_font = special_font_dict['default-font']
- 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)
- list_of_styles = paragraph_def_obj.make_paragraph_def()
- 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)
- body_styles_obj.insert_info()
- self._ParseRtf__bracket_match('body_styles_info')
- self._ParseRtf__bracket_match('paragraph_def_info')
- table_obj = table.Table(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level)
- table_data = table_obj.make_table()
- self._ParseRtf__bracket_match('table_info')
- 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)
- table_info_obj.insert_info()
- self._ParseRtf__bracket_match('table__data_info')
- if self._ParseRtf__form_lists:
- 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)
- make_list_obj.make_lists()
- self._ParseRtf__bracket_match('form_lists_info')
-
- if self._ParseRtf__headings_to_sections:
- 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)
- headings_to_sections_obj.make_sections()
- self._ParseRtf__bracket_match('headings_to_sections_info')
-
- if self._ParseRtf__group_styles:
- 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)
- group_styles_obj.group_styles()
- self._ParseRtf__bracket_match('group_styles_info')
-
- if self._ParseRtf__group_borders:
- 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)
- group_borders_obj.group_borders()
- self._ParseRtf__bracket_match('group_borders_info')
-
- inline_obj = inline.Inline(in_file = self._ParseRtf__temp_file, bug_handler = RtfInvalidCodeException, copy = self._ParseRtf__copy, run_level = self._ParseRtf__run_level)
- inline_obj.form_tags()
- self._ParseRtf__bracket_match('inline_info')
- 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)
- hex2utf_obj.convert_hex_2_utf8()
- header_obj.join_headers()
- footnote_obj.join_footnotes()
- 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)
- tags_obj.convert_to_tags()
- 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)
- output_obj.output()
- os.remove(self._ParseRtf__temp_file)
- return self._ParseRtf__exit_level
-
-
- def __bracket_match(self, file_name):
- if self._ParseRtf__run_level > 2:
- (good_br, msg) = self._ParseRtf__check_brack_obj.check_brackets()
- if good_br:
- pass
- else:
- msg += msg + " in file '" + file_name + "'\n"
- raise RtfInvalidCodeException, msg
- good_br
-
-
- def __return_code(self, num):
- if num == None:
- return None
- if int(num) > self._ParseRtf__exit_level:
- self._ParseRtf__exit_level = num
-
-
-
- def __make_temp_file(self, file):
- write_file = 'rtf_write_file'
- read_obj = None if hasattr(file, 'read') else open(file, 'r')
- write_obj = open(write_file, 'w')
- line = 'dummy'
- while line:
- line = read_obj.read(1000)
- write_obj.write(line)
- write_obj.close()
- return write_file
-
-
-