
    'je%                    >   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+d0d%Z,d1d'Z-d2d)Z. G d* d          Z/d3d,Z0d-S )4a
  
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                  T    e Zd Z	 dedddd	Zedd            Zd Zd Zd Z	ddZ
dS )r   N)namedocxrecordOptional[XRecord]r!   strr"   Optional[Drawing]c                  |D|t          j                    }n.t          t           |j                            di                     }|| _        t          |          | _        t          | j        | j                  | _	        dS )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_entityr#   r%   r!   parse_xrecorddata)selfr#   r!   r"   s       G/DATA/AppData/hermes/venv/lib/python3.11/site-packages/ezdxf/urecord.py__init__zUserRecord.__init__@   si    0 ?{!+--w(>(>y"(M(MNNII	%24<%K%K			    returnOptional[str]c                @    | j         j                            d          S zGDXF handle of the underlying :class:`~ezdxf.entities.XRecord` instance.handler#   dxfgetr.   s    r/   r6   zUserRecord.handleb        |##H---r1   c                    | S N r:   s    r/   	__enter__zUserRecord.__enter__g       r1   c                .    |                                   d S r=   commitr.   exc_typeexc_valexc_tbs       r/   __exit__zUserRecord.__exit__j       r1   c                *    t          | j                  S zReturn str(self).)r%   r-   r:   s    r/   __str__zUserRecord.__str__m   s    49~~r1   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#   tagsr:   s    r/   rC   zUserRecord.commitq   s$     0	49EE|r1   r=   )r#   r$   r!   r%   r"   r&   r2   r3   r2   r   )__name__
__module____qualname__DEFAULT_NAMEr0   propertyr6   r?   rH   rL   rC   r>   r1   r/   r   r   ?   s         &* L !!% L  L  L  L  L  LD . . . X.           r1   r#   r   r!   r%   r2   listc                   g }| j         }|rv|d         \  }}|t          k    r,||k    r&t          j        t	          |            d|           |                    d t          |dd                    D                        |S )Nr   z is not an user record of type c              3     K   | ]}|V  d S r=   r>   ).0items     r/   	<genexpr>z parse_xrecord.<locals>.<genexpr>   s"      ;;TD;;;;;;r1   r   )rO   TYPE_GROUP_CODEr   DXFTypeErrorr%   extendparse_items)r#   r!   r-   rO   codevalues         r/   r,   r,      s    D<D <1ge?""u}}$w<<FFFF   	;;[abb%:%:;;;;;;Kr1   rO   r   c                   g }g }| D ]}|\  }}|t           k    r#|                    t          |                     6|t          k    r#|                    t	          |                     d|t
          k    r#|                    t          |                     |t          k    r#|                    t          |                     |t          k    r.|t          k    s|t          k    r|                    |           g }|t          k    r|t          k    s|t          k    r	 |                                }n+# t          $ r t!          j        dt           d          w xY w|t          k    r0|                    t%          t'          |                               n|                    |           |}t!          j        d| d| d          |rt!          j        dt           d          |S )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)rO   stackitemstagra   rb   
prev_levels          r/   r`   r`      s(   EE ! !e>!!LLU$$$$^##LLU$$$$%%%LLu&&&&_$$LLe%%%%***Z5J#6#6LLEE***X(!2!2"YY[[

   -7.7 7 7  
   !!$uU||"4"45555!!%(((EE%?t??u???    
%.%. . .
 
 	
 Ls   "D77(Er-   r   c                    t                      }|                    t          t          |                      |                    t          |                     |S r=   )r   rf   r   r]   r_   tags_from_list)r!   r-   rO   s      r/   rN   rN      sF    66DKK--...KKt$$%%%Kr1   rw   c           	        t                      }| D ]}t          |t                    rlt          |          dk    rt	          j        d          d|v sd|v rt	          j        d          |                    t          t          |                     t          |t                    r)|                    t          t          |                     t          |t                    r*|                    t          t          |                     t          |t                    r*|                    t          t          |                     @t          |t                    r7|                    t          t          t          |                               t          |t                     r~|                    t          t"          t$                               |                    t)          |                     |                    t          t"          t*                               t          |t,                    r|                    t          t"          t.                               |                    t)          t1          |                               |                    t          t"          t2                               t	          j        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   ru   rf   r   re   rh   rg   rj   ri   r   rk   r   r   rl   rm   r_   r{   ro   r   rn   key_value_listrp   r^   type)rw   rO   r[   s      r/   r{   r{      st   66D H HdC   	H4yy4);   t||tt||)=   KK~t445555c"" 	HKK~t445555e$$ 	HKK/667777d## 	HKK556666d## 	HKKT

;;<<<<h'' 		HKK4jAABBBKKt,,---KK4h??@@@@g&& 	HKK4jAABBBKK~d';';<<===KK4h??@@@@$%F$t**%F%FGGGKr1   r   c              #  N   K   |                                  D ]\  }}|V  |V  d S r=   )rw   )r-   kvs      r/   r   r      s?      

  1 r1   c                  T    e Zd Z	 dddddZedd
            Zd Zd ZddZddZ	dS )r   N)r"   r#   r$   r"   r&   c                   |D|t          j                    }n.t          t           |j                            di                     }|| _        t          | j        j                  | _        dS )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_datarO   r-   )r.   r#   r"   s      r/   r0   zBinaryRecord.__init__   s[    , ?{!+--w(>(>y"(M(MNN,T\->??			r1   r2   r3   c                @    | j         j                            d          S r5   r7   r:   s    r/   r6   zBinaryRecord.handle  r;   r1   c                    | S r=   r>   r:   s    r/   r?   zBinaryRecord.__enter__  r@   r1   c                .    |                                   d S r=   rB   rD   s       r/   rH   zBinaryRecord.__exit__  rI   r1   r%   c                *    t          | j                  S rK   )r   r-   r:   s    r/   rL   zBinaryRecord.__str__  s    ty)))r1   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#   rO   r:   s    r/   rC   zBinaryRecord.commit  sn     Iy*5
 
 	" 	"!	" 	" 
 4I! 	
 
 
 |r1   r=   )r#   r$   r"   r&   rP   )r2   r%   rQ   )
rR   rS   rT   r0   rV   r6   r?   rH   rL   rC   r>   r1   r/   r   r      s         &*@ "&	@ @ @ @ @ @> . . . X.    * * * *     r1   r   c                j    | r0| d         j         dk    rd                    d | D                       S dS )Nr   r   r1   c              3  :   K   | ]}|j         d k    |j        V  dS )r   N)ra   rb   )rZ   ts     r/   r\   z$parse_binary_data.<locals>.<genexpr>,  s*      ??A3??r1   )ra   join)rO   s    r/   r   r   *  s@     Q##xx????????sr1   N)r#   r   r!   r%   r2   rW   )rO   r   r2   rW   )r!   r%   r-   r   r2   r   )rw   r   r2   r   )r-   r   r2   r   )rO   r   r2   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]   re   rg   ri   rk   rl   rm   ro   rn   rp   rU   __all__r   r,   r`   rN   r{   r   r   r   r>   r1   r/   <module>r      s<   0 # " " " " "                        " " " " " " : : : : : : : : $ $ $ $ $ $ ! ! ! ! ! ! ! !       2 2 2 2 2 2 '&&&&&&  


(= = = = = = = =@
 
 
 
* * * *Z      D   ? ? ? ? ? ? ? ?D     r1   