<commit_before>"""Wrapper class for registry-related shell commands."""
import time
import argparse
from ice import ascii_table
from . import ShellExt


class RegistryShell(ShellExt):
    """Wrapper class for registry-related shell commands."""

    def __init__(self, registry, logger, debug=False):
        """
        :param ice.registry.client.RegistryClient registry:
        :param logging.Logger logger:
        :param bool debug: Set to True for debug behaviour.
        """
        self.registry = registry
        super(RegistryShell, self).__init__(logger, debug)

    def start(self, shell):
        """Starts the shell extension.

        It initializes the extension object and calls the
        shell.add_command to setup the shell hooks.

        :param ice.shell.Shell shell:
        """
        super(RegistryShell, self).start(shell)
        shell.add_command('inst_ls', self.ls_inst)
        shell.add_command(
            'inst_wait', self.run_wait,
            parser=self.get_wait_parser()
        )
        shell.add_command(
            'inst_del', self.del_inst,
            usage='<Instance id> [<Instance id> ...]'
        )
        shell.add_command(
            'inst_show', self.show_inst,
            usage='<Instance id> [<Instance id> ...]'
        )

    def ls_inst(self):
        """Lists instances."""
        inst_list = self.registry.get_instances_list(
            self.shell.get_session()
        )
        if inst_list is None:
            self.logger.error('Failed to find instances!')
            return

        self.logger.info('Found %d instances' % len(inst_list))
        table = ascii_table.ASCIITable()
        table.add_column('id', ascii_table.ASCIITableColumn('Id', 27))
        table.add_column('public_ip_addr',
                         ascii_table.ASCIITableColumn('Public IP', 23))
        table.add_column('cloud_id',
                         ascii_table.ASCIITableColumn('Cloud Id', 30))

        for inst in inst_list:
            table.add_row({
                'id': inst.id,
                'public_ip_addr': inst.public_ip_addr,
                'cloud_id': inst.cloud_id
            })

        print ascii_table.ASCIITableRenderer().render(table)

    def get_wait_parser(self):
        parser = argparse.ArgumentParser(prog='inst_wait', add_help=False)
        parser.add_argument(
            '-n', metavar='<Amount of instances>', dest='amt', type=int,
            default=1
        )
        parser.add_argument(
            '-t', metavar='<Timeout (sec.)>', dest='timeout', default=120
        )
        return parser

    def run_wait(self, args):
        """Waits for instances to appear."""
        res = self._wait(args.amt, args.timeout)
        if res:
            self.logger.info('Instances are ready!')
        else:
            self.logger.error('Timeout!')

    def _wait(self, amt, timeout=120):
        """Wait for `amt` number of instances to appear in the pool.

        :param int amt: The expected number of instances.
        :param int timeout: The timeout of the command, in seconds.<commit_msg>Print private in `inst_ls` since cloud_id is empty<commit_after>"""Wrapper class for registry-related shell commands."""
import time
import argparse
from ice import ascii_table
from . import ShellExt


class RegistryShell(ShellExt):
    """Wrapper class for registry-related shell commands."""

    def __init__(self, registry, logger, debug=False):
        """
        :param ice.registry.client.RegistryClient registry:
        :param logging.Logger logger:
        :param bool debug: Set to True for debug behaviour.
        """
        self.registry = registry
        super(RegistryShell, self).__init__(logger, debug)

    def start(self, shell):
        """Starts the shell extension.

        It initializes the extension object and calls the
        shell.add_command to setup the shell hooks.

        :param ice.shell.Shell shell:
        """
        super(RegistryShell, self).start(shell)
        shell.add_command('inst_ls', self.ls_inst)
        shell.add_command(
            'inst_wait', self.run_wait,
            parser=self.get_wait_parser()
        )
        shell.add_command(
            'inst_del', self.del_inst,
            usage='<Instance id> [<Instance id> ...]'
        )
        shell.add_command(
            'inst_show', self.show_inst,
            usage='<Instance id> [<Instance id> ...]'
        )

    def ls_inst(self):
        """Lists instances."""
        inst_list = self.registry.get_instances_list(
            self.shell.get_session()
        )
        if inst_list is None:
            self.logger.error('Failed to find instances!')
            return

        self.logger.info('Found %d instances' % len(inst_list))
        table = ascii_table.ASCIITable()
        table.add_column('id', ascii_table.ASCIITableColumn('Id', 28))
        table.add_column('public_ip_addr',
                         ascii_table.ASCIITableColumn('Public IP', 26))
        table.add_column('private_ip_addr',
                         ascii_table.ASCIITableColumn('Private IP', 26))

        for inst in inst_list:
            row = {
                'id': inst.id,
                'public_ip_addr': inst.public_ip_addr,
                'private_ip_addr': ''
            }
            for net in inst.networks:
                if net['iface'] == 'eth0':
                    row['private_ip_addr'] = net['addr']
            table.add_row(row)

        print ascii_table.ASCIITableRenderer().render(table)

    def get_wait_parser(self):
        parser = argparse.ArgumentParser(prog='inst_wait', add_help=False)
        parser.add_argument(
            '-n', metavar='<Amount of instances>', dest='amt', type=int,
            default=1
        )
        parser.add_argument(
            '-t', metavar='<Timeout (sec.)>', dest='timeout', default=120
        )
        return parser

    def run_wait(self, args):
        """Waits for instances to appear."""
        res = self._wait(args.amt, args.timeout)
        if res:
            self.logger.info('Instances are ready!')
        else:
            self.logger.error('Timeout!')

    def _wait(self, amt, timeout=120):
        """Wait for `amt` number of instances to appear in the pool.

        :param int amt: The expected number of instances.
        :param int timeout: The timeout of the command, in seconds.
{'fraction_non_alphanumeric': 0.11260978670012547, 'fraction_numerical': 0.004234629861982434, 'mean_word_length': 2.098639455782313, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '5492391', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1767, 'n_words': 443}