
    *Ce                        d dl mZ d dlmZ d dlZddlmZmZ  G d de      Z G d d	e      Z	 G d
 de      Z
 G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z ed        Z ed! ej.                  e	j0                  ee	j2                  eiej4                  e
j6                  ee
j8                  eiej:                   ed"       ej<                  ej>                  eej@                  eiejB                  ejD                  eiejF                  ejH                  eii      Z%d# Z&d$e'fd%Z(y)&    )defaultdict)IntEnumN   )ffilibc                   $    e Zd ZdZdZdZdZdZdZy)NETPLAN_ERROR_DOMAINSr                  N)	__name__
__module____qualname__NETPLAN_PARSER_ERRORNETPLAN_VALIDATION_ERRORNETPLAN_FILE_ERRORNETPLAN_BACKEND_ERRORNETPLAN_EMITTER_ERRORNETPLAN_FORMAT_ERROR     0/usr/lib/python3/dist-packages/netplan/_utils.pyr	   r	      s&     r   r	   c                       e Zd ZdZdZy)NETPLAN_PARSER_ERRORSr   r   N)r   r   r   NETPLAN_ERROR_INVALID_YAMLNETPLAN_ERROR_INVALID_CONFIGr   r   r   r   r   $   s    !"#$ r   r   c                       e Zd ZdZdZy)NETPLAN_VALIDATION_ERRORSr   r   N)r   r   r   NETPLAN_ERROR_CONFIG_GENERICNETPLAN_ERROR_CONFIG_VALIDATIONr   r   r   r    r    )   s    #$ &'#r   r    c                       e Zd ZdZdZy)NETPLAN_BACKEND_ERRORSr   r   N)r   r   r   NETPLAN_ERROR_UNSUPPORTEDNETPLAN_ERROR_VALIDATIONr   r   r   r$   r$   .   s     ! r   r$   c                       e Zd ZdZy)NETPLAN_EMITTER_ERRORSr   N)r   r   r   NETPLAN_ERROR_YAML_EMITTERr   r   r   r(   r(   3   s    !"r   r(   c                       e Zd ZdZy)NETPLAN_FORMAT_ERRORSr   N)r   r   r   !NETPLAN_ERROR_FORMAT_INVALID_YAMLr   r   r   r+   r+   7   s    ()%r   r+   c                       e Zd ZddZd Zy)NetplanExceptionNc                 .    || _         || _        || _        y N)domainerrormessage)selfr3   r1   r2   s       r   __init__zNetplanException.__init__<   s    
r   c                     | j                   S r0   )r3   r4   s    r   __str__zNetplanException.__str__A   s    ||r   NNN)r   r   r   r5   r8   r   r   r   r.   r.   ;   s    
r   r.   c                       e Zd Zed        Zy)NetplanFileExceptionc                     | j                   S r0   )r2   r7   s    r   errnozNetplanFileException.errnoF   s    zzr   N)r   r   r   propertyr=   r   r   r   r;   r;   E   s     r   r;   c                   (     e Zd ZdZdZd fd	Z xZS )NetplanValidationExceptiona  
    Netplan Validation errors are expected to contain the YAML file name
    from where the error was found.

    A validation error might happen after the parsing stage. libnetplan walks
    through its internal representation of the network configuration and checks
    if all the requirements are met. For example, if it finds that the key
    "set-name" is used by an interface, it will check if "match" is present.
    As "set-name" requires "match" to work, it will emit a validation error
    if it's not found.
    z((?P<file_path>.*\.yaml): (?P<message>.*)c                     t         |   |||       t        j                  | j                  |      }|st        d|       |d   | _        |d   | _        y )Nz@The validation error message does not have the expected format: 	file_pathr3   )superr5   rematch!SCHEMA_VALIDATION_ERROR_MSG_REGEX
