Exemple #1
0
def main(argv):
    options, args = parser.parse_args(args=argv)
    args = [x.decode(sys.stdin.encoding) for x in args]
    logger.setLevel(logging.DEBUG if options.debug else logging.INFO)

    a = Author(options.author)
    logger.info("Loading book list (%d)" % options.author)
    a.load()
    logger.info("%d books loaded" % len(a.books))

    for file in chain(*prepare_args(options.recursive, args)):
        if not os.path.isfile(file):
            logger.info("Skipping %s: not a file" % file)
            continue

        try:
            book = Fb2Book.fromFile(file, options.strict)
            if not book.isValid():
                logger.warn(u"Invalid file %s" % file)
        except NotAFBZException:
            logger.warning("Not a valid fbz file: " + file)
            continue
        except DocumentInvalid, e:
            logger.error(u"Invalid file %s: %s" % (file, e))
            continue

        title = book.getTitle()
        b = a.findBook(title)
        if not b:
            logger.warn(u"No book %s found" % title)
            continue

        if not book.setYearAggressive(b.year):
            logger.debug(u"No changes are need to be made for «%s»" % title)
            continue
        else:
            logger.info(u"Book «%s» year is set to %d" % (title, b.year))

        book.save()
Exemple #2
0
def main(sys_argv):
    options, args = parser.parse_args(sys_argv[1:])
    logger.setLevel(logging.DEBUG if options.debug else logging.INFO)

    if not options.output:
        raise ArgumentsException('No output specified')
    if not options.title:
        raise ArgumentsException('No book title')

    books_combined = BookCreator(options.title.decode('utf-8'))
    bookstats = BookStat()

    for bookID, file in enumerate(chain(*imap(glob.iglob, args))):
        if not os.path.isfile(file):
            logger.info('Skipping %s: not a file' % file)
            continue

        try:
            book = Book.fromFile(file, True)
        except NotAFBZException:
            logger.warning('Not a valid fbz file: ' + file)
            continue

        bodies = book.getBodies()

        if len(bodies) > 2 or len(bodies) == 0:
            logger.error("Book %s has %d bodies" % (file, len(bodies)))
            continue

        if len(bodies) == 2 and bodies[1].attrib.get('name') != 'notes':
            logger.error("Book %s second body bodies is not [notes]" % file)
            continue

        ## #############################

        bookinfo = bookstats.process(book, bookID)

        ##################

        sp = Section(bodies[0])
        new_section = sp.rebuild_section(
            annotation=book.getAnnotation(),
            cover=book.getCover(),
            epigraphs=book.getEpigraphs(),
            title=book.getTitle(),
        )

        booknotes = []
        if len(bodies) > 1:
            for _pos, noteSection in enumerate(bodies[1]):
                if not noteSection.tag == fb2tag('section'):
                    if _pos or not noteSection.tag == fb2tag('title') :
                        logger.warn("Wrong note: %s in %s" % (noteSection.tag, file))
                    continue

                booknotes.append(noteSection)

        books_combined.insertBook(bookinfo.key, new_section, booknotes)

        for binary in book.getBinaries():
            bID = binary.attrib['id']
            if not bookinfo.referes(bID):
                logger.info('Skipping binary %s' % bID)
                continue

            books_combined.addBinary(binary)

    try:
        book_output = books_combined.finish(bookstats)
        book_output.saveAs(options.output, options.zip)
    except DocumentInvalid, e:
        logger.critical('Not a valid book: %s' % e)