<commit_before>#   Copyright 2012-2013 OpenStack, LLC.
#
#   Licensed under the Apache License, Version 2.0 (the "License"); you may
#   not use this file except in compliance with the License. You may obtain
#   a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#   License for the specific language governing permissions and limitations
#   under the License.

"""Datasource action implemenations"""

from cliff import command
from cliff import lister
from cliff import show
from oslo_log import log as logging
from oslo_serialization import jsonutils
import six

from congressclient.common import parseractions
from congressclient.common import utils


class ListDatasources(lister.Lister):
    """List Datasources."""

    log = logging.getLogger(__name__ + '.ListDatasources')

    def get_parser(self, prog_name):
        parser = super(ListDatasources, self).get_parser(prog_name)
        return parser

    def take_action(self, parsed_args):
        client = self.app.client_manager.congressclient
        data = client.list_datasources()['results']
        # Type is always None, so disabling it for now.
        columns = ['id', 'name', 'enabled', 'driver', 'config']
        formatters = {'config': utils.format_dict}
        return (columns,
                (utils.get_dict_properties(s, columns,
                                           formatters=formatters)
                 for s in data))


class ListDatasourceTables(lister.Lister):
    """List datasource tables."""

    log = logging.getLogger(__name__ + '.ListDatasourceTables')

    def get_parser(self, prog_name):
        parser = super(ListDatasourceTables, self).get_parser(prog_name)
        parser.add_argument(
            'datasource_name',
            metavar="<datasource>",
            help="Name or ID of the datasource")
        return parser

    def take_action(self, parsed_args):
        self.log.debug('take_action(%s)' % parsed_args)
        client = self.app.client_manager.congressclient
        name_or_id = parsed_args.datasource_name
        data = client.list_datasource_tables(name_or_id)['results']
        columns = ['id']
        formatters = {'DatasourceTables': utils.format_list}
        return (columns,
                (utils.get_dict_properties(s, columns,
                                           formatters=formatters)
                 for s in data))


class ShowDatasourceStatus(show.ShowOne):
    """List status for datasource."""

    log = logging.getLogger(__name__ + '.ShowDatasourceStatus')

    def get_parser(self, prog_name):
        parser = super(ShowDatasourceStatus, self).get_parser(prog_name)
        parser.add_argument(
            'datasource_name',
            metavar="<datasource>",
            help="Name or ID of the datasource")
        return parser

    def take_action(self, parsed_args):
        self.log.debug('take_action(%s)' % parsed_args)
        client = self.app.client_manager.congressclient
        datasource_id = parsed_args.datasource_name
        data = client.list_datasource_status(datasource_id)
        return zip(*sorted(six.iteritems(data)))


class ShowDatasourceActions(lister.Lister):
    """List supported actions for datasource."""

    log = logging.getLogger(__name__ + '.ShowDatasourceActions')

    def get_parser(self, prog_name):
        parser = super(ShowDatasourceActions, self).get_parser(prog_name)
        parser.add_argument(
            'datasource_name',
            metavar="<datasource>",
            help="Name or ID of the datasource")
        return parser

    def take_action(self, parsed_args):
        self.log.debug('take_action(%s)' % parsed_args)
        # as we know output it's long, limit column length here
        if parsed_args.max_width == 0:
            parsed_args.max_width = 40

        client = self.app.client_manager.congressclient
        datasource_id = parsed_args.datasource_name
        data = client.list_datasource_actions(datasource_id)
        formatters = {'args': utils.format_long_dict_list}
        newdata = [{'action': x['name'],
                    'args': x['args'],
                    'description': x['description']}
                   for x in data['results']]
        columns = ['action', 'args', 'description']
        return (columns, (utils.get_dict_properties(s,
                                                    columns,
                                                    formatters=formatters)
                for s in newdata))


