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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import array
  5. import string
  6. import sys
  7. import calibre.utils.chm.chmlib as chmlib
  8. from calibre.constants import plugins
  9. (extra, extra_err) = plugins['chm_extra']
  10. if extra_err:
  11.     raise RuntimeError('Failed to load chm.extra: ' + extra_err)
  12. extra_err
  13. charset_table = {
  14.     0: 'iso8859_1',
  15.     238: 'iso8859_2',
  16.     178: 'iso8859_6',
  17.     161: 'iso8859_7',
  18.     177: 'iso8859_8',
  19.     162: 'iso8859_9',
  20.     222: 'iso8859_11',
  21.     186: 'iso8859_13',
  22.     204: 'cp1251',
  23.     255: 'cp437',
  24.     128: 'cp932',
  25.     134: 'cp936',
  26.     129: 'cp949',
  27.     136: 'cp950',
  28.     1: None,
  29.     2: None,
  30.     130: None,
  31.     163: None,
  32.     77: None }
  33. locale_table = {
  34.     1078: ('iso8859_1', 'Afrikaans', 'Western Europe & US'),
  35.     1052: ('iso8859_2', 'Albanian', 'Central Europe'),
  36.     1025: ('iso8859_6', 'Arabic_Saudi_Arabia', 'Arabic'),
  37.     2049: ('iso8859_6', 'Arabic_Iraq', 'Arabic'),
  38.     3073: ('iso8859_6', 'Arabic_Egypt', 'Arabic'),
  39.     4097: ('iso8859_6', 'Arabic_Libya', 'Arabic'),
  40.     5121: ('iso8859_6', 'Arabic_Algeria', 'Arabic'),
  41.     6145: ('iso8859_6', 'Arabic_Morocco', 'Arabic'),
  42.     7169: ('iso8859_6', 'Arabic_Tunisia', 'Arabic'),
  43.     8193: ('iso8859_6', 'Arabic_Oman', 'Arabic'),
  44.     9217: ('iso8859_6', 'Arabic_Yemen', 'Arabic'),
  45.     10241: ('iso8859_6', 'Arabic_Syria', 'Arabic'),
  46.     11265: ('iso8859_6', 'Arabic_Jordan', 'Arabic'),
  47.     12289: ('iso8859_6', 'Arabic_Lebanon', 'Arabic'),
  48.     13313: ('iso8859_6', 'Arabic_Kuwait', 'Arabic'),
  49.     14337: ('iso8859_6', 'Arabic_UAE', 'Arabic'),
  50.     15361: ('iso8859_6', 'Arabic_Bahrain', 'Arabic'),
  51.     16385: ('iso8859_6', 'Arabic_Qatar', 'Arabic'),
  52.     1067: (None, 'Armenian', 'Armenian'),
  53.     1068: ('iso8859_9', 'Azeri_Latin', 'Turkish'),
  54.     2092: ('cp1251', 'Azeri_Cyrillic', 'Cyrillic'),
  55.     1069: ('iso8859_1', 'Basque', 'Western Europe & US'),
  56.     1059: ('cp1251', 'Belarusian', 'Cyrillic'),
  57.     1026: ('cp1251', 'Bulgarian', 'Cyrillic'),
  58.     1027: ('iso8859_1', 'Catalan', 'Western Europe & US'),
  59.     1028: ('cp950', 'Chinese_Taiwan', 'Traditional Chinese'),
  60.     2052: ('cp936', 'Chinese_PRC', 'Simplified Chinese'),
  61.     3076: ('cp950', 'Chinese_Hong_Kong', 'Traditional Chinese'),
  62.     4100: ('cp936', 'Chinese_Singapore', 'Simplified Chinese'),
  63.     5124: ('cp950', 'Chinese_Macau', 'Traditional Chinese'),
  64.     1050: ('iso8859_2', 'Croatian', 'Central Europe'),
  65.     1029: ('iso8859_2', 'Czech', 'Central Europe'),
  66.     1030: ('iso8859_1', 'Danish', 'Western Europe & US'),
  67.     1043: ('iso8859_1', 'Dutch_Standard', 'Western Europe & US'),
  68.     2067: ('iso8859_1', 'Dutch_Belgian', 'Western Europe & US'),
  69.     1033: ('iso8859_1', 'English_United_States', 'Western Europe & US'),
  70.     2057: ('iso8859_1', 'English_United_Kingdom', 'Western Europe & US'),
  71.     3081: ('iso8859_1', 'English_Australian', 'Western Europe & US'),
  72.     4105: ('iso8859_1', 'English_Canadian', 'Western Europe & US'),
  73.     5129: ('iso8859_1', 'English_New_Zealand', 'Western Europe & US'),
  74.     6153: ('iso8859_1', 'English_Irish', 'Western Europe & US'),
  75.     7177: ('iso8859_1', 'English_South_Africa', 'Western Europe & US'),
  76.     8201: ('iso8859_1', 'English_Jamaica', 'Western Europe & US'),
  77.     9225: ('iso8859_1', 'English_Caribbean', 'Western Europe & US'),
  78.     10249: ('iso8859_1', 'English_Belize', 'Western Europe & US'),
  79.     11273: ('iso8859_1', 'English_Trinidad', 'Western Europe & US'),
  80.     12297: ('iso8859_1', 'English_Zimbabwe', 'Western Europe & US'),
  81.     13321: ('iso8859_1', 'English_Philippines', 'Western Europe & US'),
  82.     1061: ('iso8859_13', 'Estonian', 'Baltic'),
  83.     1080: ('iso8859_1', 'Faeroese', 'Western Europe & US'),
  84.     1065: ('iso8859_6', 'Farsi', 'Arabic'),
  85.     1035: ('iso8859_1', 'Finnish', 'Western Europe & US'),
  86.     1036: ('iso8859_1', 'French_Standard', 'Western Europe & US'),
  87.     2060: ('iso8859_1', 'French_Belgian', 'Western Europe & US'),
  88.     3084: ('iso8859_1', 'French_Canadian', 'Western Europe & US'),
  89.     4108: ('iso8859_1', 'French_Swiss', 'Western Europe & US'),
  90.     5132: ('iso8859_1', 'French_Luxembourg', 'Western Europe & US'),
  91.     6156: ('iso8859_1', 'French_Monaco', 'Western Europe & US'),
  92.     1079: (None, 'Georgian', 'Georgian'),
  93.     1031: ('iso8859_1', 'German_Standard', 'Western Europe & US'),
  94.     2055: ('iso8859_1', 'German_Swiss', 'Western Europe & US'),
  95.     3079: ('iso8859_1', 'German_Austrian', 'Western Europe & US'),
  96.     4103: ('iso8859_1', 'German_Luxembourg', 'Western Europe & US'),
  97.     5127: ('iso8859_1', 'German_Liechtenstein', 'Western Europe & US'),
  98.     1032: ('iso8859_7', 'Greek', 'Greek'),
  99.     1037: ('iso8859_8', 'Hebrew', 'Hebrew'),
  100.     1081: (None, 'Hindi', 'Indic'),
  101.     1038: ('iso8859_2', 'Hungarian', 'Central Europe'),
  102.     1039: ('iso8859_1', 'Icelandic', 'Western Europe & US'),
  103.     1057: ('iso8859_1', 'Indonesian', 'Western Europe & US'),
  104.     1040: ('iso8859_1', 'Italian_Standard', 'Western Europe & US'),
  105.     2064: ('iso8859_1', 'Italian_Swiss', 'Western Europe & US'),
  106.     1041: ('cp932', 'Japanese', 'Japanese'),
  107.     1087: ('cp1251', 'Kazakh', 'Cyrillic'),
  108.     1111: (None, 'Konkani', 'Indic'),
  109.     1042: ('cp949', 'Korean', 'Korean'),
  110.     1062: ('iso8859_13', 'Latvian', 'Baltic'),
  111.     1063: ('iso8859_13', 'Lithuanian', 'Baltic'),
  112.     1071: ('cp1251', 'Macedonian', 'Cyrillic'),
  113.     1086: ('iso8859_1', 'Malay_Malaysia', 'Western Europe & US'),
  114.     2110: ('iso8859_1', 'Malay_Brunei_Darussalam', 'Western Europe & US'),
  115.     1102: (None, 'Marathi', 'Indic'),
  116.     1044: ('iso8859_1', 'Norwegian_Bokmal', 'Western Europe & US'),
  117.     2068: ('iso8859_1', 'Norwegian_Nynorsk', 'Western Europe & US'),
  118.     1045: ('iso8859_2', 'Polish', 'Central Europe'),
  119.     1046: ('iso8859_1', 'Portuguese_Brazilian', 'Western Europe & US'),
  120.     2070: ('iso8859_1', 'Portuguese_Standard', 'Western Europe & US'),
  121.     1048: ('iso8859_2', 'Romanian', 'Central Europe'),
  122.     1049: ('cp1251', 'Russian', 'Cyrillic'),
  123.     1103: (None, 'Sanskrit', 'Indic'),
  124.     2074: ('iso8859_2', 'Serbian_Latin', 'Central Europe'),
  125.     3098: ('cp1251', 'Serbian_Cyrillic', 'Cyrillic'),
  126.     1051: ('iso8859_2', 'Slovak', 'Central Europe'),
  127.     1060: ('iso8859_2', 'Slovenian', 'Central Europe'),
  128.     1034: ('iso8859_1', 'Spanish_Trad_Sort', 'Western Europe & US'),
  129.     2058: ('iso8859_1', 'Spanish_Mexican', 'Western Europe & US'),
  130.     3082: ('iso8859_1', 'Spanish_Modern_Sort', 'Western Europe & US'),
  131.     4106: ('iso8859_1', 'Spanish_Guatemala', 'Western Europe & US'),
  132.     5130: ('iso8859_1', 'Spanish_Costa_Rica', 'Western Europe & US'),
  133.     6154: ('iso8859_1', 'Spanish_Panama', 'Western Europe & US'),
  134.     7178: ('iso8859_1', 'Spanish_Dominican_Repub', 'Western Europe & US'),
  135.     8202: ('iso8859_1', 'Spanish_Venezuela', 'Western Europe & US'),
  136.     9226: ('iso8859_1', 'Spanish_Colombia', 'Western Europe & US'),
  137.     10250: ('iso8859_1', 'Spanish_Peru', 'Western Europe & US'),
  138.     11274: ('iso8859_1', 'Spanish_Argentina', 'Western Europe & US'),
  139.     12298: ('iso8859_1', 'Spanish_Ecuador', 'Western Europe & US'),
  140.     13322: ('iso8859_1', 'Spanish_Chile', 'Western Europe & US'),
  141.     14346: ('iso8859_1', 'Spanish_Uruguay', 'Western Europe & US'),
  142.     15370: ('iso8859_1', 'Spanish_Paraguay', 'Western Europe & US'),
  143.     16394: ('iso8859_1', 'Spanish_Bolivia', 'Western Europe & US'),
  144.     17418: ('iso8859_1', 'Spanish_El_Salvador', 'Western Europe & US'),
  145.     18442: ('iso8859_1', 'Spanish_Honduras', 'Western Europe & US'),
  146.     19466: ('iso8859_1', 'Spanish_Nicaragua', 'Western Europe & US'),
  147.     20490: ('iso8859_1', 'Spanish_Puerto_Rico', 'Western Europe & US'),
  148.     1089: ('iso8859_1', 'Swahili', 'Western Europe & US'),
  149.     1053: ('iso8859_1', 'Swedish', 'Western Europe & US'),
  150.     2077: ('iso8859_1', 'Swedish_Finland', 'Western Europe & US'),
  151.     1097: (None, 'Tamil', 'Indic'),
  152.     1092: ('cp1251', 'Tatar', 'Cyrillic'),
  153.     1054: ('iso8859_11', 'Thai', 'Thai'),
  154.     1055: ('iso8859_9', 'Turkish', 'Turkish'),
  155.     1058: ('cp1251', 'Ukrainian', 'Cyrillic'),
  156.     1056: ('iso8859_6', 'Urdu', 'Arabic'),
  157.     1091: ('iso8859_9', 'Uzbek_Latin', 'Turkish'),
  158.     2115: ('cp1251', 'Uzbek_Cyrillic', 'Cyrillic'),
  159.     1066: (None, 'Vietnamese', 'Vietnamese') }
  160.  
  161. class CHMFile:
  162.     filename = ''
  163.     file = None
  164.     title = ''
  165.     home = '/'
  166.     index = None
  167.     topics = None
  168.     encoding = None
  169.     lcid = None
  170.     binaryindex = None
  171.     
  172.     def __init__(self):
  173.         self.searchable = 0
  174.  
  175.     
  176.     def LoadCHM(self, archiveName):
  177.         if self.filename != None:
  178.             self.CloseCHM()
  179.         
  180.         self.file = chmlib.chm_open(archiveName)
  181.         if self.file == None:
  182.             return 0
  183.         self.filename = archiveName
  184.         self.GetArchiveInfo()
  185.         return 1
  186.  
  187.     
  188.     def CloseCHM(self):
  189.         if self.filename != None:
  190.             chmlib.chm_close(self.file)
  191.             self.file = None
  192.             self.filename = ''
  193.             self.title = ''
  194.             self.home = '/'
  195.             self.index = None
  196.             self.topics = None
  197.             self.encoding = None
  198.         
  199.  
  200.     
  201.     def GetArchiveInfo(self):
  202.         self.searchable = False
  203.         self.lcid = None
  204.         (result, ui) = chmlib.chm_resolve_object(self.file, '/#SYSTEM')
  205.         if result != chmlib.CHM_RESOLVE_SUCCESS:
  206.             sys.stderr.write('GetArchiveInfo: #SYSTEM does not exist\n')
  207.             return 0
  208.         (size, text) = chmlib.chm_retrieve_object(self.file, ui, 0x4L, ui.length)
  209.         if size == 0:
  210.             sys.stderr.write('GetArchiveInfo: file size = 0\n')
  211.             return 0
  212.         buff = array.array('B', text)
  213.         index = 0
  214.         while index < size:
  215.             cursor = buff[index] + buff[index + 1] * 256
  216.             if cursor == 0:
  217.                 index += 2
  218.                 cursor = buff[index] + buff[index + 1] * 256
  219.                 index += 2
  220.                 self.topics = '/' + text[index:index + cursor - 1]
  221.             elif cursor == 1:
  222.                 index += 2
  223.                 cursor = buff[index] + buff[index + 1] * 256
  224.                 index += 2
  225.                 self.index = '/' + text[index:index + cursor - 1]
  226.             elif cursor == 2:
  227.                 index += 2
  228.                 cursor = buff[index] + buff[index + 1] * 256
  229.                 index += 2
  230.                 self.home = '/' + text[index:index + cursor - 1]
  231.             elif cursor == 3:
  232.                 index += 2
  233.                 cursor = buff[index] + buff[index + 1] * 256
  234.                 index += 2
  235.                 self.title = text[index:index + cursor - 1]
  236.             elif cursor == 4:
  237.                 index += 2
  238.                 cursor = buff[index] + buff[index + 1] * 256
  239.                 index += 2
  240.                 self.lcid = buff[index] + buff[index + 1] * 256
  241.             elif cursor == 6:
  242.                 index += 2
  243.                 cursor = buff[index] + buff[index + 1] * 256
  244.                 index += 2
  245.                 tmp = text[index:index + cursor - 1]
  246.                 if not self.topics:
  247.                     tmp1 = '/' + tmp + '.hhc'
  248.                     tmp2 = '/' + tmp + '.hhk'
  249.                     (res1, ui1) = chmlib.chm_resolve_object(self.file, tmp1)
  250.                     (res2, ui2) = chmlib.chm_resolve_object(self.file, tmp2)
  251.                     if not (self.topics) and res1 == chmlib.CHM_RESOLVE_SUCCESS:
  252.                         self.topics = '/' + tmp + '.hhc'
  253.                     
  254.                     if not (self.index) and res2 == chmlib.CHM_RESOLVE_SUCCESS:
  255.                         self.index = '/' + tmp + '.hhk'
  256.                     
  257.                 
  258.             elif cursor == 16:
  259.                 index += 2
  260.                 cursor = buff[index] + buff[index + 1] * 256
  261.                 index += 2
  262.                 self.encoding = text[index:index + cursor - 1]
  263.             else:
  264.                 index += 2
  265.                 cursor = buff[index] + buff[index + 1] * 256
  266.                 index += 2
  267.             index += cursor
  268.         self.GetWindowsInfo()
  269.         if not self.lcid:
  270.             self.lcid = extra.get_lcid(self.file)
  271.         
  272.         return 1
  273.  
  274.     
  275.     def GetTopicsTree(self):
  276.         if self.topics == None:
  277.             return None
  278.         (size, text) = chmlib.chm_retrieve_object(self.file, ui, 0x0L, ui.length)
  279.         if size == 0:
  280.             sys.stderr.write('GetTopicsTree: file size = 0\n')
  281.             return None
  282.         return text
  283.  
  284.     
  285.     def GetIndex(self):
  286.         if self.index == None:
  287.             return None
  288.         (size, text) = chmlib.chm_retrieve_object(self.file, ui, 0x0L, ui.length)
  289.         if size == 0:
  290.             sys.stderr.write('GetIndex: file size = 0\n')
  291.             return None
  292.         return text
  293.  
  294.     
  295.     def ResolveObject(self, document):
  296.         if self.file:
  297.             path = document
  298.             return chmlib.chm_resolve_object(self.file, path)
  299.         return (1, None)
  300.  
  301.     
  302.     def RetrieveObject(self, ui, start = -1, length = -1):
  303.         if self.file and ui:
  304.             if length == -1:
  305.                 len = ui.length
  306.             else:
  307.                 len = length
  308.             if start == -1:
  309.                 st = 0x0L
  310.             else:
  311.                 st = long(start)
  312.             return chmlib.chm_retrieve_object(self.file, ui, st, len)
  313.         return (0, '')
  314.  
  315.     
  316.     def Search(self, text, wholewords = 0, titleonly = 0):
  317.         if text and text != '' and self.file:
  318.             return extra.search(self.file, text, wholewords, titleonly)
  319.         return None
  320.  
  321.     
  322.     def IsSearchable(self):
  323.         return self.searchable
  324.  
  325.     
  326.     def GetEncoding(self):
  327.         if self.encoding:
  328.             vals = string.split(self.encoding, ',')
  329.             if len(vals) > 2:
  330.                 
  331.                 try:
  332.                     return charset_table[int(vals[2])]
  333.                 except KeyError:
  334.                     pass
  335.                 except:
  336.                     None<EXCEPTION MATCH>KeyError
  337.                 
  338.  
  339.             None<EXCEPTION MATCH>KeyError
  340.         
  341.  
  342.     
  343.     def GetLCID(self):
  344.         if self.lcid in locale_table:
  345.             return locale_table[self.lcid]
  346.         return None
  347.  
  348.     
  349.     def GetDWORD(self, buff, idx = 0):
  350.         result = buff[idx] + (buff[idx + 1] << 8) + (buff[idx + 2] << 16) + (buff[idx + 3] << 24)
  351.         if result == 0xFFFFFFFFL:
  352.             result = 0
  353.         
  354.         return result
  355.  
  356.     
  357.     def GetString(self, text, idx):
  358.         next = string.find(text, '\x00', idx)
  359.         chunk = text[idx:next]
  360.         return chunk
  361.  
  362.     
  363.     def GetWindowsInfo(self):
  364.         (result, ui) = chmlib.chm_resolve_object(self.file, '/#WINDOWS')
  365.         if result != chmlib.CHM_RESOLVE_SUCCESS:
  366.             return -1
  367.         (size, text) = chmlib.chm_retrieve_object(self.file, ui, 0x0L, 8)
  368.         if size < 8:
  369.             return -2
  370.         buff = array.array('B', text)
  371.         num_entries = self.GetDWORD(buff, 0)
  372.         entry_size = self.GetDWORD(buff, 4)
  373.         if num_entries < 1:
  374.             return -3
  375.         (size, text) = chmlib.chm_retrieve_object(self.file, ui, 0x8L, entry_size)
  376.         if size < entry_size:
  377.             return -4
  378.         buff = array.array('B', text)
  379.         toc_index = self.GetDWORD(buff, 96)
  380.         idx_index = self.GetDWORD(buff, 100)
  381.         dft_index = self.GetDWORD(buff, 104)
  382.         (result, ui) = chmlib.chm_resolve_object(self.file, '/#STRINGS')
  383.         if result != chmlib.CHM_RESOLVE_SUCCESS:
  384.             return -5
  385.         (size, text) = chmlib.chm_retrieve_object(self.file, ui, 0x0L, ui.length)
  386.         if size == 0:
  387.             return -6
  388.  
  389.  
  390.