<commit_before>########
# Copyright (c) 2014 GigaSpaces Technologies Ltd. All rights reserved
#
# 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 os
import shutil
import tarfile

from contextlib import closing

from cloudify.utils import get_kerberos_indication
from cloudify_rest_client.exceptions import (CloudifyClientError,
                                             UserUnauthorizedError)

from . import init
from .. import env
from .. import utils
from ..cli import cfy
from .. import constants
from ..cli import helptexts
from ..exceptions import CloudifyCliError
from ..table import print_data, print_single


EXPORTED_KEYS_DIRNAME = '.exported-ssh-keys'
EXPORTED_SSH_KEYS_DIR = os.path.join(env.PROFILES_DIR, EXPORTED_KEYS_DIRNAME)
PROFILE_COLUMNS = ['name', 'manager_ip', 'manager_username', 'manager_tenant',
                   'ssh_user', 'ssh_key_path', 'ssh_port', 'kerberos_env',
                   'rest_port', 'rest_protocol', 'rest_certificate']


@cfy.group(name='profiles')
@cfy.options.common_options
def profiles():
    """Handle Cloudify CLI profiles

    Each profile can manage a single Cloudify manager.

    A profile is automatically created when using the `cfy profiles use`
    command.

    Profiles are named according to the IP of the manager they manage.
    """
    if not env.is_initialized():
        init.init_local_profile()


@profiles.command(name='show-current',
                  short_help='Retrieve current profile information')
@cfy.options.common_options
@cfy.pass_logger
def show(logger):
    """Shows your current active profile and it's properties
    """
    active_profile_name = env.get_active_profile()
    if active_profile_name == 'local':
        logger.info("You're currently working in local mode. "
                    "To use a manager run `cfy profiles use MANAGER_IP`")
        return

    active_profile = _get_profile(env.get_active_profile())
    print_single(PROFILE_COLUMNS, active_profile, 'Active profile:')


@profiles.command(name='list',
                  short_help='List profiles')
@cfy.options.common_options
@cfy.pass_logger
def list(logger):
    """List all profiles
    """
    current_profile = env.get_active_profile()

    profiles = []
    profile_names = env.get_profile_names()
    for profile in profile_names:
        profile_data = _get_profile(profile)
        if profile == current_profile:
            # Show the currently active profile by appending *
            profile_data['name'] = '*' + profile_data['name']
        profiles.append(profile_data)

    if profiles:
        logger.info('Listing all profiles...')
        print_data(PROFILE_COLUMNS, profiles, 'Profiles:')

    if not profile_names:
        logger.info(<commit_msg>CY-873 cfy profiles show: show cluster nodes (#887)<commit_after>########
# Copyright (c) 2014 GigaSpaces Technologies Ltd. All rights reserved
#
# 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 os
import shutil
import tarfile

from contextlib import closing

from cloudify.utils import get_kerberos_indication
from cloudify_rest_client.exceptions import (CloudifyClientError,
                                             UserUnauthorizedError)

from . import init
from .. import env
from .. import utils
from ..cli import cfy
from .. import constants
from ..cli import helptexts
from ..exceptions import CloudifyCliError
from ..table import print_data, print_single


EXPORTED_KEYS_DIRNAME = '.exported-ssh-keys'
EXPORTED_SSH_KEYS_DIR = os.path.join(env.PROFILES_DIR, EXPORTED_KEYS_DIRNAME)
PROFILE_COLUMNS = ['name', 'manager_ip', 'manager_username', 'manager_tenant',
                   'ssh_user', 'ssh_key_path', 'ssh_port', 'kerberos_env',
                   'rest_port', 'rest_protocol', 'rest_certificate']
CLUSTER_PROFILE_COLUMNS = ['profile_name', 'cluster_node_name'] \
    + PROFILE_COLUMNS[1:]


@cfy.group(name='profiles')
@cfy.options.common_options
def profiles():
    """Handle Cloudify CLI profiles

    Each profile can manage a single Cloudify manager.

    A profile is automatically created when using the `cfy profiles use`
    command.

    Profiles are named according to the IP of the manager they manage.
    """
    if not env.is_initialized():
        init.init_local_profile()


def _format_cluster_profile(profile):
    """Format the list of cluster nodes for display

    In `cfy cluster show`, we show the profile details of every stored
    cluster node.
    """
    common_attributes = {k: profile.get(k) for k in PROFILE_COLUMNS}
    nodes = []
    for node in profile['cluster']:
        # merge the common attrs with node data, but rename node's name
        # attribute to cluster_node, because the attribute 'name' is
        # reserved for the profile name
        node_data = dict(node)
        node_data['cluster_node_name'] = node_data.pop('name')
        nodes.append(dict(common_attributes, **node_data))
    return nodes


@profiles.command(name='show-current',
                  short_help='Retrieve current profile information')
@cfy.options.common_options
@cfy.pass_logger
def show(logger):
    """Shows your current active profile and it's properties
    """
    active_profile_name = env.get_active_profile()
    if active_profile_name == 'local':
        logger.info("You're currently working in local mode. "
                    "To use a manager run `cfy profiles use MANAGER_IP`")
        return

    active_profile = _get_profile(env.get_active_profile())
    if active_profile.get('cluster'):

        columns = PROFILE_COLUMNS[:1] + ['cluster_node_name'] \
            + PROFILE_COLUMNS[1:]
        print_data(columns, _format_cluster_profile(active_profile),
                   'Cluster nodes in profile {0}:'
                   .format(active_profile['name']),
                   labels={
                   'profile_name': 'name',
                   'cluster_node_name': 'cluster node name'})
    else:
        print_single(PROFILE_COLUMNS, active_profile, 'Active profile:')


@profiles.command(name='list',
                  short_help='List profiles')
@cfy.options.common_options
@cfy.pass_logger
def list(logger):
    """List all profiles
    """
    current_profile = env.get_active_profile()

    profiles = []
    profile_names = env.get_profile_names()
    for profile in profile_names:
        profile_data = _get_profile(profile)
        if profile == current_profile:
            # Show the currently active profile by appending *
            profile_data['name'] = '*' + profile_data['name']
        profiles.append(profile_data)

    if profiles:
        logger.info('Listing all profiles...')
        print_data(PROFILE_COLUMNS, profiles, 'Profiles:')

    if not profile_names:
        logger.info(
{'fraction_non_alphanumeric': 0.09494897300090428, 'fraction_numerical': 0.003358739180984369, 'mean_word_length': 3.253846153846154, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7128820', 'n_tokens_mistral': 2214, 'n_tokens_neox': 2143, 'n_words': 674}