a
    DgH%                     @   s`   d dl mZmZmZ G dd deZdd Zdd Zdd	 Zd
d Z	dd Z
dd Zdd ZdS )    )	ListBlockStreamBlockStructBlockc                       s4   e Zd ZdZddd fdd
Z fddZ  ZS )InvalidBlockDefErrorz'Exception for invalid block definitionsN)instancerevisionc                   s"   || _ || _t j|i | d S )N)r   r   super__init__)selfr   r   argskwargs	__class__ ^/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/blocks/migrations/utils.pyr	      s    zInvalidBlockDefError.__init__c                    sj   d}| j d urX|d| j jj| j j7 }| jd urJ|d| jj| jj7 }| jrX|d7 }|t 	 7 }|S )N z#Invalid block def in {} object ({})z# for revision id ({}) created at {}
)
r   formatr   __name__idr   Z
created_atr   r   __str__)r
   messager   r   r   r      s    

zInvalidBlockDefError.__str__)r   
__module____qualname____doc__r	   r   __classcell__r   r   r   r   r      s   r   c                 C   s   | |d kS )Nr   r   )Z
block_name
block_pathr   r   r   should_alter_block   s    r   c                 K   s   t |dkr|| S t|tr8t| f|||d|S t|trZt| f|||d|S t|tr|t| f|||d|S t	d|  dS )ae  
    Maps the value of a block.

    Args:
        block_value:
            The value of the block. This would be a list or dict of children for structural blocks.
        block_def:
            The definition of the block.
        block_path:
            A '.' separated list of names of the blocks from the current block (not included) to
            the nested block of which the value will be passed to the operation.
        operation:
            An Operation class instance (extends `BaseBlockOperation`), which has an `apply` method
            for mapping values.

    Returns:
        mapped_value:
    r   )	operation	block_defr   zUnexpected Structural Block: N)
lenapply
isinstancer   map_stream_block_valuer   map_list_block_valuer   map_struct_block_value
ValueError)Zblock_valuer   r   r   r   r   r   r   map_block_value%   sB    



	r'   c              	   K   s   g }| D ]}t |d |s&|| qz|j|d  }W n$ ty\   td|d Y n0 t|d f||dd d|}|i |d|i q|S )a  
    Maps each child block in a StreamBlock value.

    Args:
        stream_block_value:
            The value of the StreamBlock, a list of child blocks
        block_def:
            The definition of the StreamBlock
        block_path:
            A '.' separated list of names of the blocks from the current block (not included) to
            the nested block of which the value will be passed to the operation.

    Returns
        mapped_value:
            The value of the StreamBlock after mapping all the children.
    typezNo current block def named {}value   Nr   r   )r   appendchild_blocksKeyErrorr   r   r'   )Zstream_block_valuer   r   r   mapped_valuechild_blockchild_block_defmapped_child_valuer   r   r   r#   b   s(    

r#   c           	   	   K   s   i }|   D ]r\}}t||s(|||< qz|j| }W n  tyV   td| Y n0 t|f||dd d|}|||< q|S )a  
    Maps each child block in a StructBlock value.

    Args:
        stream_block_value:
            The value of the StructBlock, a dict of child blocks
        block_def:
            The definition of the StructBlock
        block_path:
            A '.' separated list of names of the blocks from the current block (not included) to
            the nested block of which the value will be passed to the operation.

    Returns
        mapped_value:
            The value of the StructBlock after mapping all the children.
    zNo current block def named r*   Nr+   )itemsr   r-   r.   r   r'   )	Zstruct_block_valuer   r   r   r/   keyZchild_valuer1   Zaltered_child_valuer   r   r   r%      s$    



r%   c                 K   sP   g }t | D ]>}t|d f|j|dd d|}|i |d|i q|S )a
  
    Maps each child block in a ListBlock value.

    Args:
        stream_block_value:
            The value of the ListBlock, a list of child blocks
        block_def:
            The definition of the ListBlock
        block_path:
            A '.' separated list of names of the blocks from the current block (not included) to
            the nested block of which the value will be passed to the operation.

    Returns
        mapped_value:
            The value of the ListBlock after mapping all the children.
    r)   r*   Nr+   )formatted_list_child_generatorr'   r0   r,   )list_block_valuer   r   r   r/   r0   r2   r   r   r   r$      s    
r$   c                 c   s^   d}t | d tsd}n d| d vs4| d d dkr8d}| D ]}|sL|V  q<d|dV  q<d S )NFr   Tr(   item)r(   r)   )r"   dict)r6   Zis_old_formatchildr   r   r   r5      s    r5   c                 K   s<   |dkrg }n
| d}|jj}t| f|||d|}|S )a/  
    Applies changes to raw stream data

    Args:
        raw_data:
            The current stream data (a list of top level blocks)
        block_path_str:
            A '.' separated list of names of the blocks from the top level block to the nested
            block of which the value will be passed to the operation.

            eg:- 'simplestream.struct1' would point to,
                [..., { type: simplestream, value: [..., { type: struct1, value: {...} }] }]

            NOTE: If we're directly applying changes on the top level stream block, then this will
            be "".

            NOTE: When the path contains a ListBlock child, 'item' must be added to the block as
            the name of said child.

            eg:- 'list1.item.stream1' where the list child is a StructBlock would point to,
                [
                    ...,
                    {
                        type: list1,
                        value: [
                            {
                                type: item,
                                value: { ..., stream1: [...] }
                            },
                            ...
                        ]
                    }
                ]
        operation:
            A subclass of `operations.BaseBlockOperation`. It will have the `apply` method
            for applying changes to the matching block values.
        streamfield:
            The streamfield for which data is being migrated. This is used to get the definitions
            of the blocks.

    Returns:
        altered_raw_data:
    r   .)r   r   r   )splitfieldZstream_blockr'   )raw_dataZblock_path_strr   Zstreamfieldr   r   r   Zaltered_raw_datar   r   r   apply_changes_to_raw_data   s    /
r>   N)Zwagtail.blocksr   r   r   	Exceptionr   r   r'   r#   r%   r$   r5   r>   r   r   r   r   <module>   s   =*("