jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def edit_all_cc_data(book_id, book, to_save):
    cc = calibre_db.session.query(db.CustomColumns).filter(db.CustomColumns.datatype.notin_(db.cc_exceptions)).all()
    return edit_cc_data(book_id, book, to_save, cc)
//  [python]
def edit_all_cc_data(book_id, book, to_save):
    cc = calibre_db.session.query(db.Custom_Columns).filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all()
    return edit_cc_data(book_id, book, to_save, cc)
//  [python]
def edit_book_comments(comments, book):
    modify_date = False
    if comments:
        comments = clean_html(comments)
    if len(book.comments):
        if book.comments[0].text != comments:
            book.comments[0].text = comments
            modify_date = True
    else:
        if comments:
            book.comments.append(db.Comments(comment=comments, book=book.id))
            modify_date = True
    return modify_date
//  [python]
def edit_book_comments(comments, book):
    modif_date = False
    if comments:
        comments = clean_html(comments)
    if len(book.comments):
        if book.comments[0].text != comments:
            book.comments[0].text = comments
            modif_date = True
    else:
        if comments:
            book.comments.append(db.Comments(text=comments, book=book.id))
            modif_date = True
    return modif_date
//  [python]
def edit_single_cc_data(book_id, book, column_id, to_save):
    cc = (calibre_db.session.query(db.CustomColumns)
          .filter(db.CustomColumns.datatype.notin_(db.cc_exceptions))
          .filter(db.CustomColumns.id == column_id)
          .all())
    return edit_cc_data(book_id, book, to_save, cc)
//  [python]
def edit_single_cc_data(book_id, book, column_id, to_save):
    cc = (calibre_db.session.query(db.Custom_Columns)
          .filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions))
          .filter(db.Custom_Columns.id == column_id)
          .all())
    return edit_cc_data(book_id, book, to_save, cc)
//  [python]
def edit_book_languages(languages, book, upload_mode=False, invalid=None):
    input_languages = languages.split(',')
    unknown_languages = []
    if not upload_mode:
        input_l = isoLanguages.get_language_codes(get_locale(), input_languages, unknown_languages)
    else:
        input_l = isoLanguages.get_valid_language_codes(get_locale(), input_languages, unknown_languages)
    for lang in unknown_languages:
        log.error("'%s' is not a valid language", lang)
        if isinstance(invalid, list):
            invalid.append(lang)
        else:
            raise ValueError(_(u"'%(langname)s' is not a valid language", langname=lang))
    # ToDo: Not working correct
    if upload_mode and len(input_l) == 1:
        # If the language of the file is excluded from the users view, it's not imported, to allow the user to view
        # the book it's language is set to the filter language
        if input_l[0] != current_user.filter_language() and current_user.filter_language() != "all":
            input_l[0] = calibre_db.session.query(db.Languages). \
                filter(db.Languages.lang_code == current_user.filter_language()).first().lang_code
    # Remove duplicates
    input_l = helper.uniq(input_l)
    return modify_database_object(input_l, book.languages, db.Languages, calibre_db.session, 'languages')
//  [python]
def edit_book_languages(languages, book, upload=False, invalid=None):
    input_languages = languages.split(',')
    unknown_languages = []
    if not upload:
        input_l = isoLanguages.get_language_codes(get_locale(), input_languages, unknown_languages)
    else:
        input_l = isoLanguages.get_valid_language_codes(get_locale(), input_languages, unknown_languages)
    for l in unknown_languages:
        log.error("'%s' is not a valid language", l)
        if isinstance(invalid, list):
            invalid.append(l)
        else:
            raise ValueError(_(u"'%(langname)s' is not a valid language", langname=l))
    # ToDo: Not working correct
    if upload and len(input_l) == 1:
        # If the language of the file is excluded from the users view, it's not imported, to allow the user to view
        # the book it's language is set to the filter language
        if input_l[0] != current_user.filter_language() and current_user.filter_language() != "all":
            input_l[0] = calibre_db.session.query(db.Languages). \
                filter(db.Languages.lang_code == current_user.filter_language()).first().lang_code
    # Remove duplicates
    input_l = helper.uniq(input_l)
    return modify_database_object(input_l, book.languages, db.Languages, calibre_db.session, 'languages')
//  [python]
def edit_book_series_index(series_index, book):
    # Add default series_index to book
    modify_date = False
    series_index = series_index or '1'
    if not series_index.replace('.', '', 1).isdigit():
        flash(_("%(seriesindex)s is not a valid number, skipping", seriesindex=series_index), category="warning")
        return False
    if str(book.series_index) != series_index:
        book.series_index = series_index
        modify_date = True
    return modify_date
//  [python]
def edit_book_series_index(series_index, book):
    # Add default series_index to book
    modif_date = False
    series_index = series_index or '1'
    if not series_index.replace('.', '', 1).isdigit():
        flash(_("%(seriesindex)s is not a valid number, skipping", seriesindex=series_index), category="warning")
        return False
    if str(book.series_index) != series_index:
        book.series_index = series_index
        modif_date = True
    return modif_date
