§
    ôc+  ã                   ó°   — 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mZ dZ	dZ
d„ Zdd„Zd	„ Zd
„ Zd„ Zdd„Zdd„ZddhZh d£ZdZdd„ZdZd„ Zd„ Z	 dd„ZdS )é    N)Úyaml)ÚloadÚ	normalizeé   é   c           	      óÖ   — | j         j        |         d                              dt          j                             dt          j                             d¦  «        d¦  «        ¦  «         dS )z‚
    Using some ruamel.yaml black magic, insert a blank line in the config right before the given
    field and its comments.
    é   r   ú
N)ÚcaÚitemsÚinsertr   ÚtokensÚCommentTokenÚerrorÚCommentMark)ÚconfigÚ
field_names     ú;/usr/lib/python3/dist-packages/borgmatic/config/generate.pyÚ_insert_newline_before_commentr      s]   € ð
 „I„O�JÔ Ô"×)Ò)Ø	�4Œ;×#Ò# D­$¬*×*@Ò*@ÀÑ*CÔ*CÀTÑJÔJñô ð ð ð ó    Fc                 ó>  ‡— |                       d¦  «        }|                       d¦  «        }|�|S |dk    rQt          j                             t	          | d         ‰d¬¦  «        g¦  «        }t          || ‰t          z  ¬¦  «         n–|d	k    rmt          j                             ˆfd
„| d                              ¦   «         D ¦   «         ¦  «        }‰t          z  |rt          ndz   }t          || ||¬¦  «         n#t          d                     ‰| ¦  «        ¦  «        ‚|S )z¡
    Given a loaded configuration schema, generate and return sample config for it. Include comments
    for each section based on the schema "description".
    ÚtypeÚexampleNÚarrayr   T)Úparent_is_sequence©ÚindentÚobjectc                 ó@   •— g | ]\  }}|t          |‰d z   ¦  «        f‘ŒS )r	   )Ú_schema_to_sample_configuration)Ú.0r   Ú
sub_schemaÚlevels      €r   ú
<listcomp>z3_schema_to_sample_configuration.<locals>.<listcomp>)   sB   ø€ ð ð ð á*�J 
ð Õ<¸ZÈÐQRÉÑSÔSÐTðð ð r   Ú
propertiesr   )r   Ú
skip_firstz%Schema at level {} is unsupported: {})Úgetr   ÚcommentsÚCommentedSeqr    Ú&add_comments_to_configuration_sequenceÚINDENTÚCommentedMapr   ÚSEQUENCE_INDENTÚ$add_comments_to_configuration_objectÚ
ValueErrorÚformat)Úschemar#   r   Úschema_typer   r   r   s    `     r   r    r       sL  ø€ ð
 —*’*˜VÑ$Ô$€KØ�jŠj˜Ñ#Ô#€GØÐØˆà�gÒÐÝ”×+Ò+Ý,¨V°G¬_¸eÐX\Ð]Ñ]Ô]Ð^ñ
