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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import os
  6. import tempfile
  7. from calibre.ebooks.rtf2xml import copy
  8.  
  9. class Sections:
  10.     
  11.     def __init__(self, in_file, bug_handler, copy = None, run_level = 1):
  12.         self._Sections__file = in_file
  13.         self._Sections__bug_handler = bug_handler
  14.         self._Sections__copy = copy
  15.         self._Sections__run_level = run_level
  16.         self._Sections__write_to = tempfile.mktemp()
  17.  
  18.     
  19.     def __initiate_values(self):
  20.         self._Sections__mark_start = 'mi<mk<sect-start\n'
  21.         self._Sections__mark_end = 'mi<mk<sect-end__\n'
  22.         self._Sections__in_field = 0
  23.         self._Sections__section_values = { }
  24.         self._Sections__list_of_sec_values = []
  25.         self._Sections__field_num = []
  26.         self._Sections__section_num = 0
  27.         self._Sections__state = 'before_body'
  28.         self._Sections__found_first_sec = 0
  29.         self._Sections__text_string = ''
  30.         self._Sections__field_instruction_string = ''
  31.         self._Sections__state_dict = {
  32.             'before_body': self._Sections__before_body_func,
  33.             'body': self._Sections__body_func,
  34.             'before_first_sec': self._Sections__before_first_sec_func,
  35.             'section': self._Sections__section_func,
  36.             'section_def': self._Sections__section_def_func,
  37.             'sec_in_field': self._Sections__sec_in_field_func }
  38.         self._Sections__body_dict = {
  39.             'cw<sc<section___': self._Sections__found_section_func,
  40.             'mi<mk<sec-fd-beg': self._Sections__found_sec_in_field_func,
  41.             'cw<sc<sect-defin': self._Sections__found_section_def_bef_sec_func }
  42.         self._Sections__section_def_dict = {
  43.             'cw<pf<par-def___': (self._Sections__end_sec_def_func, None),
  44.             'mi<mk<body-open_': (self._Sections__end_sec_def_func, None),
  45.             'cw<tb<columns___': (self._Sections__attribute_func, 'columns'),
  46.             'cw<pa<margin-lef': (self._Sections__attribute_func, 'margin-left'),
  47.             'cw<pa<margin-rig': (self._Sections__attribute_func, 'margin-right'),
  48.             'mi<mk<header-ind': (self._Sections__end_sec_def_func, None),
  49.             'tx<nu<__________': (self._Sections__end_sec_premature_func, None),
  50.             'cw<ci<font-style': (self._Sections__end_sec_premature_func, None),
  51.             'cw<ci<font-size_': (self._Sections__end_sec_premature_func, None) }
  52.         self._Sections__sec_in_field_dict = {
  53.             'mi<mk<sec-fd-end': self._Sections__end_sec_in_field_func }
  54.  
  55.     
  56.     def __found_section_def_func(self, line):
  57.         self._Sections__state = 'section_def'
  58.         self._Sections__section_values.clear()
  59.  
  60.     
  61.     def __attribute_func(self, line, name):
  62.         attribute = name
  63.         value = line[20:-1]
  64.         self._Sections__section_values[attribute] = value
  65.  
  66.     
  67.     def __found_section_func(self, line):
  68.         self._Sections__state = 'section'
  69.         self._Sections__write_obj.write(line)
  70.         self._Sections__section_num += 1
  71.  
  72.     
  73.     def __found_section_def_bef_sec_func(self, line):
  74.         self._Sections__section_num += 1
  75.         self._Sections__found_section_def_func(line)
  76.         self._Sections__write_obj.write(line)
  77.  
  78.     
  79.     def __section_func(self, line):
  80.         if self._Sections__token_info == 'cw<sc<sect-defin':
  81.             self._Sections__found_section_def_func(line)
  82.         
  83.         self._Sections__write_obj.write(line)
  84.  
  85.     
  86.     def __section_def_func(self, line):
  87.         (action, name) = self._Sections__section_def_dict.get(self._Sections__token_info, (None, None))
  88.         if action:
  89.             action(line, name)
  90.             if self._Sections__in_field:
  91.                 self._Sections__sec_in_field_string += line
  92.             else:
  93.                 self._Sections__write_obj.write(line)
  94.         else:
  95.             self._Sections__write_obj.write(line)
  96.  
  97.     
  98.     def __end_sec_def_func(self, line, name):
  99.         if not self._Sections__in_field:
  100.             self._Sections__state = 'body'
  101.         else:
  102.             self._Sections__state = 'sec_in_field'
  103.         self._Sections__write_section(line)
  104.  
  105.     
  106.     def __end_sec_premature_func(self, line, name):
  107.         if not self._Sections__in_field:
  108.             self._Sections__state = 'body'
  109.         else:
  110.             self._Sections__state = 'sec_in_field'
  111.         self._Sections__write_section(line)
  112.         self._Sections__write_obj.write('cw<pf<par-def___<nu<true\n')
  113.         self._Sections__write_obj.write('ob<nu<open-brack<0000\n')
  114.         self._Sections__write_obj.write('cb<nu<clos-brack<0000\n')
  115.  
  116.     
  117.     def __write_section(self, line):
  118.         my_string = self._Sections__mark_start
  119.         if self._Sections__found_first_sec:
  120.             my_string += 'mi<tg<close_____<section\n'
  121.         else:
  122.             self._Sections__found_first_sec = 1
  123.         my_string += 'mi<tg<open-att__<section<num>%s' % str(self._Sections__section_num)
  124.         my_string += '<num-in-level>%s' % str(self._Sections__section_num)
  125.         my_string += '<type>rtf-native'
  126.         my_string += '<level>0'
  127.         keys = self._Sections__section_values.keys()
  128.         if len(keys) > 0:
  129.             for key in keys:
  130.                 my_string += '<%s>%s' % (key, self._Sections__section_values[key])
  131.             
  132.         
  133.         my_string += '\n'
  134.         my_string += self._Sections__mark_end
  135.         if self._Sections__state == 'body':
  136.             self._Sections__write_obj.write(my_string)
  137.         elif self._Sections__state == 'sec_in_field':
  138.             self._Sections__handle_sec_def(my_string)
  139.         elif self._Sections__run_level > 3:
  140.             msg = 'missed a flag\n'
  141.             raise self._Sections__bug_handler, msg
  142.         
  143.  
  144.     
  145.     def __handle_sec_def(self, my_string):
  146.         values_dict = self._Sections__section_values
  147.         self._Sections__list_of_sec_values.append(values_dict)
  148.  
  149.     
  150.     def __body_func(self, line):
  151.         action = self._Sections__body_dict.get(self._Sections__token_info)
  152.         if action:
  153.             action(line)
  154.         else:
  155.             self._Sections__write_obj.write(line)
  156.  
  157.     
  158.     def __before_body_func(self, line):
  159.         if self._Sections__token_info == 'mi<mk<body-open_':
  160.             self._Sections__state = 'before_first_sec'
  161.         
  162.         self._Sections__write_obj.write(line)
  163.  
  164.     
  165.     def __before_first_sec_func(self, line):
  166.         if self._Sections__token_info == 'cw<sc<sect-defin':
  167.             self._Sections__state = 'section_def'
  168.             self._Sections__section_num += 1
  169.             self._Sections__section_values.clear()
  170.         elif self._Sections__token_info == 'cw<pf<par-def___':
  171.             self._Sections__state = 'body'
  172.             self._Sections__section_num += 1
  173.             self._Sections__write_obj.write('mi<tg<open-att__<section<num>%s<num-in-level>%s<type>rtf-native<level>0\n' % (str(self._Sections__section_num), str(self._Sections__section_num)))
  174.             self._Sections__found_first_sec = 1
  175.         elif self._Sections__token_info == 'tx<nu<__________':
  176.             self._Sections__state = 'body'
  177.             self._Sections__section_num += 1
  178.             self._Sections__write_obj.write('mi<tg<open-att__<section<num>%s<num-in-level>%s<type>rtf-native<level>0\n' % (str(self._Sections__section_num), str(self._Sections__section_num)))
  179.             self._Sections__write_obj.write('cw<pf<par-def___<true\n')
  180.             self._Sections__found_first_sec = 1
  181.         
  182.         self._Sections__write_obj.write(line)
  183.  
  184.     
  185.     def __found_sec_in_field_func(self, line):
  186.         self._Sections__state = 'sec_in_field'
  187.         self._Sections__sec_in_field_string = line
  188.         self._Sections__in_field = 1
  189.  
  190.     
  191.     def __sec_in_field_func(self, line):
  192.         action = self._Sections__sec_in_field_dict.get(self._Sections__token_info)
  193.         if action:
  194.             action(line)
  195.         else:
  196.             self._Sections__write_obj.write(line)
  197.  
  198.     
  199.     def __end_sec_in_field_func(self, line):
  200.         self._Sections__state = 'body'
  201.         self._Sections__in_field = 0
  202.         self._Sections__write_obj.write(line)
  203.  
  204.     
  205.     def __print_field_sec_attributes(self):
  206.         num = self._Sections__field_num[0]
  207.         self._Sections__field_num = self._Sections__field_num[1:]
  208.         self._Sections__write_obj.write('mi<tg<close_____<section\nmi<tg<open-att__<section<num>%s' % str(num))
  209.         if self._Sections__list_of_sec_values:
  210.             keys = self._Sections__list_of_sec_values[0].keys()
  211.             for key in keys:
  212.                 self._Sections__write_obj.write('<%s>%s\n' % (key, self._Sections__list_of_sec_values[0][key]))
  213.             
  214.             self._Sections__list_of_sec_values = self._Sections__list_of_sec_values[1:]
  215.         
  216.         self._Sections__write_obj.write('<level>0')
  217.         self._Sections__write_obj.write('<type>rtf-native')
  218.         self._Sections__write_obj.write('<num-in-level>%s' % str(self._Sections__section_num))
  219.         self._Sections__write_obj.write('\n')
  220.  
  221.     
  222.     def __found_section_in_field_func(self, line):
  223.         self._Sections__section_num += 1
  224.         self._Sections__field_num.append(self._Sections__section_num)
  225.         self._Sections__sec_in_field_string += line
  226.  
  227.     
  228.     def __found_section_def_in_field_func(self, line):
  229.         self._Sections__state = 'section_def'
  230.         self._Sections__section_values.clear()
  231.  
  232.     
  233.     def make_sections(self):
  234.         self._Sections__initiate_values()
  235.         read_obj = open(self._Sections__file, 'r')
  236.         self._Sections__write_obj = open(self._Sections__write_to, 'w')
  237.         line_to_read = 1
  238.         while line_to_read:
  239.             line_to_read = read_obj.readline()
  240.             line = line_to_read
  241.             self._Sections__token_info = line[:16]
  242.             action = self._Sections__state_dict.get(self._Sections__state)
  243.             if action == None:
  244.                 sys.stderr.write('no no matching state in module sections.py\n')
  245.                 sys.stderr.write(self._Sections__state + '\n')
  246.             
  247.             action(line)
  248.         read_obj.close()
  249.         self._Sections__write_obj.close()
  250.         copy_obj = copy.Copy(bug_handler = self._Sections__bug_handler)
  251.         if self._Sections__copy:
  252.             copy_obj.copy_file(self._Sections__write_to, 'sections.data')
  253.         
  254.         copy_obj.rename(self._Sections__write_to, self._Sections__file)
  255.         os.remove(self._Sections__write_to)
  256.  
  257.  
  258.