//  [python]
def handle_author_on_edit(book, author_name, update_stored=True):
    # handle author(s)
    input_authors, renamed = prepare_authors(author_name)
    '''input_authors = author_name.split('&')
    input_authors = list(map(lambda it: it.strip().replace(',', '|'), input_authors))
    # Remove duplicates in authors list
    input_authors = helper.uniq(input_authors)
    # we have all author names now
    if input_authors == ['']:
        input_authors = [_(u'Unknown')]  # prevent empty Author

    renamed = list()
    for in_aut in input_authors:
        renamed_author = calibre_db.session.query(db.Authors).filter(db.Authors.name == in_aut).first()
        if renamed_author and in_aut != renamed_author.name:
            renamed.append(renamed_author.name)
            all_books = calibre_db.session.query(db.Books) \
                .filter(db.Books.authors.any(db.Authors.name == renamed_author.name)).all()
            sorted_renamed_author = helper.get_sorted_author(renamed_author.name)
            sorted_old_author = helper.get_sorted_author(in_aut)
            for one_book in all_books:
                one_book.author_sort = one_book.author_sort.replace(sorted_renamed_author, sorted_old_author)'''

    change = modify_database_object(input_authors, book.authors, db.Authors, calibre_db.session, 'author')

    # Search for each author if author is in database, if not, author name and sorted author name is generated new
    # everything then is assembled for sorted author field in database
    sort_authors_list = list()
    for inp in input_authors:
        stored_author = calibre_db.session.query(db.Authors).filter(db.Authors.name == inp).first()
        if not stored_author:
            stored_author = helper.get_sorted_author(inp)
        else:
            stored_author = stored_author.sort
        sort_authors_list.append(helper.get_sorted_author(stored_author))
    sort_authors = ' & '.join(sort_authors_list)
    if book.author_sort != sort_authors and update_stored:
        book.author_sort = sort_authors
        change = True
    return input_authors, change, renamed
//  [python]
def handle_author_on_edit(book, author_name, update_stored=True):
    # handle author(s)
    # renamed = False
    input_authors = author_name.split('&')
    input_authors = list(map(lambda it: it.strip().replace(',', '|'), input_authors))
    # Remove duplicates in authors list
    input_authors = helper.uniq(input_authors)
    # we have all author names now
    if input_authors == ['']:
        input_authors = [_(u'Unknown')]  # prevent empty Author

    renamed = list()
    for in_aut in input_authors:
        renamed_author = calibre_db.session.query(db.Authors).filter(db.Authors.name == in_aut).first()
        if renamed_author and in_aut != renamed_author.name:
            renamed.append(renamed_author.name)
            all_books = calibre_db.session.query(db.Books) \
                .filter(db.Books.authors.any(db.Authors.name == renamed_author.name)).all()
            sorted_renamed_author = helper.get_sorted_author(renamed_author.name)
            sorted_old_author = helper.get_sorted_author(in_aut)
            for one_book in all_books:
                one_book.author_sort = one_book.author_sort.replace(sorted_renamed_author, sorted_old_author)

    change = modify_database_object(input_authors, book.authors, db.Authors, calibre_db.session, 'author')

    # Search for each author if author is in database, if not, author name and sorted author name is generated new
    # everything then is assembled for sorted author field in database
    sort_authors_list = list()
    for inp in input_authors:
        stored_author = calibre_db.session.query(db.Authors).filter(db.Authors.name == inp).first()
        if not stored_author:
            stored_author = helper.get_sorted_author(inp)
        else:
            stored_author = stored_author.sort
        sort_authors_list.append(helper.get_sorted_author(stored_author))
    sort_authors = ' & '.join(sort_authors_list)
    if book.author_sort != sort_authors and update_stored:
        book.author_sort = sort_authors
        change = True
    return input_authors, change, renamed
//  [python]
def modify_identifiers(input_identifiers, db_identifiers, db_session):
    """Modify Identifiers to match input information.
       input_identifiers is a list of read-to-persist Identifiers objects.
       db_identifiers is a list of already persisted list of Identifiers objects."""
    changed = False
    error = False
    input_dict = dict([(identifier.type.lower(), identifier) for identifier in input_identifiers])
    if len(input_identifiers) != len(input_dict):
        error = True
    db_dict = dict([(identifier.type.lower(), identifier) for identifier in db_identifiers])
    # delete db identifiers not present in input or modify them with input val
    for identifier_type, identifier in db_dict.items():
        if identifier_type not in input_dict.keys():
            db_session.delete(identifier)
            changed = True
        else:
            input_identifier = input_dict[identifier_type]
            identifier.type = input_identifier.type
            identifier.val = input_identifier.val
    # add input identifiers not present in db
    for identifier_type, identifier in input_dict.items():
        if identifier_type not in db_dict.keys():
            db_session.add(identifier)
            changed = True
    return changed, error
//  [python]
def modify_identifiers(input_identifiers, db_identifiers, db_session):
    """Modify Identifiers to match input information.
       input_identifiers is a list of read-to-persist Identifiers objects.
       db_identifiers is a list of already persisted list of Identifiers objects."""
    changed = False
    error = False
    input_dict = dict([(identifier.type.lower(), identifier) for identifier in input_identifiers])
    if len(input_identifiers) != len(input_dict):
        error = True
    db_dict = dict([(identifier.type.lower(), identifier) for identifier in db_identifiers ])
    # delete db identifiers not present in input or modify them with input val
    for identifier_type, identifier in db_dict.items():
        if identifier_type not in input_dict.keys():
            db_session.delete(identifier)
            changed = True
        else:
            input_identifier = input_dict[identifier_type]
            identifier.type = input_identifier.type
            identifier.val = input_identifier.val
    # add input identifiers not present in db
    for identifier_type, identifier in input_dict.items():
        if identifier_type not in db_dict.keys():
            db_session.add(identifier)
            changed = True
    return changed, error
