Logo Search packages:      
Sourcecode: calibre version File versions  Download package

preamble_rest.py

#########################################################################
#                                                                       #
#                                                                       #
#   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.                            #
#                                                                       #
#   You should have received a copy of the GNU General Public License   #
#   along with this program; if not, write to the Free Software         #
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA            #
#   02111-1307 USA                                                      #
#                                                                       #
#                                                                       #
#########################################################################
import sys,os
from calibre.ebooks.rtf2xml import copy
00020 class Preamble:
    """
    Fix the reamaing parts of the preamble. This module does very little. It
    makes sure that no text gets put in the revision of list table. In the
    future, when I understand how to interprett he revision table and list
    table, I will make these methods more functional.
    """
00027     def __init__(self, file, bug_handler,  platform, default_font, code_page,
    copy=None, temp_dir=None):
        """
        Required:
            file--file to parse
            platform --Windows or Macintosh
            default_font -- the default font
            code_page --the code page (ansi1252, for example)
        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=file
        self.__bug_handler = bug_handler
        self.__copy = copy
        self.__default_font = default_font
        self.__code_page = code_page
        self.__platform = platform
        if temp_dir:
            self.__write_to = os.path.join(temp_dir,"info_table_info.data")
        else:
            self.__write_to = "info_table_info.data"
00052     def __initiate_values(self):
        """
        Initiate all values.
        """
        self.__state = 'default'
        self.__text_string = ''
        self.__state_dict = {
        'default'   : self.__default_func,
        'revision'  : self.__revision_table_func,
        'list_table'  : self.__list_table_func,
        'body'        : self.__body_func,
        }
        self.__default_dict = {
        'mi<mk<rtfhed-beg'      : self.__found_rtf_head_func,
        'mi<mk<listabbeg_'      : self.__found_list_table_func,
        'mi<mk<revtbl-beg'      : self.__found_revision_table_func,
        'mi<mk<body-open_'      : self.__found_body_func,
        }
    def __default_func(self, line):
        action = self.__default_dict.get(self.__token_info)
        if action:
            action(line)
        else:
            self.__write_obj.write(line)
00076     def __found_rtf_head_func(self, line):
        """
        Requires:
            line -- the line to parse
        Returns:
            nothing.
        Logic:
            Write to the output file the default font info, the code page
            info, and the platform info.
        """
        self.__write_obj.write(
            'mi<tg<empty-att_<rtf-definition'
            '<default-font>%s<code-page>%s'
            '<platform>%s\n' % (self.__default_font, self.__code_page,
            self.__platform)
        )
    def __found_list_table_func(self, line):
        self.__state = 'list_table'
    def __list_table_func(self, line):
        if self.__token_info == 'mi<mk<listabend_':
            self.__state = 'default'
        elif line[0:2] == 'tx':
            pass
        else:
            self.__write_obj.write(line)
    def __found_revision_table_func(self, line):
        self.__state = 'revision'
    def __revision_table_func(self, line):
        if self.__token_info == 'mi<mk<revtbl-end':
            self.__state = 'default'
        elif line[0:2] == 'tx':
            pass
        else:
            self.__write_obj.write(line)
    def __found_body_func(self, line):
        self.__state = 'body'
        self.__write_obj.write(line)
    def __body_func(self, line):
        self.__write_obj.write(line)
00115     def fix_preamble(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. The state can either be defaut, the revision table, or
            the list table.
        """
        self.__initiate_values()
        read_obj = open(self.__file, 'r')
        self.__write_obj = open(self.__write_to, 'w')
        line_to_read = 1
        while line_to_read:
            line_to_read = read_obj.readline()
            line = line_to_read
            self.__token_info = line[:16]
            action = self.__state_dict.get(self.__state)
            if action == None:
                sys.stderr.write('no no matching state in module preamble_rest.py\n')
                sys.stderr.write(self.__state + '\n')
            action(line)
        read_obj.close()
        self.__write_obj.close()
        copy_obj = copy.Copy(bug_handler = self.__bug_handler)
        if self.__copy:
            copy_obj.copy_file(self.__write_to, "preamble_div.data")
        copy_obj.rename(self.__write_to, self.__file)
        os.remove(self.__write_to)

Generated by  Doxygen 1.6.0   Back to index