home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import os
- import tempfile
- from calibre.ebooks.rtf2xml import copy
-
- class Footnote:
-
- def __init__(self, in_file, bug_handler, copy = None, run_level = 1):
- self._Footnote__file = in_file
- self._Footnote__bug_handler = bug_handler
- self._Footnote__copy = copy
- self._Footnote__write_to = tempfile.mktemp()
- self._Footnote__found_a_footnote = 0
-
-
- def __first_line_func(self, line):
- if self._Footnote__token_info == 'cw<nt<type______':
- self._Footnote__write_to_foot_obj.write('mi<tg<open-att__<footnote<type>endnote<num>%s\n' % self._Footnote__footnote_count)
- else:
- self._Footnote__write_to_foot_obj.write('mi<tg<open-att__<footnote<num>%s\n' % self._Footnote__footnote_count)
- self._Footnote__first_line = 0
-
-
- def __in_footnote_func(self, line):
- if self._Footnote__first_line:
- self._Footnote__first_line_func(line)
-
- if self._Footnote__token_info == 'cw<ci<footnot-mk':
- num = str(self._Footnote__footnote_count)
- self._Footnote__write_to_foot_obj.write(line)
- self._Footnote__write_to_foot_obj.write('tx<nu<__________<%s\n' % num)
-
- if self._Footnote__cb_count == self._Footnote__footnote_bracket_count:
- self._Footnote__in_footnote = 0
- self._Footnote__write_obj.write(line)
- self._Footnote__write_to_foot_obj.write('mi<mk<foot___clo\n')
- self._Footnote__write_to_foot_obj.write('mi<tg<close_____<footnote\n')
- self._Footnote__write_to_foot_obj.write('mi<mk<footnt-clo\n')
- else:
- self._Footnote__write_to_foot_obj.write(line)
-
-
- def __found_footnote(self, line):
- self._Footnote__found_a_footnote = 1
- self._Footnote__in_footnote = 1
- self._Footnote__first_line = 1
- self._Footnote__footnote_count += 1
- self._Footnote__cb_count = 0
- self._Footnote__footnote_bracket_count = self._Footnote__ob_count
- self._Footnote__write_obj.write('mi<mk<footnt-ind<%04d\n' % self._Footnote__footnote_count)
- self._Footnote__write_to_foot_obj.write('mi<mk<footnt-ope<%04d\n' % self._Footnote__footnote_count)
-
-
- def __default_sep(self, line):
- if self._Footnote__token_info == 'cw<nt<footnote__':
- self._Footnote__found_footnote(line)
-
- self._Footnote__write_obj.write(line)
- if self._Footnote__token_info == 'cw<ci<footnot-mk':
- num = str(self._Footnote__footnote_count + 1)
- self._Footnote__write_obj.write('tx<nu<__________<%s\n' % num)
-
-
-
- def __initiate_sep_values(self):
- self._Footnote__bracket_count = 0
- self._Footnote__ob_count = 0
- self._Footnote__cb_count = 0
- self._Footnote__footnote_bracket_count = 0
- self._Footnote__in_footnote = 0
- self._Footnote__first_line = 0
- self._Footnote__footnote_count = 0
-
-
- def separate_footnotes(self):
- self._Footnote__initiate_sep_values()
- read_obj = open(self._Footnote__file)
- self._Footnote__write_obj = open(self._Footnote__write_to, 'w')
- self._Footnote__footnote_holder = tempfile.mktemp()
- self._Footnote__write_to_foot_obj = open(self._Footnote__footnote_holder, 'w')
- line_to_read = 1
- while line_to_read:
- line_to_read = read_obj.readline()
- line = line_to_read
- self._Footnote__token_info = line[:16]
- if self._Footnote__token_info == 'ob<nu<open-brack':
- self._Footnote__ob_count = line[-5:-1]
-
- if self._Footnote__token_info == 'cb<nu<clos-brack':
- self._Footnote__cb_count = line[-5:-1]
-
- if self._Footnote__in_footnote:
- self._Footnote__in_footnote_func(line)
- continue
- self._Footnote__default_sep(line)
- self._Footnote__write_obj.close()
- read_obj.close()
- self._Footnote__write_to_foot_obj.close()
- read_obj = open(self._Footnote__footnote_holder, 'r')
- write_obj = open(self._Footnote__write_to, 'a')
- write_obj.write('mi<mk<sect-close\nmi<mk<body-close\nmi<tg<close_____<section\nmi<tg<close_____<body\nmi<tg<close_____<doc\nmi<mk<footnt-beg\n')
- line = 1
- while line:
- line = read_obj.readline()
- write_obj.write(line)
- write_obj.write('mi<mk<footnt-end\n')
- read_obj.close()
- write_obj.close()
- os.remove(self._Footnote__footnote_holder)
- copy_obj = copy.Copy(bug_handler = self._Footnote__bug_handler)
- if self._Footnote__copy:
- copy_obj.copy_file(self._Footnote__write_to, 'footnote_separate.data')
-
- copy_obj.rename(self._Footnote__write_to, self._Footnote__file)
- os.remove(self._Footnote__write_to)
-
-
- def update_info(self, file, copy):
- self._Footnote__file = file
- self._Footnote__copy = copy
-
-
- def __get_foot_body_func(self, line):
- if self._Footnote__token_info == 'mi<mk<footnt-beg':
- self._Footnote__state = 'foot'
- else:
- self._Footnote__write_obj.write(line)
-
-
- def __get_foot_foot_func(self, line):
- if self._Footnote__token_info == 'mi<mk<footnt-end':
- self._Footnote__state = 'body'
- else:
- self._Footnote__write_to_foot_obj.write(line)
-
-
- def __get_footnotes(self):
- read_obj = open(self._Footnote__file)
- self._Footnote__write_obj = open(self._Footnote__write_to, 'w')
- self._Footnote__write_to_foot_obj = open(self._Footnote__footnote_holder, 'w')
- line = 1
- while line:
- line = read_obj.readline()
- self._Footnote__token_info = line[:16]
- if self._Footnote__state == 'body':
- self._Footnote__get_foot_body_func(line)
- continue
- if self._Footnote__state == 'foot':
- self._Footnote__get_foot_foot_func(line)
- continue
- read_obj.close()
- self._Footnote__write_obj.close()
- self._Footnote__write_to_foot_obj.close()
-
-
- def __get_foot_from_temp(self, num):
- look_for = 'mi<mk<footnt-ope<' + num + '\n'
- found_foot = 0
- string_to_return = ''
- line = 1
- while line:
- line = self._Footnote__read_from_foot_obj.readline()
- if found_foot:
- if line == 'mi<mk<footnt-clo\n':
- return string_to_return
- string_to_return = string_to_return + line
- continue
- line == 'mi<mk<footnt-clo\n'
- if line == look_for:
- found_foot = 1
- continue
-
-
- def __join_from_temp(self):
- self._Footnote__read_from_foot_obj = open(self._Footnote__footnote_holder, 'r')
- read_obj = open(self._Footnote__write_to, 'r')
- self._Footnote__write_obj = open(self._Footnote__write_to2, 'w')
- line = 1
- while line:
- line = read_obj.readline()
- if line[:16] == 'mi<mk<footnt-ind':
- line = self._Footnote__get_foot_from_temp(line[17:-1])
-
- self._Footnote__write_obj.write(line)
- read_obj.close()
-
-
- def join_footnotes(self):
- if not self._Footnote__found_a_footnote:
- return None
- self._Footnote__write_to2 = tempfile.mktemp()
- self._Footnote__state = 'body'
- self._Footnote__get_footnotes()
- self._Footnote__join_from_temp()
- self._Footnote__write_obj.close()
- self._Footnote__read_from_foot_obj.close()
- copy_obj = copy.Copy(bug_handler = self._Footnote__bug_handler)
- if self._Footnote__copy:
- copy_obj.copy_file(self._Footnote__write_to2, 'footnote_joined.data')
-
- copy_obj.rename(self._Footnote__write_to2, self._Footnote__file)
- os.remove(self._Footnote__write_to2)
- os.remove(self._Footnote__footnote_holder)
-
-
-