ô 
ˆõ 	/¨v°vÀuÍvÁ~ÐWÑWÔWÐWÐWØ	˜Ò	 Ð	 Ý”×+Ò+ðð ð ð à.4°\Ô.B×.HÒ.HÑ.JÔ.Jðñ ô ñ
ô 
ˆð �&‘.Ð8JÐ%Q¥_ _ÐPQÑRˆÝ,Ø�F 6Ð6Hð	
ñ 	
ô 	
ð 	
ð 	
õ Ð@×GÒGÈÈvÑVÔVÑWÔWÐWà€Mr   c                 ó  — |                       ¦   «         }|r|                     d¦  «        r| S t          j        d| ¦  «        }|r|                     d¦  «        nd}t          |¦  «        }d                     || |d …         f¦  «        S )Nú#z(\s*)r   Ú z# )ÚlstripÚ
startswithÚreÚmatchÚgroupÚlenÚjoin)ÚlineÚstripped_lineÚmatchesÚindent_spacesÚcount_indent_spacess        r   Ú_comment_out_linerB   8   s�   € à—K’K‘M”M€MØð ˜M×4Ò4°SÑ9Ô9ð Øˆõ Œh�x Ñ&Ô&€GØ(/Ð7�G—M’M !Ñ$Ô$Ð$°R€MÝ˜mÑ,Ô,Ðà�9Š9�m TÐ*=Ð*>Ð*>Ô%?Ð@ÑAÔAÐAr   c                 ó`  — g }d}|                       d¦  «        D ]€}|                     ¦   «                              d                     t          ¦  «        ¦  «        rd}ŒD|                     ¦   «         sd}|                     |rt          |¦  «        n|¦  «         Œ�d                     |¦  «        S )aò  
    Post-process a rendered configuration string to comment out optional key/values, as determined
    by a sentinel in the comment before each key.

    The idea is that the pre-commented configuration prevents the user from having to comment out a
    bunch of configuration they don't care about to get to a minimal viable configuration file.

    Ideally ruamel.yaml would support commenting out keys during configuration generation, but it's
    not terribly easy to accomplish that way.
    Fr
   z# {}T)ÚsplitÚstripr7   r0   ÚCOMMENTED_OUT_SENTINELÚappendrB   r<   )Úrendered_configÚlinesÚoptionalr=   s       r   Ú#_comment_out_optional_configurationrK   F   s³   € ð €EØ€Hà×%Ò% dÑ+Ô+ð Dð Dˆð �:Š:‰<Œ<×"Ò" 6§=¢=Õ1GÑ#HÔ#HÑIÔIð 	ØˆHØð �zŠz‰|Œ|ð 	ØˆHà�Š°ÐBÕ& tÑ,Ô,Ð,¸dÑCÔCÐCÐCà�9Š9�UÑÔÐr   c                 ó   — t          j        ¦   «         }|                     t          t          t          z   t          ¬¦  «         t          j        ¦   «         }|                     | |¦  «         |                     ¦   «         S )zh
    Given a config data structure of nested OrderedDicts, render the config as YAML and return it.
    )ÚmappingÚsequenceÚoffset)	r   ÚYAMLr   r+   r-   ÚioÚStringIOÚdumpÚgetvalue)r   ÚdumperÚrendereds      r   Úrender_configurationrW   d   s]   € õ ŒY‰[Œ[€FØ
‡M‚M�&­6µOÑ+CÍF€MÑSÔSÐSÝŒ{‰}Œ}€HØ
‡K‚K�˜Ñ!Ô!Ð!à×ÒÑÔÐr   é€  c                 óÈ  — |sAt           j                             | ¦  «        r"t          d                     | ¦  «        ¦  «        ‚	 t          j        t           j                             | ¦  «        d¬¦  «         n# t          t          f$ r Y nw xY wt          | d¦  «        5 }| 	                    |¦  «         ddd¦  «         n# 1 swxY w Y   t          j
        | |¦  «         dS )zã
    Given a target config filename and rendered config YAML, write it out to file. Create any
    containing directories as needed. But if the file already exists and overwrite is False,
    abort before writing anything.
    zA{} already exists. Aborting. Use --overwrite to replace the file.iÀ  )ÚmodeÚwN)ÚosÚpathÚexistsÚFileExistsErrorr0   ÚmakedirsÚdirnameÚFileNotFoundErrorÚopenÚwriteÚchmod)Úconfig_filenamerH   rZ   Ú	overwriteÚconfig_files        r   Úwrite_configurationri   p   s,  € ð ð 
�œŸš¨Ñ8Ô8ð 
ÝØO×VÒVØñô ñ
ô 
ð 	
ðÝ
Œ•B”G—O’O OÑ4Ô4¸5ÐAÑAÔAÐAÐAøÝÕ.Ð/ð ð ð Øˆðøøøõ 
ˆo˜sÑ	#Ô	#ð + {Ø×Ò˜/Ñ*Ô*Ð*ð+ð +ð +ñ +ô +ð +ð +ð +ð +ð +ð +øøøð +ð +ð +ð +õ „Hˆ_˜dÑ#Ô#Ð#Ð#Ð#s$   Á3A9 Á9BÂBÂ CÃCÃ	Cc                 ó<  — |d                               d¦  «        dk    rdS | d                              ¦   «         D ]_}|d         d                               |i ¦  «        }|                      d¦  «        }|r|s dS | d                              ||¬¦  «          dS dS )	aý  
    If the given config sequence's items are object, then mine the schema for the description of the
    object's first item, and slap that atop the sequence. Indent the comment the given number of
    characters.

    Doing this for sequences of maps results in nice comments that look like:

    ```
    things:
        # First key description. Added by this function.
        - key: foo
          # Second key description. Added by add_comments_to_configuration_object().
          other: bar
    ```
    r   r   r   Nr   r%   Údescriptionr   )r'   ÚkeysÚyaml_set_start_comment)r   r1   r   r   Úfield_schemark   s         r   r*   r*   ˆ   s¼   € ð  ˆg„×Ò˜6Ñ"Ô" hÒ.Ð.Øˆà˜Q”i—n’nÑ&Ô&ð ð ˆ
Ø˜g” |Ô4×8Ò8¸ÀRÑHÔHˆØ"×&Ò& }Ñ5Ô5ˆð ð 	 ;ð 	ØˆFˆFàˆqŒ	×(Ò(¨¸VÐ(ÑDÔDÐDð 	ˆˆðð r   ÚlocationÚ	retention>   Ú
keep_dailyÚrepositoriesÚsource_directoriesÚCOMMENT_OUTc                 óÂ  — t          |                      ¦   «         ¦  «        D ]¼\  }}|r|dk    rŒ|d                              |i ¦  «        }|                     dd¦  «                             ¦   «         }|t          vr.|t
          vr%|rd                     |t          f¦  «        nt          }|r|sŒŽ|                      |||¬¦  «         |dk    rt          | |¦  «         Œ½dS )zÁ
    Using descriptions from a schema as a source, add those descriptions as comments to the given
    config mapping, before each field. Indent the comment the given number of characters.
    r   r%   rk   r5   r
   )ÚkeyÚbeforer   N)
Ú	enumeraterl   r'   rE   ÚREQUIRED_SECTION_NAMESÚREQUIRED_KEYSr<   rF   Ú!yaml_set_comment_before_after_keyr   )r   r1   r   r&   Úindexr   rn   rk   s           r   r.   r.   ¯   s  € õ
 ' v§{¢{¡}¤}Ñ5Ô5ð ?ð ?ÑˆˆzØð 	˜% 1š*˜*Øà˜lÔ+×/Ò/°
¸BÑ?Ô?ˆØ"×&Ò& }°bÑ9Ô9×?Ò?ÑAÔAˆð
 Õ3Ð3Ð3¸
Í-Ð8WÐ8Wð ð,�—	’	˜;Õ(>Ð?Ñ@Ô@Ð@å+ð ð ð 	 ;ð 	Øà×0Ò0°ZÈÐ\bÐ0ÑcÔcÐcà�1Š9ˆ9Ý*¨6°:Ñ>Ô>Ð>øð1?ð ?r   r	   c                 ó  — 	 | j         j        |         t                   d         j        }n# t          $ r Y dS w xY w|d                     t          ¦  «        k    r1| j         j        |         t                                        ¦   «          dS dS )a  
    Given a configuration CommentedMap and a top-level field name in it, remove any "commented out"
    sentinel found at the end of its YAML comments. This prevents the given field name from getting
    commented out by downstream processing that consumes the sentinel.
    éÿÿÿÿNz# {}
)r   r   ÚRUAMEL_YAML_COMMENTS_INDEXÚvalueÚKeyErrorr0   rF   Úpop)r   r   Úlast_comment_values      r   Úremove_commented_out_sentinelr„   Ò   s”   € ðØ#œYœ_¨ZÔ8Õ9SÔTÐUWÔXÔ^ÐÐøÝð ð ð Øˆˆðøøøð ˜XŸ_š_Õ-CÑDÔDÒDÐDØŒ	Œ˜
Ô#Õ$>Ô?×CÒCÑEÔEÐEÐEÐEð EÐDs   ‚(+ «
9¸9c                 ó"  ‡— |s| S | rt          |t          j        j        ¦  «        s|S |                     ¦   «         D ]Ñ\  }}t          | |¦  «         t          |t          j        j        ¦  «        rt          | |         |¦  «        | |<   ŒNt          |t          j        j        ¦  «        rYt          |t          ¦  «        sD| |         Št          j
                             ˆfd„t          |¦  «        D ¦   «         ¦  «        | |<   ŒÆ||         | |<   ŒÒ| S )a9  
    Deep merge the given source configuration dict into the destination configuration CommentedMap,
    favoring values from the source when there are collisions.

    The purpose of this is to upgrade configuration files from old versions of borgmatic by adding
    new
    configuration keys and comments.
    c                 ól   •— g | ]0\  }}t          |t          ‰¦  «        k     r‰|         nd |¦  «        ‘Œ1S )N)Ú+merge_source_configuration_into_destinationr;   )r!   r|   Úsource_itemÚdestination_values      €r   r$   z?merge_source_configuration_into_destination.<locals>.<listcomp>ÿ   s_   ø€ ð ð ð ñ
 +˜˜{õ	 @Ø49½CÐ@QÑ<RÔ<RÒ4RÐ4RÐ)¨%Ô0Ð0ÐX\Ø#ñô ðð ð r   )Ú
isinstanceÚcollectionsÚabcÚMappingr   r„   r‡   ÚSequenceÚstrr   r(   r)   rx   )Údestination_configÚsource_configr   Úsource_valuer‰   s       @r   r‡   r‡   á   sM  ø€ ð ð "Ø!Ð!Øð ¥Z°½{¼Ô?VÑ%WÔ%Wð ØÐà$1×$7Ò$7Ñ$9Ô$9ð Cð CÑ ˆ
�Lõ 	&Ð&8¸*ÑEÔEÐEõ �l¥K¤OÔ$;Ñ<Ô<ð 	Ý-XØ" :Ô.°ñ.ô .Ð˜zÑ*ð õ �l¥K¤OÔ$<Ñ=Ô=ð 	ÅjÐQ]Õ_bÑFcÔFcð 	Ø 2°:Ô >ÐÝ-1¬]×-GÒ-Gðð ð ð õ
 /8¸Ñ.EÔ.Eðñ ô ñ.ô .Ð˜zÑ*ð ð *7°zÔ)BÐ˜:Ñ&Ð&àÐr   c                 ó4  — t          j        t          |¦  «        ¦  «        }d}| r)t          j        | ¦  «        }t          j        | |¦  «         t          t          |¦  «        |¦  «        }t          |t          t          |¦  «        ¦  «        |¬¦  «         dS )a§  
    Given an optional source configuration filename, and a required destination configuration
    filename, the path to a schema filename in a YAML rendition of the JSON Schema format, and
    whether to overwrite a destination file, write out a sample configuration file based on that
    schema. If a source filename is provided, merge the parsed contents of that configuration into
    the generated configuration.
    N)rg   )r   Úround_trip_loadrc   r   Úload_configurationr   r‡   r    ri   rK   rW   )Úsource_filenameÚdestination_filenameÚschema_filenamerg   r1   r‘   r�   s          r   Úgenerate_sample_configurationr™     s§   € õ Ô!¥$ Ñ"7Ô"7Ñ8Ô8€FØ€Màð <ÝÔ/°Ñ@Ô@ˆÝÔ˜O¨]Ñ;Ô;Ð;åDÝ'¨Ñ/Ô/°ñô Ðõ ØÝ+Õ,@ÐASÑ,TÔ,TÑUÔUØðñ ô ð ð ð r   )r   F)rX   F)r   )F)r‹   rQ   r\   r8   Úruamelr   Úborgmatic.configr   r   r+   r-   r   r    rB   rK   rW   ri   r*   ry   rz   rF   r.   r   r„   r‡   r™   © r   r   ú<module>r�      s_  ðØ Ð Ð Ð Ø 	€	€	€	Ø 	€	€	€	Ø 	€	€	€	à Ð Ð Ð Ð Ð à ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,à	
€Ø€ðð ð ðð ð ð ð@Bð Bð Bðð ð ð<	ð 	ð 	ð$ð $ð $ð $ð0ð ð ð ðD % kÐ2Ð ØDÐDÐD€Ø&Ð ð?ð ?ð ?ð ?ð@ Ð ðFð Fð Fð+ð +ð +ð^ GLðð ð ð ð ð r   