//  [python]
def merge_list_book():
    vals = request.get_json().get('Merge_books')
    to_file = list()
    if vals:
        # load all formats from target book
        to_book = calibre_db.get_book(vals[0])
        vals.pop(0)
        if to_book:
            for file in to_book.data:
                to_file.append(file.format)
            to_name = helper.get_valid_filename(to_book.title,
                                                chars=96) + ' - ' + helper.get_valid_filename(to_book.authors[0].name,
                                                                                              chars=96)
            for book_id in vals:
                from_book = calibre_db.get_book(book_id)
                if from_book:
                    for element in from_book.data:
                        if element.format not in to_file:
                            # create new data entry with: book_id, book_format, uncompressed_size, name
                            filepath_new = os.path.normpath(os.path.join(config.config_calibre_dir,
                                                                         to_book.path,
                                                                         to_name + "." + element.format.lower()))
                            filepath_old = os.path.normpath(os.path.join(config.config_calibre_dir,
                                                                         from_book.path,
                                                                         element.name + "." + element.format.lower()))
                            copyfile(filepath_old, filepath_new)
                            to_book.data.append(db.Data(to_book.id,
                                                        element.format,
                                                        element.uncompressed_size,
                                                        to_name))
                    delete_book_from_table(from_book.id, "", True)
                    return json.dumps({'success': True})
    return ""
//  [python]
def merge_list_book():
    vals = request.get_json().get('Merge_books')
    to_file = list()
    if vals:
        # load all formats from target book
        to_book = calibre_db.get_book(vals[0])
        vals.pop(0)
        if to_book:
            for file in to_book.data:
                to_file.append(file.format)
            to_name = helper.get_valid_filename(to_book.title, chars=96) + ' - ' + \
                      helper.get_valid_filename(to_book.authors[0].name, chars=96)
            for book_id in vals:
                from_book = calibre_db.get_book(book_id)
                if from_book:
                    for element in from_book.data:
                        if element.format not in to_file:
                            # create new data entry with: book_id, book_format, uncompressed_size, name
                            filepath_new = os.path.normpath(os.path.join(config.config_calibre_dir,
                                                                         to_book.path,
                                                                         to_name + "." + element.format.lower()))
                            filepath_old = os.path.normpath(os.path.join(config.config_calibre_dir,
                                                                         from_book.path,
                                                                         element.name + "." + element.format.lower()))
                            copyfile(filepath_old, filepath_new)
                            to_book.data.append(db.Data(to_book.id,
                                                        element.format,
                                                        element.uncompressed_size,
                                                        to_name))
                    delete_book_from_table(from_book.id,"", True)
                    return json.dumps({'success': True})
    return ""
//  [python]
def render_edit_book(book_id):
    cc = calibre_db.session.query(db.CustomColumns).filter(db.CustomColumns.datatype.notin_(db.cc_exceptions)).all()
    book = calibre_db.get_filtered_book(book_id, allow_show_archived=True)
    if not book:
        flash(_(u"Oops! Selected book title is unavailable. File does not exist or is not accessible"),
              category="error")
        return redirect(url_for("web.index"))

    for lang in book.languages:
        lang.language_name = isoLanguages.get_language_name(get_locale(), lang.lang_code)

    book.authors = calibre_db.order_authors([book])

    author_names = []
    for authr in book.authors:
        author_names.append(authr.name.replace('|', ','))

    # Option for showing convertbook button
    valid_source_formats = list()
    allowed_conversion_formats = list()
    kepub_possible = None
    if config.config_converterpath:
        for file in book.data:
            if file.format.lower() in constants.EXTENSIONS_CONVERT_FROM:
                valid_source_formats.append(file.format.lower())
    if config.config_kepubifypath and 'epub' in [file.format.lower() for file in book.data]:
        kepub_possible = True
        if not config.config_converterpath:
            valid_source_formats.append('epub')

    # Determine what formats don't already exist
    if config.config_converterpath:
        allowed_conversion_formats = constants.EXTENSIONS_CONVERT_TO[:]
        for file in book.data:
            if file.format.lower() in allowed_conversion_formats:
                allowed_conversion_formats.remove(file.format.lower())
    if kepub_possible:
        allowed_conversion_formats.append('kepub')
    return render_title_template('book_edit.html', book=book, authors=author_names, cc=cc,
                                 title=_(u"edit metadata"), page="editbook",
                                 conversion_formats=allowed_conversion_formats,
                                 config=config,
                                 source_formats=valid_source_formats)
