%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /snap/core/17212/usr/lib/python3/dist-packages/cloudinit/config/__pycache__/
Upload File :
Create Path :
Current File : //snap/core/17212/usr/lib/python3/dist-packages/cloudinit/config/__pycache__/schema.cpython-35.pyc



"�T`6L�@s�dZddlmZddlmZddlmZmZddlZddl	m
Z
ddlmZddl
Z
ddlZddlZddlZddlZdd	d
dddiZd
ZdZdZdZdZdZdZGdd�de�Zdd�Zd
dd�Zdd�Zd
dd�Zdd�Z d d!�Z!d"d#�Z"d$d%d&�Z#d'd(d)�Z$d*d+�Z%da&d,d-�Z'd.d/�Z(dd0d1�Z)d2d3�Z*d4d5�Z+e,d6kr�ej-e+��dS)7zFschema.py: Set of module functions for processing cloud-config schema.�)�read_cfg_paths)�importer)�find_modules�	load_fileN)�defaultdict)�deepcopyT�trueF�falseZnulls	UNDEFINEDs
#cloud-configz�
{name}
{title_underbar}
**Summary:** {title}

{description}

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

**Module frequency:** {frequency}

**Supported distros:** {distros}

**Config schema**:
{property_doc}
{examples}
z/{prefix}**{prop_name}:** ({type}) {description}zF{prefix}Each item in **{prop_name}** list supports the following keys:z
**Examples**::

z
    # --- Example{0} ---cs+eZdZdZf�fdd�Z�S)�SchemaValidationErrorz<Raised when validating a cloud-config file against a schema.csN||_dd�|D�}djdj|��}tt|�j|�dS)z�Init the exception an n-tuple of schema errors.

        @param schema_errors: An n-tuple of the format:
            ((flat.config.key, msg),)
        cSs(g|]\}}dj||��qS)z{0}: {1})�format)�.0Z
config_key�message�r�9/usr/lib/python3/dist-packages/cloudinit/config/schema.py�
<listcomp>7s	z2SchemaValidationError.__init__.<locals>.<listcomp>zCloud config schema errors: {0}z, N)�
schema_errorsr�join�superr
�__init__)�selfrZerror_messagesr
)�	__class__rrr/s		
zSchemaValidationError.__init__)�__name__�
__module__�__qualname__�__doc__rrr)rrr
,sr
cCsRyddlm}Wntk
r,dSYnX|jj|d�pQt|tf�S)zWTYPE_CHECKER override allowing bytes for string type

    For jsonschema v. 3.0.0+
    r)�Draft4ValidatorF�string)�
jsonschemar�ImportError�TYPE_CHECKERZis_type�
isinstance�bytes)Zchecker�instancerrrr�is_schema_byte_string>s
	r#cCs�y0ddlm}m}ddlm}m}Wn#tk
rUtjd�dSYnXt	|d�r�|j
jdt�}||d|�}n@|j
}	ttf|	d<|d	|jd
|jddd
|	�}||d|��}
f}x^t|
j|�ddd��D];}djdd�|jD��}
||
|jff7}qW|r�|rft|��n,dd�|D�}tjddj|��dS)a�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.
    r)r�
FormatChecker)�create�extendz<Ignoring schema validation. python-jsonschema is not presentNrr�type_checkerZmeta_schemaZ
validators�versionZdraft4Z
default_typesZformat_checker�keycSs|jS)N)�path)�errr�<lambda>tsz-validate_cloudconfig_schema.<locals>.<lambda>�.cSsg|]}t|��qSr)�str)r�prrrrus	z/validate_cloudconfig_schema.<locals>.<listcomp>cSs(g|]\}}dj||��qS)z{0}: {1})r)r�k�msgrrrr{s	zInvalid config:
%s�
)rrr$Zjsonschema.validatorsr%r&r�logging�debug�hasattrrZredefiner#Z
DEFAULT_TYPESr.r!ZMETA_SCHEMAZ
VALIDATORS�sortedZiter_errorsrr*r
r
Zwarning)�config�schema�strictrr$r%r&r'ZcloudinitValidator�typesZ	validator�errors�errorr*Zmessagesrrr�validate_cloudconfig_schemaKs8

						(r=cCs�|s
|Si}|r%t||�}tt�}g}g}x�|D]�\}}tjd|�}	|	r�|	j�\}
}|t|
�j|�nd}|||j|�|dk	rDdjd|
d|d|�}qDW|j	�j
d�}d}
x�t|d�D]�\}}
||}|r�g}xG|D]?}|jd	j|
��|jd
j|
|��|
d7}
q1W|j|
ddj|��q|j|
�qW|jd
jdj|���dj|�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 schema_errors: List of tuples from a JSONSchemaValidationError. The
        tuples consist of (schemapath, error_message).
    z&format-l(?P<line>\d+)\.c(?P<col>\d+).*NzLine {line} column {col}: {msg}�line�colr1r2�zE{0}z# E{0}: {1}z		# �,z# Errors: -------------
{0}

)
�_schemapath_for_cloudconfigr�list�re�match�groups�int�appendr�decode�split�	enumerater)�cloudconfig�original_contentrZschemapathsZerrors_by_lineZerror_footerZannotated_contentr*r1rEr>r?�linesZerror_index�line_numberr;Zerror_labelr<rrr�annotated_cloudconfig_filesD		

