
    Zii"                       d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	 ddl
mZmZmZ erddl
mZ g d	Zd
 eeefD             Zd'dZ	 	 d(d)dZd*d+dZd*d,dZ	 	 d(d-dZ	 	 d(d.d Z	 	 d(d/d$Zd0d1d%Z G d& de          ZdS )2zB
Python Frontmatter: Parse and manage posts with YAML frontmatter
    )annotationsN)TYPE_CHECKINGIterable   )u)YAMLHandlerJSONHandlerTOMLHandler)BaseHandler)parseloadloadsdumpdumpsc                &    g | ]}| |            S N ).0Handlers     O/root/projects/butler/venv/lib/python3.11/site-packages/frontmatter/__init__.py
<listcomp>r      s.        GII    textstrhandlersIterable[BaseHandler]returnBaseHandler | Nonec                B    |D ]}|                     |           r|c S dS )a  
    Figure out which handler to use, based on metadata.
    Returns a handler instance or None.

    ``text`` should be unicode text about to be parsed.

    ``handlers`` is a dictionary where keys are opening delimiters
    and values are handler instances.
    N)detect)r   r   handlers      r   detect_formatr"      s<       >>$ 	NNN	 4r   utf-8encodingr!   defaultsobjecttuple[dict[str, object], str]c                   t          | |                                          } |                                }|pt          | t                    }||| fS 	 |                    |           \  }}n# t          $ r || fcY S w xY w|                    |          }t          |t                    r|
                    |           ||                                fS )a  
    Parse text with frontmatter, return metadata and content.
    Pass in optional metadata defaults as keyword args.

    If frontmatter is not found, returns an empty metadata dictionary
    (or defaults) and original text content.

    .. testsetup:: *

        >>> import frontmatter

    .. doctest::

        >>> with open('tests/yaml/hello-world.txt') as f:
        ...     metadata, content = frontmatter.parse(f.read())
        >>> print(metadata['title'])
        Hello, world!

    )r   stripcopyr"   r   split
