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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import tempfile
  6. from calibre.ebooks.rtf2xml import copy
  7.  
  8. class Footnote:
  9.     
  10.     def __init__(self, in_file, bug_handler, copy = None, run_level = 1):
  11.         self._Footnote__file = in_file
  12.         self._Footnote__bug_handler = bug_handler
  13.         self._Footnote__copy = copy
  14.         self._Footnote__write_to = tempfile.mktemp()
  15.         self._Footnote__found_a_footnote = 0
  16.  
  17.     
  18.     def __first_line_func(self, line):
  19.         if self._Footnote__token_info == 'cw<nt<type______':
  20.             self._Footnote__write_to_foot_obj.write('mi<tg<open-att__<footnote<type>endnote<num>%s\n' % self._Footnote__footnote_count)
  21.         else:
  22.             self._Footnote__write_to_foot_obj.write('mi<tg<open-att__<footnote<num>%s\n' % self._Footnote__footnote_count)
  23.         self._Footnote__first_line = 0
  24.  
  25.     
  26.     def __in_footnote_func(self, line):
  27.         if self._Footnote__first_line:
  28.             self._Footnote__first_line_func(line)
  29.         
  30.         if self._Footnote__token_info == 'cw<ci<footnot-mk':
  31.             num = str(self._Footnote__footnote_count)
  32.             self._Footnote__write_to_foot_obj.write(line)
  33.             self._Footnote__write_to_foot_obj.write('tx<nu<__________<%s\n' % num)
  34.         
  35.         if self._Footnote__cb_count == self._Footnote__footnote_bracket_count:
  36.             self._Footnote__in_footnote = 0
  37.             self._Footnote__write_obj.write(line)
  38.             self._Footnote__write_to_foot_obj.write('mi<mk<foot___clo\n')
  39.             self._Footnote__write_to_foot_obj.write('mi<tg<close_____<footnote\n')
  40.             self._Footnote__write_to_foot_obj.write('mi<mk<footnt-clo\n')
  41.         else:
  42.             self._Footnote__write_to_foot_obj.write(line)
  43.  
  44.     
  45.     def __found_footnote(self, line):
  46.         self._Footnote__found_a_footnote = 1
  47.         self._Footnote__in_footnote = 1
  48.         self._Footnote__first_line = 1
  49.         self._Footnote__footnote_count += 1
  50.         self._Footnote__cb_count = 0
  51.         self._Footnote__footnote_bracket_count = self._Footnote__ob_count
  52.         self._Footnote__write_obj.write('mi<mk<footnt-ind<%04d\n' % self._Footnote__footnote_count)
  53.         self._Footnote__write_to_foot_obj.write('mi<mk<footnt-ope<%04d\n' % self._Footnote__footnote_count)
  54.  
  55.     
  56.     def __default_sep(self, line):
  57.         if self._Footnote__token_info == 'cw<nt<footnote__':
  58.             self._Footnote__found_footnote(line)
  59.         
  60.         self._Footnote__write_obj.write(line)
  61.         if self._Footnote__token_info == 'cw<ci<footnot-mk':
  62.             num = str(self._Footnote__footnote_count + 1)
  63.             self._Footnote__write_obj.write('tx<nu<__________<%s\n' % num)
  64.         
  65.  
  66.     
  67.     def __initiate_sep_values(self):
  68.         self._Footnote__bracket_count = 0
  69.         self._Footnote__ob_count = 0
  70.         self._Footnote__cb_count = 0
  71.         self._Footnote__footnote_bracket_count = 0
  72.         self._Footnote__in_footnote = 0
  73.         self._Footnote__first_line = 0
  74.         self._Footnote__footnote_count = 0
  75.  
  76.     
  77.     def separate_footnotes(self):
  78.         self._Footnote__initiate_sep_values()
  79.         read_obj = open(self._Footnote__file)
  80.         self._Footnote__write_obj = open(self._Footnote__write_to, 'w')
  81.         self._Footnote__footnote_holder = tempfile.mktemp()
  82.         self._Footnote__write_to_foot_obj = open(self._Footnote__footnote_holder, 'w')
  83.         line_to_read = 1
  84.         while line_to_read:
  85.             line_to_read = read_obj.readline()
  86.             line = line_to_read
  87.             self._Footnote__token_info = line[:16]
  88.             if self._Footnote__token_info == 'ob<nu<open-brack':
  89.                 self._Footnote__ob_count = line[-5:-1]
  90.             
  91.             if self._Footnote__token_info == 'cb<nu<clos-brack':
  92.                 self._Footnote__cb_count = line[-5:-1]
  93.             
  94.             if self._Footnote__in_footnote:
  95.                 self._Footnote__in_footnote_func(line)
  96.                 continue
  97.             self._Footnote__default_sep(line)
  98.         self._Footnote__write_obj.close()
  99.         read_obj.close()
  100.         self._Footnote__write_to_foot_obj.close()
  101.         read_obj = open(self._Footnote__footnote_holder, 'r')
  102.         write_obj = open(self._Footnote__write_to, 'a')
  103.         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')
  104.         line = 1
  105.         while line:
  106.             line = read_obj.readline()
  107.             write_obj.write(line)
  108.         write_obj.write('mi<mk<footnt-end\n')
  109.         read_obj.close()
  110.         write_obj.close()
  111.         os.remove(self._Footnote__footnote_holder)
  112.         copy_obj = copy.Copy(bug_handler = self._Footnote__bug_handler)
  113.         if self._Footnote__copy:
  114.             copy_obj.copy_file(self._Footnote__write_to, 'footnote_separate.data')
  115.         
  116.         copy_obj.rename(self._Footnote__write_to, self._Footnote__file)
  117.         os.remove(self._Footnote__write_to)
  118.  
  119.     
  120.     def update_info(self, file, copy):
  121.         self._Footnote__file = file
  122.         self._Footnote__copy = copy
  123.  
  124.     
  125.     def __get_foot_body_func(self, line):
  126.         if self._Footnote__token_info == 'mi<mk<footnt-beg':
  127.             self._Footnote__state = 'foot'
  128.         else:
  129.             self._Footnote__write_obj.write(line)
  130.  
  131.     
  132.     def __get_foot_foot_func(self, line):
  133.         if self._Footnote__token_info == 'mi<mk<footnt-end':
  134.             self._Footnote__state = 'body'
  135.         else:
  136.             self._Footnote__write_to_foot_obj.write(line)
  137.  
  138.     
  139.     def __get_footnotes(self):
  140.         read_obj = open(self._Footnote__file)
  141.         self._Footnote__write_obj = open(self._Footnote__write_to, 'w')
  142.         self._Footnote__write_to_foot_obj = open(self._Footnote__footnote_holder, 'w')
  143.         line = 1
  144.         while line:
  145.             line = read_obj.readline()
  146.             self._Footnote__token_info = line[:16]
  147.             if self._Footnote__state == 'body':
  148.                 self._Footnote__get_foot_body_func(line)
  149.                 continue
  150.             if self._Footnote__state == 'foot':
  151.                 self._Footnote__get_foot_foot_func(line)
  152.                 continue
  153.         read_obj.close()
  154.         self._Footnote__write_obj.close()
  155.         self._Footnote__write_to_foot_obj.close()
  156.  
  157.     
  158.     def __get_foot_from_temp(self, num):
  159.         look_for = 'mi<mk<footnt-ope<' + num + '\n'
  160.         found_foot = 0
  161.         string_to_return = ''
  162.         line = 1
  163.         while line:
  164.             line = self._Footnote__read_from_foot_obj.readline()
  165.             if found_foot:
  166.                 if line == 'mi<mk<footnt-clo\n':
  167.                     return string_to_return
  168.                 string_to_return = string_to_return + line
  169.                 continue
  170.             line == 'mi<mk<footnt-clo\n'
  171.             if line == look_for:
  172.                 found_foot = 1
  173.                 continue
  174.  
  175.     
  176.     def __join_from_temp(self):
  177.         self._Footnote__read_from_foot_obj = open(self._Footnote__footnote_holder, 'r')
  178.         read_obj = open(self._Footnote__write_to, 'r')
  179.         self._Footnote__write_obj = open(self._Footnote__write_to2, 'w')
  180.         line = 1
  181.         while line:
  182.             line = read_obj.readline()
  183.             if line[:16] == 'mi<mk<footnt-ind':
  184.                 line = self._Footnote__get_foot_from_temp(line[17:-1])
  185.             
  186.             self._Footnote__write_obj.write(line)
  187.         read_obj.close()
  188.  
  189.     
  190.     def join_footnotes(self):
  191.         if not self._Footnote__found_a_footnote:
  192.             return None
  193.         self._Footnote__write_to2 = tempfile.mktemp()
  194.         self._Footnote__state = 'body'
  195.         self._Footnote__get_footnotes()
  196.         self._Footnote__join_from_temp()
  197.         self._Footnote__write_obj.close()
  198.         self._Footnote__read_from_foot_obj.close()
  199.         copy_obj = copy.Copy(bug_handler = self._Footnote__bug_handler)
  200.         if self._Footnote__copy:
  201.             copy_obj.copy_file(self._Footnote__write_to2, 'footnote_joined.data')
  202.         
  203.         copy_obj.rename(self._Footnote__write_to2, self._Footnote__file)
  204.         os.remove(self._Footnote__write_to2)
  205.         os.remove(self._Footnote__footnote_holder)
  206.  
  207.  
  208.