#----------------------------------------------------------------------------
#  ALTRAN_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.
#
#  Copyright (C) Altran ACT S.A.S. 2018,2019,2020,2021.  All rights reserved.
#
#  ALTRAN_PROLOG_END_TAG
#
# %Z%  %ci%  %fn%, %R%, %t%, %G% %U%
#----------------------------------------------------------------------------
# Import Python Modules
#----------------------------------------------------------------------------
from xml.etree import ElementTree as xml
from xml.dom import minidom
from collections import OrderedDict
import os
import sys
#Retrieve python version
version=sys.version_info[0]+(sys.version_info[1]*.1)
#Check for the python version,import imp module depricated from python version3.1 onwards
if  version < 3.1:
    import imp    
    cl_utilities=imp.load_source('cl_utilities','/usr/es/lib/python/cl_utilities')
else:
    import importlib.util
    def import_path(path):
        module_name = os.path.basename(path)
        spec = importlib.util.spec_from_loader(module_name,importlib.machinery.SourceFileLoader(module_name, path))
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
        sys.modules[module_name] = module
        return module    
    cl_utilities=import_path('/usr/es/lib/python/cl_utilities')
from cl_utilities import *

#----------------------------------------------------------------------------
# Global Definitions
#----------------------------------------------------------------------------
odmdir = os.environ.get('ODMDIR')

if not odmdir:
    odmdir="/etc/es/objrepos"

cbm_conf_file_path=odmdir + "/cloud_backup_configuration.xml"

UTILS_LOG = "clutils.log"
CAT_FILE = "scripts.cat"

#----------------------------------------------------------------------------
# Functions
#----------------------------------------------------------------------------

def prettify(elem):
    """
    Function    : prettify
    Description : This function inserts new line for every data in a xml
                  string and pretties it
    Arguments   : Element name
    Return      : Returns a pretty-printed XML string for the Element
    """
    rough_bytes = xml.tostring(elem, 'utf-8')
    rough_string = rough_bytes.decode('utf-8').replace("\n","").replace("\t","")
    reparsed = minidom.parseString(rough_string)
    return reparsed.toprettyxml(indent="\t")

def parse_elements(inputArgs, elements):
    """
    Function    : parse_elements 
    Description : This function parses the input arguments 
    Arguments   : inputArgs- List of input arguments[attr=value]
                  elements- Used to save as key value in dictonary.
    Return      : Returns the parsed input arguments
    """

    elemDict = OrderedDict()
    error_list=[]

    # Split user provided Element, value pairs
    for entry in inputArgs:
        value=entry.split("=")
        extra_list=[s for s in elements if value[0] in s] #If any extra input arguments adding to error list.
        if not extra_list: 
            error_list.append(value[0])
        else:
            # Get user values in "Element=value" format and save the value in dictonary.
            if len(value) > 1:
                if value[0]=="Resource_group" or value[0]=="Storage_name":
                    elemDict['Name'] = value[1]
                else:
                    elemDict[value[0]]=value[1]
            else:
                elemDict[value[0]]=""

    if len(error_list) > 1:
        defMsg = "Error: Invalid arguments %1$s entered and ignoring those arguments."
        displayMsg2(UTILS_LOG,43,CAT_FILE,72,defMsg,','.join(error_list))
    return elemDict