//  [python]
def render_edit_book(book_id):
    cc = calibre_db.session.query(db.Custom_Columns).filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all()
    book = calibre_db.get_filtered_book(book_id, allow_show_archived=True)
    if not book:
        flash(_(u"Oops! Selected book title is unavailable. File does not exist or is not accessible"), category="error")
        return redirect(url_for("web.index"))

    for lang in book.languages:
        lang.language_name = isoLanguages.get_language_name(get_locale(), lang.lang_code)

    book.authors = calibre_db.order_authors([book])

    author_names = []
    for authr in book.authors:
        author_names.append(authr.name.replace('|', ','))

    # Option for showing convertbook button
    valid_source_formats=list()
    allowed_conversion_formats = list()
    kepub_possible=None
    if config.config_converterpath:
        for file in book.data:
            if file.format.lower() in constants.EXTENSIONS_CONVERT_FROM:
                valid_source_formats.append(file.format.lower())
    if config.config_kepubifypath and 'epub' in [file.format.lower() for file in book.data]:
        kepub_possible = True
        if not config.config_converterpath:
            valid_source_formats.append('epub')

    # Determine what formats don't already exist
    if config.config_converterpath:
        allowed_conversion_formats = constants.EXTENSIONS_CONVERT_TO[:]
        for file in book.data:
            if file.format.lower() in allowed_conversion_formats:
                allowed_conversion_formats.remove(file.format.lower())
    if kepub_possible:
        allowed_conversion_formats.append('kepub')
    return render_title_template('book_edit.html', book=book, authors=author_names, cc=cc,
                                 title=_(u"edit metadata"), page="editbook",
                                 conversion_formats=allowed_conversion_formats,
                                 config=config,
                                 source_formats=valid_source_formats)
//  [python]
def prepare_authors_on_upload(title, authr):
    if title != _(u'Unknown') and authr != _(u'Unknown'):
        entry = calibre_db.check_exists_book(authr, title)
        if entry:
            log.info("Uploaded book probably exists in library")
            flash(_(u"Uploaded book probably exists in the library, consider to change before upload new: ")
                  + Markup(render_title_template('book_exists_flash.html', entry=entry)), category="warning")

    input_authors, renamed = prepare_authors(authr)

    sort_authors_list = list()
    db_author = None
    for inp in input_authors:
        stored_author = calibre_db.session.query(db.Authors).filter(db.Authors.name == inp).first()
        if not stored_author:
            if not db_author:
                db_author = db.Authors(inp, helper.get_sorted_author(inp), "")
                calibre_db.session.add(db_author)
                calibre_db.session.commit()
            sort_author = helper.get_sorted_author(inp)
        else:
            if not db_author:
                db_author = stored_author
            sort_author = stored_author.sort
        sort_authors_list.append(sort_author)
    sort_authors = ' & '.join(sort_authors_list)
    return sort_authors, input_authors, db_author, renamed
//  [python]
def prepare_authors_on_upload(title, authr):
    if title != _(u'Unknown') and authr != _(u'Unknown'):
        entry = calibre_db.check_exists_book(authr, title)
        if entry:
            log.info("Uploaded book probably exists in library")
            flash(_(u"Uploaded book probably exists in the library, consider to change before upload new: ")
                  + Markup(render_title_template('book_exists_flash.html', entry=entry)), category="warning")

    # handle authors
    input_authors = authr.split('&')
    # handle_authors(input_authors)
    input_authors = list(map(lambda it: it.strip().replace(',', '|'), input_authors))
    # Remove duplicates in authors list
    input_authors = helper.uniq(input_authors)

    # we have all author names now
    if input_authors == ['']:
        input_authors = [_(u'Unknown')]  # prevent empty Author

    renamed = list()
    for in_aut in input_authors:
        renamed_author = calibre_db.session.query(db.Authors).filter(db.Authors.name == in_aut).first()
        if renamed_author and in_aut != renamed_author.name:
            renamed.append(renamed_author.name)
            all_books = calibre_db.session.query(db.Books) \
                .filter(db.Books.authors.any(db.Authors.name == renamed_author.name)).all()
            sorted_renamed_author = helper.get_sorted_author(renamed_author.name)
            sorted_old_author = helper.get_sorted_author(in_aut)
            for one_book in all_books:
                one_book.author_sort = one_book.author_sort.replace(sorted_renamed_author, sorted_old_author)

    sort_authors_list = list()
    db_author = None
    for inp in input_authors:
        stored_author = calibre_db.session.query(db.Authors).filter(db.Authors.name == inp).first()
        if not stored_author:
            if not db_author:
                db_author = db.Authors(inp, helper.get_sorted_author(inp), "")
                calibre_db.session.add(db_author)
                calibre_db.session.commit()
            sort_author = helper.get_sorted_author(inp)
        else:
            if not db_author:
                db_author = stored_author
            sort_author = stored_author.sort
        sort_authors_list.append(sort_author)
    sort_authors = ' & '.join(sort_authors_list)
    return sort_authors, input_authors, db_author, renamed
//  [python]
def table_xchange_author_title():
    vals = request.get_json().get('xchange')
    edited_books_id = False
    if vals:
        for val in vals:
            modify_date = False
            book = calibre_db.get_book(val)
            authors = book.title
            book.authors = calibre_db.order_authors([book])
            author_names = []
            for authr in book.authors:
                author_names.append(authr.name.replace('|', ','))

            title_change = handle_title_on_edit(book, " ".join(author_names))
            input_authors, authorchange, renamed = handle_author_on_edit(book, authors)
            if authorchange or title_change:
                edited_books_id = book.id
                modify_date = True

            if config.config_use_google_drive:
                gdriveutils.updateGdriveCalibreFromLocal()

            if edited_books_id:
                helper.update_dir_structure(edited_books_id, config.config_calibre_dir, input_authors[0],
                                            renamed_author=renamed)
            if modify_date:
                book.last_modified = datetime.utcnow()
            try:
                calibre_db.session.commit()
            except (OperationalError, IntegrityError) as e:
                calibre_db.session.rollback()
                log.error_or_exception("Database error: %s", e)
                return json.dumps({'success': False})

            if config.config_use_google_drive:
                gdriveutils.updateGdriveCalibreFromLocal()
        return json.dumps({'success': True})
    return ""
