B
    `F                 @   s   d Z ddlmZmZmZ eZddlZddlZddl	Z	ddl
Z
ddlmZ ddlmZmZmZ dd Zd	d
 Zdd Zdd ZdddZdddZdddZd ddZG dd de	jZdS )!zFunctions for disk IO.    )absolute_importdivisionprint_functionN   )types)ENCODINGto_bytesto_textc             C   s   t t| S )z:Parse and return the json content from the specified path.)jsonloadsread_text_file)path r   H/home/dcms/DCMS/lib/python3.7/site-packages/ansible_test/_internal/io.pyread_json_file   s    r   c             C   s   t t| S )z2Return the contents of the specified path as text.)r	   read_binary_file)r   r   r   r   r      s    r   c          	   C   s   t | 
}| S Q R X dS )z3Return the contents of the specified path as bytes.N)open_binary_fileread)r   filer   r   r   r      s    
r   c          
   C   sH   yt t|  W n0 tk
rB } z|jtjkr2 W dd}~X Y nX dS )zGCreate a directory at path, including any necessary parent directories.N)osmakedirsr   OSErrorerrnoEEXIST)r   exr   r   r   	make_dirs#   s
    r   FTc             C   s:   t j|||rdnd|rdnd|dd }t| ||d dS )z\Write the given json content to the specified path, optionally creating missing directories.   N)z, z: ),:)	sort_keysindent
separatorscls
)create_directories)r
   dumpswrite_text_file)r   contentr$   	formattedencoderZtext_contentr   r   r   write_json_file,   s    

r*   c          	   C   s<   |rt tj|  t| d}|t| W dQ R X dS )z\Write the given text content to the specified path, optionally creating missing directories.wbN)r   r   r   dirnamer   writer   )r   r'   r$   r   r   r   r   r&   =   s    r&   rc             C   s(   d|krt d| tjt| |tdS )z$Open the given path for text access.bz*mode cannot include "b" for text files: %s)encoding)	Exceptionioopenr   r   )r   moder   r   r   open_text_fileF   s    r5   rbc             C   s$   d|krt d| tt| |S )z&Open the given path for binary access.r/   z*mode must include "b" for binary files: %s)r1   r2   r3   r   )r   r4   r   r   r   r   O   s    r   c                   s    e Zd ZdZ fddZ  ZS )SortedSetEncoderzEncode sets as sorted lists.c                s"   t |trt|S tt| |S )N)
isinstancesetsortedsuperr7   default)selfobj)	__class__r   r   r<   Z   s    
zSortedSetEncoder.default)__name__
__module____qualname____doc__r<   __classcell__r   r   )r?   r   r7   X   s   r7   )FTN)F)r.   )r6   )rC   
__future__r   r   r   type__metaclass__r   r2   r
   r    r   tr0   r   r   r	   r   r   r   r   r*   r&   r5   r   JSONEncoderr7   r   r   r   r   <module>   s&     

	
	
	