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

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-30 12:49:41 UTC
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: james.westby@ubuntu.com-20090730124941-kviipg9ypwgppulc
Tags: upstream-0.6.3+dfsg
ImportĀ upstreamĀ versionĀ 0.6.3+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
'''Read meta information from TXT files'''
 
2
 
 
3
from __future__ import with_statement
 
4
 
 
5
__license__   = 'GPL v3'
 
6
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
 
7
 
 
8
import re
 
9
 
 
10
from calibre.ebooks.metadata import MetaInformation
 
11
 
 
12
def get_metadata(stream, extract_cover=True):
 
13
    """ Return metadata as a L{MetaInfo} object """
 
14
    mi = MetaInformation(_('Unknown'), [_('Unknown')])
 
15
    stream.seek(0)
 
16
 
 
17
    mdata = u''
 
18
    for x in range(0, 4):
 
19
        line = stream.readline().decode('utf-8')
 
20
        if line == '':
 
21
            break
 
22
        else:
 
23
            mdata += line
 
24
    
 
25
    mo = re.search('(?u)^[ ]*(?P<title>.+)[ ]*(\n{3}|(\r\n){3}|\r{3})[ ]*(?P<author>.+)[ ]*(\n|\r\n|\r)$', mdata)
 
26
    if mo != None:
 
27
        mi.title = mo.group('title')
 
28
        mi.authors = mo.group('author').split(',')
 
29
 
 
30
    return mi