!rPc
Cs�|dkrWtj�dkr*td��t�}|jd�}t|dd�}n9tjj|�s~tdj|���t|dd�}|j	t
�s�dd	j|t
j��ff}t|�}|r�t
ti||j��|�ytj|�}Wntjk
r"}	z�d
}
}d}t|	d�r\t|	d�r\t|	d�}n-t|	d�r�t|	d�r�t|	d�}|r�|jd
}
|jd
}d
jd|
d|�dj|t|	��ff}t|�}|rt
ti||j��||	�WYdd}	~	XnXyt||dd�WnGtk
r�}	z'|rnt
t|||	j���WYdd}	~	XnXdS)aValidate 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.
    Nrz?Unable to read system userdata as non-root user. Try using sudoZuserdata_rawrIFzConfigfile {0} does not existzformat-l1.c1z"File {0} needs to begin with "{1}"r@Zcontext_markZproblem_markzformat-l{line}.c{col}r>r?zFile {0} is not valid yaml. {1}r9T)�os�getuid�RuntimeErrorrZ
get_ipath_currr*�existsr�
startswith�CLOUD_CONFIG_HEADERrIr
�printrPr�yaml�	safe_loadZ	YAMLErrorr5�getattrr>�columnr.r=)
Zconfig_pathr8�annotate�pathsZuser_data_fileZcontentr;r<rLr+r>r[Zmarkrrr�validate_cloudconfig_file�s\			


r^cCs�|j�jd�}i}d}d}g}x�t|d�D]�\}}ttj||�j�d�}	|j�}|s=|jd�r�q=|r�|d\}
}nd}
d}|jd�rwd|d}|r�|j	|�r�|d	t|��}t
|�}
|||
<ttj||dd	��j�d�}|d7}|	}
|	|7}	||d	�}|d7}nd}|jd
d�\}
}|r�|d|
}
xr|	|
kr|r|j�\}
}|dkr|	|
krdj|jd�d	d��}Pq�d}
d}q�W|j
|	|
f�|r�|j�}|jd�r�|j
|	d
|
df�xDtdttj|���D]$}|
dt
|�}|||<q�W|||
<q=W|S)z�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
    r2rz^(\s*)r@�#�z- z.%dN�:r-�[�z.0���rdrdrd)rIrJrK�lenrDrErF�striprU�endswithr.�poprrH�rangerXrY)r7rMZ
content_linesZschema_line_numbersZ
list_indexZRE_YAML_INDENTZscopesrOr>Zindent_depthZprevious_depthZpath_prefixZprevious_list_idxr)Zitem_indent�valueZinner_list_indexZlist_keyrrrrB�s`"
,


"
%rBcCs�|jdt�}|tkrD|jd�rDdd�|dD�}t|t�rbdj|�}|jdi�}|jdd�}xB|jdi�D].}|r�|d7}|d	t|�d
7}q�W|r�dj||�S|S)zEReturn a string representing a property type from a given jsonschema.�typeZenumcSs(g|]}ttj||���qSr)r.�	_YAML_MAP�get)rr0rrrr5s	z&_get_property_type.<locals>.<listcomp>�/�itemsr`ZoneOf�(�)z
{0} of {1})rm�SCHEMA_UNDEFINEDr rCr�_get_property_typer)Z
property_dictZ
property_typeroZsub_property_typeZsub_itemrrrrs0s
rscCs_|d}tjdd|�}tjddj|�|�}tjddj|�|�}|S)a�Parse description from the schema 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 schema.
    @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{}-)rD�subr)�description�prefixZlist_paragraphrrr�_parse_descriptionDs
