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

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/metadata/meta.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-04-05 18:42:16 UTC
  • mfrom: (1.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20090405184216-cyb0x4edrwjcaw33
Tags: 0.5.9+dfsg-1
* New upstream release. (Closes: #525339)
* manpages-installation.patch: Encode generated manpages as UTF-8, to avoid
  UnicodeDecodeErrors when writing them out to files.
* debian/control: Demote calibre dependency of calibre-bin to Recommends:,
  which is sufficient and avoids a circular dependency. (Closes: #522059)
* debian/control: Drop build dependency help2man, current version does not
  need it any more.
* debian/control: Drop versioned build dependency on python-mechanize,
  current sid version is enough.
* debian/rules: Copy "setup.py install" command from cdbs'
  python-distutils.mk, since the current version broke this. This is a
  hackish workaround until #525436 gets fixed.
* debian/rules: Drop using $(wildcard ), use `ls`; the former does not work
  any more.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
import os, re, collections
6
6
 
7
7
from calibre.utils.config import prefs
8
 
 
 
8
 
9
9
from calibre.ebooks.metadata.opf2 import OPF
10
10
 
11
11
from calibre.customize.ui import get_file_type_metadata, set_file_type_metadata
15
15
                       'html', 'htm', 'xhtml', 'xhtm',
16
16
                       'rtf', 'fb2', 'pdf', 'prc', 'odt',
17
17
                       'epub', 'lit', 'lrx', 'lrf', 'mobi',
18
 
                       'rb', 'imp'
 
18
                       'rb', 'imp', 'azw'
19
19
                      ]
20
20
 
21
21
# The priorities for loading metadata from different file types
37
37
        mi2 = opf_metadata(opf)
38
38
        if mi2 is not None and mi2.title:
39
39
            return mi2
40
 
    
 
40
 
41
41
    for path, ext in zip(formats, extensions):
42
42
        with open(path, 'rb') as stream:
43
43
            try:
44
 
                mi.smart_update(get_metadata(stream, stream_type=ext, use_libprs_metadata=True))
 
44
                newmi = get_metadata(stream, stream_type=ext,
 
45
                                     use_libprs_metadata=True)
 
46
                mi.smart_update(newmi)
45
47
            except:
46
48
                continue
47
49
            if getattr(mi, 'application_id', None) is not None:
48
50
                return mi
49
 
    
 
51
 
50
52
    if not mi.title:
51
53
        mi.title = _('Unknown')
52
54
    if not mi.authors:
58
60
    if stream_type: stream_type = stream_type.lower()
59
61
    if stream_type in ('html', 'html', 'xhtml', 'xhtm', 'xml'):
60
62
        stream_type = 'html'
61
 
    if stream_type in ('mobi', 'prc'):
 
63
    if stream_type in ('mobi', 'prc', 'azw'):
62
64
        stream_type = 'mobi'
63
65
    if stream_type in ('odt', 'ods', 'odp', 'odg', 'odf'):
64
66
        stream_type = 'odt'
65
 
        
 
67
 
66
68
    opf = None
67
69
    if hasattr(stream, 'name'):
68
70
        c = os.path.splitext(stream.name)[0]+'.opf'
69
71
        if os.access(c, os.R_OK):
70
72
            opf = opf_metadata(os.path.abspath(c))
71
 
        
 
73
 
72
74
    if use_libprs_metadata and getattr(opf, 'application_id', None) is not None:
73
75
        return opf
74
 
    
 
76
 
75
77
    mi = MetaInformation(None, None)
76
78
    if prefs['read_file_metadata']:
77
79
        mi = get_file_type_metadata(stream, stream_type)
78
 
        
 
80
 
79
81
    name = os.path.basename(getattr(stream, 'name', ''))
80
82
    base = metadata_from_filename(name)
81
83
    if base.title == os.path.splitext(name)[0] and base.authors is None:
96
98
    base.smart_update(mi)
97
99
    if opf is not None:
98
100
        base.smart_update(opf)
99
 
        
 
101
 
100
102
    return base
101
103
 
102
104
def set_metadata(stream, mi, stream_type='lrf'):
103
105
    if stream_type:
104
106
        stream_type = stream_type.lower()
105
107
    set_file_type_metadata(stream, mi, stream_type)
106
 
    
107
 
    
 
108
 
 
109
 
108
110
def metadata_from_filename(name, pat=None):
109
 
    name = os.path.splitext(name)[0]
 
111
    name = name.rpartition('.')[0]
110
112
    mi = MetaInformation(None, None)
111
113
    if pat is None:
112
114
        pat = re.compile(prefs.get('filename_pattern'))
159
161
            mi = MetaInformation(opf)
160
162
            if hasattr(opf, 'cover') and opf.cover:
161
163
                cpath = os.path.join(os.path.dirname(opfpath), opf.cover)
162
 
                if os.access(cpath, os.R_OK):                     
 
164
                if os.access(cpath, os.R_OK):
163
165
                    fmt = cpath.rpartition('.')[-1]
164
166
                    data = open(cpath, 'rb').read()
165
167
                    mi.cover_data = (fmt, data)