<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright 2010 Asidev s.r.l.

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.
"""

import inspect


class BaseInterface(object):

    def __init__(self, client):
        self.api = client
        if not hasattr(self, 'root_url'):
            self.root_url = '/{}'.format(self.name)

    def get_completions(self, command, parts):
        """ This function get called during autocompletion.
            command is the command name, i.e. tasks_list <TAB> call this function
            on TaskInterface with "list" as command.
            parts is a list of kw to complete against
        """

        # first: if we have a get_${command}_completion call it
        attr = "get_{}_completions"
        try:
            return getattr(self, attr)(command, parts)

        except AttributeError:
            pass

        # use the default complete function
        return self.default_complete(command, parts)

    def default_complete(self, command, parts):
        """ This function is very simple: if command has a single argument
            we assume that it is self, so we do no completion at all.
            If the command has more than one arg, we can't know what to do,
            so we do nothing.
            Else autocomplete on primary keys, i.e. call get_list()
        """

        kw = parts[0]
        try:
            if len(inspect.getargspec(getattr(self, command)).args) == 1:
                return []

        except AttributeError:
            return []

        # string startswith themselves, avoid replication
        res = set(self.get_list(quiet=True))
        if kw in res:
            return []

        return [w for w in res if w.startswith(kw)]

    def get_url(self, *parts):
        url = self.root_url
        for part in parts:
            if not part.startswith("/"):
                part = "/{}".format(part)
            url = "{}{}".format(url, part)
        return url

    def remove(self, resource):
        self.api.delete(self.get_url(resource))

    def get_list(self, quiet=False):
        try:
            response, content = self.api.get(self.root_url, quiet=quiet)

        except ValueError:
            return []

        else:
            return content

    def list(self):
        for res in self.get_list():
            print " * {}".format(res)

    def info(self, resource):
        try:
            response, content = self.api.get(self.get_url(resource))

        except ValueError:
            pass

        else:<commit_msg>Added support for full output in list() command.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright 2010 Asidev s.r.l.

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.
"""

import plac
import inspect


class BaseInterface(object):

    def __init__(self, client):
        self.api = client
        if not hasattr(self, 'root_url'):
            self.root_url = '/{}'.format(self.name)

    def get_completions(self, command, parts):
        """ This function get called during autocompletion.
            command is the command name, i.e. tasks_list <TAB> call this function
            on TaskInterface with "list" as command.
            parts is a list of kw to complete against
        """

        # first: if we have a get_${command}_completion call it
        attr = "get_{}_completions"
        try:
            return getattr(self, attr)(command, parts)

        except AttributeError:
            pass

        # use the default complete function
        return self.default_complete(command, parts)

    def default_complete(self, command, parts):
        """ This function is very simple: if command has a single argument
            we assume that it is self, so we do no completion at all.
            If the command has more than one arg, we can't know what to do,
            so we do nothing.
            Else autocomplete on primary keys, i.e. call get_list()
        """

        kw = parts[0]
        try:
            argsspec = inspect.getargspec(getattr(self, command))
            num_non_kw_params = len(argsspec.args)
            if argsspec.defaults:
                num_non_kw_params = num_non_kw_params - len(argsspec.defaults)
            if num_non_kw_params == 1:
                return []

        except AttributeError:
            return []

        # string startswith themselves, avoid replication
        res = set(self.get_list(quiet=True))
        if kw in res:
            return []

        return [w for w in res if w.startswith(kw)]

    def get_url(self, *parts):
        url = self.root_url
        for part in parts:
            if not part.startswith("/"):
                part = "/{}".format(part)
            url = "{}{}".format(url, part)
        return url

    def remove(self, resource):
        self.api.delete(self.get_url(resource))

    def get_list(self, quiet=False):
        try:
            response, content = self.api.get(self.root_url, quiet=quiet)

        except ValueError:
            return []

        else:
            return content.keys()

    @plac.annotations(
        full=('Get complete output', 'flag', 'f')
    )
    def list(self, full=False):
        try:
            response, content = self.api.get(self.root_url, quiet=True)
            for res, info in content.iteritems():
                print " * {}".format(res)

            if full:
                for attr in sorted(info.keys()):
                    print "   > {:<20}:{}".format(attr, info[attr])

        except ValueError:
            return

    def info(self, resource):
        try:
            response, content = self.api.get(self.get_url(resource))

        except ValueError:
            pass

        else:
{'fraction_non_alphanumeric': 0.0926230734699985, 'fraction_numerical': 0.003591201556187341, 'mean_word_length': 2.0646492434662997, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3075992', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1765, 'n_words': 650}