
    ܍~c$                        d Z ddlZddlZddl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 ddlmZ ddlmZ ddlmZ ddlmZmZmZmZmZmZmZ ddlZdd	lmZmZ dd
lmZ ddl m!Z!m"Z"m#Z# 	 ddl$m%Z& e&Z%n# e'$ r e(Z%Y nw xY w ee!d          Z! ej)        e*          Z+dZ,dZ-ddddZ.dZ/dZ0dZ1dZ2dZ3dZ4dZ5dZ6dZ7erddlZddl8m9Z9m:Z:  G d d e:          Z;ne<Z; G d! d"e%          Z= G d# d$e          Z>ee>         Z?dd%d&d'e?d(ee@         d)e@d*e@fd+ZA G d, d-eB          ZCd. ZDdfd/ee@         d*e@fd0ZEe=fdeFd1e<d2ee(         fd3ZGe=fd2ee(         fd4ZHe=fd2ee(         fd5ZId6 ZJdgd1e<fd7ZK	 	 	 	 	 dhd9e<d1e<d:eFd;eFd<eFd=eFfd>ZL G d? d@          ZMdddAdBe<dCeNdDe<dEee?         dFee?         d*ee@eNf         fdGZOdidHZPdI ZQdJ ZRdKe<dLe@d*ee@         fdMZSdKe<dNe<d*e@fdOZTd*e@fdPZUdQe<dNe<fdRZVdQe<fdSZWdTe<d*e@fdUZXdjd1e<dNe<d*e@fdWZYdXe;d*e@fdYZZdXe;d*e@fdZZ[dfdXe;d1ee<         d*e@fd[Z\d*e<fd\Z]d]e^d*e@fd^Z_d*e@fd_Z`d*e<fd`Zad*e<fdaZbdfdbZcdc Zddd Zee*dek    r ejf         ee                       dS dS )kzFschema.py: Set of module functions for processing cloud-config schema.    N)defaultdict)Iterable)deepcopy)partial)chain)TYPE_CHECKINGList
NamedTupleOptionalTypeUnioncast)importersafeyaml)Init)errorget_modules_from_dir	load_fileValidationErrorT)sys_exitz!versions.schema.cloud-config.jsonzschema-cloud-config-v1.jsontruefalsenull)TFNs   #cloud-configz
{name}
{title_underbar}
**Summary:** {title}

{description}

**Internal name:** ``{id}``

**Module frequency:** {frequency}

**Supported distros:** {distros}

{activate_by_schema_keys}{property_header}
{property_doc}

{examples}
z**Config schema**:z3{prefix}**{prop_name}:** ({prop_type}){description}zH{prefix}Each object in **{prop_name}** list supports the following keys:z**Examples**::

z
    # --- Example{0} ---
deprecatedzDEPRECATED: )NotRequired	TypedDictc                       e Zd ZU eed<   eed<   eed<   eed<   ej        e         ed<   ej        e         ed<   eed<   eee                  ed<   d	S )

MetaSchemanameidtitledescriptiondistrosexamples	frequencyactivate_by_schema_keysN)__name__
__module____qualname__str__annotations__typingr	   r        9/usr/lib/python3/dist-packages/cloudinit/config/schema.pyr   r   M   s}         			


