home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1108 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  5.8 KB  |  128 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 Info:
  10.     
  11.     def __init__(self, in_file, bug_handler, copy = None, run_level = 1):
  12.         self._Info__file = in_file
  13.         self._Info__bug_handler = bug_handler
  14.         self._Info__copy = copy
  15.         self._Info__run_level = run_level
  16.         self._Info__write_to = tempfile.mktemp()
  17.  
  18.     
  19.     def __initiate_values(self):
  20.         self._Info__text_string = ''
  21.         self._Info__state = 'before_info_table'
  22.         self._Info__state_dict = {
  23.             'before_info_table': self._Info__before_info_table_func,
  24.             'after_info_table': self._Info__after_info_table_func,
  25.             'in_info_table': self._Info__in_info_table_func,
  26.             'collect_text': self._Info__collect_text_func,
  27.             'collect_tokens': self._Info__collect_tokens_func }
  28.         self._Info__info_table_dict = {
  29.             'cw<di<title_____': (self._Info__found_tag_with_text_func, 'title'),
  30.             'cw<di<author____': (self._Info__found_tag_with_text_func, 'author'),
  31.             'cw<di<keywords__': (self._Info__found_tag_with_text_func, 'keywords'),
  32.             'cw<di<doc-notes_': (self._Info__found_tag_with_text_func, 'doc-notes'),
  33.             'cw<di<subject___': (self._Info__found_tag_with_text_func, 'subject'),
  34.             'cw<di<operator__': (self._Info__found_tag_with_text_func, 'operator'),
  35.             'cw<di<create-tim': (self._Info__found_tag_with_tokens_func, 'creation-time'),
  36.             'cw<di<revis-time': (self._Info__found_tag_with_tokens_func, 'revision-time'),
  37.             'cw<di<edit-time_': (self._Info__single_field_func, 'editing-time'),
  38.             'cw<di<num-of-wor': (self._Info__single_field_func, 'number-of-words'),
  39.             'cw<di<num-of-chr': (self._Info__single_field_func, 'number-of-characters'),
  40.             'cw<di<num-of-pag': (self._Info__single_field_func, 'number-of-pages') }
  41.         self._Info__token_dict = {
  42.             'year______': 'year',
  43.             'month_____': 'month',
  44.             'day_______': 'day',
  45.             'minute____': 'minute',
  46.             'revis-time': 'revision-time',
  47.             'num-of-wor': 'number-of-words',
  48.             'num-of-chr': 'number-of-characters',
  49.             'num-of-pag': 'number-of-pages' }
  50.  
  51.     
  52.     def __before_info_table_func(self, line):
  53.         if self._Info__token_info == 'mi<mk<doc-in-beg':
  54.             self._Info__state = 'in_info_table'
  55.         
  56.         self._Info__write_obj.write(line)
  57.  
  58.     
  59.     def __in_info_table_func(self, line):
  60.         if self._Info__token_info == 'mi<mk<doc-in-end':
  61.             self._Info__state = 'after_info_table'
  62.         else:
  63.             (action, tag) = self._Info__info_table_dict.get(self._Info__token_info, (None, None))
  64.             if action:
  65.                 action(line, tag)
  66.             else:
  67.                 self._Info__write_obj.write(line)
  68.  
  69.     
  70.     def __found_tag_with_text_func(self, line, tag):
  71.         self._Info__tag = tag
  72.         self._Info__state = 'collect_text'
  73.  
  74.     
  75.     def __collect_text_func(self, line):
  76.         if self._Info__token_info == 'mi<mk<docinf-end':
  77.             self._Info__state = 'in_info_table'
  78.             self._Info__write_obj.write('mi<tg<open______<%s\ntx<nu<__________<%s\nmi<tg<close_____<%s\n' % (self._Info__tag, self._Info__text_string, self._Info__tag))
  79.             self._Info__text_string = ''
  80.         elif line[0:2] == 'tx':
  81.             self._Info__text_string += line[17:-1]
  82.         
  83.  
  84.     
  85.     def __found_tag_with_tokens_func(self, line, tag):
  86.         self._Info__state = 'collect_tokens'
  87.         self._Info__text_string = 'mi<tg<empty-att_<%s' % tag
  88.  
  89.     
  90.     def __collect_tokens_func(self, line):
  91.         pass
  92.  
  93.     
  94.     def __single_field_func(self, line, tag):
  95.         value = line[20:-1]
  96.         self._Info__write_obj.write('mi<tg<empty-att_<%s<%s>%s\n' % (tag, tag, value))
  97.  
  98.     
  99.     def __after_info_table_func(self, line):
  100.         self._Info__write_obj.write(line)
  101.  
  102.     
  103.     def fix_info(self):
  104.         self._Info__initiate_values()
  105.         read_obj = open(self._Info__file, 'r')
  106.         self._Info__write_obj = open(self._Info__write_to, 'w')
  107.         line_to_read = 1
  108.         while line_to_read:
  109.             line_to_read = read_obj.readline()
  110.             line = line_to_read
  111.             self._Info__token_info = line[:16]
  112.             action = self._Info__state_dict.get(self._Info__state)
  113.             if action == None:
  114.                 sys.stderr.write('no no matching state in module styles.py\n')
  115.                 sys.stderr.write(self._Info__state + '\n')
  116.             
  117.             action(line)
  118.         read_obj.close()
  119.         self._Info__write_obj.close()
  120.         copy_obj = copy.Copy(bug_handler = self._Info__bug_handler)
  121.         if self._Info__copy:
  122.             copy_obj.copy_file(self._Info__write_to, 'info.data')
  123.         
  124.         copy_obj.rename(self._Info__write_to, self._Info__file)
  125.         os.remove(self._Info__write_to)
  126.  
  127.  
  128.