from __future__ import unicode_literals, absolute_import, print_function, division ######################################################################### # # # # # copyright 2002 Paul Henry Tremblay # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # # General Public License for more details. # # # # # ######################################################################### import sys, os, re from calibre.ebooks.rtf2xml import copy from calibre.ptempfile import better_mktemp from . import open_for_read, open_for_write class Info: """ Make tags for document-information """ def __init__(self, in_file, bug_handler, copy=None, run_level=1, ): """ Required: 'file'--file to parse Optional: 'copy'-- whether to make a copy of result for debugging 'temp_dir' --where to output temporary results (default is directory from which the script is run.) Returns: nothing """ self.__file = in_file self.__bug_handler = bug_handler self.__copy = copy self.__run_level = run_level self.__write_to = better_mktemp() def __initiate_values(self): """ Initiate all values. """ self.__text_string = '' self.__state = 'before_info_table' self.rmspace = re.compile(r'\s+') self.__state_dict = { 'before_info_table': self.__before_info_table_func, 'after_info_table': self.__after_info_table_func, 'in_info_table' : self.__in_info_table_func, 'collect_text' : self.__collect_text_func, 'collect_tokens' : self.__collect_tokens_func, } self.__info_table_dict = { 'cw33\n def __collect_tokens_func(self, line): """ Requires: line -- line to parse Returns: nothing Logic: This function collects all the token information and adds it to the text string until the end of the field is found. First check of the end of the information field. If found, write the text string to the file. If not found, get the relevant information from the text string. This information cannot be directly added to the text string, because it exists in abbreviated form. (num-of-wor) I want to check this information in a dictionary to convert it to a longer, readable form. If the key does not exist in the dictionary, print out an error message. Otherise add the value to the text string. (num-of-wor => number-of-words) """ # cw 3: msg = 'No dictionary match for %s\n' % att raise self.__bug_handler(msg) else: self.__text_string += '<%s>%s' % (att_changed, value) def __single_field_func(self, line, tag): value = line[20:-1] self.__write_obj.write( 'mi%s\n' % (tag, tag, value) ) def __after_info_table_func(self, line): """ Requires: line --line to write to file Returns: nothing Logic: After the end of the information table, simple write the line to the file. """ self.__write_obj.write(line) def fix_info(self): """ Requires: nothing Returns: nothing (changes the original file) Logic: Read one line in at a time. Determine what action to take based on the state. If the state is before the information table, look for the beginning of the style table. If the state is in the information table, use other methods to parse the information style table, look for lines with style info, and substitute the number with the name of the style. If the state if afer the information table, simply write the line to the output file. """ self.__initiate_values() with open_for_read(self.__file) as read_obj: with open_for_write(self.__write_to) as self.__write_obj: for line in read_obj: self.__token_info = line[:16] action = self.__state_dict.get(self.__state) if action is None: sys.stderr.write('No matching state in module styles.py\n') sys.stderr.write(self.__state + '\n') action(line) copy_obj = copy.Copy(bug_handler=self.__bug_handler) if self.__copy: copy_obj.copy_file(self.__write_to, "info.data") copy_obj.rename(self.__write_to, self.__file) os.remove(self.__write_to)