File manager - Edit - /opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/ini_manage.cpython-310.pyc
Back
o ;j#U � @ s� d Z ddlZddlZddlZddlmZ ddlZddlZddl Zddl ZddlmZ e� e�ZdZdd� Zejdejd �Ze�d �Ze�d�Zddd�Zddd�Zddd�Zddd�Zddd�Zddd�ZG dd� de�ZG dd� de�ZdS ) z� Edit ini files :maintainer: <akilesh1597@gmail.com> :maturity: new :depends: re :platform: all (for example /etc/sysctl.conf) � N)�OrderedDict)�CommandExecutionErrorZinic C s t S )z Rename to ini )�__virtualname__� r r �K/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/ini_manage.py�__virtual__ s r z^\s*\[(.+?)\]\s*$)�flagsz^\s*(#|;)\s*(.*)z (\s+)(.*)�=Fc C s0 |pi }t j| |||d�}|�|�}|�� |S )a� Edit an ini file, replacing one or more sections. Returns a dictionary containing the changes made. Args: file_name (str): The full path to the ini file. sections (dict): A dictionary representing the sections to be edited in the ini file. The keys are the section names and the values are a dictionary containing the options. If the ini file does not contain sections the keys and values represent the options. The default is ``None``. separator (str): The character used to separate keys and values. Standard ini files use the "=" character. The default is ``=``. .. versionadded:: 2016.11.0 encoding (str): A string value representing encoding of the target ini file. If ``None`` is passed, it uses the system default which is likely ``utf-8``. Default is ``None`` .. versionadded:: 3006.6 no_spaces (bool): A bool value that specifies that the key/value separator will be wrapped with spaces. This parameter was added to have the ability to not wrap the separator with spaces. Default is ``False``, which maintains backwards compatibility. .. warning:: This will affect all key/value pairs in the ini file, not just the specific value being set. .. versionadded:: 3006.10 Returns: dict: A dictionary representing the changes made to the ini file API Example: .. code-block:: python import salt.client with salt.client.get_local_client() as sc: sc.cmd( 'target', 'ini.set_option', ['path_to_ini_file', '{"section_to_change": {"key": "value"}}'] ) CLI Example: .. code-block:: bash salt '*' ini.set_option /path/to/ini '{section_foo: {key: value}}' �� separator�encoding� no_spaces)�_Ini�get_ini_file�update�flush)� file_nameZsectionsr r r �inifile�changesr r r � set_option( s >� r c C sL t j| ||d�}|r z|�|i ��|d�W S ty Y dS w |�|d�S )aK Get value of a key from a section in an ini file. Returns ``None`` if no matching key was found. Args: file_name (str): The full path to the ini file. section (str): A string value representing the section of the ini that the option is in. If the option is not in a section, leave this empty. option (str): A string value representing the option to search for. separator (str): The character used to separate keys and values. Standard ini files use the "=" character. The default is ``=``. .. versionadded:: 2016.11.0 encoding (str): A string value representing encoding of the target ini file. If ``None`` is passed, it uses the system default which is likely ``utf-8``. Default is ``None`` .. versionadded:: 3006.6 Returns: str: The value as defined in the ini file, or ``None`` if empty or not found API Example: .. code-block:: python import salt.client with salt.client.get_local_client() as sc: sc.cmd('target', 'ini.get_option', [path_to_ini_file, section_name, option]) CLI Example: .. code-block:: bash salt '*' ini.get_option /path/to/ini section_name option_name �r r N)r r �get�AttributeError)r �section�optionr r r r r r � get_optiono s 2�r c C sR t j| ||d�}t|�|�ttf�r|�|i ��|d�}n|�|d�}|�� |S )ar Remove a key/value pair from a section in an ini file. Returns the value of the removed key, or ``None`` if nothing was removed. Args: file_name (str): The full path to the ini file. section (str): A string value representing the section of the ini that the option is in. If the option is not in a section, leave this empty. option (str): A string value representing the option to search for. separator (str): The character used to separate keys and values. Standard ini files use the "=" character. The default is ``=``. .. versionadded:: 2016.11.0 encoding (str): A string value representing encoding of the target ini file. If ``None`` is passed, it uses the system default which is likely ``utf-8``. Default is ``None`` .. versionadded:: 3006.6 Returns: str: A string value representing the option that was removed or ``None`` if nothing was removed API Example: .. code-block:: python import salt sc = salt.client.get_local_client() sc.cmd('target', 'ini.remove_option', [path_to_ini_file, section_name, option]) CLI Example: .. code-block:: bash salt '*' ini.remove_option /path/to/ini section_name option_name r N)r r � isinstancer �dictr �popr )r r r r r r �valuer r r � remove_option� s 2r c C sL t j| ||d�}i }|�|i ��� D ]\}}|d dkr#|�||i� q|S )a Retrieve a section from an ini file. Returns the section as a dictionary. If the section is not found, an empty dictionary is returned. Args: file_name (str): The full path to the ini file. section (str): A string value representing name of the section to search for. separator (str): The character used to separate keys and values. Standard ini files use the "=" character. The default is ``=``. .. versionadded:: 2016.11.0 encoding (str): A string value representing encoding of the target ini file. If ``None`` is passed, it uses the system default which is likely ``utf-8``. Default is ``None`` .. versionadded:: 3006.6 Returns: dict: A dictionary containing the names and values of all items in the section of the ini file. If the section is not found, an empty dictionary is returned API Example: .. code-block:: python import salt.client with salt.client.get_local_client() as sc: sc.cmd('target', 'ini.get_section', [path_to_ini_file, section_name]) CLI Example: .. code-block:: bash salt '*' ini.get_section /path/to/ini section_name r r �#)r r r �itemsr �r r r r r �ret�keyr r r r �get_section� s /�r&