Current File : //lib/python2.7/site-packages/cloudinit/config/schema.pyc
�
oB�]c@s�dZddlmZddlmZddlmZmZddlZddl	m
Z
ddlmZddl
Z
ddlZddlZddlZddlZide6d	e6d
d6ZdZdZd
ZdZdZdZdefd��YZed�Zd�Zed�Z d�Z!d�Z"dd�Z#dd�Z$d�Z%da&d�Z'd�Z(dd�Z)d �Z*d!�Z+e,d"kr�ej-e+��ndS(#sFschema.py: Set of module functions for processing cloud-config schema.i����(tprint_function(timporter(tfind_modulest	load_fileN(tdefaultdict(tdeepcopyttruetfalsetnullt	UNDEFINEDs
#cloud-configs�
{name}
{title_underbar}
**Summary:** {title}

{description}

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

**Module frequency:** {frequency}

**Supported distros:** {distros}

**Config schema**:
{property_doc}
{examples}
s/{prefix}**{prop_name}:** ({type}) {description}s
**Examples**::

s
    # --- Example{0} ---tSchemaValidationErrorcBseZdZdd�ZRS(s<Raised when validating a cloud-config file against a schema.cCsf||_g|D]\}}dj||�^q}djdj|��}tt|�j|�dS(s�Init the exception an n-tuple of schema errors.

        @param schema_errors: An n-tuple of the format:
            ((flat.config.key, msg),)
        s{0}: {1}sCloud config schema errors: {0}s, N(t
schema_errorstformattjointsuperR
t__init__(tselfRt
config_keytmessageterror_messages((s;/usr/lib/python2.7/site-packages/cloudinit/config/schema.pyR.s	(((t__name__t
__module__t__doc__R(((s;/usr/lib/python2.7/site-packages/cloudinit/config/schema.pyR
+sc
Cs'yddlm}m}Wntk
r;tjd�dSX||d|��}d}xgt|j|�dd��D]G}djg|j	D]}t
|�^q��}	||	|jff7}qvW|r#|r�t|��q#g|D]\}
}d	j
|
|�^q�}tjd
dj|��ndS(
s�Validate provided config meets the schema definition.

    @param config: Dict of cloud configuration settings validated against
        schema.
    @param schema: jsonschema dict describing the supported schema definition
       for the cloud config module (config.cc_*).
    @param strict: Boolean, when True raise SchemaValidationErrors instead of
       logging warnings.

    @raises: SchemaValidationError when provided config does not validate
        against the provided schema.
    i����(tDraft4Validatort
FormatCheckers<Ignoring schema validation. python-jsonschema is not presentNtformat_checkertkeycSs|jS(N(tpath(te((s;/usr/lib/python2.7/site-packages/cloudinit/config/schema.pyt<lambda>Rst.s{0}: {1}sInvalid config:
%ss
((t
jsonschemaRRtImportErrortloggingtdebugtsortedtiter_errorsR
RtstrRR
Rtwarning(
tconfigtschematstrictRRt	validatorterrorsterrortpRtktmsgtmessages((s;/usr/lib/python2.7/site-packages/cloudinit/config/schema.pytvalidate_cloudconfig_schema=s 

%++cCs�|s
|Si}|r(t||�}ntt�}d}g}g}x�|D]�\}}	tjd|�}
|
r�|
j�\}}|t|�j|	�nd}|||j|	�|dk	r�dj	d|d|d|	�}	n|jdj	||	��|d7}qMW|j
�jd�}
d}x�t|
�D]�\}}||d}|r�d	j
gtd
t|��D]}dj	||�^qp�}|t|�7}|j|d|�q4|j|�q4W|jd
j	dj
|���dj
|�S(s|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 schema_errors: List of tuples from a JSONSchemaValidationError. The
        tuples consist of (schemapath, error_message).
    is&format-l(?P<line>\d+)\.c(?P<col>\d+).*sLine {line} column {col}: {msg}tlinetcolR/s# E{0}: {1}s
t,isE{0}s		# s# Errors: -------------
{0}

N(t_schemapath_for_cloudconfigRtlisttretmatchtgroupstinttappendtNoneRtdecodetsplitt	enumerateR
trangetlen(tcloudconfigtoriginal_contentRtschemapathsterrors_by_lineterror_countterror_footertannotated_contentRR/R8R2R3tlinestline_numberR+tcountterror_label((s;/usr/lib/python2.7/site-packages/cloudinit/config/schema.pytannotated_cloudconfig_file]sH		5cCstjj|�s*tdj|���nt|dt�}|jt�s�ddj|tj	��ff}t
|�}|r�tti||j
��n|�nytj|�}Wntjk
r�}d}}	d
}
t|d�rt|d�rt|d�}
n0t|d�r;t|d�r;t|d�}
n|
r^|
jd}|
jd}	ndjd	|d
|	�dj|t|��ff}t
|�}|r�tti||j
��n|�nXyt||dt�Wn8t
k
r}|rtt|||j
��n�nXd
S(s�Validate cloudconfig file adheres to a specific jsonschema.

    @param config_path: Path to the yaml cloud-config file to parse.
    @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.
    sConfigfile {0} does not existR=sformat-l1.c1s"File {0} needs to begin with "{1}"itcontext_marktproblem_marksformat-l{line}.c{col}R2R3sFile {0} is not valid yaml. {1}R)N(tosRtexiststRuntimeErrorRRtFalset
startswithtCLOUD_CONFIG_HEADERR=R
tprintRMRtyamlt	safe_loadt	YAMLErrorR<thasattrtgetattrR2tcolumnR%R1tTrue(tconfig_pathR(tannotatetcontentR+R,RBRR2R\tmark((s;/usr/lib/python2.7/site-packages/cloudinit/config/schema.pytvalidate_cloudconfig_file�sJ		


cCs|j�jd�}i}d}d}g}x�t|d�D]�\}}ttj||�j�d�}	|j�}|s=|jd�r�q=n|r�|d\}
}nd}
d}|jd�r�t	|�}|d}
|d7}nd}|jd	d�\}}
x7|	|
krA|r2|j
�\}
}qd}
d}qW|rY|d
|}n|j|	|f�|
r�|
j�}
|
jd�r�|j|	d|d
f�xGtdtt
j|
���D]$}|d
t	|�}|||<q�Wq�n|||<q=W|S(s�Return a dictionary mapping schemapath to original_content line number.

    @param config: The yaml.loaded config dictionary of a cloud-config file.
    @param original_content: The simple file content of the cloud-config file
    s
is^(\s*)it#i����ts- t:Rt[is.0(R=R>R?RAR7R8R9tstripRTR%tpopR;R@RWRX(R'RCt
content_linestschema_line_numberst
list_indextRE_YAML_INDENTtscopesRJR2tindent_depthtprevious_depthtpath_prefixRtvaluetinner_list_indextlist_key((s;/usr/lib/python2.7/site-packages/cloudinit/config/schema.pyR5�sJ"


%cCs|jdt�}|tkr_|jd�r_g|dD]}ttj||��^q8}nt|t�r�dj|�}n|jdi�}|jdd�}xE|jdi�D]1}|r�|d7}n|dt|�d7}q�W|rd	j||�S|S(
sEReturn a string representing a property type from a given jsonschema.ttypetenumt/titemsRdtoneOft(t)s
{0} of {1}(	tgettSCHEMA_UNDEFINEDR%t	_YAML_MAPt
isinstanceR6R
t_get_property_typeR(t
property_dictt
property_typeR.Rwtsub_property_typetsub_item((s;/usr/lib/python2.7/site-packages/cloudinit/config/schema.pyR�s2
s    c
Cs�|d}g}x�|jdi�j�D]�\}}|jdd�}|jtjd|d|dt|�d|jdd���d|kr)|jt|d|��q)q)Wd	j|�S(
sDReturn restructured text describing the supported schema properties.s    t
propertiestdescriptionRdtprefixt	prop_nameRts
s

(	R{RwR;tSCHEMA_PROPERTY_TMPLRRtreplacet_get_property_docR
(R(R�t
new_prefixR�tprop_keytprop_configR�((s;/usr/lib/python2.7/site-packages/cloudinit/config/schema.pyR�s
%Rdc	Cs�|jd�}|sdSt}x�t|�D]|\}}|jd�}g|D]}dj|�^qN}|tkr�|jdtj|d��n|dj|�7}q,W|S(sCReturn restructured text describing the schema examples if present.texamplesRds
s    {0}ii(R{tSCHEMA_EXAMPLES_HEADERR?R>RtinserttSCHEMA_EXAMPLES_SPACER_TEMPLATER
(	R(R�R�trst_contentRKtexampleRIR2tindented_lines((s;/usr/lib/python2.7/site-packages/cloudinit/config/schema.pyt_get_schema_exampless"cCsmt|�}t|�|d<t|�|d<dj|d�|d<tjdd|d�|d<tj|�S(	s�Return reStructured text rendering the provided jsonschema.

    @param schema: Dict of jsonschema to render.
    @raise KeyError: If schema lacks an expected key.
    tproperty_docR�s, tdistrosRt-tnamettitle_underbar(RR�R�R
R7tsubtSCHEMA_DOC_TMPLR(R(tschema_copy((s;/usr/lib/python2.7/site-packages/cloudinit/config/schema.pytget_schema_doc)scCs�tr
tSidd6dd6gd6}tjjtjjt��}t|�}xk|j�D]]\}}tj	|dgdg�\}}|r\tj
|d�}|dj|j�q\q\W|a|S(	s>Return jsonschema coalesced from all cc_* cloud-config module.s'http://json-schema.org/draft-04/schema#s$schemascloud-config-schematidtallOfscloudinit.configR(i(
tFULL_SCHEMARPRtdirnametabspatht__file__RRwRtfind_modulet
import_moduleR;R((tfull_schematconfigs_dirtpotential_handlerst_fnametmod_nametmod_locst_looked_locstmod((s;/usr/lib/python2.7/site-packages/cloudinit/config/schema.pyt
get_schema;scCs$t|dtj�tjd�dS(Ntfilei(RVtsyststderrtexit(R((s;/usr/lib/python2.7/site-packages/cloudinit/config/schema.pyR,Psc	Cs||s!tjdddd�}n|jdddd�|jd	d
ddd
tdd�|jdddd
tdd�|S(s0Return a parser for supported cmdline arguments.tprogscloudconfig-schemaR�s.Validate cloud-config files or document schemas-cs
--config-filethelps.Path of the cloud-config yaml file to validates-ds--doctactiont
store_truetdefaultsPrint schema documentations
--annotates/Annotate existing cloud-config file with errors(targparsetArgumentParsertadd_argumentRS(tparser((s;/usr/lib/python2.7/site-packages/cloudinit/config/schema.pyt
get_parserUs	cCs|j|jg}t|�s+t|�r8td�nt�}|jr�yt|j||j�WnQtk
r�}|js�tt	|��q�q�t
k
r�}tt	|��q�Xtdj|j��n|jrx%|dD]}tt
|��q�WndS(s@Handle provided schema args and perform the appropriate actions.s/Expected either --config-file argument or --docsValid cloud-config file {0}R�N(tconfig_filetdoctanytallR,R�RbR_R
R%RRRVRR�(R�targstexclusive_argsR�Rt	subschema((s;/usr/lib/python2.7/site-packages/cloudinit/config/schema.pythandle_schema_argsds"
				cCs t�}td|j��dS(sDTool to validate schema of a cloud-config file or print schema docs.scloudconfig-schemai(R�R�t
parse_args(R�((s;/usr/lib/python2.7/site-packages/cloudinit/config/schema.pytmainzs	t__main__(.Rt
__future__Rt	cloudinitRtcloudinit.utilRRR�tcollectionsRtcopyRR!RPR7R�RWR]RSR<R}R|RUR�R�R�R�t
ValueErrorR
R1RMRbR5RR�R�R�R�R�R,R�R�R�RR�(((s;/usr/lib/python2.7/site-packages/cloudinit/config/schema.pyt<module>sF 	13	1