<commit_before># Copyright 2012 OpenStack, LLC
# 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.

"""
DEPRECATED functions that implement the same command line interface as the
legacy glance client.
"""

import argparse
import sys

from glanceclient.common import utils


SUCCESS = 0
FAILURE = 1


def get_image_fields_from_args(args):
    """
    Validate the set of arguments passed as field name/value pairs
    and return them as a mapping.
    """
    fields = {}
    for arg in args:
        pieces = arg.strip(',').split('=')
        if len(pieces) != 2:
            msg = ("Arguments should be in the form of field=value. "
                   "You specified %s." % arg)
            raise RuntimeError(msg)
        fields[pieces[0]] = pieces[1]

    return fields


def get_image_filters_from_args(args):
    """Build a dictionary of query filters based on the supplied args."""
    try:
        fields = get_image_fields_from_args(args)
    except RuntimeError, e:
        print e
        return FAILURE

    SUPPORTED_FILTERS = ['name', 'disk_format', 'container_format', 'status',
                         'min_ram', 'min_disk', 'size_min', 'size_max',
                         'changes-since']
    filters = {}
    for (key, value) in fields.items():
        if key not in SUPPORTED_FILTERS:
            key = 'property-%s' % (key,)
        filters[key] = value

    return filters


def print_image_formatted(client, image):
    """
    Formatted print of image metadata.

    :param client: The Glance client object
    :param image: The image metadata
    """
    print "URI: http://%s:%s/v1/images/%s" % (
                                      client.endpoint[0],
                                      client.endpoint[1],
                                      image.id)
    print "Id: %s" % image.id
    print "Public: " + (image.is_public and "Yes" or "No")
    print "Protected: " + (image.protected and "Yes" or "No")
    print "Name: %s" % getattr(image, 'name', '')
    print "Status: %s" % image.status
    print "Size: %d" % int(image.size)
    print "Disk format: %s" % getattr(image, 'disk_format', '')
    print "Container format: %s" % getattr(image, 'container_format', '')
    print "Minimum Ram Required (MB): %s" % image.min_ram
    print "Minimum Disk Required (GB): %s" % image.min_disk
    if hasattr(image, 'owner'):
        print "Owner: %s" % image.owner
    if len(image.properties) > 0:
        for k, v in image.properties.items():
            print "Property '%s': %s" % (k, v)
    print "Created at: %s" % image.created_at
    if hasattr(image, 'deleted_at'):
        print "Deleted at: %s" % image.deleted_at
    if hasattr(image, 'updated_at'):
        print "Updated at: %s" % image.updated_at


@utils.arg('--silent-upload', action="store_true",
           help="DEPRECATED! Animations are always off.")
@utils.arg('fields', default=[], nargs='*', help=argparse.SUPPRESS)
def do_add(gc, args):
    """DEPRECATED! Use image-create instead."""
    try:
        fields = get_image_fields_from_args(args.fields)
    except RuntimeError, e:<commit_msg>Corrects URI to display hostname, port properly<commit_after># Copyright 2012 OpenStack, LLC
# 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.

"""
DEPRECATED functions that implement the same command line interface as the
legacy glance client.
"""

import argparse
import sys
import urlparse

from glanceclient.common import utils


SUCCESS = 0
FAILURE = 1


def get_image_fields_from_args(args):
    """
    Validate the set of arguments passed as field name/value pairs
    and return them as a mapping.
    """
    fields = {}
    for arg in args:
        pieces = arg.strip(',').split('=')
        if len(pieces) != 2:
            msg = ("Arguments should be in the form of field=value. "
                   "You specified %s." % arg)
            raise RuntimeError(msg)
        fields[pieces[0]] = pieces[1]

    return fields


def get_image_filters_from_args(args):
    """Build a dictionary of query filters based on the supplied args."""
    try:
        fields = get_image_fields_from_args(args)
    except RuntimeError, e:
        print e
        return FAILURE

    SUPPORTED_FILTERS = ['name', 'disk_format', 'container_format', 'status',
                         'min_ram', 'min_disk', 'size_min', 'size_max',
                         'changes-since']
    filters = {}
    for (key, value) in fields.items():
        if key not in SUPPORTED_FILTERS:
            key = 'property-%s' % (key,)
        filters[key] = value

    return filters


def print_image_formatted(client, image):
    """
    Formatted print of image metadata.

    :param client: The Glance client object
    :param image: The image metadata
    """
    uri_parts = urlparse.urlparse(client.endpoint)
    if uri_parts.port:
        hostbase = "%s:%s" % (uri_parts.hostname, uri_parts.port)
    else:
        hostbase = uri_parts.hostname
    print "URI: %s://%s/v1/images/%s" % (uri_parts.scheme, hostbase, image.id)
    print "Id: %s" % image.id
    print "Public: " + (image.is_public and "Yes" or "No")
    print "Protected: " + (image.protected and "Yes" or "No")
    print "Name: %s" % getattr(image, 'name', '')
    print "Status: %s" % image.status
    print "Size: %d" % int(image.size)
    print "Disk format: %s" % getattr(image, 'disk_format', '')
    print "Container format: %s" % getattr(image, 'container_format', '')
    print "Minimum Ram Required (MB): %s" % image.min_ram
    print "Minimum Disk Required (GB): %s" % image.min_disk
    if hasattr(image, 'owner'):
        print "Owner: %s" % image.owner
    if len(image.properties) > 0:
        for k, v in image.properties.items():
            print "Property '%s': %s" % (k, v)
    print "Created at: %s" % image.created_at
    if hasattr(image, 'deleted_at'):
        print "Deleted at: %s" % image.deleted_at
    if hasattr(image, 'updated_at'):
        print "Updated at: %s" % image.updated_at


@utils.arg('--silent-upload', action="store_true",
           help="DEPRECATED! Animations are always off.")
@utils.arg('fields', default=[], nargs='*', help=argparse.SUPPRESS)
def do_add(gc, args):
    """DEPRECATED! Use image-create instead."""
    try:
        fields = get_image_fields_from_args(args.fields)
    except RuntimeError, e:
{'fraction_non_alphanumeric': 0.11985373781148428, 'fraction_numerical': 0.004333694474539545, 'mean_word_length': 2.71852970795569, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3844087', 'n_tokens_mistral': 2240, 'n_tokens_neox': 2197, 'n_words': 734}