home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- __license__ = 'GPL v3'
- __copyright__ = '2009, John Schember <john@nachtimwald.com>'
- __docformat__ = 'restructuredtext en'
- import os
- import glob
- import re
- from calibre.ebooks.metadata import MetaInformation
- from calibre.ptempfile import TemporaryDirectory
- from calibre.utils.zipfile import ZipFile
- from calibre import prepare_string_for_xml
-
- def get_metadata(stream, extract_cover = True):
- mi = MetaInformation(_('Unknown'), [
- _('Unknown')])
- stream.seek(0)
- pml = ''
- if stream.name.endswith('.pmlz'):
-
- try:
- tdir = _[1]
- zf = ZipFile(stream)
- zf.extractall(tdir)
- pmls = glob.glob(os.path.join(tdir, '*.pml'))
- for p in pmls:
-
- try:
- p_stream = _[2]
- pml += p_stream.read()
- finally:
- pass
-
-
- if extract_cover:
- mi.cover_data = get_cover(os.path.splitext(os.path.basename(stream.name))[0], tdir, True)
- finally:
- pass
-
- else:
- pml = stream.read()
- if extract_cover:
- mi.cover_data = get_cover(os.path.splitext(os.path.basename(stream.name))[0], os.path.abspath(os.path.dirname(stream.name)))
-
- for comment in re.findall('(?mus)\\\\v.*?\\\\v', pml):
- m = re.search('TITLE="(.*?)"', comment)
- if m:
- mi.title = re.sub('[\x00-\x1f]', '', prepare_string_for_xml(m.group(1).strip().decode('cp1252', 'replace')))
-
- m = re.search('AUTHOR="(.*?)"', comment)
- if m:
- if mi.authors == [
- _('Unknown')]:
- mi.authors = []
-
- mi.authors.append(re.sub('[\x00-\x1f]', '', prepare_string_for_xml(m.group(1).strip().decode('cp1252', 'replace'))))
-
- m = re.search('PUBLISHER="(.*?)"', comment)
- if m:
- mi.publisher = re.sub('[\x00-\x1f]', '', prepare_string_for_xml(m.group(1).strip().decode('cp1252', 'replace')))
-
- m = re.search('COPYRIGHT="(.*?)"', comment)
- if m:
- mi.rights = re.sub('[\x00-\x1f]', '', prepare_string_for_xml(m.group(1).strip().decode('cp1252', 'replace')))
-
- m = re.search('ISBN="(.*?)"', comment)
- if m:
- mi.isbn = re.sub('[\x00-\x1f]', '', prepare_string_for_xml(m.group(1).strip().decode('cp1252', 'replace')))
- continue
-
- return mi
-
-
- def get_cover(name, tdir, top_level = False):
- cover_path = ''
- cover_data = None
- if top_level:
- cover_path = None if os.path.exists(os.path.join(tdir, 'cover.png')) else ''
-
- if not cover_path:
- if os.path.exists(os.path.join(tdir, name + '_img', 'cover.png')):
- pass
- elif os.path.exists(os.path.join(os.path.join(tdir, 'images'), 'cover.png')):
- pass
-
- cover_path = ''
-
- return ('png', cover_data)
-
-