~ubuntu-branches/ubuntu/karmic/calibre/karmic

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/oeb/transforms/trimmanifest.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-30 12:49:41 UTC
  • mfrom: (1.3.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090730124941-qjdsmri25zt8zocn
Tags: 0.6.3+dfsg-0ubuntu1
* New upstream release. Please see http://calibre.kovidgoyal.net/new_in_6/
  for the list of new features and changes.
* remove_postinstall.patch: Update for new version.
* build_debug.patch: Does not apply any more, disable for now. Might not be
  necessary any more.
* debian/copyright: Fix reference to versionless GPL.
* debian/rules: Drop obsolete dh_desktop call.
* debian/rules: Add workaround for weird Python 2.6 setuptools behaviour of
  putting compiled .so files into src/calibre/plugins/calibre/plugins
  instead of src/calibre/plugins.
* debian/rules: Drop hal fdi moving, new upstream version does not use hal
  any more. Drop hal dependency, too.
* debian/rules: Install udev rules into /lib/udev/rules.d.
* Add debian/calibre.preinst: Remove unmodified
  /etc/udev/rules.d/95-calibre.rules on upgrade.
* debian/control: Bump Python dependencies to 2.6, since upstream needs
  it now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
__license__   = 'GPL v3'
7
7
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
8
8
 
9
 
from itertools import chain
10
9
from urlparse import urldefrag
 
10
 
 
11
import cssutils
 
12
 
11
13
from calibre.ebooks.oeb.base import CSS_MIME, OEB_DOCS
12
 
from calibre.ebooks.oeb.base import LINK_SELECTORS, CSSURL_RE
13
 
from calibre.ebooks.oeb.base import urlnormalize
 
14
from calibre.ebooks.oeb.base import urlnormalize, iterlinks
14
15
 
15
16
class ManifestTrimmer(object):
16
 
    def transform(self, oeb, context):
 
17
    @classmethod
 
18
    def config(cls, cfg):
 
19
        return cfg
 
20
 
 
21
    @classmethod
 
22
    def generate(cls, opts):
 
23
        return cls()
 
24
 
 
25
    def __call__(self, oeb, context):
17
26
        oeb.logger.info('Trimming unused files from manifest...')
 
27
        self.opts = context
18
28
        used = set()
19
 
        hrefs = oeb.manifest.hrefs
20
29
        for term in oeb.metadata:
21
30
            for item in oeb.metadata[term]:
22
31
                if item.value in oeb.manifest.hrefs:
34
43
        while unchecked:
35
44
            new = set()
36
45
            for item in unchecked:
37
 
                if (item.media_type in OEB_DOCS or 
 
46
                if (item.media_type in OEB_DOCS or
38
47
                    item.media_type[-4:] in ('/xml', '+xml')) and \
39
48
                   item.data is not None:
40
 
                    hrefs = [sel(item.data) for sel in LINK_SELECTORS]
41
 
                    for href in chain(*hrefs):
 
49
                    hrefs = [r[2] for r in iterlinks(item.data)]
 
50
                    for href in hrefs:
42
51
                        href = item.abshref(urlnormalize(href))
43
52
                        if href in oeb.manifest.hrefs:
44
53
                            found = oeb.manifest.hrefs[href]
45
54
                            if found not in used:
46
55
                                new.add(found)
47
56
                elif item.media_type == CSS_MIME:
48
 
                    for match in CSSURL_RE.finditer(item.data):
49
 
                        href = match.group('url')
 
57
                    for href in cssutils.getUrls(item.data):
50
58
                        href = item.abshref(urlnormalize(href))
51
59
                        if href in oeb.manifest.hrefs:
52
60
                            found = oeb.manifest.hrefs[href]