# -*- coding:utf-8 -*-
"""
Function: change_port_mode.py moudle. This moudle mainly involves the
Get fru info function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2022-2022
"""
import xml.etree.ElementTree
from scripts import common_function


INVALID_VALUE = None


def changeportmode_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    Change port mode
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('changeportmode',
                                   help='''get fru info''')
    sub_parser.add_argument('-P', dest='Port', required=False,
                            help='''change port mode''')
    parser_dict['changeportmode'] = sub_parser
    return 'changeportmode'


def changeportmode(client, args):
    """
    Function Description:change port mode
    Parameter:client refishClient: class object
    """
    config = "0"
    if args.Port == "True":
        config = "1"
    change_one_xml(common_function.get_urest_config_path(), 'portRestricted', config)
    common_function.print_info('Command executed successfully.')


def change_one_xml(xml_path, xml_dw, update_content):
    doc = xml.etree.ElementTree.parse(xml_path)
    root = doc.getroot()
    # 查找修改路径
    sub1 = root.find(xml_dw)
    # 修改标签内容
    sub1.text = update_content
    # 保存修改
    doc.write(xml_path)


