<commit_before># Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.

from __future__ import absolute_import

import os
import json
import logging

from os.path import join as pjoin

import six

from st2client.commands import resource
from st2client.commands.noop import NoopCommand
from st2client.formatters import table
from st2client.models.keyvalue import KeyValuePair
from st2client.utils.date import format_isodate_for_user_timezone

LOG = logging.getLogger(__name__)

DEFAULT_SCOPE = 'system'


class KeyValuePairBranch(resource.ResourceBranch):

    def __init__(self, description, app, subparsers, parent_parser=None):
        super(KeyValuePairBranch, self).__init__(
            KeyValuePair, description, app, subparsers,
            parent_parser=parent_parser,
            commands={
                'list': KeyValuePairListCommand,
                'get': KeyValuePairGetCommand,
                'delete': KeyValuePairDeleteCommand,
                'create': NoopCommand,
                'update': NoopCommand
            })

        # Registers extended commands
        self.commands['set'] = KeyValuePairSetCommand(self.resource, self.app,
                                                      self.subparsers)
        self.commands['load'] = KeyValuePairLoadCommand(
            self.resource, self.app, self.subparsers)
        self.commands['delete_by_prefix'] = KeyValuePairDeleteByPrefixCommand(
            self.resource, self.app, self.subparsers)

        # Remove unsupported commands
        # TODO: Refactor parent class and make it nicer
        del self.commands['create']
        del self.commands['update']


class KeyValuePairListCommand(resource.ResourceTableCommand):
    display_attributes = ['name', 'value', 'secret', 'encrypted', 'scope', 'user',
                          'expire_timestamp']
    attribute_transform_functions = {
        'expire_timestamp': format_isodate_for_user_timezone,
    }

    def __init__(self, resource, *args, **kwargs):

        self.default_limit = 50

        super(KeyValuePairListCommand, self).__init__(resource, 'list',
                                                      'Get the list of the %s most recent %s.' %
                                                      (self.default_limit,
                                                       resource.get_plural_display_name().lower()),
                                                      *args, **kwargs)
        self.resource_name = resource.get_plural_display_name().lower()
        # Filter options
        self.parser.add_argument('--prefix', help=('Only return values with names starting with '
                                                   'the provided prefix.'))
        self.parser.add_argument('--decrypt', action='store_true',
                                 help='Decrypt secrets and displays plain text.')
        self.parser.add_argument('-s', '--scope', default='system', dest='scope',
                                 help='Scope item is under. Example: "user".')
        self.parser.add_argument('-u', '--user', dest='user', default=None,
                                 help='User for user scoped items (admin only).')
        self.parser.add_argument('-n', '--last', type=int, dest='last',<commit_msg>Default to "all" scope when using "st2 key list" CLI command.<commit_after># Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.

from __future__ import absolute_import

import os
import json
import logging

from os.path import join as pjoin

import six

from st2client.commands import resource
from st2client.commands.noop import NoopCommand
from st2client.formatters import table
from st2client.models.keyvalue import KeyValuePair
from st2client.utils.date import format_isodate_for_user_timezone

LOG = logging.getLogger(__name__)

DEFAULT_SCOPE = 'all'


class KeyValuePairBranch(resource.ResourceBranch):

    def __init__(self, description, app, subparsers, parent_parser=None):
        super(KeyValuePairBranch, self).__init__(
            KeyValuePair, description, app, subparsers,
            parent_parser=parent_parser,
            commands={
                'list': KeyValuePairListCommand,
                'get': KeyValuePairGetCommand,
                'delete': KeyValuePairDeleteCommand,
                'create': NoopCommand,
                'update': NoopCommand
            })

        # Registers extended commands
        self.commands['set'] = KeyValuePairSetCommand(self.resource, self.app,
                                                      self.subparsers)
        self.commands['load'] = KeyValuePairLoadCommand(
            self.resource, self.app, self.subparsers)
        self.commands['delete_by_prefix'] = KeyValuePairDeleteByPrefixCommand(
            self.resource, self.app, self.subparsers)

        # Remove unsupported commands
        # TODO: Refactor parent class and make it nicer
        del self.commands['create']
        del self.commands['update']


class KeyValuePairListCommand(resource.ResourceTableCommand):
    display_attributes = ['name', 'value', 'secret', 'encrypted', 'scope', 'user',
                          'expire_timestamp']
    attribute_transform_functions = {
        'expire_timestamp': format_isodate_for_user_timezone,
    }

    def __init__(self, resource, *args, **kwargs):

        self.default_limit = 50

        super(KeyValuePairListCommand, self).__init__(resource, 'list',
                                                      'Get the list of the %s most recent %s.' %
                                                      (self.default_limit,
                                                       resource.get_plural_display_name().lower()),
                                                      *args, **kwargs)
        self.resource_name = resource.get_plural_display_name().lower()
        # Filter options
        self.parser.add_argument('--prefix', help=('Only return values with names starting with '
                                                   'the provided prefix.'))
        self.parser.add_argument('--decrypt', action='store_true',
                                 help='Decrypt secrets and displays plain text.')
        self.parser.add_argument('-s', '--scope', default=DEFAULT_SCOPE, dest='scope',
                                 help='Scope item is under. Example: "user".')
        self.parser.add_argument('-u', '--user', dest='user', default=None,
                                 help='User for user scoped items (admin only).')
        self.parser.add_argument('-n', '--last', type=int, dest='last',
{'fraction_non_alphanumeric': 0.08863126402393418, 'fraction_numerical': 0.0028671154325604587, 'mean_word_length': 2.331810631229236, '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': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7307703', 'n_tokens_mistral': 1967, 'n_tokens_neox': 1934, 'n_words': 601}