//  [python]
def table_xchange_author_title():
    vals = request.get_json().get('xchange')
    if vals:
        for val in vals:
            modif_date = False
            book = calibre_db.get_book(val)
            authors = book.title
            book.authors = calibre_db.order_authors([book])
            author_names = []
            for authr in book.authors:
                author_names.append(authr.name.replace('|', ','))

            title_change = handle_title_on_edit(book, " ".join(author_names))
            input_authors, authorchange, renamed = handle_author_on_edit(book, authors)
            if authorchange or title_change:
                edited_books_id = book.id
                modif_date = True

            if config.config_use_google_drive:
                gdriveutils.updateGdriveCalibreFromLocal()

            if edited_books_id:
                helper.update_dir_structure(edited_books_id, config.config_calibre_dir, input_authors[0],
                                           renamed_author=renamed)
            if modif_date:
                book.last_modified = datetime.utcnow()
            try:
                calibre_db.session.commit()
            except (OperationalError, IntegrityError) as e:
                calibre_db.session.rollback()
                log.error_or_exception("Database error: %s", e)
                return json.dumps({'success': False})

            if config.config_use_google_drive:
                gdriveutils.updateGdriveCalibreFromLocal()
        return json.dumps({'success': True})
    return ""
//  [python]
def table_get_custom_enum(c_id):
    ret = list()
    cc = (calibre_db.session.query(db.CustomColumns)
          .filter(db.CustomColumns.id == c_id)
          .filter(db.CustomColumns.datatype.notin_(db.cc_exceptions)).one_or_none())
    ret.append({'value': "", 'text': ""})
    for idx, en in enumerate(cc.get_display_dict()['enum_values']):
        ret.append({'value': en, 'text': en})
    return json.dumps(ret)
//  [python]
def table_get_custom_enum(c_id):
    ret = list()
    cc = (calibre_db.session.query(db.Custom_Columns)
              .filter(db.Custom_Columns.id == c_id)
              .filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).one_or_none())
    ret.append({'value': "", 'text': ""})
    for idx, en in enumerate(cc.get_display_dict()['enum_values']):
        ret.append({'value': en, 'text': en})
    return json.dumps(ret)
//  [python]
def render_delete_book_result(book_format, json_response, warning, book_id):
    if book_format:
        if json_response:
            return json.dumps([warning, {"location": url_for("edit-book.edit_book", book_id=book_id),
                                         "type": "success",
                                         "format": book_format,
                                         "message": _('Book Format Successfully Deleted')}])
        else:
            flash(_('Book Format Successfully Deleted'), category="success")
            return redirect(url_for('edit-book.edit_book', book_id=book_id))
    else:
        if json_response:
            return json.dumps([warning, {"location": url_for('web.index'),
                                         "type": "success",
                                         "format": book_format,
                                         "message": _('Book Successfully Deleted')}])
        else:
            flash(_('Book Successfully Deleted'), category="success")
            return redirect(url_for('web.index'))
//  [python]
def render_delete_book_result(book_format, jsonResponse, warning, book_id):
    if book_format:
        if jsonResponse:
            return json.dumps([warning, {"location": url_for("editbook.edit_book", book_id=book_id),
                                         "type": "success",
                                         "format": book_format,
                                         "message": _('Book Format Successfully Deleted')}])
        else:
            flash(_('Book Format Successfully Deleted'), category="success")
            return redirect(url_for('editbook.edit_book', book_id=book_id))
    else:
        if jsonResponse:
            return json.dumps([warning, {"location": url_for('web.index'),
                                         "type": "success",
                                         "format": book_format,
                                         "message": _('Book Successfully Deleted')}])
        else:
            flash(_('Book Successfully Deleted'), category="success")
            return redirect(url_for('web.index'))
//  [python]
def upload_cover(cover_request, book):
    if 'btn-upload-cover' in cover_request.files:
        requested_file = cover_request.files['btn-upload-cover']
        # check for empty request
        if requested_file.filename != '':
            if not current_user.role_upload():
                abort(403)
            ret, message = helper.save_cover(requested_file, book.path)
            if ret is True:
                return True
            else:
                flash(message, category="error")
                return False
    return None
//  [python]
def upload_cover(request, book):
    if 'btn-upload-cover' in request.files:
        requested_file = request.files['btn-upload-cover']
        # check for empty request
        if requested_file.filename != '':
            if not current_user.role_upload():
                abort(403)
            ret, message = helper.save_cover(requested_file, book.path)
            if ret is True:
                return True
            else:
                flash(message, category="error")
                return False
    return None
//  [python]
def jstree_data(node, selected_node):
    result = []

    result.append('{')
    result.append(format_html('"text": "{}",', node.label))
    result.append(
        '"state": {{ "opened": true, "selected": {} }},'.format(
            'true' if node == selected_node else 'false'
        )
    )
    result.append(
        '"data": {{ "href": "{}" }},'.format(node.get_absolute_url())
    )

    children = node.get_children().order_by('label',)

    if children:
        result.append('"children" : [')
        for child in children:
            result.extend(jstree_data(node=child, selected_node=selected_node))

        result.append(']')

    result.append('},')

    return result