S!!!!+c""""!,T#Y!777777r/   r   c                       e Zd ZdS )SchemaDeprecationErrorN)r(   r)   r*   r.   r/   r0   r2   r2   [   s        Dr/   r2   c                   0    e Zd ZU eed<   eed<   defdZdS )SchemaProblempathmessagereturnc                 $    | j          d| j         S )N: )r5   r6   selfs    r0   formatzSchemaProblem.formatc   s    )--t|---r/   N)r(   r)   r*   r+   r,   r<   r.   r/   r0   r4   r4   _   sC         
IIILLL. . . . . . .r/   r4   , prefix	separatorschema_problemsr?   r@   r7   c                ^    |                     t          d |                     }|r| | }|S )Nc                 *    |                                  S N)r<   )ps    r0   <lambda>z)_format_schema_problems.<locals>.<lambda>p   s    QXXZZ r/   )joinmap)rA   r?   r@   	formatteds       r0   _format_schema_problemsrJ   j   sA     s#7#7IIJJI +*y**	r/   c                   V     e Zd ZdZ	 	 ddee         dee         f fdZdefdZ xZ	S )	SchemaValidationErrorz<Raised when validating a cloud-config file against a schema.Nschema_errorsschema_deprecationsc                     d}|r|t          |d          z  }|r|r|dz  }|t          |d          z  }t                                          |           || _        || _        dS )zInit the exception an n-tuple of schema errors.

        @param schema_errors: An n-tuple of the format:
            ((flat.config.key, msg),)
        @param schema_deprecations: An n-tuple of the format:
            ((flat.config.key, msg),)
         zCloud config schema errors: )r?   

"Cloud config schema deprecations: N)rJ   super__init__rM   rN   )r;   rM   rN   r6   	__class__s       r0   rT   zSchemaValidationError.__init__y   s      	.&D   G  	 "6!.#;   G 	!!!*#6   r/   r7   c                 *    t          | j                  S rD   )boolrM   r:   s    r0   
has_errorsz SchemaValidationError.has_errors   s    D&'''r/   )NN)
r(   r)   r*   __doc__r   SchemaProblemsrT   rW   rX   __classcell__)rU   s   @r0   rL   rL   v   s        FF 378<7 7/7 &n57 7 7 7 7 78(D ( ( ( ( ( ( ( (r/   rL   c                     	 ddl m} n# t          $ r Y dS w xY w|j                            |d          pt          |t          f          S )zWTYPE_CHECKER override allowing bytes for string type

    For jsonschema v. 3.0.0+
    r   )Draft4ValidatorFstring)
jsonschemar]   ImportErrorTYPE_CHECKERis_type
isinstancebytes)checkerinstancer]   s      r0   is_schema_byte_stringrg      sr    
.......   uu'//(  (	Huh	'	'(s   	 
r#   c                 v    | rt            |  S t                               dd                                          S )N:.)DEPRECATED_PREFIXreplacestrip)r#   s    r0   _add_deprecation_msgrn      s=     3#2[222$$S#..44666r/   schema
error_typec              #   t   K   |r3|                     d          }t          |          } ||          V  dS dS )zJsonschema validator for `deprecated` items.

    It raises a instance of `error_type` if deprecated that must be handled,
    otherwise the instance is consider faulty.
    r#   N)getrn   )
_validatorr   	_instancero   rp   r#   msgs          r0   _validator_deprecatedrv      sR        jj//";//joo r/   c              #     K   ddl m} g }g }t          |          D ]\  }}	t          |                     ||	|                    }
t          t          fd|
                    }t          t          fd|
                    }|s|                    |            n(|                    |            ||d|          V  |E d{V  dS )	zJsonschema validator for `anyOf`.

    It treats occurrences of `error_type` as non-errors, but yield them for
    external processing. Useful to process schema annotations, as `deprecated`.
    r   r   schema_pathc                 &    t          |            S rD   rc   erp   s    r0   rF   z_anyOf.<locals>.<lambda>       Az)B)B%B r/   c                 $    t          |           S rD   r{   r|   s    r0   rF   z_anyOf.<locals>.<lambda>       Z:66 r/   , is not valid under any of the given schemascontextN)r_   r   	enumeratelistdescendfilterextend)	validatoranyOfrf   _schemarp   r   
all_errorsall_deprecationsindex	subschemaall_errserrsdeprecationss       `        r0   _anyOfr      s<      +*****J%e,, 
 
yh	uEE
 
 FBBBBHMMNN6666AA
 
  	##L111E$o@HJ
 
 
 	
 	
 	
  r/   c              #   b   K   ddl m} t          |          }g }g }|D ]\  }	}
t                               |
|	                    }t          t          fd|                    }t          t          fd|                    }|s|
}|                    |            n(|                    |            |d|          V   fd|D             }|rH|                    |           d	                    d
 |D                       } |d|          V  dS |E d{V  dS )zJsonschema validator for `oneOf`.

    It treats occurrences of `error_type` as non-errors, but yield them for
    external processing. Useful to process schema annotations, as `deprecated`.
    r   r   rx   c                 &    t          |            S rD   r{   r|   s    r0   rF   z_oneOf.<locals>.<lambda>   r~   r/   c                 $    t          |           S rD   r{   r|   s    r0   rF   z_oneOf.<locals>.<lambda>   r   r/   r   r   c                 F    g | ]\  }}                     |          |S r.   )is_valid).0isrf   r   s      r0   
<listcomp>z_oneOf.<locals>.<listcomp>  s2    NNN1i.@.@1.M.MN!NNNr/   r=   c              3   4   K   | ]}t          |          V  d S rD   )repr)r   ro   s     r0   	<genexpr>z_oneOf.<locals>.<genexpr>
  s(      @@6$v,,@@@@@@r/   z is valid under each of N)	r_   r   r   r   r   r   r   appendrG   )r   oneOfrf   r   rp   r   
subschemasr   r   r   r   r   r   r   first_valid
more_validreprss   ` ` `            r0   _oneOfr      s      +*****5!!JJ& 
 
yh	uEE
 
 FBBBBHMMNN6666AA
 
  	#K##L111E$o@HJ
 
 
 	
 	
 	

 ONNNN
NNNJ $+&&&		@@Z@@@@@o.6hh>
 
 	
 	
 	
 	
 	
 $#########r/   c                     ddl m} m} ddlm} t          | j                  }d|d<   ddi|d         d	<   i }t          | d
          r%| j        	                    dt                    }d|i}n| j        }t          t          f|d<   d|i}t          | j                  }t           |t"          <   t$          |d<   t&          |d<    |d||dd|}dd}	|	|_        ||fS )zGet metaschema validator and format checker

    Older versions of jsonschema require some compatibility changes.

    @returns: Tuple: (jsonschema.Validator, FormatChecker)
    @raises: ImportError when jsonschema is not present
    r   )r]   FormatChecker)createFadditionalPropertiestyper^   
propertieslabelra   type_checkerdefault_typesr   r   draft4)meta_schema
validatorsversionNc                 p    t          d |                     ||                    }t          |d          du S )zgOverride version of `is_valid`.

        It does ignore instances of `SchemaDeprecationError`.
        c                 .    t          | t                     S rD   )rc   r2   r}   s    r0   rF   z<get_jsonschema_validator.<locals>.is_valid.<locals>.<lambda>P  s    *)   r/   N)r   iter_errorsnext)r;   rf   r   __errorss        r0   r   z*get_jsonschema_validator.<locals>.is_validJ  sI    
   Xw//	
 
 FD!!T))r/   r.   rD   )r_   r]   r   jsonschema.validatorsr   r   META_SCHEMAhasattrra   redefinerg   DEFAULT_TYPESr+   rd   dict
VALIDATORSrv   DEPRECATED_KEYr   r   r   )
r]   r   r   strict_metaschemavalidator_kwargsr   typesr   cloudinitValidatorr   s
             r0   get_jsonschema_validatorr     sY    :9999999,,,,,, !!<==05,- 17/Al#G,// 4&3<<+
 
 L
  - ,h+U3 o011J!6J~ Jw Jw %  	 * * * * #+..r/   c           	      ,   ddl m} 	 |                     |           dS # |$ ro}|rGt          t	          d                    d |j        D                       |j                  g          |t          	                    d|           Y d}~dS d}~ww xY w)a   Validate provided schema meets the metaschema definition. Return strict
    Validator and FormatChecker for use in validation
    @param validator: Draft4Validator instance used to validate the schema
    @param schema: schema to validate
    @param throw: Sometimes the validator and checker are required, even if
        the schema is invalid. Toggle for whether to raise
        SchemaValidationError or log warnings.

    @raises: ImportError when jsonschema is not present
    @raises: SchemaValidationError when the schema is invalid
    r   )SchemaErrorrj   c                 ,    g | ]}t          |          S r.   r+   r   rE   s     r0   r   z3validate_cloudconfig_metaschema.<locals>.<listcomp>t  s    !;!;!;Q#a&&!;!;!;r/   rM   zGMeta-schema validation failed, attempting to validate config anyway: %sN)
jsonschema.exceptionsr   check_schemarL   r4   rG   r5   r6   LOGwarning)r   ro   throwr   errs        r0   validate_cloudconfig_metaschemar   \  s     211111
v&&&&& 
 
 
  	'!!;!;#(!;!;!;<<ck     		
 	
 	
 	
 	
 	
 	
 	
 	

s    BA$BBFconfigstrictr   log_detailslog_deprecationsc                    |t                      }	 t                      \  }}|rt          ||d           n+# t          $ r t                              d           Y dS w xY w || |                      }g }	g }
t          |                    |           d           D ]\}d                    d	 |j	        D                       }t          ||j                  f}t          |t                    r|
|z  }
W|	|z  }	]|r.|
r,t          |
d
d          }t                              |           |r|	s|
rt!          |	|
          |	r3|rt          |	dd          }nd}t                              |           dS dS )a  Validate provided config meets the schema definition.

    @param config: Dict of cloud configuration settings validated against
        schema. Ignored if strict_metaschema=True
    @param schema: jsonschema dict describing the supported schema definition
       for the cloud config module (config.cc_*). If None, validate against
       global schema.
    @param strict: Boolean, when True raise SchemaValidationErrors instead of
       logging warnings.
    @param strict_metaschema: Boolean, when True validates schema using strict
       metaschema definition at runtime (currently unused)
    @param log_details: Boolean, when True logs details of validation errors.
       If there are concerns about logging sensitive userdata, this should
       be set to False.
    @param log_deprecations: Controls whether to log deprecations or not.

    @raises: SchemaValidationError when provided config does not validate
        against the provided schema.
    @raises: RuntimeError when provided config sourced from YAML is not a dict.
    NF)r   z5Ignoring schema validation. jsonschema is not present)format_checkerc                     | j         S rD   )r5   r   s    r0   rF   z-validate_cloudconfig_schema.<locals>.<lambda>  s    QV r/   keyrj   c                 ,    g | ]}t          |          S r.   r   r   s     r0   r   z/validate_cloudconfig_schema.<locals>.<listcomp>  s    444AQ444r/   z"Deprecated cloud-config provided:

r>   zInvalid cloud-config provided:
zeInvalid cloud-config provided: Please run 'sudo cloud-init schema --system' to see the schema errors.)
get_schemar   r   r`   r   debugsortedr   rG   r5   r4   r6   rc   r2   rJ   r   rL   )r   ro   r   r   r   r   r   r   r   r   r   r   r5   problemr6   detailss                   r0   validate_cloudconfig_schemar     s   8 ~.F.H.H+	] 	+"F%       		IJJJ #"6--//JJJIF#%L	--f55;K;KLLL  xx4444455 u}557e344 	G#LLgFF L )8
 
 

 	G :6 :\ :#FL999  	-9  GG) 
 	G s   %8 $A A c                   
   e Zd ZdededefdZededee         defd            Z	d	e
fd
Ze	 ddee         dee         dee         dededefd            Zdee         dededee         fdZde
de
deeef         fdZdS )
_Annotatorcloudconfigoriginal_contentschemamarksc                 0    || _         || _        || _        d S rD   )_cloudconfig_original_content_schemamarks)r;   r   r   r   s       r0   rT   z_Annotator.__init__  s"     (!1'r/   r"   contentr7   c                 >    d                     |          }d|  d| dS )Nr   # z: -------------
rQ   )rG   )r"   r   bodys      r0   _build_footerz_Annotator._build_footer  s,    yy!!6E66D6666r/   rA   c                 p   t          t                    }|D ]\  }}t          j        d|          }|r@|                                \  }}|t          |                                       |           n(d }|| j        |                                      |           |d                    |||          }|S )Nz&format-l(?P<line>\d+)\.c(?P<col>\d+).*zLine {line} column {col}: {msg})linecolru   )	r   r   rematchgroupsintr   r   r<   )r;   rA   errors_by_liner5   ru   r   r   r   s           r0   _build_errors_by_linez _Annotator._build_errors_by_line  s    $T*** 	 	KT3HFMME D!LLNN	cs4yy)005555t067>>sCCC7>>3C ?   r/   rP   problemslabelsfooterr   label_prefixc                     | D ]=}| | }|                     |           |                     d| d|            |dz  }>|S )Nr   r9      )r   )r   r   r   r   r   r   r   s          r0   _add_problemsz_Annotator._add_problems  sh       	 	G#,U,,EMM%   MM1u1111222QJEEr/   linesr   deprecations_by_linec                     g }g }g }d}d}t          |d          D ]\  }	}
||	         }||	         }|s|reg }                     ||||d          }                     ||||d          }|                    |
dz   d                    |          z              ~|                    |
           |                    t           fdt          d d	|fd
|ff                               |S )Nr  E)r   Dz		# ,c                      j         |  S rD   )r   )seqr;   s    r0   rF   z._Annotator._annotate_content.<locals>.<lambda>  s    .D.4 r/   c                 ,    t          | d                   S )Nr  )rW   )r  s    r0   rF   z._Annotator._annotate_content.<locals>.<lambda>  s    SV r/   ErrorsDeprecations)r   r  r   rG   r   rH   r   )r;   r  r   r  annotated_contenterror_footerdeprecation_footererror_indexdeprecation_indexline_numberr   r   r   r   s   `             r0   _annotate_contentz_Annotator._annotate_content  s_    "$(*!*5!!4!4 	/ 	/K#K0F/<L / /$&"00FL+C 1   %)$6$6 &%!$ %7 % %! "((388F;K;K)KLLLL!((....  4444,,!<0');< 	 		
 	
 	
 ! r/   rM   rN   c                    |s	|s| j         S | j                                                             d          }t          | j        t
                    s.d                    ||                     ddg          gz             S |                     |          }|                     |          }| 	                    |||          }d                    |          S )Nr   r  z&# E1: Cloud-config is not a YAML dict.)
r   decodesplitrc   r   r   rG   r   r   r  )r;   rM   rN   r  r   r  r  s          r0   annotatez_Annotator.annotate'  s    
  	*%8 	*))&--//55d;;$+T22 		99&& #K"L    33MBB#99:MNN 22>#7
 
 yy*+++r/   N)rP   )r(   r)   r*   r   rd   rT   staticmethodr+   r	   r   rZ   r   r   r  r  r   r  r.   r/   r0   r   r     s       ((  ( 	( ( ( ( 7S 749 7 7 7 7 \7^        s)S	 S	 	
  
   \*!Cy*! *! #	*!
 
c*! *! *! *!X,%, ,, 
sEz		, , , , , ,r/   r   rM   rN   r   r   r   rM   rN   c                T    t          | ||                              |pg |pg           S )a  Return contents of the cloud-config file annotated with schema errors.

    @param cloudconfig: YAML-loaded dict from the original_content or empty
        dict if unparseable.
    @param original_content: The contents of a cloud-config file
    @param schemamarks: Dict with schema marks.
    @param schema_errors: Instance of `SchemaProblems`.
    @param schema_deprecations: Instance of `SchemaProblems`.

    @return Annotated schema
    )r   r  )r   r   r   rM   rN   s        r0   annotated_cloudconfig_filer  A  s9    & k#3[AAJJ06B  r/   c           
         | t          j                    dk    rt          d          t          g           }|                    d           |                                 t          |j                            d          d	          }nRt           j	        
                    |           s"t          d
                    |                     t          | d	          }|                    t                    stt          dd                    | t                                                              g}t!          |          }|r%t#          t%          i |i |j                             |	 |rt)          j        |          \  }}nt)          j        |          }i }n# t.          j        $ r}	dx}
}d}t3          |	d          r!t5          |	d          rt5          |	d          }n0t3          |	d          r t5          |	d          rt5          |	d          }|r|j        dz   }
|j        dz   }t          d                    |
|          d                    | t;          |	                              g}t!          |          }|r%t#          t%          i |i |j                             ||	d}	~	ww xY wt=          |t>                    s|st          d          	 tA          ||dd           dS # t           $ rt}	|r,t#          t%          ||||	j        |	j!                             n&tE          |	j!        dd          }t#          |           |	#                                r Y d}	~	dS d}	~	ww xY w)a  Validate cloudconfig file adheres to a specific jsonschema.

    @param config_path: Path to the yaml cloud-config file to parse, or None
        to default to system userdata from Paths object.
    @param schema: Dict describing a valid jsonschema to validate against.
    @param annotate: Boolean set True to print original config file with error
        annotations on the offending lines.

    @raises SchemaValidationError containing any of schema_errors encountered.
    @raises RuntimeError when config_path does not exist.
    Nr   z?Unable to read system userdata as non-root user. Try using sudo)ds_depstrust)existingcloud_configF)r  zConfigfile {0} does not existzformat-l1.c1z"File {0} needs to begin with "{1}"r   r  context_markproblem_markzformat-l{line}.c{col})r   r   zFile {0} is not valid yaml. {1}z Cloud-config is not a YAML dict.T)r   r   r  rR   r=   r>   )$osgetuidRuntimeErrorr   fetchconsume_datar   paths	get_ipathr5   existsr<   
startswithCLOUD_CONFIG_HEADERr4   r  rL   printr  rM   r   load_with_marksloadyaml	YAMLErrorr   getattrr   columnr+   rc   r   r   rN   rJ   rX   )config_pathro   r  initr   r   r   r   marksr}   r   r5  markr6   s                 r0   validate_cloudconfig_filer:  Y  s    9;;!"   B

G
$$$DJ00@@OOOw~~k** 	/66{CC   K666122 4;;!4!;!;!=!=  
 &f-- 	*53F    
  	!)!9'!B!BK"-00KEN   v1n%% 	.'!^*D*D 	.1n--DDQ'' 	.GA~,F,F 	.1n--D 	%9q=D[1_F'..Df.EE188c!ffMM 
 &f-- 	*53F    
 /0 k4(( C 	CABBB#u	
 	
 	
 	
 	
 	
 !    	*"#/()(=      .%;  G
 'NNN<<>> 		 	 	 	 	 	%s2   .0F   J20C=J--J2K1 1
M/;A)M**M/c                 &    | dk    rdS | dk    rdS dS )zProvide a sorting weight for documentation of property types.

    Weight values ensure 'array' sorted after 'object' which is sorted
    after anything else which remains unsorted.
    array   objectr  r   r.   )values    r0   _sort_property_orderr@    s)     q	(		q1r/   c              #      K   | D ]M}t          |t                    r2t          |t          t          f          st	          |          E d {V  I|V  Nd S rD   )rc   r   r+   rd   _flatten)xsxs     r0   rB  rB    sr        a"" 	:a#u+F+F 	{{""""""""GGGG	 r/   property_dict	multi_keyc                    g }|                      |i           D ]}|                     t                    r|                     d          r4|                    d |                     dg           D                        f|                     d          r|                    |d                    t	          t          |                    S )Nenumc                 L    g | ]!}d t                               ||           d "S ``	_YAML_MAPrr   )r   
enum_values     r0   r   z,_collect_subschema_types.<locals>.<listcomp>  sB       " Cz:>>BBB  r/   r   )rr   r   r   r   r   rB  )rE  rF  property_typesr   s       r0   _collect_subschema_typesrP    s    N"&&y"55 5 5	==(( 	==   	5!! &/mmFB&?&?      ]]6"" 	5!!)F"3444(()))r/   defsc                    t          | |           |                     dg           }t          |t                    s|g}|                     d          rd | d         D             }nq|                     d          r$|                    t          | d                     n8|                     d          r#|                    t          | d                     t          |          dk    r	|d         }n0|                    t                     d		                    |          }|                     d
i           }|                    dg           }t          |t                    s|g}t          |          }t          |                    di           |                    di                     D ]0}t          ||          }|r|dk    r|                    |           1|r[t          |          dk    r| d|d          S |                    t                     dd		                    |           d}	| d|	 S |pdS )zNReturn a string representing a property type from a given
    jsonschema.
    r   rH  c                 L    g | ]!}d t                               ||           d "S rJ  rL  r   ks     r0   r   z&_get_property_type.<locals>.<listcomp>  s?     
 
 
-.(q!$$(((
 
 
r/   r   r   r  r   r   /items	UNDEFINEDz of ())_flatten_schema_refsrr   rc   r   r   rP  lensortr@  rG   rW   r   _get_property_typer   )
rE  rQ  rO  property_typerW  sub_property_typesprune_undefinedsub_itemsub_typesub_property_docs
             r0   r^  r^    s    ---"&&vr22Nnd++ *()   P
 
2?2G
 
 
 
		7	#	# P6}gNNOOOO			7	#	# P6}gNNOOO
>a&q) 455500gr**E62..($// 201-..O%))GR00%))GR2H2HII , ,%h55 	x;66 !!(++++ 8!""a''#@@);A)>@@@$8999>sxx(:;;>>>77%5777'K'r/   c                     |dz  }t          j        dd|           } t          j        dd                    |          |           } t          j        dd                    |          |           } | S )a  Parse description from the meta in a format that we can better
    display in our docs. This parser does three things:

    - Guarantee that a paragraph will be in a single line
    - Guarantee that each new paragraph will be aligned with
      the first paragraph
    - Proper align lists of items

    @param description: The original description in the meta.
    @param prefix: The number of spaces used to align the current description
       z
(\S)\n(\S)z\1 \2z\n\nz\n\n{}z\n( +)-z\n{}-)r   subr<   )r#   r?   list_paragraphs      r0   _parse_descriptionri    sm     aZN&+>>K&)"2"26":":KHHK&HOON33[ K r/   src_cfgc                 H   d| v rD|                      d                              dd          }|                     ||                    d| v rd| d         v rP| d                              d                              dd          }| d                             ||                    d| d         v rY| d         d         D ]J}d|v rD|                     d                              dd          }|                    ||                    Kt          |                     dg           |                     dg           |                     dg                     D ]J}d|v rD|                     d                              dd          }|                    ||                    KdS )	zEFlatten schema: replace $refs in src_cfg with definitions from $defs.z$refz#/$defs/rP   rW  r   r   allOfN)poprl   updater   rr   )rj  rQ  	reference
sub_schemas       r0   r[  r[  +  s   KK''//
B??	tI''''WW%%%(,,V44<<ZLLIG##DO444gg&&&%g.w7 7 7
Z'' *v 6 6 > >z2 N NI%%d9o666GR  GR  GR    / /

 Z"v..66z2FFId9o.../ /r/   c                 n    |                      dd          }|sdS |D ]}|                     |           dS )zFlatten schema: Merge allOf.

    If a schema as allOf, then all of the sub-schemas must hold. Therefore
    it is safe to merge them.
    rl  N)rm  rn  )rj  sub_schemasrp  s      r0   _flatten_schema_all_ofrs  E  sR     ++gt,,K ! # #
z""""# #r/   prop_configc                 ,   |                      dd          }|                      di           }|                      di           }g }g }|rg|                    d          }|                      t                    s|                    |           n"|                    t	          |                     t          ||          D ]}|                     d          s|                     t                    s/|                    |d                             d                     a|                    t           |d                             d                      d                    t          ||                    }|rd| }|d         d	k    r|dz  }|S )
aG  Return accumulated property description.

    Account for the following keys:
    - top-level description key
    - any description key present in each subitem under anyOf or allOf

    Order and deprecated property description after active descriptions.
    Add a trailing stop "." to any description not ending with ":".
    r#   rP   r   r   rj   z.  ri   )rr   rstripr   r   rn   r   rk   rG   )rt  
