<commit_before>#---------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
#---------------------------------------------------------------------------------------------

import argparse
from collections import OrderedDict
from datetime import datetime
import os
import re

from azure.cli.commands.client_factory import get_mgmt_service_client
from azure.cli.commands.validators import validate_key_value_pairs

from azure.mgmt.storage import StorageManagementClient
from azure.storage.models import ResourceTypes, Services
from azure.storage.table import TablePermissions

# region PARAMETER VALIDATORS

def validate_client_parameters(namespace):
    """ Retrieves storage connection parameters from environment variables and parses out
    connection string into account name and key """
    n = namespace

    if not n.connection_string:
        n.connection_string = os.environ.get('AZURE_STORAGE_CONNECTION_STRING')

    # if connection string supplied or in environment variables, extract account key and name
    if n.connection_string:
        conn_dict = validate_key_value_pairs(n.connection_string)
        n.account_name = conn_dict['AccountName']
        n.account_key = conn_dict['AccountKey']

    # otherwise, simply try to retrieve the remaining variables from environment variables
    if not n.account_name:
        n.account_name = os.environ.get('AZURE_STORAGE_ACCOUNT')
    if not n.account_key:
        n.account_key = os.environ.get('AZURE_STORAGE_KEY')
    if not n.sas_token:
        n.sas_token = os.environ.get('AZURE_SAS_TOKEN')

    # if account name is specified but no key, attempt to query
    if n.account_name and not n.account_key:
        scf = get_mgmt_service_client(StorageManagementClient)
        acc_id = next((x for x in scf.storage_accounts.list() if x.name == n.account_name), None).id
        if acc_id:
            from azure.cli.commands.arm import parse_resource_id
            rg = parse_resource_id(acc_id)['resource_group']
            n.account_key = \
                scf.storage_accounts.list_keys(rg, n.account_name).keys[0].value #pylint: disable=no-member

def get_content_setting_validator(settings_class):
    def validator(namespace):
        namespace.content_settings = settings_class(
            content_type=namespace.content_type,
            content_disposition=namespace.content_disposition,
            content_encoding=namespace.content_encoding,
            content_language=namespace.content_language,
            content_md5=namespace.content_md5,
            cache_control=namespace.content_cache_control
        )
        del namespace.content_type,
        del namespace.content_disposition,
        del namespace.content_encoding,
        del namespace.content_language,
        del namespace.content_md5,
        del namespace.content_cache_control
    return validator

def validate_encryption(namespace):
    ''' Builds up the encryption object for storage account operations based on the
    list of services passed in. '''
    if namespace.encryption:
        from azure.mgmt.storage.models import Encryption, EncryptionServices, EncryptionService
        services = {service: EncryptionService(True) for service in namespace.encryption}
        namespace.encryption = Encryption(EncryptionServices(**services))

def validate_entity(namespace):
    ''' Converts a list of key value pairs into a dictionary. Ensures that required<commit_msg>Fix Error if Non-existent Storage Account Specified (#787)<commit_after>#---------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
#---------------------------------------------------------------------------------------------

import argparse
from collections import OrderedDict
from datetime import datetime
import os
import re

from azure.cli.commands.client_factory import get_mgmt_service_client
from azure.cli.commands.validators import validate_key_value_pairs

from azure.mgmt.storage import StorageManagementClient
from azure.storage.models import ResourceTypes, Services
from azure.storage.table import TablePermissions

# region PARAMETER VALIDATORS

def validate_client_parameters(namespace):
    """ Retrieves storage connection parameters from environment variables and parses out
    connection string into account name and key """
    n = namespace

    if not n.connection_string:
        n.connection_string = os.environ.get('AZURE_STORAGE_CONNECTION_STRING')

    # if connection string supplied or in environment variables, extract account key and name
    if n.connection_string:
        conn_dict = validate_key_value_pairs(n.connection_string)
        n.account_name = conn_dict['AccountName']
        n.account_key = conn_dict['AccountKey']

    # otherwise, simply try to retrieve the remaining variables from environment variables
    if not n.account_name:
        n.account_name = os.environ.get('AZURE_STORAGE_ACCOUNT')
    if not n.account_key:
        n.account_key = os.environ.get('AZURE_STORAGE_KEY')
    if not n.sas_token:
        n.sas_token = os.environ.get('AZURE_SAS_TOKEN')

    # if account name is specified but no key, attempt to query
    if n.account_name and not n.account_key:
        scf = get_mgmt_service_client(StorageManagementClient)
        acc = next((x for x in scf.storage_accounts.list() if x.name == n.account_name), None)
        if acc:
            from azure.cli.commands.arm import parse_resource_id
            rg = parse_resource_id(acc.id)['resource_group']
            n.account_key = \
                scf.storage_accounts.list_keys(rg, n.account_name).keys[0].value #pylint: disable=no-member
        else:
            raise ValueError("Storage account '{}' not found.".format(n.account_name))

def get_content_setting_validator(settings_class):
    def validator(namespace):
        namespace.content_settings = settings_class(
            content_type=namespace.content_type,
            content_disposition=namespace.content_disposition,
            content_encoding=namespace.content_encoding,
            content_language=namespace.content_language,
            content_md5=namespace.content_md5,
            cache_control=namespace.content_cache_control
        )
        del namespace.content_type,
        del namespace.content_disposition,
        del namespace.content_encoding,
        del namespace.content_language,
        del namespace.content_md5,
        del namespace.content_cache_control
    return validator

def validate_encryption(namespace):
    ''' Builds up the encryption object for storage account operations based on the
    list of services passed in. '''
    if namespace.encryption:
        from azure.mgmt.storage.models import Encryption, EncryptionServices, EncryptionService
        services = {service: EncryptionService(True) for service in namespace.encryption}
        namespace.encryption = Encryption(EncryptionServices(**services))

def validate_entity(namespace):
    ''' Converts a list of key value pairs into a dictionary. Ensures that required
{'fraction_non_alphanumeric': 0.11486761710794298, 'fraction_numerical': 0.001493550577053632, 'mean_word_length': 2.928533333333333, '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': '2409170', 'n_tokens_mistral': 1884, 'n_tokens_neox': 1876, 'n_words': 514}