a )g,@sddlmZmZmZeZddlmZmZddl Z ddl m Z ddl mZddlmZddlmZmZddlmZddlmZGd d d eZdS) )absolute_importdivisionprint_function)pathwalkN) AnsibleError) string_types) to_nativeto_text) ActionBase) combine_varscseZdZdZgdZgdZddgZddgZdd Zd d Z dfd d Z ddZ ddZ ddZ ddZddZdddZddZZS) ActionModuleF)ZyamlZymlZjson)dirdepthfiles_matching ignore_files extensionsignore_unknown_extensionsfile _raw_paramsnamehash_behaviourcCsx|js d|_|jr(td|j|_nd|_|js\}}}|d7}||jksR|jdkrf|||fVq,qlq,dS)z Recursively iterate over a directory and sort the files in alphabetical order. Do not iterate pass the set depth. The default depth is unlimited. r)onerrorcSs|dS)Nrr$)xr$r$r%z2ActionModule._traverse_dir_depth..)rPr2N)rrr,r_sortr)r#Z current_depthZ sorted_walkZ current_rootr\Z current_filesr$r$r%r>s z ActionModule._traverse_dir_depthc CsT|jD]H}ztd||r&WdSWqtyLd|}t|Yq0qdS)z Return True if a file matches the list of ignore_files. Args: filename (str): The filename that is being matched against. Returns: Boolean z{0}$TzInvalid regular expression: {0}F)rrsearchr Exceptionr)r#r]Z file_typerKr$r$r% _ignore_files    zActionModule._ignore_filecCs0t|}tt|dko,|ddd|jvS)z Verify if source file has a valid extension Args: source_file (str): The full path of source file or source file. Returns: Bool r2rWN)rsplitextboollenr0)r#r-Zfile_extr$r$r%_is_valid_file_exts zActionModule._is_valid_file_extc Cst}d}d}|r:||s:d}dt|d|j}nz|j|\}}t|dd}|j |M_ |jj |||d}|st}t |tsd}d t|}n|j ||||||fS) z Loads a file and converts the output into a valid Python dict. Args: filename (str): The source file. Returns: Tuple (bool, str, dict) FTz({0} does not have a valid extension: {1}z, Zsurrogate_or_strict)errors) file_namer6z'{0} must be stored as a dictionary/hash)r!rkrr rYr0Z_loaderZ_get_file_contentsr r6loadrr7appendr@) r#r]validate_extensionsrJrrKZb_datar6datar$r$r%rAs$    zActionModule._load_filesc Cst}d}d}|D]}d}|jjrHt|jjj|t|ddkrHd}qt||}|jrj|j|sjd}|s|s|j rt |r| |s| |r|j |dd\}}} |s|| qt |r| |s|j |dd\}}} |s|| q|||fS)a> Load the found yml files and update/overwrite the dictionary. Args: root_dir (str): The base directory of the list of files that is being passed. var_files: (list): List of files to iterate over and load into a dictionary. Returns: Tuple (bool, str, dict) Frlr3zmain.ymlT)rq)r!r(rXrrYrZrrrerr<rgrkrAr@) r#rLZ var_filesrJrrKr]Z stop_iterfilepathZ loaded_datar$r$r%r?s0      zActionModule._load_files_in_dir)NN)F)__name__ __module__ __qualname__ZTRANSFERS_FILESr/r8r9r:r&r1rCr;r_r>rgrkrAr? __classcell__r$r$rSr%r sR r )Z __future__rrrtypeZ __metaclass__osrrrZansible.constantsZ constantsrDZansible.errorsrZansible.module_utils.sixrZansible.module_utils._textr r Zansible.plugins.actionr Zansible.utils.varsr r r$r$r$r%s