prop_descrr   r   descriptionsdeprecated_descriptionsrb  r#   s           r0   _get_property_descriptionr|  R  s    33JOOGR((EOOGR((EL  M&&s++
~.. 	M
++++#**+?
+K+KLLL%''  ||M** 	||N++ 	 7 > >s C CDDDD#**$Kh}&=&D&DS&I&IKK    ))E,0GHHIIK '+''r?c!!3Kr/       c                 &    |dz   }g }                      d          du rdS  fddD             } fd|D             }|D ]<}|                                D ]#\  }}	t          |	|           t          |	           |	                     d          du r=t	          |	          }
|	                     d|          }|                    t                              ||t          |
|          t          |	|          	                     |	                     d
          }|r/t          ||           |                     d          s|                     d          rY|                    t                              ||                     |dz  }|                    t          |||                     |                     dg           D ]}|                     d          s|                     d          rY|                    t                              ||                     |dz  }|                    t          |||                     d|	v sd|	v r%|                    t          |	||                     %>d                    |          S )zDReturn restructured text describing the supported schema properties.r}  hiddenTrP   c                 0    g | ]}d vs
|d          v|S )r  r.   r   r   ro   s     r0   r   z%_get_property_doc.<locals>.<listcomp>  s=       6!!Sx0@%@%@ 	%@%@%@r/   )r   patternPropertiesc                 <    g | ]}                     |i           S r.   )rr   r  s     r0   r   z%_get_property_doc.<locals>.<listcomp>  s'    EEE

