home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import sys
- import os
- import tempfile
- from calibre.ebooks.rtf2xml import copy
-
- class Sections:
-
- def __init__(self, in_file, bug_handler, copy = None, run_level = 1):
- self._Sections__file = in_file
- self._Sections__bug_handler = bug_handler
- self._Sections__copy = copy
- self._Sections__run_level = run_level
- self._Sections__write_to = tempfile.mktemp()
-
-
- def __initiate_values(self):
- self._Sections__mark_start = 'mi<mk<sect-start\n'
- self._Sections__mark_end = 'mi<mk<sect-end__\n'
- self._Sections__in_field = 0
- self._Sections__section_values = { }
- self._Sections__list_of_sec_values = []
- self._Sections__field_num = []
- self._Sections__section_num = 0
- self._Sections__state = 'before_body'
- self._Sections__found_first_sec = 0
- self._Sections__text_string = ''
- self._Sections__field_instruction_string = ''
- self._Sections__state_dict = {
- 'before_body': self._Sections__before_body_func,
- 'body': self._Sections__body_func,
- 'before_first_sec': self._Sections__before_first_sec_func,
- 'section': self._Sections__section_func,
- 'section_def': self._Sections__section_def_func,
- 'sec_in_field': self._Sections__sec_in_field_func }
- self._Sections__body_dict = {
- 'cw<sc<section___': self._Sections__found_section_func,
- 'mi<mk<sec-fd-beg': self._Sections__found_sec_in_field_func,
- 'cw<sc<sect-defin': self._Sections__found_section_def_bef_sec_func }
- self._Sections__section_def_dict = {
- 'cw<pf<par-def___': (self._Sections__end_sec_def_func, None),
- 'mi<mk<body-open_': (self._Sections__end_sec_def_func, None),
- 'cw<tb<columns___': (self._Sections__attribute_func, 'columns'),
- 'cw<pa<margin-lef': (self._Sections__attribute_func, 'margin-left'),
- 'cw<pa<margin-rig': (self._Sections__attribute_func, 'margin-right'),
- 'mi<mk<header-ind': (self._Sections__end_sec_def_func, None),
- 'tx<nu<__________': (self._Sections__end_sec_premature_func, None),
- 'cw<ci<font-style': (self._Sections__end_sec_premature_func, None),
- 'cw<ci<font-size_': (self._Sections__end_sec_premature_func, None) }
- self._Sections__sec_in_field_dict = {
- 'mi<mk<sec-fd-end': self._Sections__end_sec_in_field_func }
-
-
- def __found_section_def_func(self, line):
- self._Sections__state = 'section_def'
- self._Sections__section_values.clear()
-
-
- def __attribute_func(self, line, name):
- attribute = name
- value = line[20:-1]
- self._Sections__section_values[attribute] = value
-
-
- def __found_section_func(self, line):
- self._Sections__state = 'section'
- self._Sections__write_obj.write(line)
- self._Sections__section_num += 1
-
-
- def __found_section_def_bef_sec_func(self, line):
- self._Sections__section_num += 1
- self._Sections__found_section_def_func(line)
- self._Sections__write_obj.write(line)
-
-
- def __section_func(self, line):
- if self._Sections__token_info == 'cw<sc<sect-defin':
- self._Sections__found_section_def_func(line)
-
- self._Sections__write_obj.write(line)
-
-
- def __section_def_func(self, line):
- (action, name) = self._Sections__section_def_dict.get(self._Sections__token_info, (None, None))
- if action:
- action(line, name)
- if self._Sections__in_field:
- self._Sections__sec_in_field_string += line
- else:
- self._Sections__write_obj.write(line)
- else:
- self._Sections__write_obj.write(line)
-
-
- def __end_sec_def_func(self, line, name):
- if not self._Sections__in_field:
- self._Sections__state = 'body'
- else:
- self._Sections__state = 'sec_in_field'
- self._Sections__write_section(line)
-
-
- def __end_sec_premature_func(self, line, name):
- if not self._Sections__in_field:
- self._Sections__state = 'body'
- else:
- self._Sections__state = 'sec_in_field'
- self._Sections__write_section(line)
- self._Sections__write_obj.write('cw<pf<par-def___<nu<true\n')
- self._Sections__write_obj.write('ob<nu<open-brack<0000\n')
- self._Sections__write_obj.write('cb<nu<clos-brack<0000\n')
-
-
- def __write_section(self, line):
- my_string = self._Sections__mark_start
- if self._Sections__found_first_sec:
- my_string += 'mi<tg<close_____<section\n'
- else:
- self._Sections__found_first_sec = 1
- my_string += 'mi<tg<open-att__<section<num>%s' % str(self._Sections__section_num)
- my_string += '<num-in-level>%s' % str(self._Sections__section_num)
- my_string += '<type>rtf-native'
- my_string += '<level>0'
- keys = self._Sections__section_values.keys()
- if len(keys) > 0:
- for key in keys:
- my_string += '<%s>%s' % (key, self._Sections__section_values[key])
-
-
- my_string += '\n'
- my_string += self._Sections__mark_end
- if self._Sections__state == 'body':
- self._Sections__write_obj.write(my_string)
- elif self._Sections__state == 'sec_in_field':
- self._Sections__handle_sec_def(my_string)
- elif self._Sections__run_level > 3:
- msg = 'missed a flag\n'
- raise self._Sections__bug_handler, msg
-
-
-
- def __handle_sec_def(self, my_string):
- values_dict = self._Sections__section_values
- self._Sections__list_of_sec_values.append(values_dict)
-
-
- def __body_func(self, line):
- action = self._Sections__body_dict.get(self._Sections__token_info)
- if action:
- action(line)
- else:
- self._Sections__write_obj.write(line)
-
-
- def __before_body_func(self, line):
- if self._Sections__token_info == 'mi<mk<body-open_':
- self._Sections__state = 'before_first_sec'
-
- self._Sections__write_obj.write(line)
-
-
- def __before_first_sec_func(self, line):
- if self._Sections__token_info == 'cw<sc<sect-defin':
- self._Sections__state = 'section_def'
- self._Sections__section_num += 1
- self._Sections__section_values.clear()
- elif self._Sections__token_info == 'cw<pf<par-def___':
- self._Sections__state = 'body'
- self._Sections__section_num += 1
- 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)))
- self._Sections__found_first_sec = 1
- elif self._Sections__token_info == 'tx<nu<__________':
- self._Sections__state = 'body'
- self._Sections__section_num += 1
- 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)))
- self._Sections__write_obj.write('cw<pf<par-def___<true\n')
- self._Sections__found_first_sec = 1
-
- self._Sections__write_obj.write(line)
-
-
- def __found_sec_in_field_func(self, line):
- self._Sections__state = 'sec_in_field'
- self._Sections__sec_in_field_string = line
- self._Sections__in_field = 1
-
-
- def __sec_in_field_func(self, line):
- action = self._Sections__sec_in_field_dict.get(self._Sections__token_info)
- if action:
- action(line)
- else:
- self._Sections__write_obj.write(line)
-
-
- def __end_sec_in_field_func(self, line):
- self._Sections__state = 'body'
- self._Sections__in_field = 0
- self._Sections__write_obj.write(line)
-
-
- def __print_field_sec_attributes(self):
- num = self._Sections__field_num[0]
- self._Sections__field_num = self._Sections__field_num[1:]
- self._Sections__write_obj.write('mi<tg<close_____<section\nmi<tg<open-att__<section<num>%s' % str(num))
- if self._Sections__list_of_sec_values:
- keys = self._Sections__list_of_sec_values[0].keys()
- for key in keys:
- self._Sections__write_obj.write('<%s>%s\n' % (key, self._Sections__list_of_sec_values[0][key]))
-
- self._Sections__list_of_sec_values = self._Sections__list_of_sec_values[1:]
-
- self._Sections__write_obj.write('<level>0')
- self._Sections__write_obj.write('<type>rtf-native')
- self._Sections__write_obj.write('<num-in-level>%s' % str(self._Sections__section_num))
- self._Sections__write_obj.write('\n')
-
-
- def __found_section_in_field_func(self, line):
- self._Sections__section_num += 1
- self._Sections__field_num.append(self._Sections__section_num)
- self._Sections__sec_in_field_string += line
-
-
- def __found_section_def_in_field_func(self, line):
- self._Sections__state = 'section_def'
- self._Sections__section_values.clear()
-
-
- def make_sections(self):
- self._Sections__initiate_values()
- read_obj = open(self._Sections__file, 'r')
- self._Sections__write_obj = open(self._Sections__write_to, 'w')
- line_to_read = 1
- while line_to_read:
- line_to_read = read_obj.readline()
- line = line_to_read
- self._Sections__token_info = line[:16]
- action = self._Sections__state_dict.get(self._Sections__state)
- if action == None:
- sys.stderr.write('no no matching state in module sections.py\n')
- sys.stderr.write(self._Sections__state + '\n')
-
- action(line)
- read_obj.close()
- self._Sections__write_obj.close()
- copy_obj = copy.Copy(bug_handler = self._Sections__bug_handler)
- if self._Sections__copy:
- copy_obj.copy_file(self._Sections__write_to, 'sections.data')
-
- copy_obj.rename(self._Sections__write_to, self._Sections__file)
- os.remove(self._Sections__write_to)
-
-
-