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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. __license__ = 'GPL v3'
  6. __copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
  7. from urlparse import urldefrag
  8. import cssutils
  9. from calibre.ebooks.oeb.base import CSS_MIME, OEB_DOCS
  10. from calibre.ebooks.oeb.base import urlnormalize, iterlinks
  11.  
  12. class ManifestTrimmer(object):
  13.     
  14.     def config(cls, cfg):
  15.         return cfg
  16.  
  17.     config = classmethod(config)
  18.     
  19.     def generate(cls, opts):
  20.         return cls()
  21.  
  22.     generate = classmethod(generate)
  23.     
  24.     def __call__(self, oeb, context):
  25.         oeb.logger.info('Trimming unused files from manifest...')
  26.         self.opts = context
  27.         used = set()
  28.         for term in oeb.metadata:
  29.             for item in oeb.metadata[term]:
  30.                 if item.value in oeb.manifest.hrefs:
  31.                     used.add(oeb.manifest.hrefs[item.value])
  32.                     continue
  33.                 if item.value in oeb.manifest.ids:
  34.                     used.add(oeb.manifest.ids[item.value])
  35.                     continue
  36.             
  37.         
  38.         for ref in oeb.guide.values():
  39.             (path, _) = urldefrag(ref.href)
  40.             if path in oeb.manifest.hrefs:
  41.                 used.add(oeb.manifest.hrefs[path])
  42.                 continue
  43.         
  44.         for item in oeb.spine:
  45.             used.add(item)
  46.         
  47.         unchecked = used
  48.         while unchecked:
  49.             new = set()
  50.             for item in unchecked:
  51.                 if (item.media_type in OEB_DOCS or item.media_type[-4:] in ('/xml', '+xml')) and item.data is not None:
  52.                     hrefs = [ r[2] for r in iterlinks(item.data) ]
  53.                     for href in hrefs:
  54.                         href = item.abshref(urlnormalize(href))
  55.                         if href in oeb.manifest.hrefs:
  56.                             found = oeb.manifest.hrefs[href]
  57.                             if found not in used:
  58.                                 new.add(found)
  59.                             
  60.                         found not in used
  61.                     
  62.                 []
  63.                 if item.media_type == CSS_MIME:
  64.                     for href in cssutils.getUrls(item.data):
  65.                         href = item.abshref(urlnormalize(href))
  66.                         if href in oeb.manifest.hrefs:
  67.                             found = oeb.manifest.hrefs[href]
  68.                             if found not in used:
  69.                                 new.add(found)
  70.                             
  71.                         found not in used
  72.                     
  73.                 []
  74.             
  75.             used.update(new)
  76.             unchecked = new
  77.         for item in oeb.manifest.values():
  78.             if item not in used:
  79.                 oeb.logger.info('Trimming %r from manifest' % item.href)
  80.                 oeb.manifest.remove(item)
  81.                 continue
  82.         
  83.  
  84.  
  85.