3++EEEr/   r   )r?   	prop_namer#   	prop_typerW  r   r  )r?   r  )rQ  r?   r   rQ   )rr   rW  r[  rs  r|  r   SCHEMA_PROPERTY_TMPLr<   ri  r^  SCHEMA_LIST_ITEM_TMPL_get_property_docrG   )ro   rQ  r?   
new_prefixr   property_keysproperty_schemasprop_schemaprop_keyrt  r#   r   rW  
alt_schemas   `             r0   r  r  y  s.   &JJzz(t##r   6  M
 FEEE}EEE' 7 7%0%6%6%8%8 6	 6	!Hk d333";///x((D003K@@K  OOGX66E$++!# 2; G G0dCC	 ,      OOG,,E $UD11199\** 	eii8K.L.L 	%%-44#- 5    
 &(J%%)%d:NNN   #())GR"8"8  J!~~l33 z~~+8 8  #))188'1U 9    
 #f,
"))- *j     ++&+55!!%#$z    e6	n ;;z"""r/   metac                 h   |                      d          }|sdS t          }t          |          D ]\  }}t          j        |d                              d          }|t          k    r1|                    dt                              |dz                        |d	                    |          z  }|S )zAReturn restructured text describing the meta examples if present.r%   rP   r}  r   r   r  )
rr   SCHEMA_EXAMPLES_HEADERr   textwrapindentr  insertSCHEMA_EXAMPLES_SPACER_TEMPLATEr<   rG   )r  r%   rst_contentcountexampleindented_liness         r0   _get_examplesr    s    xx
##H r(K#H-- 1 1w!&99??EE000!!299%!)DD   	tyy000r/   c                     |                      d          sdS d                    d | d         D                       }d| dS )Nr'   rP   r=   c              3   "   K   | ]
}d | d V  dS )rK  Nr.   rT  s     r0   r   z3_get_activate_by_schema_keys_doc.<locals>.<genexpr>  s;        
Q


     r/   z**Activate only on keys:** rQ   )rr   rG   )r  schema_keyss     r0    _get_activate_by_schema_keys_docr    sd    88-.. r))   !:;    K ;::::r/   c                    |t                      }| r|st          d          t          |                                           }h d}dh}d}||z
  rd                    ||z
            }n ||z
  |z
  rd                    ||z
            }|rt          |          t          t          |                     }d|d<   |                    d	i           }|                    | d
                   r1|                    | d
         i           }t          t          |          }	 t          ||          |d<   n/# t          $ r" t                              d           d|d<   Y nw xY w|d         r
t          |d<   t          |           |d<   d                    | d                   |d<   t#          j        dd| d                   |d<   t'          |           |d<   t)          j        di |}|S )zReturn reStructured text rendering the provided metadata.

    @param meta: Dict of metadata to render.
    @param schema: Optional module schema, if absent, read global schema.
    @raise KeyError: If metadata lacks an expected key.
    Nz"Expected non-empty meta and schema>   r!   r    r"   r$   r%   r&   r#   r'   rP   z(Missing required keys in module meta: {}z3Additional unexpected keys found in module meta: {}property_header$defsr!   )rQ  property_docz3Unable to render property_doc due to invalid schemar%   r=   r$   rj   -r    title_underbarr.   )r   
ValueErrorsetkeysr<   KeyErrorr   r   rr   r   r  AttributeErrorr   r   SCHEMA_PROPERTY_HEADERr  rG   r   rg  r  SCHEMA_DOC_TMPL)	r  ro   r  required_keysoptional_keyserror_message	meta_copyrQ  templates	            r0   get_meta_docr    sC    ~ ?v ?=>>>tyy{{D  M //MMt 	
BIID 
 
 
		- 
AHH}$  	
  &}%%% Xd^^$$I#%I ::gr""DxxT
 $$t*b))dF##'$5f4$H$H$H	.!! ' ' 'IJJJ$&	.!!!'   >'=	#$)$//Ij99T)_55Ii"$&sDL"A"AI+K, ,I'( %22	22HOs   !D6 6)E"!E"c                      t           j                            t           j                            t                              } t          |           S rD   )r%  r5   dirnameabspath__file__r   )configs_dirs    r0   get_modulesr    s1    '//"'//(";";<<K,,,r/   requested_modulesc                    d}t          t                                                                dgz   }t          |                               t          |                    }|rCt          d                    t          |          d                    |                               |D ]M}d| v s|| v rCt          j	        |dgdg          \  }}|r&t          j
        |d                   }||j        pdz  }N|S )zaLoad module docstrings

    Docstrings are generated on module load. Reduce, reuse, recycle.
    rP   allz+Invalid --docs value {}. Must be one of: {}r=   cloudinit.configr  r   )r   r  valuesr  
differencer   r<   rG   r   find_moduleimport_modulerY   )r  docsall_modulesinvalid_docsmod_namemod_locs_mods           r0   load_docr    s   
 D{}}++--..%8K())44S5E5EFFL 
9@@\""		+&& 	
 	
 	
   * *%%%5F)F)F$0-. MXq  *,Xa[99)r)Kr/   c                      t           j                            t           j                            t           j                            t
                              d          S )Nschemas)r%  r5   rG   r  r  r  r.   r/   r0   get_schema_dirr  7  s4    7<<(A(ABBINNNr/   c                  ^   t           j                            t                      t                    } d}	 t          j        t          |                     }n3# t          $ r&}t          
                    d| |           Y d}~nd}~ww xY w|s!t          
                    d|            i dg d}|S )z?Return jsonschema coalesced from all cc_* cloud-config modules.Nz$Cannot parse JSON schema file %s. %szCNo base JSON schema files found at %s. Setting default empty schemaz'http://json-schema.org/draft-04/schema#)r  z$schemarl  )r%  r5   rG   r  USERDATA_SCHEMA_FILEjsonloadsr   	Exceptionr   r   )schema_filefull_schemar}   s      r0   r   r   ;  s     ',,~//1EFFKKLj;!7!788 L L L:KKKKKKKKKL 

,	
 	
 	
 @
 

 s   !A 
B!BBc                     t                      } t                                                      D ]Q\  }}t          j        |dgdg          \  }}|r/t          j        |d                   }|j        | |j        d         <   R| S )z<Return metadata coalesced from all cc_* cloud-config module.r  r  r   r!   )r   r  rW  r   r  r  r  )	full_metar  r  r  r  s        r0   get_metar  Z  s    I$,,.. 1 1H*)*VH
 
!  	1(!55C(+Ichtn%r/   c                     | st          j        dd          } |                     ddd           |                     dd	d
d           |                     dddd           |                     dd	d
d           | S )z0Return a parser for supported cmdline arguments.cloudconfig-schemaz.Validate cloud-config files or document schema)progr#   z-cz--config-filez.Path of the cloud-config yaml file to validate)helpz--system
store_trueFz)Validate the system cloud-config userdata)actiondefaultr  z-dz--docs+zCPrint schema module docs. Choices: all or space-delimited cc_names.)nargsr  z
--annotatez/Annotate existing cloud-config file with errors)argparseArgumentParseradd_argumentparsers    r0   
get_parserr  g  s     
(%H
 
 
 =    
 8	     )     >	     Mr/   c                    |j         |j        |j        g}t          d |D                       dk    rt	          d           |j        r|j        rt	          d           t                      }|j         s|j        r	 t          |j         ||j                   |j         d}n|j         }t          d|           dS # t          $ r4}|j        s"t	          t          |                     Y d}~dS Y d}~dS d}~wt          $ r'}t	          t          |                     Y d}~dS d}~ww xY w|j        r#t          t          |j                             dS dS )z@Handle provided schema args and perform the appropriate actions.c                     g | ]}||S r.   r.   )r   args     r0   r   z&handle_schema_args.<locals>.<listcomp>  s    111CS1C111r/   r  z;Expected one of --config-file, --system or --docs argumentsz;Invalid flag combination. Cannot use --annotate with --docsNzsystem userdatazValid cloud-config:)config_filer  systemr\  r   r  r   r:  r/  rL   r+   r'  r  )r    argsexclusive_argsr  cfg_namer}   s         r0   handle_schema_argsr    s   &	4;?N
11>11122a77KLLL} M MKLLL,,K #4; #	3% +t}   ',+'22222 % 	 	 	= c!ff      	 	 	#a&&MMMMMMMMM	 
 #hty!!"""""# #s$   ;B9 9
D&#C22D&?D!!D&c                  f    t                      } t          d|                                            dS )zDTool to validate schema of a cloud-config file or print schema docs.r  r   )r  r  
parse_argsr  s    r0   mainr    s-    \\F+V->->-@-@AAA1r/   __main__rD   )T)NFFTF)F)r}  )grY   r  r  loggingr%  r   sysr  collectionsr   collections.abcr   copyr   	functoolsr   	itertoolsr   r-   r   r	   r
   r   r   r   r   r2  	cloudinitr   r   cloudinit.stagesr   cloudinit.utilr   r   r   r_   r   _ValidationErrorr`   r  	getLoggerr(   r   VERSIONED_USERDATA_SCHEMA_FILEr  rM  r.  r  r  r  r  r  r  r   rk   typing_extensionsr   r   r   r   r2   r4   rZ   r+   rJ   r  rL   rg   rn   rW   rv   r   r   r   r   r   r   rd   r  r:  r@  rB  rP  r^  ri  r[  rs  r|  r  r  r  r  r  r   r  r  r   r  r  r  r  exitr.   r/   r0   <module>r      s   L L    				 				 



  # # # # # # $ $ $ $ $ $                   O O O O O O O O O O O O O O O O O O  ( ( ( ( ( ( ( ( ! ! ! ! ! ! A A A A A A A A A A >>>>>>&OO      OOO  	%%%g!!!D  5 '88	& $ . L N  . "> " 
  MMM888888888 8 8 8 8Y 8 8 8 8 J	 	 	 	 	_ 	 	 	. . . . .J . . . m$ !		 	 	#	 SM	 		
 		 	 	 	 (  (  (  (  (J  (  (  (F( ( (7 7hsm 7s 7 7 7 7 #9  	
 Y   . #9!  ! 
 Y!  !  !  ! R #9,$ ,$
 Y,$ ,$ ,$ ,$^G/ G/ G/T 
  
t  
  
  
  
J #"J JJJ J 	J
 J J J J JZr, r, r, r, r, r, r, r,t /348   
 N+ ".1 3:   0g g g gT
 
 
  *D *S *T#Y * * * *")(d )($ )(3 )( )( )( )(Xs    ,/$ /d / / / /4
#D 
# 
# 
# 
#$4 $C $ $ $ $NE# E#d E#$ E## E# E# E# E#P
 s     ;: ;# ; ; ; ;; ;z ;8D> ;S ; ; ; ;|-T - - - -
     4O O O O OD    >
$ 
 
 
 
! ! ! !H# # #8   zCHTTVV s   ,A5 5A?>A?