<commit_before>from argh import arg
from six import iteritems

import pnc_cli.common as common
import pnc_cli.cli_types as types
import pnc_cli.utils as utils
from pnc_cli.swagger_client import ProductRest
from pnc_cli.swagger_client import ProductsApi
import pnc_cli.user_config as uc

products_api = ProductsApi(uc.user.get_api_client())

__author__ = 'thauser'


def create_product_object(**kwargs):
    created_product = ProductRest()
    for key, value in iteritems(kwargs):
        setattr(created_product, key, value)
    return created_product


@arg("name", help="Name for the Product", type=types.unique_product_name)
@arg("abbreviation", help="The abbreviation or \"short name\" of the new Product", type=types.unique_product_abbreviation)
@arg("-d", "--description", help="Detailed description of the new Product")
@arg("-p", "--product-code", help="The Product code for the new Product")
@arg("-sn", "--pgm-system-name", help="The system code for the new Product")
@arg("-pvids", "--product-version-ids", type=types.existing_product_version, nargs='+',
     help="Space separated list of associated ProductVersion ids.")
def create_product(name, abbreviation, **kwargs):
    """
    Create a new Product
    """
    product = create_product_object(name=name, abbreviation=abbreviation, **kwargs)
    response = utils.checked_api_call(products_api, 'create_new', body=product)
    if response:
        return utils.format_json(response.content)


@arg("product-id", help="ID of the Product to update", type=types.existing_product_id)
@arg("-n", "--name", help="New name for the Product", type=types.unique_product_name)
@arg("-d", "--description", help="New Product description")
@arg("-a", "--abbreviation", help="New abbreviation")
@arg("-p", "--product-code", help="New Product code")
@arg("-sn", "--pgm-system-name", help="New system name")
@arg("--product-version-ids", type=types.existing_product_version, nargs='+',
     help="Space separated list of associated ProductVersion ids.")
def update_product(product_id, **kwargs):
    """
    Update a Product with new information
    """
    to_update = products_api.get_specific(id=product_id).content

    for key, value in iteritems(kwargs):
        if value is not None:
            setattr(to_update, key, value)

    response = utils.checked_api_call(
        products_api, 'update', id=product_id, body=to_update)
    if response:
        return utils.format_json(response.content)

<commit_msg>NCL-3393 : products.py<commit_after>from argh import arg
from six import iteritems

import pnc_cli.common as common
import pnc_cli.cli_types as types
import pnc_cli.utils as utils
from pnc_cli.swagger_client import ProductRest
from pnc_cli.swagger_client import ProductsApi
import pnc_cli.user_config as uc

products_api = ProductsApi(uc.user.get_api_client())

__author__ = 'thauser'


def create_product_object(**kwargs):
    created_product = ProductRest()
    for key, value in iteritems(kwargs):
        setattr(created_product, key, value)
    return created_product


@arg("name", help="Name for the Product", type=types.unique_product_name)
@arg("abbreviation", help="The abbreviation or \"short name\" of the new Product", type=types.unique_product_abbreviation)
@arg("-d", "--description", help="Detailed description of the new Product")
@arg("-p", "--product-code", help="The Product code for the new Product")
@arg("-sn", "--pgm-system-name", help="The system code for the new Product")
@arg("-pvids", "--product-version-ids", type=types.existing_product_version, nargs='+',
     help="Space separated list of associated ProductVersion ids.")
def create_product(name, abbreviation, **kwargs):
    data = create_product_raw(name, abbreviation, **kwargs)
    if data:
        return utils.format_json(data)

def create_product_raw(name, abbreviation, **kwargs):
    """
    Create a new Product
    """
    product = create_product_object(name=name, abbreviation=abbreviation, **kwargs)
    response = utils.checked_api_call(products_api, 'create_new', body=product)
    if response:
        return response.content


@arg("product-id", help="ID of the Product to update", type=types.existing_product_id)
@arg("-n", "--name", help="New name for the Product", type=types.unique_product_name)
@arg("-d", "--description", help="New Product description")
@arg("-a", "--abbreviation", help="New abbreviation")
@arg("-p", "--product-code", help="New Product code")
@arg("-sn", "--pgm-system-name", help="New system name")
@arg("--product-version-ids", type=types.existing_product_version, nargs='+',
     help="Space separated list of associated ProductVersion ids.")
def update_product(product_id, **kwargs):
    """
    Update a Product with new information
    """
    to_update = products_api.get_specific(id=product_id).content

    for key, value in iteritems(kwargs):
        if value is not None:
            setattr(to_update, key, value)

    response = utils.checked_api_call(
        products_api, 'update', id=product_id, body=to_update)
    if response:
        return utils.format_json(response.content)


{'fraction_non_alphanumeric': 0.1339618935376154, 'fraction_numerical': 0.000785700255352583, 'mean_word_length': 4.846153846153846, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5730935', 'n_tokens_mistral': 1551, 'n_tokens_neox': 1535, 'n_words': 419}