//  [python]
def jstree_data(node, selected_node):
    result = []
    result.append('{')
    result.append('"text": "{}",'.format(node.label))
    result.append(
        '"state": {{ "opened": true, "selected": {} }},'.format(
            'true' if node == selected_node else 'false'
        )
    )
    result.append(
        '"data": {{ "href": "{}" }},'.format(node.get_absolute_url())
    )

    children = node.get_children().order_by('label',)

    if children:
        result.append('"children" : [')
        for child in children:
            result.extend(jstree_data(node=child, selected_node=selected_node))

        result.append(']')

    result.append('},')

    return result
//  [python]
def is_safe_url(url, host=None):
    """
    Return ``True`` if the url is a safe redirection (i.e. it doesn't point to
    a different host and uses a safe scheme).

    Always returns ``False`` on an empty url.
    """
    if url is not None:
        url = url.strip()
    if not url:
        return False
    # Chrome treats \ completely as / in paths but it could be part of some
    # basic auth credentials so we need to check both URLs.
    return _is_safe_url(url, host) and _is_safe_url(url.replace('\\', '/'), host)
//  [python]
def is_safe_url(url, host=None):
    """
    Return ``True`` if the url is a safe redirection (i.e. it doesn't point to
    a different host and uses a safe scheme).

    Always returns ``False`` on an empty url.
    """
    if url is not None:
        url = url.strip()
    if not url:
        return False
    # Chrome treats \ completely as /
    url = url.replace('\\', '/')
    # Chrome considers any URL with more than two slashes to be absolute, but
    # urlparse is not so flexible. Treat any url with three slashes as unsafe.
    if url.startswith('///'):
        return False
    url_info = urlparse(url)
    # Forbid URLs like http:///example.com - with a scheme, but without a hostname.
    # In that URL, example.com is not the hostname but, a path component. However,
    # Chrome will still consider example.com to be the hostname, so we must not
    # allow this syntax.
    if not url_info.netloc and url_info.scheme:
        return False
    # Forbid URLs that start with control characters. Some browsers (like
    # Chrome) ignore quite a few control characters at the start of a
    # URL and might consider the URL as scheme relative.
    if unicodedata.category(url[0])[0] == 'C':
        return False
    return ((not url_info.netloc or url_info.netloc == host) and
            (not url_info.scheme or url_info.scheme in ['http', 'https']))
//  [python]
def env() -> Environment:
    from openapi_python_client import utils

    TEMPLATE_FILTERS = {"snakecase": utils.snake_case, "kebabcase": utils.kebab_case}
    env = Environment(loader=PackageLoader("openapi_python_client"), trim_blocks=True, lstrip_blocks=True)
    env.filters.update(TEMPLATE_FILTERS)
    return env
//  [python]
def env() -> Environment:
    from openapi_python_client import utils

    TEMPLATE_FILTERS = {"snakecase": utils.snake_case, "spinalcase": utils.spinal_case}
    env = Environment(loader=PackageLoader("openapi_python_client"), trim_blocks=True, lstrip_blocks=True)
    env.filters.update(TEMPLATE_FILTERS)
    return env
//  [python]
def test_create_catalog(self):
    pardir = self.get_test_dir(erase=1)
    cat = catalog.get_catalog(pardir,'c')
    assert_(cat is not None)
    cat.close()
    remove_tree(pardir)
//  [python]
def test_create_catalog(self):
    pardir = self.get_test_dir(erase=1)
    cat = catalog.get_catalog(pardir,'c')
    assert_(cat is not None)
    cat.close()
    self.remove_dir(pardir)
//  [python]
def test_nonexistent_catalog_is_none(self):
    pardir = self.get_test_dir(erase=1)
    cat = catalog.get_catalog(pardir,'r')
    remove_tree(pardir)
    assert_(cat is None)
//  [python]
def test_nonexistent_catalog_is_none(self):
    pardir = self.get_test_dir(erase=1)
    cat = catalog.get_catalog(pardir,'r')
    self.remove_dir(pardir)
    assert_(cat is None)
//  [python]
def extract_messages(obj_list):
    """
    Extract "messages" from a list of exceptions or other objects.

    For ValidationErrors, `messages` are flattened into the output.
    For Exceptions, `args[0]` is added into the output.
    For other objects, `force_text` is called.

    :param obj_list: List of exceptions etc.
    :type obj_list: Iterable[object]
    :rtype: Iterable[str]
    """
    for obj in obj_list:
        if isinstance(obj, ValidationError):
            for msg in obj.messages:
                yield escape(force_text(msg))
            continue
        if isinstance(obj, Exception):
            if len(obj.args):
                yield escape(force_text(obj.args[0]))
                continue
        yield escape(force_text(obj))
//  [python]
def extract_messages(obj_list):
    """
    Extract "messages" from a list of exceptions or other objects.

    For ValidationErrors, `messages` are flattened into the output.
    For Exceptions, `args[0]` is added into the output.
    For other objects, `force_text` is called.

    :param obj_list: List of exceptions etc.
    :type obj_list: Iterable[object]
    :rtype: Iterable[str]
    """
    for obj in obj_list:
        if isinstance(obj, ValidationError):
            for msg in obj.messages:
                yield force_text(msg)
            continue
        if isinstance(obj, Exception):
            if len(obj.args):
                yield force_text(obj.args[0])
                continue
        yield force_text(obj)