rxz    c	
Csh|d}g}xH|jdi�j�D].\}}|jdd�}|jtjd|d|dt|�dt||���|jd�}|r2t|t�r�x�|D]}|jt	|d|��q�Wn`t|t
�r2|jd�r2|jtjd|d|��|d7}|jt	|d|��d|kr)|jt	|d|��q)Wd	j|�S)
zDReturn restructured text describing the supported schema properties.z    �
propertiesrvr`rwZ	prop_namerkroz

)
rmrorH�SCHEMA_PROPERTY_TMPLrrsrxr rC�_get_property_doc�dict�SCHEMA_LIST_ITEM_TMPLr)	r8rwZ
new_prefixryZprop_keyZprop_configrvro�itemrrrr{Zs0
%

r{r`cCs�|jd�}|sdSt}xxt|�D]j\}}|jd�}dd�|D�}|tkr�|jdtj|d��|dj|�7}q,W|S)zCReturn restructured text describing the schema examples if present.�examplesr`r2cSsg|]}dj|��qS)z    {0})r)rr>rrrr�s	z(_get_schema_examples.<locals>.<listcomp>rr@)rm�SCHEMA_EXAMPLES_HEADERrKrJ�insert�SCHEMA_EXAMPLES_SPACER_TEMPLATErr)r8rwrZrst_content�countZexamplerNZindented_linesrrr�_get_schema_examplesxsr�cCsmt|�}t|�|d<t|�|d<dj|d�|d<tjdd|d�|d<tj|�S)	z�Return reStructured text rendering the provided jsonschema.

    @param schema: Dict of jsonschema to render.
    @raise KeyError: If schema lacks an expected key.
    Zproperty_docrz, Zdistrosr-�-�nameZtitle_underbar)rr{r�rrDru�SCHEMA_DOC_TMPLr)r8Zschema_copyrrr�get_schema_doc�sr�cCs�tr
tSdddddgi}tjjtjjt��}t|�}xh|j�D]Z\}}tj	|dgdg�\}}|rYtj
|d�}|dj|j�qYW|a|S)	z>Return jsonschema coalesced from all cc_* cloud-config module.z$schemaz'http://json-schema.org/draft-04/schema#�idzcloud-config-schema�allOfzcloudinit.configr8r)
�FULL_SCHEMArQr*�dirname�abspath�__file__rror�find_module�
import_modulerHr8)�full_schemaZconfigs_dirZpotential_handlersZ_fname�mod_nameZmod_locsZ_looked_locs�modrrr�
get_schema�sr�cCs$t|dtj�tjd�dS)N�filer@)rW�sys�stderr�exit)r
rrrr<�sr<cCs�|stjdddd�}|jdddd�|jd	d
ddd
dd�|jdddddd�|jdd
ddd
dd�|S)z0Return a parser for supported cmdline arguments.�progzcloudconfig-schemarvz.Validate cloud-config files or document schemaz-cz
--config-file�helpz.Path of the cloud-config yaml file to validatez--system�action�
store_true�defaultFz)Validate the system cloud-config userdataz-dz--docs�nargs�+zCPrint schema module docs. Choices: all or space-delimited cc_names.z
--annotatez/Annotate existing cloud-config file with errors)�argparse�ArgumentParser�add_argument)�parserrrr�
get_parser�s		r�c	Cs�|j|j|jg}tdd�|D��dkrAtd�t�}|js\|jryt|j||j�Wnrtk
r�}z|js�tt	|��WYdd}~Xq�t
k
r�}ztt	|��WYdd}~Xq�X|jdkrd}n	|j}td|�n�|jr�dd�|d	D�}|d
g7}t|j�j
t|��}|r�tdjt|�dj|���xD|d	D]8}d
|jks�|d
|jkr�tt|��q�WdS)z@Handle provided schema args and perform the appropriate actions.cSsg|]}|r|�qSrr)r�argrrrr�s	z&handle_schema_args.<locals>.<listcomp>r@z;Expected one of --config-file, --system or --docs argumentsNzsystem userdatazValid cloud-config:cSsg|]}|d�qS)r�r)r�	subschemarrrr�s	r��allz-Invalid --docs value {0}. Must be one of: {1}z, r�)Zconfig_fileZdocs�systemrer<r�r^r\r
r.rSrW�set�
differencerrCrr�)	r��argsZexclusive_argsr�r+Zcfg_nameZ
schema_idsZinvalid_docsr�rrr�handle_schema_args�s6
		"#			
	"r�cCs t�}td|j��dS)zDTool to validate schema of a cloud-config file or print schema docs.zcloudconfig-schemar)r�r��
parse_args)r�rrr�main�s	r��__main__).rZcloudinit.cmd.develrZ	cloudinitrZcloudinit.utilrrr��collectionsr�copyrr3rQrDr�rXrlrrrVr�rzr}r�r��
ValueErrorr
r#r=rPr^rBrsrxr{r�r�r�r�r<r�r�r�rr�rrrr�<module>sL
4/C?!

Zerion Mini Shell 1.0