<commit_before>from __future__ import unicode_literals, division, absolute_import
from builtins import *  # pylint: disable=unused-import, redefined-builtin

import logging
from math import ceil

from flask import jsonify
from sqlalchemy import desc, asc

from flexget.api import api, APIResource
from flexget.api.app import BadRequest, etag, link_header, pagination_parser
from flexget.plugins.output.history import History

log = logging.getLogger('history')

history_api = api.namespace('history', description='Entry History')


class ObjectsContainer(object):
    base_history_object = {
        'type': 'object',
        'properties': {
            'details': {'type': 'string'},
            'filename': {'type': 'string'},
            'id': {'type': 'integer'},
            'task': {'type': 'string'},
            'time': {'type': 'string', 'format': 'date-time'},
            'title': {'type': 'string'},
            'url': {'type': 'string'}
        }
    }

    history_list_object = {'type': 'array', 'items': base_history_object}


history_list_schema = api.schema('history.list', ObjectsContainer.history_list_object)

sort_choices = ('id', 'task', 'filename', 'url', 'title', 'time', 'details')

# Create pagination parser
history_parser = pagination_parser(sort_choices=sort_choices, default='time')
history_parser.add_argument('task', help='Filter by task name')


@history_api.route('/')
@api.doc(parser=history_parser)
class HistoryAPI(APIResource):
    @etag
    @api.response(BadRequest)
    @api.response(200, model=history_list_schema)
    def get(self, session=None):
        """ List of previously accepted entries """
        args = history_parser.parse_args()

        # Pagination and sorting params
        page = args['page']
        per_page = args['per_page']
        sort_by = args['sort_by']
        sort_order = args['order']

        # Filter param
        task = args['task']

        # Build query count
        count_query = session.query(History)
        if task:
            count_query = count_query.filter(History.task == task)
        count = count_query.count()

        if not count:
            return jsonify([])

        pages = int(ceil(count / float(per_page)))

        if page > pages:
            raise BadRequest('page %s does not exist' % page)

        start = (page - 1) * per_page
        finish = start + per_page

        # Choose sorting order
        if sort_order == 'desc':
            order = desc
        else:
            order = asc

        # Build item query
        items = session.query(History)
        if task:
            items = items.filter(History.task == task)
        items = items.order_by(order(getattr(History, sort_by))).slice(start, finish)

        # Create Link header
        full_url = self.api.base_url + history_api.path
        link = link_header(full_url, page, per_page, pages)

        # Create response
        rsp = jsonify([item.to_dict() for item in items])

        # Add link header to response
        rsp.headers.extend(link)
        return rsp<commit_msg>style tweak<commit_after>from __future__ import unicode_literals, division, absolute_import
from builtins import *  # pylint: disable=unused-import, redefined-builtin

import logging
from math import ceil

from flask import jsonify
from sqlalchemy import desc, asc

from flexget.api import api, APIResource
from flexget.api.app import BadRequest, etag, link_header, pagination_parser
from flexget.plugins.output.history import History

log = logging.getLogger('history')

history_api = api.namespace('history', description='Entry History')


class ObjectsContainer(object):
    base_history_object = {
        'type': 'object',
        'properties': {
            'details': {'type': 'string'},
            'filename': {'type': 'string'},
            'id': {'type': 'integer'},
            'task': {'type': 'string'},
            'time': {'type': 'string', 'format': 'date-time'},
            'title': {'type': 'string'},
            'url': {'type': 'string'}
        }
    }

    history_list_object = {'type': 'array', 'items': base_history_object}


history_list_schema = api.schema('history.list', ObjectsContainer.history_list_object)

sort_choices = ('id', 'task', 'filename', 'url', 'title', 'time', 'details')

# Create pagination parser
history_parser = pagination_parser(sort_choices=sort_choices, default='time')
history_parser.add_argument('task', help='Filter by task name')


@history_api.route('/')
@api.doc(parser=history_parser)
class HistoryAPI(APIResource):
    @etag
    @api.response(BadRequest)
    @api.response(200, model=history_list_schema)
    def get(self, session=None):
        """ List of previously accepted entries """
        args = history_parser.parse_args()

        # Pagination and sorting params
        page = args['page']
        per_page = args['per_page']
        sort_by = args['sort_by']
        sort_order = args['order']

        # Filter param
        task = args['task']

        # Build query count
        count_query = session.query(History)
        if task:
            count_query = count_query.filter(History.task == task)
        count = count_query.count()

        if not count:
            return jsonify([])

        pages = int(ceil(count / float(per_page)))

        if page > pages:
            raise BadRequest('page %s does not exist' % page)

        start = (page - 1) * per_page
        finish = start + per_page

        # Choose sorting order
        order = desc if sort_order == 'desc' else asc

        # Build item query
        items = session.query(History)
        if task:
            items = items.filter(History.task == task)
        items = items.order_by(order(getattr(History, sort_by))).slice(start, finish)

        # Create Link header
        full_url = self.api.base_url + history_api.path
        link = link_header(full_url, page, per_page, pages)

        # Create response
        rsp = jsonify([item.to_dict() for item in items])

        # Add link header to response
        rsp.headers.extend(link)
        return rsp
{'fraction_non_alphanumeric': 0.12302371541501976, 'fraction_numerical': 0.0013175230566534915, 'mean_word_length': 2.786159600997506, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4203086', 'n_tokens_mistral': 1770, 'n_tokens_neox': 1714, 'n_words': 455}