//  [python]
def job_browse(job_id: int, path):
    """
    Browse directory of the job.
    :param job_id: int
    :param path: str
    """

    try:
        # Query job information
        job_info = query_internal_api(f"/internal/jobs/{job_id}", "get")

        # Base directory of the job
        job_base_dir = os.path.dirname(os.path.dirname(job_info["outputdir"]))

    except Exception as err:
        # Display error on the GUI
        flash(str(err), "danger")
        return redirect(url_for("job_page", job_id=job_id))

    # Join the base and the requested path
    abs_path = safe_join(job_base_dir, path)

    # URL path variable for going back
    back_path = os.path.dirname(abs_path).replace(job_base_dir, "")

    # If path doesn't exist
    if not os.path.exists(abs_path):
        flash("Directory for this job does not exist.", "warning")
        return redirect(url_for("job_page", job_id=job_id))

    # Check if path is a file and send
    if os.path.isfile(abs_path):
        return send_file(abs_path)

    files_info = []

    # Show directory contents
    files = os.listdir(abs_path)

    # Store directory information
    for file in files:
        files_info.append({
            "file": file,
            "directory": os.path.isdir(os.path.join(abs_path, file))
        })

    return render_template('job_dir.html', title=f"Job {job_id} Directory",
                           job_id=job_id,
                           abs_path=abs_path,
                           files_info=files_info,
                           back_path=back_path)
//  [python]
def job_browse(job_id: int, path):
    """
    Browse directory of the job.
    :param job_id: int
    :param path: str
    """

    try:
        # Query job information
        job_info = query_internal_api(f"/internal/jobs/{job_id}", "get")

        # Base directory of the job
        job_base_dir = os.path.dirname(os.path.dirname(job_info["outputdir"]))

    except Exception as err:
        # Display error on the GUI
        flash(str(err), "danger")
        return redirect(url_for("job_page", job_id=job_id))

    # Join the base and the requested path
    abs_path = os.path.join(job_base_dir, path)

    # URL path variable for going back
    back_path = os.path.dirname(abs_path).replace(job_base_dir, "")

    # If path doesn't exist
    if not os.path.exists(abs_path):
        flash("Directory for this job does not exist.", "warning")
        return redirect(url_for("job_page", job_id=job_id))

    # Check if path is a file and send
    if os.path.isfile(abs_path):
        return send_file(abs_path)

    files_info = []

    # Show directory contents
    files = os.listdir(abs_path)

    # Store directory information
    for file in files:
        files_info.append({
            "file": file,
            "directory": os.path.isdir(os.path.join(abs_path, file))
        })

    return render_template('job_dir.html', title=f"Job {job_id} Directory",
                           job_id=job_id,
                           abs_path=abs_path,
                           files_info=files_info,
                           back_path=back_path)
//  [python]
def __init__(self, expression, delimiter, **extra):
    delimiter_expr = Value(str(delimiter))
    super().__init__(expression, delimiter_expr, **extra)
//  [python]
def __init__(self, expression, delimiter, **extra):
    super().__init__(expression, delimiter=delimiter, **extra)
//  [python]
def testValuesInVariable(self):
  indices = constant_op.constant([[0]], dtype=dtypes.int64)
  values = variables.Variable([1], trainable=False, dtype=dtypes.float32)
  shape = constant_op.constant([1], dtype=dtypes.int64)

  sp_input = sparse_tensor.SparseTensor(indices, values, shape)
  sp_output = sparse_ops.sparse_add(sp_input, sp_input)

  with test_util.force_cpu():
    self.evaluate(variables.global_variables_initializer())
    output = self.evaluate(sp_output)
    self.assertAllEqual(output.values, [2])
//  [python]
def testValuesInVariable(self):
  indices = constant_op.constant([[1]], dtype=dtypes.int64)
  values = variables.Variable([1], trainable=False, dtype=dtypes.float32)
  shape = constant_op.constant([1], dtype=dtypes.int64)

  sp_input = sparse_tensor.SparseTensor(indices, values, shape)
  sp_output = sparse_ops.sparse_add(sp_input, sp_input)

  with test_util.force_cpu():
    self.evaluate(variables.global_variables_initializer())
    output = self.evaluate(sp_output)
    self.assertAllEqual(output.values, [2])
//  [python]
def test_filename(self):
    with NamedTemporaryFile() as tmp:
        fp = memmap(tmp.name, dtype=self.dtype, mode='w+',
                    shape=self.shape)
        abspath = os.path.abspath(tmp.name)
        fp[:] = self.data[:]
        self.assertEqual(abspath, fp.filename)
        b = fp[:1]
        self.assertEqual(abspath, b.filename)
        del b
        del fp
//  [python]
def test_filename(self):
    tmpname = mktemp('', 'mmap')
    fp = memmap(tmpname, dtype=self.dtype, mode='w+',
                   shape=self.shape)
    abspath = os.path.abspath(tmpname)
    fp[:] = self.data[:]
    self.assertEqual(abspath, fp.filename)
    b = fp[:1]
    self.assertEqual(abspath, b.filename)
    del b
    del fp
    os.unlink(tmpname)
