
    Oge%                    0   d Z ddlmZ ddlmZmZmZmZmZm	Z	m
Z
 ddlmZ ddlmZ ddlmZmZ ddlmZ ddlmZmZ dd	lmZ dd
lmZ erddlmZ dZdZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'ddgZ( G d d      Z)d!dZ*d"dZ+d#dZ,d$dZ-d%dZ. G d d      Z/d&dZ0y )'a
  
UserRecord(): store user data in a XRECORD entity.

The group code 302 is used as a structure tag.

All supported data types have a fixed group code:
    - str: 1
    - int: 90 - 32-bit values
    - float: 40 - doubles
    - Vec3, Vec2: 10, 20, 30 - Vec2 is stored as Vec3
    - list, tuple: starts with tag (302, "[") and ends with tag (302, "]")
    - dict: starts with tag (302, "{") and ends with tag (302, "}")

The str type can have a max. length of 2049 characters and cannot contain "
"
or "".

This is an advanced feature for experienced programmers, handle with care!
The attribute UserRecord.data is a simple Python list with read/write access.

The UserRecord can store nested list and dict objects.

BinaryData(): store arbitrary binary data in a XRECORD entity

    )annotations)TYPE_CHECKINGOptionalIterableSequenceMappingMutableSequencecast)const)XRecord)Tagsbinary_data_to_dxf_tags)dxftag)Vec3Vec2)take2)bytes_to_hexstr)Drawing      Z   (   
   i.  []{}
UserRecordBinaryRecordc                  V    e Zd Z	 d	edd	 	 	 	 	 d
dZedd       Zd Zd Zd Z	ddZ
y)r   N)namedocc                  |A|t        j                         }n*t        t         |j                  j	                  di             }|| _        t        |      | _        t        | j
                  | j                        | _	        y)a  Setup a :class:`UserRecord` with the given `name`.

        The data is stored in the given `xrecord` object, or in a new created
        :class:`~ezdxf.entities.XRecord` instance if ``None``. If `doc` is not
        ``None`` the new xrecord is added to the OBJECTS section of the DXF
        document.

        Changes of the content has to be committed at the end to be stored in
        the underlying :attr:`xrecord` object.

        Args:
            xrecord (XRecord): underlying :class:`~ezdxf.entities.XRecord` instance,
                if ``None`` a new one will be created
            name (str): name of the user list
            doc (Drawing): DXF document or ``None``

        NXRECORD)
r   newr
   objects
new_entityxrecordstrr!   parse_xrecorddata)selfr(   r!   r"   s       R/var/www/html/public_html/myphp/venv/lib/python3.12/site-packages/ezdxf/urecord.py__init__zUserRecord.__init__@   s^    0 ?{!++-w(>(>y"(MNI	%24<<%K	    c                L    | j                   j                  j                  d      S zGDXF handle of the underlying :class:`~ezdxf.entities.XRecord` instance.handler(   dxfgetr,   s    r-   r2   zUserRecord.handleb        ||##H--r/   c                    | S N r6   s    r-   	__enter__zUserRecord.__enter__g       r/   c                $    | j                          y r9   commitr,   exc_typeexc_valexc_tbs       r-   __exit__zUserRecord.__exit__j       r/   c                ,    t        | j                        S zReturn str(self).)r)   r+   r6   s    r-   __str__zUserRecord.__str__m   s    499~r/   c                x    t        | j                  | j                        | j                  _        | j                  S )a8  Store :attr:`data` in the underlying :class:`~ezdxf.entities.XRecord`
        instance. This call is not required if using the class by the ``with``
        statement.

        Raises:
            DXFValueError: invalid chars ``"\n"`` or ``"\r"`` in a string
            DXFTypeError: invalid data type

        )compile_user_recordr!   r+   r(   tagsr6   s    r-   r?   zUserRecord.commitq   s)     0		499E||r/   r9   )r(   Optional[XRecord]r!   r)   r"   Optional[Drawing]returnzOptional[str]rO   r   )__name__
__module____qualname__DEFAULT_NAMEr.   propertyr2   r;   rD   rH   r?   r:   r/   r-   r   r   ?   sc     &* L !!% L" L 	 L
  LD . .r/   c                    g }| j                   }|r]|d   \  }}|t        k7  r(||k7  r#t        j                  t	        |        d|       |j                  d t        |dd        D               |S )Nr   z is not an user record of type c              3      K   | ]  }|  y wr9   r:   ).0items     r-   	<genexpr>z parse_xrecord.<locals>.<genexpr>   s     ;TD;s   r   )rK   TYPE_GROUP_CODEr   DXFTypeErrorr)   extendparse_items)r(   r!   r+   rK   codevalues         r-   r*   r*      sx    D<<D1ge?"u}$$w<. ?vF  	;[ab%:;;Kr/   c                J   g }g }| D ]O  }|\  }}|t         k(  r|j                  t        |             -|t        k(  r|j                  t	        |             Q|t
        k(  r|j                  t        |             u|t        k(  r|j                  t        |             |t        k(  r&|t        k(  s	|t        k(  r|j                  |       g }|t        k(  re|t        k(  s	|t        k(  rS	 |j                         }|t        k(  r$|j                  t%        t'        |                   n|j                  |       |}6t!        j(                  d| d| d       |rt!        j"                  dt         d      |S # t        $ r t!        j"                  dt         d      w xY w)Nz6invalid nested structure, mismatch of structure tags (z, ...)zinvalid group code in tag: (z, )z5invalid nested structure, mismatch of structure tags()STR_GROUP_CODEappendr)   INT_GROUP_CODEintFLOAT_GROUP_CODEfloatVEC3_GROUP_CODEr   COLLECTION_GROUP_CODE
START_LIST
START_DICTEND_LISTEND_DICTpop
IndexErrorr   DXFStructureErrordictr   DXFValueError)rK   stackitemstagr_   r`   
prev_levels          r-   r^   r^      s   EE !e>!LLU$^#LLU$%%LLu&_$LLe%**Z5J#6LLE**X(!2"YY[
  !!$uU|"45!!%(E%%.tfBugQ? ?!D %%%&f.
 	
 L'  --./v7 s   +E;;'F"c                    t               }|j                  t        t        |              |j	                  t        |             |S r9   )r   rd   r   r[   r]   tags_from_list)r!   r+   rK   s      r-   rJ   rJ      s2    6DKK-.KKt$%Kr/   c           	        t               }| D ]J  }t        |t              r`t        |      dkD  rt	        j
                  d      d|v sd|v rt	        j
                  d      |j                  t        t        |             tt        |t              r |j                  t        t        |             t        |t              r |j                  t        t        |             t        |t              r!|j                  t        t        |             t        |t              r*|j                  t        t        t        |                   ?t        |t               rb|j                  t        t"        t$                     |j'                  t)        |             |j                  t        t"        t*                     t        |t,              rk|j                  t        t"        t.                     |j'                  t)        t1        |                   |j                  t        t"        t2                     ,t	        j4                  dt7        |              |S )Ni  z%string too long, max. 2049 characters
z%found invalid line break '\n' or '\r'zunsupported type: )r   
isinstancer)   lenr   rs   rd   r   rc   rf   re   rh   rg   r   ri   r   r   rj   rk   r]   ry   rm   r   rl   key_value_listrn   r\   type)ru   rK   rY   s      r-   ry   ry      s   6D HdC 4y4));  t|tt|))=  KK~t45c"KK~t45e$KK/67d#KK56d#KKT
;<h'KK4jABKKt,-KK4h?@g&KK4jABKK~d';<=KK4h?@$$'9$t*%FGG9H: Kr/   c              #  L   K   | j                         D ]  \  }}| |  y wr9   )ru   )r+   kvs      r-   r   r      s)     

 1s   "$c                  R    e Zd Z	 d	dd	 	 	 d
