Logo Search packages:      
Sourcecode: calibre version File versions  Download package


00001 '''Read meta information from TXT files'''

from __future__ import with_statement

__license__   = 'GPL v3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'

import os
import glob
import re

from calibre.ebooks.metadata import MetaInformation
from calibre.ptempfile import TemporaryDirectory
from calibre.utils.zipfile import ZipFile

00016 def get_metadata(stream, extract_cover=True):
    """ Return metadata as a L{MetaInfo} object """
    mi = MetaInformation(_('Unknown'), [_('Unknown')])

    pml = ''
    if stream.name.endswith('.pmlz'):
        with TemporaryDirectory('_unpmlz') as tdir:
            zf = ZipFile(stream)

            pmls = glob.glob(os.path.join(tdir, '*.pml'))
            for p in pmls:
                with open(p, 'r+b') as p_stream:
                    pml += p_stream.read()
        pml = stream.read()

    for comment in re.findall(r'(?mus)\\v.*?\\v', pml):
        m = re.search(r'TITLE="(.*?)"', comment)
        if m:
            mi.title = m.group(1).strip().decode('cp1252', 'replace')
        m = re.search(r'AUTHOR="(.*?)"', comment)
        if m:
            if mi.authors == [_('Unknown')]:
                mi.authors = []
            mi.authors.append(m.group(1).strip().decode('cp1252', 'replace'))
        m = re.search(r'PUBLISHER="(.*?)"', comment)
        if m:
            mi.publisher = m.group(1).strip().decode('cp1252', 'replace')
        m = re.search(r'COPYRIGHT="(.*?)"', comment)
        if m:
            mi.rights = m.group(1).strip().decode('cp1252', 'replace')
        m = re.search(r'ISBN="(.*?)"', comment)
        if m:
            mi.isbn = m.group(1).strip().decode('cp1252', 'replace')

    return mi

Generated by  Doxygen 1.6.0   Back to index