//  [python]
def should_run(self):
    if self.force:
        return True
    if not os.path.exists(self.bower_dir):
        return True
    if not os.path.exists(self.sanitizer_dir):
        return True

    bower_stale = mtime(self.bower_dir) < mtime(pjoin(repo_root, 'bower.json'))
    if bower_stale:
        return True

    return mtime(self.sanitizer_dir) < mtime(pjoin(repo_root, 'webpack.config.js'))
//  [python]
def should_run(self):
    if self.force:
        return True
    if not os.path.exists(self.bower_dir):
        return True
    
    return mtime(self.bower_dir) < mtime(pjoin(repo_root, 'bower.json'))
//  [python]
def render_POST(self, request):
    """
    Register with the Identity Server
    """
    send_cors(request)

    args = get_args(request, ('matrix_server_name', 'access_token'))

    matrix_server = args['matrix_server_name'].lower()

    if not is_valid_matrix_server_name(matrix_server):
        request.setResponseCode(400)
        return {
            'errcode': 'M_INVALID_PARAM',
            'error': 'matrix_server_name must be a valid Matrix server name (IP address or hostname)'
        }

    result = yield self.client.get_json(
        "matrix://%s/_matrix/federation/v1/openid/userinfo?access_token=%s"
        % (
            matrix_server,
            urllib.parse.quote(args['access_token']),
        ),
        1024 * 5,
    )

    if 'sub' not in result:
        raise Exception("Invalid response from homeserver")

    user_id = result['sub']

    if not isinstance(user_id, str):
        request.setResponseCode(500)
        return {
            'errcode': 'M_UNKNOWN',
            'error': 'The Matrix homeserver returned a malformed reply'
        }

    user_id_components = user_id.split(':', 1)

    # Ensure there's a localpart and domain in the returned user ID.
    if len(user_id_components) != 2:
        request.setResponseCode(500)
        return {
            'errcode': 'M_UNKNOWN',
            'error': 'The Matrix homeserver returned an invalid MXID'
        }

    user_id_server = user_id_components[1]

    if not is_valid_matrix_server_name(user_id_server):
        request.setResponseCode(500)
        return {
            'errcode': 'M_UNKNOWN',
            'error': 'The Matrix homeserver returned an invalid MXID'
        }

    if user_id_server != matrix_server:
        request.setResponseCode(500)
        return {
            'errcode': 'M_UNKNOWN',
            'error': 'The Matrix homeserver returned a MXID belonging to another homeserver'
        }

    tok = yield issueToken(self.sydent, user_id)

    # XXX: `token` is correct for the spec, but we released with `access_token`
    # for a substantial amount of time. Serve both to make spec-compliant clients
    # happy.
    defer.returnValue({
        "access_token": tok,
        "token": tok,
    })
//  [python]
def render_POST(self, request):
    """
    Register with the Identity Server
    """
    send_cors(request)

    args = get_args(request, ('matrix_server_name', 'access_token'))

    matrix_server = args['matrix_server_name'].lower()

    if not is_valid_hostname(matrix_server):
        request.setResponseCode(400)
        return {
            'errcode': 'M_INVALID_PARAM',
            'error': 'matrix_server_name must be a valid hostname'
        }

    result = yield self.client.get_json(
        "matrix://%s/_matrix/federation/v1/openid/userinfo?access_token=%s"
        % (
            matrix_server,
            urllib.parse.quote(args['access_token']),
        ),
        1024 * 5,
    )

    if 'sub' not in result:
        raise Exception("Invalid response from homeserver")

    user_id = result['sub']

    if not isinstance(user_id, str):
        request.setResponseCode(500)
        return {
            'errcode': 'M_UNKNOWN',
            'error': 'The Matrix homeserver returned a malformed reply'
        }

    user_id_components = user_id.split(':', 1)

    # Ensure there's a localpart and domain in the returned user ID.
    if len(user_id_components) != 2:
        request.setResponseCode(500)
        return {
            'errcode': 'M_UNKNOWN',
            'error': 'The Matrix homeserver returned an invalid MXID'
        }

    user_id_server = user_id_components[1]

    if not is_valid_hostname(user_id_server):
        request.setResponseCode(500)
        return {
            'errcode': 'M_UNKNOWN',
            'error': 'The Matrix homeserver returned an invalid MXID'
        }

    if user_id_server != matrix_server:
        request.setResponseCode(500)
        return {
            'errcode': 'M_UNKNOWN',
            'error': 'The Matrix homeserver returned a MXID belonging to another homeserver'
        }

    tok = yield issueToken(self.sydent, user_id)

    # XXX: `token` is correct for the spec, but we released with `access_token`
    # for a substantial amount of time. Serve both to make spec-compliant clients
    # happy.
    defer.returnValue({
        "access_token": tok,
        "token": tok,
    })
//  [python]
def test_can_read_token_from_query_parameters(self):
    """Tests that Sydent correctly extracts an auth token from query parameters"""
    self.sydent.run()

    request, _ = make_request(
        self.sydent.reactor, "GET",
        "/_matrix/identity/v2/hash_details?access_token=" + self.test_token
    )

    token = tokenFromRequest(request)

    self.assertEqual(token, self.test_token)
//  [python]
def test_can_read_token_from_query_parameters(self):
    """Tests that Sydent correct extracts an auth token from query parameters"""
    self.sydent.run()

    request, _ = make_request(
        self.sydent.reactor, "GET",
        "/_matrix/identity/v2/hash_details?access_token=" + self.test_token
    )

    token = tokenFromRequest(request)

    self.assertEqual(token, self.test_token)