~stub/ubuntu/precise/calibre/devel

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-04-12 11:29:25 UTC
  • mfrom: (42.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110412112925-c7171kt2bb5rmft4
Tags: 0.7.50+dfsg-2
* debian/control: Build with libpodofo-dev to enable PDF metadata.
  (Closes: #619632)
* debian/control: Add libboost1.42-dev build dependency. Apparently it is
  needed in some setups. (Closes: #619807)
* debian/rules: Call dh_sip to generate a proper sip API dependency, to
  prevent crashes like #616372 for partial upgrades.
* debian/control: Bump python-qt4 dependency to >= 4.8.3-2, which reportedly
  fixes crashes on startup. (Closes: #619701, #620125)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
__license__   = 'GPL v3'
 
4
__copyright__ = '2011, John Schember <john@nachtimwald.com>'
 
5
 
 
6
'''
 
7
Read meta information from TXT files
 
8
'''
 
9
 
 
10
import os
 
11
 
 
12
from cStringIO import StringIO
 
13
 
 
14
from calibre.ebooks.metadata import MetaInformation
 
15
from calibre.ebooks.metadata.opf2 import OPF, metadata_to_opf
 
16
from calibre.ptempfile import TemporaryDirectory
 
17
from calibre.utils.zipfile import ZipFile, safe_replace
 
18
 
 
19
def get_metadata(stream, extract_cover=True):
 
20
    '''
 
21
    Return metadata as a L{MetaInfo} object
 
22
    '''
 
23
    mi = MetaInformation(_('Unknown'), [_('Unknown')])
 
24
    stream.seek(0)
 
25
 
 
26
    with TemporaryDirectory('_untxtz_mdata') as tdir:
 
27
        try:
 
28
            zf = ZipFile(stream)
 
29
            zf.extract('metadata.opf', tdir)
 
30
            with open(os.path.join(tdir, 'metadata.opf'), 'rb') as opff:
 
31
                mi = OPF(opff).to_book_metadata()
 
32
        except:
 
33
            return mi
 
34
    return mi
 
35
 
 
36
def set_metadata(stream, mi):
 
37
    opf = StringIO(metadata_to_opf(mi))
 
38
    safe_replace(stream, 'metadata.opf', opf)