class ShowDatasourceSchema(lister.Lister):
    """Show schema for datasource."""

    log = logging.getLogger(__name__ + '.ShowDatasourceSchema')

    def get_parser(self, prog_name):
        parser = super(ShowDatasourceSchema, self).get_parser(prog_name)
        parser.add_argument(
            'datasource_name',
            metavar="<datasource>",<commit_msg>Fix the problem of default width is small<commit_after>#   Copyright 2012-2013 OpenStack, LLC.
#
#   Licensed under the Apache License, Version 2.0 (the "License"); you may
#   not use this file except in compliance with the License. You may obtain
#   a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#   License for the specific language governing permissions and limitations
#   under the License.

"""Datasource action implemenations"""

from cliff import command
from cliff import lister
from cliff import show
from oslo_log import log as logging
from oslo_serialization import jsonutils
import six

from congressclient.common import parseractions
from congressclient.common import utils


class ListDatasources(lister.Lister):
    """List Datasources."""

    log = logging.getLogger(__name__ + '.ListDatasources')

    def get_parser(self, prog_name):
        parser = super(ListDatasources, self).get_parser(prog_name)
        return parser

    def take_action(self, parsed_args):
        client = self.app.client_manager.congressclient
        data = client.list_datasources()['results']
        # Type is always None, so disabling it for now.
        columns = ['id', 'name', 'enabled', 'driver', 'config']
        formatters = {'config': utils.format_dict}
        return (columns,
                (utils.get_dict_properties(s, columns,
                                           formatters=formatters)
                 for s in data))


class ListDatasourceTables(lister.Lister):
    """List datasource tables."""

    log = logging.getLogger(__name__ + '.ListDatasourceTables')

    def get_parser(self, prog_name):
        parser = super(ListDatasourceTables, self).get_parser(prog_name)
        parser.add_argument(
            'datasource_name',
            metavar="<datasource>",
            help="Name or ID of the datasource")
        return parser

    def take_action(self, parsed_args):
        self.log.debug('take_action(%s)' % parsed_args)
        client = self.app.client_manager.congressclient
        name_or_id = parsed_args.datasource_name
        data = client.list_datasource_tables(name_or_id)['results']
        columns = ['id']
        formatters = {'DatasourceTables': utils.format_list}
        return (columns,
                (utils.get_dict_properties(s, columns,
                                           formatters=formatters)
                 for s in data))


class ShowDatasourceStatus(show.ShowOne):
    """List status for datasource."""

    log = logging.getLogger(__name__ + '.ShowDatasourceStatus')

    def get_parser(self, prog_name):
        parser = super(ShowDatasourceStatus, self).get_parser(prog_name)
        parser.add_argument(
            'datasource_name',
            metavar="<datasource>",
            help="Name or ID of the datasource")
        return parser

    def take_action(self, parsed_args):
        self.log.debug('take_action(%s)' % parsed_args)
        client = self.app.client_manager.congressclient
        datasource_id = parsed_args.datasource_name
        data = client.list_datasource_status(datasource_id)
        return zip(*sorted(six.iteritems(data)))


class ShowDatasourceActions(lister.Lister):
    """List supported actions for datasource."""

    log = logging.getLogger(__name__ + '.ShowDatasourceActions')

    def get_parser(self, prog_name):
        parser = super(ShowDatasourceActions, self).get_parser(prog_name)
        parser.add_argument(
            'datasource_name',
            metavar="<datasource>",
            help="Name or ID of the datasource")
        return parser

    def take_action(self, parsed_args):
        self.log.debug('take_action(%s)' % parsed_args)
        # as we know output it's long, limit column length here
        if parsed_args.max_width == 0:
            parsed_args.max_width = 80

        client = self.app.client_manager.congressclient
        datasource_id = parsed_args.datasource_name
        data = client.list_datasource_actions(datasource_id)
        formatters = {'args': utils.format_long_dict_list}
        newdata = [{'action': x['name'],
                    'args': x['args'],
                    'description': x['description']}
                   for x in data['results']]
        columns = ['action', 'args', 'description']
        return (columns, (utils.get_dict_properties(s,
                                                    columns,
                                                    formatters=formatters)
                for s in newdata))


class ShowDatasourceSchema(lister.Lister):
    """Show schema for datasource."""

    log = logging.getLogger(__name__ + '.ShowDatasourceSchema')

    def get_parser(self, prog_name):
        parser = super(ShowDatasourceSchema, self).get_parser(prog_name)
        parser.add_argument(
            'datasource_name',
            metavar="<datasource>",
{'fraction_non_alphanumeric': 0.09555512042294889, 'fraction_numerical': 0.002937145094967691, 'mean_word_length': 2.5942997888810697, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2629796', 'n_tokens_mistral': 2882, 'n_tokens_neox': 2743, 'n_words': 714}