ValueErrorr   
isinstancedictupdate)r   r$   r!   r%   metadatafmcontentfm_datas           r   r   r   0   s    4 T8""$$D }}H 6tX66G~mmD))GG   ~
 ll2G'4   !   W]]__$$s   A. .A?>A?fdstr | io.IOBaseboolc                    t          | d          r|                                 }nBt          j        | d|          5 }|                                }ddd           n# 1 swxY w Y   t	          ||          S )a  
    Check if a file-like object or filename has a frontmatter,
    return True if exists, False otherwise.

    If it contains a frontmatter but it is empty, return True as well.

    .. doctest::

        >>> frontmatter.check('tests/yaml/hello-world.txt')
        True

    readrN)hasattrr8   codecsopenchecks)r4   r$   r   fs       r   checkr?   c   s     r6 wwyy [S(++ 	q6688D	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 $!!!   AA!$A!c                T    t          | |          } t          | t                    dk    S )a<  
    Check if a text (binary or unicode) has a frontmatter,
    return True if exists, False otherwise.

    If it contains a frontmatter but it is empty, return True as well.

    .. doctest::

        >>> with open('tests/yaml/hello-world.txt') as f:
        ...     frontmatter.checks(f.read())
        True

    N)r   r"   r   )r   r$   s     r   r=   r=   z   s(     T8Dx((D00r   Postc                   t          | d          r|                                 }nBt          j        | d|          5 }|                                }ddd           n# 1 swxY w Y   |pt	          |t
                    }t          |||fi |S )a%  
    Load and parse a file-like object or filename,
    return a :py:class:`post <frontmatter.Post>`.

    .. doctest::

        >>> post = frontmatter.load('tests/yaml/hello-world.txt')
        >>> with open('tests/yaml/hello-world.txt') as f:
        ...     post = frontmatter.load(f)

    r8   r9   N)r:   r8   r;   r<   r"   r   r   )r4   r$   r!   r%   r   r>   s         r   r   r      s    " r6 wwyy [S(++ 	q6688D	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 6tX66Gx55H555r@   c                    t          | |          } |pt          | t                    }t          | ||fi |\  }}t	          ||fi |S )z
    Parse text (binary or unicode) and return a :py:class:`post <frontmatter.Post>`.

    .. doctest::

        >>> with open('tests/yaml/hello-world.txt') as f:
        ...     post = frontmatter.loads(f.read())

    )r   r"   r   r   rB   )r   r$   r!   r%   r0   r2   s         r   r   r      s]     T8D6tX66GdHgBBBBHg--H---r   postkwargsNonec                   t          | |fi |}t          |d          r*|                    |                    |                     dS t	          j        |d|          5 }|                    |           ddd           dS # 1 swxY w Y   dS )ak  
    Serialize :py:class:`post <frontmatter.Post>` to a string and write to a file-like object.
    Text will be encoded on the way out (utf-8 by default).

    ::

        >>> from io import BytesIO
        >>> post = frontmatter.load('tests/yaml/hello-world.txt')
        >>> f = BytesIO()
        >>> frontmatter.dump(post, f)
        >>> print(f.getvalue().decode('utf-8'))
        ---
        layout: post
        title: Hello, world!
        ---
        <BLANKLINE>
        Well, hello there, world.


    .. testcode::

        from io import BytesIO
        post = frontmatter.load('tests/yaml/hello-world.txt')
        f = BytesIO()
        frontmatter.dump(post, f)
        print(f.getvalue().decode('utf-8'))

    .. testoutput::

        ---
        layout: post
        title: Hello, world!
        ---
        <BLANKLINE>
        Well, hello there, world.

    writewN)r   r:   rI   encoder;   r<   )rE   r4   r$   r!   rF   r2   r>   s          r   r   r      s    X D',,V,,Gr7 
))***** [S(++ 	qGGG	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   BB	Bc                `    |t          | dd          pt                      } |j        | fi |S )a  
    Serialize a :py:class:`post <frontmatter.Post>` to a string and return text.
    This always returns unicode text, which can then be encoded.

    Passing ``handler`` will change how metadata is turned into text. A handler
    passed as an argument will override ``post.handler``, with
    :py:class:`YAMLHandler <frontmatter.default_handlers.YAMLHandler>` used as
    a default.

    ::

        >>> post = frontmatter.load('tests/yaml/hello-world.txt')
        >>> print(frontmatter.dumps(post)) # doctest: +NORMALIZE_WHITESPACE
        ---
        layout: post
        title: Hello, world!
        ---
        <BLANKLINE>
        Well, hello there, world.

    .. testcode::

        post = frontmatter.load('tests/yaml/hello-world.txt')
        print(frontmatter.dumps(post))

    .. testoutput::

        ---
        layout: post
        title: Hello, world!
        ---

        Well, hello there, world.

    Nr!   )getattrr   format)rE   r!   rF   s      r   r   r      s>    H $	400AKMM7>$))&)))r   c                  p    e Zd ZdZ	 d d!dZd"dZd#dZd$dZd%dZd&dZ	d'dZ
d d(dZd)dZd*dZd+dZdS ),rB   a}  
    A post contains content and metadata from Front Matter. This is what gets
    returned by :py:func:`load <frontmatter.load>` and :py:func:`loads <frontmatter.loads>`.
    Passing this to :py:func:`dump <frontmatter.dump>` or :py:func:`dumps <frontmatter.dumps>`
    will turn it back into text.

    For convenience, metadata values are available as proxied item lookups.
    Nr2   r   r!   r   r0   r&   r   rG   c                J    t          |          | _        || _        || _        d S r   )r   r2   r0   r!   )selfr2   r!   r0   s       r   __init__zPost.__init__&  s#     7|| r   namec                    | j         |         S )zGet metadata keyr0   rQ   rS   s     r   __getitem__zPost.__getitem__-  s    }T""r   itemr6   c                    || j         v S )zCheck metadata contains keyrU   )rQ   rX   s     r   __contains__zPost.__contains__1  s    t}$$r   valuec                    || j         |<   dS )zSet a metadata keyNrU   )rQ   rS   r[   s      r   __setitem__zPost.__setitem__5  s    #dr   c                    | j         |= dS )zDelete a metadata keyNrU   rV   s     r   __delitem__zPost.__delitem__9  s    M$r   bytesc                6    | j                             d          S )Nr#   )r2   rK   rQ   s    r   	__bytes__zPost.__bytes__=  s    |""7+++r   c                    | j         S r   )r2   rb   s    r   __str__zPost.__str__@  s
    |r   keydefaultc                8    | j                             ||          S )zGet a key, fallback to default)r0   get)rQ   rf   rg   s      r   ri   zPost.getC  s    }  g...r   Iterable[str]c                4    | j                                         S )zReturn metadata keys)r0   keysrb   s    r   rl   z	Post.keysG  s    }!!###r   Iterable[object]c                4    | j                                         S )zReturn metadata values)r0   valuesrb   s    r   ro   zPost.valuesK  s    }##%%%r   dict[str, object]c                L    | j                                         }| j        |d<   |S )zPost as a dict, for serializingr2   )r0   r*   r2   )rQ   ds     r   to_dictzPost.to_dictO  s%    M  |)r   r   )r2   r   r!   r   r0   r&   r   rG   )rS   r   r   r&   )rX   r&   r   r6   )rS   r   r[   r&   r   rG   )rS   r   r   rG   )r   r`   )r   r   )rf   r   rg   r&   r   r&   )r   rj   )r   rm   )r   rp   )__name__
__module____qualname____doc__rR   rW   rZ   r]   r_   rc   re   ri   rl   ro   rs   r   r   r   rB   rB     s	         ;?    # # # #% % % %$ $ $ $       , , , ,   / / / / /$ $ $ $& & & &     r   )r   r   r   r   r   r   )r#   N)
r   r   r$   r   r!   r   r%   r&   r   r'   )r#   )r4   r5   r$   r   r   r6   )r   r   r$   r   r   r6   )
r4   r5   r$   r   r!   r   r%   r&   r   rB   )
r   r   r$   r   r!   r   r%   r&   r   rB   )rE   rB   r4   r5   r$   r   r!   r   rF   r&   r   rG   r   )rE   rB   r!   r   rF   r&   r   r   )rw   
__future__r   r;   iotypingr   r   utilr   default_handlersr   r	   r
   r   __all__r   r"   r   r?   r=   r   r   r   r   r&   rB   r   r   r   <module>r~      s    # " " " " "  				 * * * * * * * *       C C C C C C C C C C  .------ 6
5
5 k:     ( "&0% 0% 0% 0% 0%f" " " " ".1 1 1 1 1( "&6 6 6 6 6< "&. . . . .0 "&	2 2 2 2 2j'* '* '* '* '*T7 7 7 7 76 7 7 7 7 7r   