o
    hH%                     @   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/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/blocks/migrations/utils.pyr	      s   zInvalidBlockDefError.__init__c                    sj   d}| j d ur,|d| j jj| j j7 }| jd ur%|d| jj| jj7 }| jr,|d7 }|t 	 7 }|S )N z#Invalid block def in {} object ({})z# for revision id ({}) created at {}
)
r   formatr   __name__idr   
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   )
block_name
block_pathr   r   r   should_alter_block   s   r   c                 K   s   t |dkr|| S t|trt| f|||d|S t|tr-t| f|||d|S t|tr>t| f|||d|S t	d|  )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: )
lenapply
isinstancer   map_stream_block_valuer   map_list_block_valuer   map_struct_block_value
ValueError)block_valuer!   r   r    r   r   r   r   map_block_value%   sB   



	r*   c              	   K   s   g }| D ]E}t |d |s|| qz	|j|d  }W n ty,   td|d w 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*   )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 ]7\}}t||s|||< qz|j| }W n ty)   td| w 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   r0   r1   r   r*   )	struct_block_valuer!   r   r   r3   keychild_valuer5   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*   r4   r/   )list_block_valuer!   r   r   r3   r4   r6   r   r   r   r&      s   
r&   c                 c   s`    d}t | d tsd}nd| d vs| d d dkrd}| D ]}|s'|V  qd|dV  qd S )NFr   Tr+   item)r+   r,   )r$   dict)r=   is_old_formatchildr   r   r   r<      s   r<   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fieldstream_blockr*   )raw_datablock_path_strr    streamfieldr   r   r!   altered_raw_datar   r   r   apply_changes_to_raw_data   s   /
rJ   N)wagtail.blocksr   r   r   	Exceptionr   r   r*   r%   r'   r&   r<   rJ   r   r   r   r   <module>   s    =*("