dZedd       Zd Zd ZddZddZ	y)r   N)r"   c                   |A|t        j                         }n*t        t         |j                  j	                  di             }|| _        t        | j
                  j                        | _        y)ap  Setup a :class:`BinaryRecord`.

        The data is stored in the given `xrecord` object, or in a new created
        :class:`~ezdxf.entities.XRecord` instance if ``None``. If `doc` is not
        ``None`` the new xrecord is added to the OBJECTS section of the DXF
        document.

        Changes of the content has to be committed at the end to be stored in
        the underlying :attr:`xrecord` object.

        Args:
            xrecord (XRecord): underlying :class:`~ezdxf.entities.XRecord` instance,
                if ``None`` a new one will be created
            doc (Drawing): DXF document or ``None``

        Nr$   )	r   r%   r
   r&   r'   r(   parse_binary_datarK   r+   )r,   r(   r"   s      r-   r.   zBinaryRecord.__init__   sS    , ?{!++-w(>(>y"(MN,T\\->->?	r/   c                L    | j                   j                  j                  d      S r1   r3   r6   s    r-   r2   zBinaryRecord.handle  r7   r/   c                    | S r9   r:   r6   s    r-   r;   zBinaryRecord.__enter__  r<   r/   c                $    | j                          y r9   r>   r@   s       r-   rD   zBinaryRecord.__exit__  rE   r/   c                ,    t        | j                        S rG   )r   r+   r6   s    r-   rH   zBinaryRecord.__str__  s    tyy))r/   c                    t        | j                  t        t        t        f      sJ d       t        | j                  ddd      | j                  _        | j                  S )zStore binary :attr:`data` in the underlying :class:`~ezdxf.entities.XRecord`
        instance. This call is not required if using the class by the ``with``
        statement.

        zexpected binary data   6     )length_group_codevalue_group_code
value_size)r}   r+   bytes	bytearray
memoryviewr   r(   rK   r6   s    r-   r?   zBinaryRecord.commit  s]     IIy*5
 	"!	" 
 4II! 	
 ||r/   r9   )r(   rL   r"   rM   rN   )rO   r)   rP   )
rQ   rR   rS   r.   rU   r2   r;   rD   rH   r?   r:   r/   r-   r   r      sT     &*@ "&	@"@ 	@> . .*r/   c                \    | r*| d   j                   dk(  rdj                  d | D              S y)Nr   r   r/   c              3  T   K   | ]   }|j                   d k(  s|j                   " yw)r   N)r_   r`   )rX   ts     r-   rZ   z$parse_binary_data.<locals>.<genexpr>,  s     ?A3?s   (()r_   join)rK   s    r-   r   r   *  s,    Q#xx????r/   N)r(   r   r!   r)   rO   list)rK   r   rO   r   )r!   r)   r+   r   rO   r   )ru   r   rO   r   )r+   r   rO   r   )rK   r   rO   r   )1__doc__
__future__r   typingr   r   r   r   r   r	   r
   ezdxf.lldxfr   ezdxf.entitiesr   ezdxf.lldxf.tagsr   r   ezdxf.lldxf.typesr   
ezdxf.mathr   r   ezdxf.toolsr   ezdxf.tools.binarydatar   ezdxf.documentr   r[   rc   re   rg   ri   rj   rk   rm   rl   rn   rT   __all__r   r*   r^   rJ   ry   r   r   r   r:   r/   r-   <module>r      s   0 #    " : $ !  2&  


(= =@
*ZD? ?Dr/   