2 # -*- coding: utf-8 -*-
 
   4 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
 
   5 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 
  10 from librarian import DirDocProvider, ParseError
 
  11 from librarian.parser import WLDocument
 
  14 if __name__ == '__main__':
 
  15     # Parse commandline arguments
 
  16     usage = """Usage: %prog [options] SOURCE [SOURCE...]
 
  17     Convert SOURCE files to FB2 format."""
 
  19     parser = optparse.OptionParser(usage=usage)
 
  21     parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
 
  22         help='print status messages to stdout')
 
  23     parser.add_option('-c', '--with-cover', action='store_true', dest='with_cover', default=False,
 
  24                       help='create default cover')
 
  25     parser.add_option('-w', '--working-copy', action='store_true', dest='working_copy', default=False,
 
  26                       help='mark the output as a working copy')
 
  27     parser.add_option('-d', '--make-dir', action='store_true', dest='make_dir', default=False,
 
  28                       help='create a directory for author and put the PDF in it')
 
  29     parser.add_option('-o', '--output-file', dest='output_file', metavar='FILE',
 
  30                       help='specifies the output file')
 
  31     parser.add_option('-O', '--output-dir', dest='output_dir', metavar='DIR',
 
  32                       help='specifies the directory for output')
 
  34     options, input_filenames = parser.parse_args()
 
  36     if len(input_filenames) < 1:
 
  41     if options.working_copy:
 
  42         flags.append('working-copy')
 
  46         for main_input in input_filenames:
 
  50             path, fname = os.path.realpath(main_input).rsplit('/', 1)
 
  51             provider = DirDocProvider(path)
 
  52             if not (options.output_file or options.output_dir):
 
  53                 output_file = os.path.splitext(main_input)[0] + '.fb2'
 
  57             doc = WLDocument.from_file(main_input, provider=provider)
 
  58             fb2 = doc.as_fb2(cover=options.with_cover, flags=flags)
 
  60             doc.save_output_file(fb2,
 
  61                 output_file, options.output_dir, options.make_dir, 'fb2')
 
  64         print '%(file)s:%(name)s:%(message)s' % {
 
  66             'name': e.__class__.__name__,