ValueErrorfilenamer3   r4   r3   r1   r2   schema_error	__class__s        r   r5   z#NetplanValidationException.__init__\   sZ    &%0xx F FP_`g_hijj$[1#I.r   r9   )r   r   r   __doc__rF   r5   __classcell__rK   s   @r   r@   r@   K   s    
 8 &	/ 	/r   r@   c                   (     e Zd ZdZdZd fd	Z xZS )NetplanParserExceptiona  
    Netplan Parser errors are expected to contain the YAML file name
    and line and column numbers from where the error was found.

    A parser error might happen during the parsing stage. Parsing errors
    might be due to invalid YAML files or invalid Netplan grammar. libnetplan
    will check for this kind of issues while it's walking through the YAML
    files, so it has access to the location where the error was found.
    zN(?P<file_path>.*):(?P<error_line>\d+):(?P<error_col>\d+): (?P<message>(\s|.)*)c                     t         |   |||       t        j                  | j                  |      }|st        d|       |d   | _        |d   | _        |d   | _        |d   | _	        y )Nz<The parser error message does not have the expected format: rB   
error_line	error_colr3   )
rC   r5   rD   rE   SCHEMA_PARSER_ERROR_MSG_REGEXrG   rH   linecolumnr3   rI   s        r   r5   zNetplanParserException.__init__w   st    &%0 xx B BGL[\c[deff$[1 .	";/#I.r   r9   )r   r   r   rL   rT   r5   rM   rN   s   @r   rP   rP   h   s     ^ "/ /r   rP   c                       e Zd Zy)NetplanBackendExceptionNr   r   r   r   r   r   rX   rX          r   rX   c                       e Zd Zy)NetplanEmitterExceptionNrY   r   r   r   r\   r\      rZ   r   r\   c                       e Zd Zy)NetplanFormatExceptionNrY   r   r   r   r^   r^      rZ   r   r^   c                      t         S r0   )r.   r   r   r   <lambda>r`      s    2B r   c                      t         S r0   )NETPLAN_EXCEPTIONS_FALLBACKr   r   r   r`   r`      s    )D r   c                      t         S r0   )r;   r   r   r   r`   r`      s    FZ r   c                 `  	 t        j                  d      }t         | g ||       }|s|d   		t         j                  k(  rt	        ddd      t        j                  	      }|dz	  }t        t        j                  d|            }t        	fd      }t        |   |   } ||||      |S )NzNetplanError **r   zUnknown error    uint32_tc                 D    t        j                  | t        |             S r0   )r   netplan_error_messagelen)berrs    r   r`   z#_checked_lib_call.<locals>.<lambda>   s    @Y@YZ]_`befgbh@i r   )r   newboolNULLr.   r   netplan_error_codeintcast_string_realloc_call_no_errorNETPLAN_EXCEPTIONS)
fnargsrefretdomain_codeerror_domain
error_codeerror_message	exceptionrk   s
            @r   _checked_lib_callr}      s    
''#
$C
r~4~~
C!f#((?"?Aq99,,S1"b(*k:;
56ij&|4Z@	|Z@@Jr   functionc                     d}|dk  rft        j                  d|      } | |      }|dk(  r|dz  }.|dk  rt        d|z        |dk(  ry t        j                  |      j	                  d      S t        d	      )
N   i   zchar[]r
   r   zUnknown error: %dzutf-8z&Halting due to string buffer size > 1M)r   rl   r.   stringdecode)r~   sizebufcodes       r   rr   rr      s    D
.ggh%}2:!8D!8"#6#=>>QY::c?))'22
C
DDr   ))collectionsr   enumr   rD   _netplan_cffir   r   r	   r   r    r$   r(   r+   	Exceptionr.   r;   r@   rP   rX   r\   r^   rb   r   r   r   r   r!   r"   r   r   r%   r&   r   r)   r   r,   rs   r}   callablerr   r   r   r   <module>r      s    $  	 #G %G %
( (
!W !
#W #*G *y + /!1 /:/- /B	. 		. 		- 	
 **BC  !!D22!<<>T!>>@V5
 	66%BBDT%EEGa9 	00+>Z2[33"<<>U";;=T6
 	33"==?V6 	22!CCE[5/G
  : EH Er   