a
    Sic                     @   s  d Z ddlZddlmZmZ ddlmZ ddlmZ ddl	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 eeddeeedddZeeddeeeedddZeeddeeeedddZd eeeedddZd!eeeedddZeedddZdS )"zHTools for generating QR codes. This module depends on the Segno library.    N)MappingAny)escape)	mark_safe)validate_arguments)PYDANTIC_CONFIG)DEFAULT_CACHE_ENABLEDDEFAULT_URL_SIGNATURE_ENABLED)make_qr_code_url)QRCodeOptions)configTdataqr_code_options
force_textc                 C   s<   |r&t jt| fi | ddiS t j| fi | S )aa  Creates a QR code that encodes the given `data` with the given `qr_code_options`.

    :param str data: The data to encode
    :param qr_code_options: Options to create and serialize the QR code.
    :param bool force_text: Tells whether we want to force the `data` to be considered as text string and encoded in byte mode.
    :rtype: segno.QRCode
    modebyte)segnomakestrZkw_maker    r   P/var/www/html/django/DPS/env/lib/python3.9/site-packages/qr_code/qrcode/maker.pymake_qr   s    "r   )r   r   r   returnc                 C   s4   t | ||d}t }|j|fi |  | S )aZ  
    Creates a bytes object representing a QR code image for the provided `data`.

    :param str data: The data to encode
    :param qr_code_options: Options to create and serialize the QR code.
    :param bool force_text: Tells whether we want to force the `data` to be considered as text string and encoded in byte mode.
    :rtype: bytes
    r   )r   ioBytesIOsavekw_savegetvalue)r   r   r   qroutr   r   r   make_qr_code_image    s    
r"   c           	   	   C   s   t | ||d}| }|d |jdkrt| trd}g d}|jrp||j}|dkrp|d ||< |j|d< |D ],}z| |}W  qW qt t	y   Y qt0 qtnt| t
st
| }n| }td|jf i | dt| d	S t|jf i |S )
z
    Generates a <svg> or <img> tag representing the QR code for the given `data`.
    This tag can be embedded into an HTML document.
    r   kindpng )zutf-8z
iso-8859-1z	shift-jisr   z
<img src="z" alt="z">)r   r   popZimage_format
isinstancebytesencodingindexdecodeUnicodeDecodeErrorr   r   Zpng_data_urir   Z
svg_inline)	r   r   r   r    kwalt	encodingseier   r   r   make_embedded_qr_code0   s.    








&r2   )r   qr_code_argsr   r   c                 C   s   t |}t| ||dS )Nr   )_options_from_argsr2   )r   r3   r   optionsr   r   r   make_qr_code_with_argsR   s    r6   c                 C   sZ   | dt}t|ts |dk }| dt}t|ts@|dk }t|}t| ||||dS )Ncache_enabledFalseurl_signature_enabled)r   r7   r9   )r&   r   r'   boolr	   r4   r
   )r   r3   r   r7   r9   r5   r   r   r   make_qr_code_url_with_argsW   s    



r;   )argsr   c                 C   sF   |  d}|r"t|tsBtdn dd |  D }tf i |}|S )z=Returns a QRCodeOptions instance from the provided arguments.r5   z3The options argument must be of type QRCodeOptions.c                 S   s"   i | ]\}}||d kr|ndqS )NoneNr   ).0kvr   r   r   
<dictcomp>j       z&_options_from_args.<locals>.<dictcomp>)getr'   r   	TypeErroritems)r<   r5   r-   r   r   r   r4   b   s    


r4   )T)T)T)T)T)__doc__r   typingr   r   django.utils.htmlr   django.utils.safestringr   r   Zpydanticr   Zqr_code.qrcoder   Zqr_code.qrcode.constantsr   r	   Zqr_code.qrcode.server
   qr_code.qrcode.utilsr   r:   r   r(   r"   r   r2   dictr6   r;   r4   r   r   r   r   <module>   s&   !