home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_950 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  8.4 KB  |  209 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. __license__ = 'GPL v3'
  6. __copyright__ = '2009, Kovid Goyal kovid@kovidgoyal.net'
  7. __docformat__ = 'restructuredtext en'
  8. import sys
  9. import os
  10. from calibre.utils.config import StringConfig
  11. from calibre.customize.ui import metadata_readers, metadata_writers
  12. from calibre.ebooks.metadata.meta import get_metadata, set_metadata
  13. from calibre.ebooks.metadata import string_to_authors, authors_to_sort_string, title_sort, MetaInformation
  14. from calibre.ebooks.lrf.meta import LRFMetaFile
  15. from calibre import prints
  16. from calibre.utils.date import parse_date
  17. USAGE = '%%prog ebook_file [' + _('options') + ']\n' + _('\nRead/Write metadata from/to ebook files.\n\nSupported formats for reading metadata: %s\n\nSupported formats for writing metadata: %s\n\nDifferent file types support different kinds of metadata. If you try to set\nsome metadata on a file type that does not support it, the metadata will be\nsilently ignored.\n')
  18.  
  19. def config():
  20.     c = StringConfig('')
  21.     c.add_opt('title', [
  22.         '-t',
  23.         '--title'], help = _('Set the title.'))
  24.     c.add_opt('authors', [
  25.         '-a',
  26.         '--authors'], help = _('Set the authors. Multiple authors should be separated by the & character. Author names should be in the order Firstname Lastname.'))
  27.     c.add_opt('title_sort', [
  28.         '--title-sort'], help = _('The version of the title to be used for sorting. If unspecified, and the title is specified, it will be auto-generated from the title.'))
  29.     c.add_opt('author_sort', [
  30.         '--author-sort'], help = _('String to be used when sorting by author. If unspecified, and the author(s) are specified, it will be auto-generated from the author(s).'))
  31.     c.add_opt('cover', [
  32.         '--cover'], help = _('Set the cover to the specified file.'))
  33.     c.add_opt('comments', [
  34.         '-c',
  35.         '--comments'], help = _('Set the ebook description.'))
  36.     c.add_opt('publisher', [
  37.         '-p',
  38.         '--publisher'], help = _('Set the ebook publisher.'))
  39.     c.add_opt('category', [
  40.         '--category'], help = _('Set the book category.'))
  41.     c.add_opt('series', [
  42.         '-s',
  43.         '--series'], help = _('Set the series this ebook belongs to.'))
  44.     c.add_opt('series_index', [
  45.         '-i',
  46.         '--index'], help = _('Set the index of the book in this series.'))
  47.     c.add_opt('rating', [
  48.         '-r',
  49.         '--rating'], help = _('Set the rating. Should be a number between 1 and 5.'))
  50.     c.add_opt('isbn', [
  51.         '--isbn'], help = _('Set the ISBN of the book.'))
  52.     c.add_opt('tags', [
  53.         '--tags'], help = _('Set the tags for the book. Should be a comma separated list.'))
  54.     c.add_opt('book_producer', [
  55.         '-k',
  56.         '--book-producer'], help = _('Set the book producer.'))
  57.     c.add_opt('language', [
  58.         '-l',
  59.         '--language'], help = _('Set the language.'))
  60.     c.add_opt('pubdate', [
  61.         '-d',
  62.         '--date'], help = _('Set the published date.'))
  63.     c.add_opt('get_cover', [
  64.         '--get-cover'], help = _('Get the cover from the ebook and save it at as the specified file.'))
  65.     c.add_opt('to_opf', [
  66.         '--to-opf'], help = _('Specify the name of an OPF file. The metadata will be written to the OPF file.'))
  67.     c.add_opt('from_opf', [
  68.         '--from-opf'], help = _('Read metadata from the specified OPF file and use it to set metadata in the ebook. Metadata specified on the command line will override metadata read from the OPF file'))
  69.     c.add_opt('lrf_bookid', [
  70.         '--lrf-bookid'], help = _('Set the BookID in LRF files'))
  71.     return c
  72.  
  73.  
  74. def filetypes():
  75.     readers = set([])
  76.     for r in metadata_readers():
  77.         readers = readers.union(set(r.file_types))
  78.     
  79.     return readers
  80.  
  81.  
  82. def option_parser():
  83.     writers = set([])
  84.     for w in metadata_writers():
  85.         writers = writers.union(set(w.file_types))
  86.     
  87.     ft = ', '.join(sorted(filetypes()))
  88.     w = ', '.join(sorted(writers))
  89.     return config().option_parser(USAGE % (ft, w))
  90.  
  91.  
  92. def do_set_metadata(opts, mi, stream, stream_type):
  93.     mi = MetaInformation(mi)
  94.     for x in ('guide', 'toc', 'manifest', 'spine'):
  95.         setattr(mi, x, None)
  96.     
  97.     from_opf = getattr(opts, 'from_opf', None)
  98.     if from_opf is not None:
  99.         OPF = OPF
  100.         import calibre.ebooks.metadata.opf2
  101.         opf_mi = MetaInformation(OPF(open(from_opf, 'rb')))
  102.         mi.smart_update(opf_mi)
  103.     
  104.     for pref in config().option_set.preferences:
  105.         if pref.name in ('to_opf', 'from_opf', 'authors', 'title_sort', 'author_sort', 'get_cover', 'cover', 'tags', 'lrf_bookid'):
  106.             continue
  107.         
  108.         val = getattr(opts, pref.name, None)
  109.         if val is not None:
  110.             setattr(mi, pref.name, val)
  111.             continue
  112.     
  113.     if getattr(opts, 'authors', None) is not None:
  114.         mi.authors = string_to_authors(opts.authors)
  115.         mi.author_sort = authors_to_sort_string(mi.authors)
  116.     
  117.     if getattr(opts, 'author_sort', None) is not None:
  118.         mi.author_sort = opts.author_sort
  119.     
  120.     if getattr(opts, 'title_sort', None) is not None:
  121.         mi.title_sort = opts.title_sort
  122.     elif getattr(opts, 'title', None) is not None:
  123.         mi.title_sort = title_sort(opts.title)
  124.     
  125.     if getattr(opts, 'series', None) is not None:
  126.         mi.series = opts.series.strip()
  127.     
  128.     if getattr(opts, 'series_index', None) is not None:
  129.         mi.series_index = float(opts.series_index.strip())
  130.     
  131.     if getattr(opts, 'pubdate', None) is not None:
  132.         mi.pubdate = parse_date(opts.pubdate, assume_utc = False, as_utc = False)
  133.     
  134.     if getattr(opts, 'cover', None) is not None:
  135.         ext = os.path.splitext(opts.cover)[1].replace('.', '').upper()
  136.         mi.cover_data = (ext, open(opts.cover, 'rb').read())
  137.     
  138.     set_metadata(stream, mi, stream_type)
  139.  
  140.  
  141. def main(args = sys.argv):
  142.     parser = option_parser()
  143.     (opts, args) = parser.parse_args(args)
  144.     if len(args) < 2:
  145.         parser.print_help()
  146.         prints(_('No file specified'), file = sys.stderr)
  147.         return 1
  148.     path = args[1]
  149.     stream = open(path, 'r+b')
  150.     stream_type = os.path.splitext(path)[1].replace('.', '').lower()
  151.     trying_to_set = False
  152.     for pref in config().option_set.preferences:
  153.         if pref.name in ('to_opf', 'get_cover'):
  154.             continue
  155.         
  156.         if getattr(opts, pref.name) is not None:
  157.             trying_to_set = True
  158.             break
  159.             continue
  160.     
  161.     mi = get_metadata(stream, stream_type)
  162.     if trying_to_set:
  163.         prints(_('Original metadata') + '::')
  164.     
  165.     metadata = unicode(mi)
  166.     if trying_to_set:
  167.         metadata = '\t' + '\n\t'.join(metadata.split('\n'))
  168.     
  169.     prints(metadata, safe_encode = True)
  170.     if trying_to_set:
  171.         stream.seek(0)
  172.         do_set_metadata(opts, mi, stream, stream_type)
  173.         stream.seek(0)
  174.         stream.flush()
  175.         lrf = None
  176.         if stream_type == 'lrf':
  177.             if opts.lrf_bookid is not None:
  178.                 lrf = LRFMetaFile(stream)
  179.                 lrf.book_id = opts.lrf_bookid
  180.             
  181.         
  182.         mi = get_metadata(stream, stream_type)
  183.         prints('\n' + _('Changed metadata') + '::')
  184.         metadata = unicode(mi)
  185.         metadata = '\t' + '\n\t'.join(metadata.split('\n'))
  186.         prints(metadata, safe_encode = True)
  187.         if lrf is not None:
  188.             prints('\tBookID:', lrf.book_id)
  189.         
  190.     
  191.     if opts.get_cover is not None:
  192.         if mi.cover_data and mi.cover_data[1]:
  193.             
  194.             try:
  195.                 f = _[2]
  196.                 f.write(mi.cover_data[1])
  197.                 prints(_('Cover saved to'), f.name)
  198.             finally:
  199.                 pass
  200.  
  201.         else:
  202.             prints(_('No cover found'), file = sys.stderr)
  203.     
  204.     return 0
  205.  
  206. if __name__ == '__main__':
  207.     sys.exit(main())
  208.  
  209.