o
    Wg                    @   s  d Z ddlmZmZmZmZ ddlmZmZ ddl	m
Z
mZmZ ddlZddlZddlZddlmZ ddlmZ dd	l	mZ ddlZejZ[d
ZG dd deZeddZ		dtddZ		duddZdvddZdwddZdwddZ dd Z!dd Z"dd  Z#d!Z$d"d# Z%d$d% Z&d&d' Z'd(d) Z(d*d+ Z)d,d- Z*dvd.d/Z+d0d1 Z,d2d3 Z-d4d5 Z.d6d7 Z/d8d9 Z0d:d; Z1d<d= Z2d>d? Z3d@dA Z4dvdBdCZ5dxdDdEZ6dFdG Z7dHdI Z8dJdK Z9dwdLdMZ:dNdO Z;dPdQ Z<dRdS Z=e>dTe?ej@ dU ZAdVdW ZBdXdY ZCdZd[ ZDdwd\d]ZEd^d_ ZFd`da ZGdbdc ZHddde ZIdfdg ZJdhdi ZKdjdk ZLG dldm dmZMG dndo doeNZOG dpdq dqZPG drds dseNZQdS )yzQR Code and Micro QR Code encoder.

DOES NOT belong to the public API.

"QR Code" and "Micro QR Code" are registered trademarks of DENSO WAVE INCORPORATED.
    )
itemgettergtltxor)partialreduce)islicechainproductN)
namedtuple   )consts)zip_longest)encodeencode_sequenceDataOverflowErrorc                   @   s   e Zd ZdZdS )r   ab      Indicates a problem that the provided data does not fit into the
    provided QR Code version or the data is too large in general.

    This exception is inherited from :py:exc:`ValueError` and is only raised
    if the data does not fit into the provided (Micro) QR Code version.

    Basically it is sufficient to catch a :py:exc:`ValueError`.
    N)__name__
__module____qualname____doc__ r   r   J/var/www/rescue_company/venv/lib/python3.10/site-packages/segno/encoder.pyr      s    r   Codez"matrix version error mask segmentsFTc	                 C   sl  t |}|s|dur|tjv rtdt| d|r.|dur.|tjvr.tdt| dt|dd}t|}|durT|durTt||sTtdt| d	t| d|tj	krd|s`|tjv rdtd
|rq|sm|tjv rqtdt
| ||}	t|	|||d}
|du r|
}n|
|krtdt| dt|
 |du r|tjkrtj}|dk }t||}t|	|||||S )a      Creates a (Micro) QR code.

    See :py:func:`segno.make` for a detailed description of the parameters.

    Contrary to ``make`` this function returns a named tuple:
    ``(matrix, version, error, mask, segments)``

    Note that ``version`` is always an integer referring
    to the values of the :py:mod:`segno.consts` constants. ``error`` is ``None``
    iff a M1 QR Code was generated, otherwise it is always an integer.

    :rtype: namedtuple
    NzA Micro QR Code version ("z-") is provided but parameter "micro" is FalsezIllegal Micro QR Code version ""Taccept_nonezMode "z" is not available in version "z>Error correction level "H" is not available for Micro QR Codesz0The ECI mode is not available for Micro QR Codesecimicroz-The provided data does not fit into version "z"Proposal: version r   )normalize_versionr   MICRO_VERSIONS
ValueErrorget_version_namenormalize_errorlevelnormalize_modeis_mode_supportedget_mode_nameERROR_LEVEL_Hprepare_datafind_versionr   
VERSION_M1ERROR_LEVEL_Lnormalize_mask_encode)contenterrorversionmodemaskencodingr   r   boost_errorsegmentsguessed_versionis_micror   r   r   r   -   s8   
r   c	                    s6  fdddd }			d ddfd	d
}
t 			dur0	dk r/tdt	 dn|du r8td|durNd|  krIdksNtd tdtdddu r[tjttddt| }d}|du rz
t	|dd}W n	 t
y   Y nw |r|	p|krt|	p| dgS t|jdkrtd|jd tjkrt| } |durt| |k rtd| dt| }|pd}	dur|
| 	}|dkrt
d	 |	| |}|durt|td}t	|ddd	ttt|d |d 	fddt|D S )!z    EXPERIMENTAL: Creates a sequence of QR codes in Structured Append mode.

    :return: Iterable of named tuples, see :py:func:`encode` for details.
    c                    s   t  }|t| | d |S )z;        Creates a Segments sequence with one item.
        )r1   r3   )Segmentsadd_segmentmake_segment)chunkr1   segsr3   r   r   one_item_segmentsa   s   z*encode_sequence.<locals>.one_item_segmentsc                    s,   t t |\ fddt|D S )Nc                    s<   g | ]} | t | |d   t |d    qS )r   )min.0idatakmr   r   
<listcomp>k   s   < z?encode_sequence.<locals>.divide_into_chunks.<locals>.<listcomp>)divmodlenrange)rD   numr   rC   r   divide_into_chunksi   s   z+encode_sequence.<locals>.divide_into_chunksNFc           
      S   s  d}|t j| | 7 }|r|t jkr|t jkr|d7 }|d7 }|r%|d7 }d}|t jkrEt| d\}}	||d |	dkr=dnd 7 }|| S |t jkrat| d	\}}	||d
 |	rYdnd 7 }|| S |t jkrp|| d 7 }|| S |t jt jfv r~|| d 7 }|| S )N         r      
   r                  )	r   CHAR_COUNT_INDICATOR_LENGTH	MODE_BYTEDEFAULT_BYTE_ENCODINGMODE_NUMERICrH   MODE_ALPHANUMERIC
MODE_KANJI
MODE_HANZI)

char_count	ver_ranger1   r3   is_eciis_saoverheadbitsrK   	remainderr   r   r   calc_qrcode_bit_lengthm   s,   


z/encode_sequence.<locals>.calc_qrcode_bit_lengthc           	         sz   t | }t|} |||dd}tj| | }tt|| }|d|d  r0d|d  nd 7 }tt|| S )zH        Returns the number of symbols for the provided version.
        T)r`   ra   rO   r      r   )rI   version_ranger   SYMBOL_CAPACITYintmathceil)	r.   r0   r/   r1   lengthr_   
bit_lengthcapacitycnt)re   r   r3   r   r   number_of_symbols_by_version   s   
$z5encode_sequence.<locals>.number_of_symbols_by_versionr   zAThis function does not accept Micro QR Code versions. Provided: "r   z;Please provide either a QR Code version or the symbol count   z)The symbol count must be in range 1 .. 16Tr   )r7   r   )r/   r0   r2   r   r4   z<This function cannot handle more than one mode (yet). Sorry.r   z2The content is not long enough to be divided into z symbolsz5The data does not fit into Structured Append version )key)r   r   ra   )totalparityc                    s0   g | ]\}}t | |d qS ))r/   r0   r2   r   r4   sa_info)r-   )rA   rB   r;   )r4   r   r/   r2   r1   r>   ru   r0   r   r   rG      s    
z#encode_sequence.<locals>.<listcomp>)NFF)r   r!   r"   r#   r   r+   r$   r,   r(   r)   r   r-   rI   modesrZ   strcalc_structured_append_paritymaxr   _StructuredAppendInfo	enumerate)r.   r/   r0   r1   r2   r3   r   r4   symbol_countrL   rp   r5   r6   sa_parity_datanum_symbolschunksr   )
r4   re   r   r3   r/   r2   r1   r>   ru   r0   r   r   Z   sv   




r   c                 C   sP  |dk }|du}t  }	|}
|}|sd}
t|}|r"t||| ||d}|r:|dd D ]}|	|d q*|	|jd | D ]
}t|	||
|| q<tj| | }t|	||
t	|	 t
|	|t	|	 t|	||t	|	 t|||	}	t|}|}t||}t||| t||| t||	| t||||\}}t|||| t|| t||||| S )z    Creates a (Micro) QR code.

    NOTE: This function does not check if the input is valid and does not belong
    to the public API.
    r   Nra   rP   rM   rN   )Bufferrg   boost_error_levelappend_bitsrt   write_segmentr   rh   write_terminatorrI   write_padding_bitswrite_pad_codewordsmake_final_messagecalc_matrix_sizemake_matrixadd_finder_patternsadd_alignment_patternsadd_codewordsfind_and_apply_best_maskadd_format_infoadd_version_infor   )r5   r/   r0   r2   r   r4   ru   r7   sa_modebuffverr_   rB   segmentrn   widthheightmatrixr   r   r   r-      s>   

r-   c                 C   s   |t jdfvrLt|dkrLt jt jt jt jg}| dk r(|  | t jk r(|  |j| ||d}||	|d d D ]}t j
|  | |krI|}q; |S |S )a      Increases the error correction level if possible.

    Returns either the provided or a better error correction level which works
    while keeping the (Micro) QR Code version.

    :param int version: Version constant.
    :param int|None error: Error level constant or ``None``
    :param Segments segments: Instance of :py:class:`Segments`
    :param bool eci: Indicates if ECI designator should be written.
    :param bool is_sa: Indicates if Structured Append mode is used.
    Nr   r   )r   r'   rI   r+   ERROR_LEVEL_MERROR_LEVEL_Qpop
VERSION_M4bit_length_with_overheadindexrh   )r0   r/   r5   r   ra   levelsdata_lengtherror_levelr   r   r   r      s   
r   c                 C   s   |j }| j}|r!|tjkr!|jtjkr!|tjd |t|jd |du r7||d |tjkr6d}||d n|tj	krF|tj
| |d  ||jtj| |  | |j dS )a      Writes a segment.

    :param buff: The byte buffer.
    :param _Segment segment: The segment to serialize.
    :param ver: ``None`` if a QR Code is written, "M1", "M2", "M3", or "M4" if a
            Micro QR Code is written.
    :param ver_range: "M1", "M2", "M3", or "M4" if a Micro QR Code is written,
            otherwise a constant representing a range of QR Code versions.
    rM   rN   Nr   rP   )r1   r   r   rX   r3   rY   MODE_ECIget_eci_assignment_numberr]   r*   MODE_TO_MICRO_MODE_MAPPINGr^   rW   extendrc   )r   r   r   r_   r   r1   r   subsetr   r   r   r     s$   



r   c                 C   s$   |  dgt|| tj|   dS )a      Writes the terminator.

    :param buff: The byte buffer.
    :param capacity: Symbol capacity.
    :param ver: ``None`` if a QR Code is written, "M1", "M2", "M3", or "M4" if a
            Micro QR Code is written.
    :param length: Length of the data bit stream.
    r   N)r   r?   r   TERMINATOR_LENGTH)r   rn   r   rl   r   r   r   r   <     $r   c                 C   s0   |t jt jfvr| dgd|d    dS dS )z    Writes padding bits if the data stream does not meet the codeword boundary.

    :param buff: The byte buffer.
    :param int length: Data stream length.
    r   rN   N)r   r*   
VERSION_M3r   )r   r0   rl   r   r   r   r   J  s   r   c                 C   s^   | j }|tjtjfv r|dg||   dS d}t|d |d  D ]
}|||d   q"dS )a=      Writes the pad codewords iff the data does not fill the capacity of the
    symbol.

    :param buff: The byte buffer.
    :param int version: The (Micro) QR Code version.
    :param int capacity: The total capacity of the symbol (incl. error correction)
    :param int length: Length of the data bit stream.
    r   ))r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   rN   rS   N)r   r   r*   r   rJ   )r   r0   rn   rl   writepad_codewordsrB   r   r   r   r   ]  s   r   )		r   r   r   r   r   r   r   r   r   	r   r   r   r   r   r   r   r   r   	r   r   r   r   r   r   r   r   r   	r   r   r   r   r   r   r   r   r   r   r   r   r   r   c                 C   s   ||k}ddt | d fdf}|r|dk rd}td}|D ]/\}}|dkr'dnd}|dkr/dnd}	|D ]}
t||
  |	|	d  | ||
  ||d < q3qdS )	a      Adds the finder pattern(s) with the separators to the matrix.

    QR Codes get three finder patterns, Micro QR Codes have just one finder
    pattern.

    ISO/IEC 18004:2015(E) -- 6.3.3 Finder pattern (page 16)
    ISO/IEC 18004:2015(E) -- 6.3.4 Separator (page 17)

    :param matrix: The matrix.
    :param tuple(int, int) matrix_size: Tuple of width and height of the matrix.
    r   r   r   rN   )r      )r   r   N)rI   rJ   _FINDER_PATTERN)r   r   r   	is_squarecornersfinder_rangerB   joffset	sepoffsetrr   r   r   r     s   .r   c                 C   s`   |rdt | fndt | d f\}}| | }d}td|D ]}|| | |< |||< |dN }qdS )a4      Adds the (horizontal and vertical) timinig pattern to the provided `matrix`.

    ISO/IEC 18004:2015(E) -- 6.3.5 Timing pattern (page 17)

    :param matrix: Matrix to add the timing pattern into.
    :param bool is_micro: Indicates if the timing pattern for a Micro QR Code
        should be added.
    r   rU   rN   r   N)rI   rJ   )r   r7   r   stopcolbitrB   r   r   r   add_timing_pattern  s   $

r   c                 C   s   ||k}|d d }|r|dk rdS d}t j|d  }td}|d }|d }	||f||	f|	|ff}
t|dd	D ]/\}}||f|
v rCq8|d |d }}|D ]}||d |d d  | ||  ||d < qNq8dS )
ai      Adds the adjustment patterns to the matrix. For versions < 2 this is a
    no-op.

    ISO/IEC 18004:2015(E) -- 6.3.6 Alignment patterns (page 17)
    ISO/IEC 18004:2015(E) -- Annex E Position of alignment patterns (page 83)

    :param matrix: An iterable of bytearrays.
    :param tuple(int, int) matrix_size: Tuple of width and height of the matrix.
       rM   rS   N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      r   )repeat)r   ALIGNMENT_POSrJ   r
   )r   r   r   r   r0   pattern	positionsalignment_rangemin_posmax_posfinder_positionsxyrB   r   r   r   r   r   r     s$   .r   c                 C   s  t | }|dk }|tjtjfvrdnd}d}t |}td}t|d ddD ]N}	|s2|	dkr2|	d8 }	t|D ]=}
|D ]8}|	| }|	| d@ dk}|sP||dk N }|rX|d |
 n|
}| | }|| dkrr||k rr|| ||< |d7 }q:q6q&|t |krtd| dt | dd	S )
a7      Adds the codewords (data and error correction) to the provided matrix.

    ISO/IEC 18004:2015(E) -- 7.7.3 Symbol character placement (page 46)

    :param matrix: The matrix to add the codewords into.
    :param codewords: Sequence of bits
    :param int version: The (Micro) QR Code version constant.
    r   r   rS   rU   z9Internal error: Adding codewords to matrix failed. Added z of z
 codewordsN)rI   r   r*   r   rJ   r!   )r   	codewordsr0   matrix_sizer7   incidxcodeword_length	range_tworightverticalzr   upwardsrB   rowr   r   r   r     s<   

r   c           
   
   C   s   ddd}t j|  | }t||\}}d}| t jt jfv r)||d dd? d}t }|tt	|dd	 t
t| D   |durI|| |tt	|d
d	 t
t| D   d}	| dv rfd}	n| dv rmd}	n| dv rsd}	|d|	  |S )ad      Constructs the final message (codewords incl. error correction).

    ISO/IEC 18004:2015(E) -- 7.6 Constructing the final message codeword sequence (page 45)

    :param int version: (Micro) QR Code version constant.
    :param int error: Error level constant.
    :param buff: Byte buffer.
    :return: Byte buffer representing the final message.
    rN   c                    s    fddt t|D S )Nc                 3       | ]	} |? d @ V  qdS r   Nr   r@   valr   r   	<genexpr>	      z8make_final_message.<locals>.to_binary.<locals>.<genexpr>)reversedrJ   )r   rl   r   r   r   	to_binary  s   z%make_final_message.<locals>.to_binaryNr   r   rM   c                 s       | ]	}|d ur|V  qd S Nr   rA   r   r   r   r   r     r   z%make_final_message.<locals>.<genexpr>c                 s   r   r   r   r   r   r   r   r     r   )rS   rP   rM   r   rU   rR   )      rq   r         rO                   !   "   rP   )r                         )rN   )r   ECCmake_blocksr*   r   r   r   r   r	   mapfrom_iterabler   )
r0   r/   r   r   ec_infosdata_blockserror_blockscw_fourresrd   r   r   r   r     s(   
(
(r   c                 C   s  |  }g g }}|j}|j}tj}tj}| D ]g}	|	j|	j }
tj|
 }t|
}t|	j	D ]P}t
t||	j}|| t|}t
|}|dg|
  t|D ]%}|| }|dkrt|| }|D ]}||| d   ||||   N  < q_qO|||d  q-q||fS )z{    Returns the data and error blocks.

    :param ec_infos: Iterable of ECC information
    :param buff: Byte buffer.
    r   r   N)tointsappendr   
GALIOS_LOG
GALIOS_EXP	num_totalnum_dataGEN_POLYrJ   
num_blocks	bytearrayr   rI   r   )r   r   r   r   r   append_data_blockappend_error_blockgen_loggen_expec_infonum_error_wordsgenrange_error_wordsrB   blocklen_dataerror_blockrE   coeflcoefnr   r   r   r   ,  s4   

&r   c                    s   t }t}t}||ko|dk }|rt}d}t}t|| t || t || |s/d d d<  fdd}t|}	|durKt	| |	| ||| || fS d}
t
|	D ]&\}}d	d
 | D }t	||||| ||||}|||rw|}|}t|}
qQ||
fS )a      Applies all mask patterns against the provided QR Code matrix and returns
    the best matrix and best pattern.

    ISO/IEC 18004:2015(E) -- 7.8.2 Data mask patterns (page 50)
    ISO/IEC 18004:2015(E) -- 7.8.3 Evaluation of data masking results (page 53)
    ISO/IEC 18004:2015(E) -- 7.8.3.1 Evaluation of QR Code symbols (page 53/54)
    ISO/IEC 18004:2015(E) -- 7.8.3.2 Evaluation of Micro QR Code symbols (page 54/55)

    :param matrix: A matrix.
    :param tuple(int, int) matrix_size: Tuple of width and height of the matrix.
    :param proposed_mask: Optional int to indicate the preferred mask.
    :rtype: tuple
    :return: A tuple of the best matrix and best data mask pattern index.
    r   r   r   r   rN   c                    s    |  | dkS )Nr   r   rB   r   function_matrixr   r   is_encoding_regiont     z4find_and_apply_best_mask.<locals>.is_encoding_regionNc                 S   s   g | ]}|d d  qS r   r   )rA   bar   r   r   rG     s    z,find_and_apply_best_mask.<locals>.<listcomp>)r   _MAX_PENALTY_SCOREevaluate_maskr   evaluate_micro_maskr   r   r   get_data_mask_functions
apply_maskr{   tuple)r   r   r   proposed_mask	is_better
best_score	eval_maskr7   r  mask_patternsbest_matrixmask_numbermask_patternrF   scorebest_patternr   r  r   r   N  s>   

r   c           	      C   sL   t |}t |D ]}| | }|D ]}|||r"||  |||N  < qqdS )a      Applies the provided mask pattern on the `matrix`.

    ISO/IEC 18004:2015(E) -- 7.8.2 Data mask patterns (page 50)

    :param tuple matrix: A tuple of bytearrays
    :param mask_pattern: A mask pattern (a function)
    :param int matrix_size: width or height of the matrix
    :param is_encoding_region: A function which returns ``True`` iff the
            row index / col index belongs to the data region.
    N)rJ   )	r   r'  r   r   r  width_rangerB   r   r   r   r   r   r    s   
r  c                 C   s   t t| ||S )a!      Evaluates the provided `matrix` of a QR code.

    ISO/IEC 18004:2015(E) -- 7.8.3 Evaluation of data masking results (page 53)

    :param matrix: The matrix to evaluate
    :param matrix_size: The width (or height) of the matrix.
    :return int: The penalty score of the matrix.
    )summask_scores)r   r   r   r   r   r   r    s   
r  c                    s  t d  fdd}d}d}d}||ksJ |t}d}d}	t }
|D ]}| | }d}d}d}d}|D ]a}|| }| | | }||
|< ||7 }||krT|d7 }n|dkr^||d	 7 }d}||kri|d7 }n|dkrs||d	 7 }d}|	r|r||  kr|	|   kr|	|d  krn n|d
7 }|}|}q7|}	|||7 }|||
7 }|dkr||d	 7 }|dkr||d	 7 }q't|d	  }dtt|d d d  }||||fS )u      Returns the penalty score features of the matrix.

    The returned value is a tuple of all penalty scores (N1, N2, N3, N4).
    Use :py:func:`evaluate_mask` for a single value (sum of all scores).


    ISO/IEC 18004:2015(E) -- 7.8.3 Evaluation of data masking results - Table 11 (page 54)

    ============================================   ====================================   ===============
    Feature                                        Evaluation condition                   Points
    ============================================   ====================================   ===============
    Adjacent modules in row/column in same color   No. of modules = (5 + i)               N1 + i
    Block of modules in same color                 Block size = m × n                     N2 ×(m-1)×(n-1)
    1 : 1 : 3 : 1 : 1 ratio                        Existence of the pattern               N3
    (dark:light:dark:light:dark) pattern in
    row/column, preceded or followed by light
    area 4 modules wide
    Proportion of dark modules in entire symbol    50 × (5 × k)% to 50 × (5 × (k + 1))%   N4 × k
    ============================================   ====================================   ===============

    N1 = 3
    N2 = 3
    N3 = 40
    N4 = 10

    :param matrix: The matrix to evaluate
    :param matrix_size: The width (or height) of the matrix.
    :return tuple: A tuple of penalty scores (ints): ``(n1, n2, n3, n4)``.
    )r   r   r   r   r   r   r   c                    s   d}|   }|dkrJ|d }|dd fv s7t| t|d dt| r7t| t|dt|d  s<|d7 }n|d }|   |}|dks|S )Nr   r   rR   rM   (   )findanyry   r?   )seqcountr   r   
n3_patternqr_sizer   r   n3_pattern_occurrences  s   

z+mask_scores.<locals>.n3_pattern_occurrencesr   Nr   r   r   rS   rP   rQ   d   2   )r  rJ   floatri   abs)r   r   r   r5  score_n1score_n2score_n3qr_module_rangedark_module_counterlast_row	n3_columnrB   r   row_prev_bitcol_prev_bitn1_row_countern1_col_counterr   row_current_bitcol_current_bitpercentscore_n4r   r2  r   r,    s^   

6r,  c                    s^   t d|}d  tfdd|D }t fdd|D }||kr)|d | S |d | S )a*      Evaluates the provided `matrix` of a Micro QR code.

    ISO/IEC 18004:2015(E) -- 7.8.3.2 Evaluation of Micro QR Code symbols (page 54)

    :param matrix: The matrix to evaluate
    :param matrix_size: The width (or height) of the matrix.
    :return int: The penalty score of the matrix.
    r   r   c                 3   s    | ]	} | d  V  qdS )r   Nr   r@   )r   r   r   r   %  r   z&evaluate_micro_mask.<locals>.<genexpr>c                 3   s    | ]} | V  qd S r   r   r@   )r?  r   r   r   &      rq   )rJ   r+  )r   r   r   module_rangesum1sum2r   )r?  r   r   r    s
   

 r  c                 C   sx   |}| dkr*|t jkr|d7 }n|t jkr|d7 }n	|t jkr#|d7 }t j| }|S |t j|  | d> 7 }t j| }|S )uz      Returns the format information for the provided error level and mask patttern.

    ISO/IEC 18004:2015(E) -- 7.9 Format information (page 55)
    ISO/IEC 18004:2015(E) -- Table C.1 — Valid format information bit sequences (page 80)

    :param int version: Version constant
    :param int error: Error level constant.
    :param int mask_pattern: Mask pattern number.
    r   rN   rq   r   rS   )r   r+   r'   r   FORMAT_INFOERROR_LEVEL_TO_MICRO_MAPPINGFORMAT_INFO_MICRO)r0   r/   r'  fmtformat_infor   r   r   calc_format_info*  s   






rR  c                 C   s   |dk }t |||}t|}|}| d }tdD ]:}	||	? d@ }
|d|	 ? d@ }|	dkr4|s4|d7 }d}|
| |	|  d< |||	| < |sR|
|d|	 < || d|	  d< q|s]d| d d< dS dS )a      Adds the format information into the provided matrix.

    ISO/IEC 18004:2015(E) -- 7.9 Format information (page 55)
    ISO/IEC 18004:2015(E) -- 7.9.1 QR Code symbols
    ISO/IEC 18004:2015(E) -- 7.9.2 Micro QR Code symbols

    :param matrix: The matrix.
    :param int version: Version constant
    :param int error: Error level constant.
    :param int mask_pattern: Mask pattern number.
    r   rN   r   rU   r   r   N)rR  ri   rJ   )r   r0   r/   r'  r7   rQ  voffsethoffset	row_eightrB   vbithbitr   r   r   r   D  s(   %r   c                 C   s   |dk rdS t j|d  }tdD ]@}||d ? d@ }||d d ? d@ }||d d ? d@ }|| d |< || d |< || d	 |< | | }||d< ||d< ||d	< qdS )
z    Adds the version information to the matrix, for versions < 7 this is a no-op.

    ISO/IEC 18004:2015(E) -- 7.10 Version information (page 58)
    rR   NrU   rP   r   rS   )r   VERSION_INFOrJ   )r   r0   version_inforB   bit1bit2bit3r   r   r   r   r     s   
r   c           	      C   s   t  }|j}t| tttfr|t| || |S | D ]3}|||}}}t|trE|d }t|dkr9|d p8|}t|dkrE|d pD|}|t||| q|S )a      Returns an iterable of `Segment` instances.

    If `content` is a string, an integer, or bytes, the returned tuple will
    have a single item. If `content` is a list or a tuple, a tuple of
    `Segment` instances of the same length is returned.

    :param content: Either a string, bytes, an integer or an iterable.
    :type content: str, bytes, int, tuple, list or any iterable
    :param mode: The global mode. If `content` is list/tuple, the `Segment`
            instances may have a different mode.
    :param encoding: The global encoding. If `content` is a list or a tuple,
            the `Segment` instances may have a different encoding.
    :rtype: Segments
    r   r   rS   )	r8   r9   
isinstancerw   bytesri   r:   r  rI   )	r.   r1   r3   r5   r9   itemseg_contentseg_modeseg_encodingr   r   r   r(     s   
r(   c                 C   s   t | tr| t| |ptjfS t| } |dur| |} n/z
tj}| |} W n$ tyK   z
tj}| |} W n tyH   d}| |} Y nw Y nw | t| |fS )a      Converts the provided data into bytes. If the data is already a byte
    sequence, it will be left unchanged.

    This function tries to use the provided `encoding` (if not ``None``)
    or the default encoding (ISO/IEC 8859-1). It uses UTF-8 as fallback.

    Returns the (byte) data, the length of the data and the encoding of the data.

    :param data: The data to encode
    :type data: str or bytes
    :param encoding: str or ``None``
    :rtype: tuple: data, data length, encoding
    Nutf-8)	r`  ra  rI   r   rY   rw   r   UnicodeErrorKANJI_ENCODING)rD   r3   r   r   r   data_to_bytes  s&   
	ri  c                 C   s  |t jkrt j}t| |\}}}|}|t jkrt|nt j}|dur8||k r7tdt| d|dt| n|}|t jkrAd}|t jt jfvrK|n|d }t	 }	|	j
}
|t jkrytd|dD ]}|||d  }|
t|t|d d  q`n|t jkrt jj}td|dD ])}|||d  }t|dkr|
||d d	 ||d  d
 q|
||d qn|t jkr|D ]}|
|d qn|t jkrtd|dD ]D}|| d> ||d  B }d|  krdkrn n|d }nd|  krdkrn n|d }ntd| |
|d? d |d@  d qnPtd|dD ]I}|| d> ||d  B }d|  kr5dkr<n n|d }nd|  krHdkrOn n|d }ntd| |
|d? d |d@  d qt|	 |||S )z    Creates a :py:class:`Segment`.

    :param data: The segment data
    :param mode: The mode.
    :param encoding: The encoding.
    :rtype: _Segment
    NzThe provided mode "z" is not applicable for z. Proposal: rS   r   rP   r   -   rT   rU   rN   i  i  i  i  i  zInvalid Hanzi bytes: `      rV   @    @    i@  zInvalid Kanji bytes:    )r   r]   HANZI_ENCODINGri  rX   	find_moder!   r&   r\   r   r   rZ   rJ   ri   rI   r[   ALPHANUMERIC_CHARSr.  _Segmentgetbits)rD   r1   r3   segment_datasegment_lengthsegment_encodingsegment_modeguessed_moder^   r   r   rB   r;   to_bytebcodediffr   r   r   r:     sn   
	


$




r:   c           	         s   | |k}|o	| dk }dg|   t  fddt|D }|rp|rM| dkrMtdD ]$}||  d d< d d	< d d
< d|d |< d|d	 |< d|d
 |< q(|d }tdD ]}d|| d< d||< |sod||  d< d|| < qU|rwt|| |S )aH      Creates a matrix of the provided `size` (w x h) initialized with the
    (illegal) value 0x2.

    The "timing pattern" is already added to the matrix and the version
    and format areas are initialized with 0x0.

    :param int width: Matrix width
    :param int height: Matrix height.
    :rtype: tuple of bytearrays
    r   rS   c                 3   s    | ]}t  V  qd S r   )r  r@   r   r   r   r   d  rI  zmake_matrix.<locals>.<genexpr>)   rU   r   rX  rY  rZ  rN   	   )r  rJ   r   )	r   r   reserve_regions
add_timingr   r7   r   rB   rU  r   r  r   r   U  s2   


r   c                 C   s   | du rdS d}z
t | } | dk }W n" ttfy4   z	tj|   } W n ttfy1   d}Y nw Y nw |sGd|   k rAdk sZn | tjvrZtd|  dd		t
tj  d
| S )a      Canonicalization of the provided `version`.

    If the `version` is ``None``, this function returns ``None``. Otherwise
    this function checks if `version` is an integer or a Micro QR Code version.
    In case the string represents a Micro QR Code version, an uppercased
    string identifier is returned.

    If the `version` does not represent a valid version identifier (aside of
    ``None``) a :py:exc:`ValueError` is raised.

    :param version: An integer, a string or ``None``.
    :raises: :py:exc:`ValueError`: In case the version is not ``None`` and does not
                represent a valid (Micro) QR Code version.
    :rtype: int, str or ``None``
    NFr   Tr   r  zUnsupported version "z". Supported: , z and 1 .. 40)ri   r!   	TypeErrorr   MICRO_VERSION_MAPPINGupperKeyErrorAttributeErrorr    joinsortedkeys)r0   r/   r   r   r   r     s&   $

r   c                 C   sb   | du s| t j v r| S zt j|   W S  ttfy0   td|  ddtt j	  w )a      Returns a (Micro) QR Code mode constant which is equivalent to the
    provided `mode`.

    In case the provided `mode` is ``None``, this function returns ``None``.
    Otherwise a mode constant is returned unless the provided parameter cannot
    be mapped to a valid mode. In the latter case, a :py:exc:`ValueError` is raised.

    :param mode: An integer or string or ``None``.
    :raises: :py:exc:`ValueError` In case the provided `mode` does not represent a valid
             QR Code mode.
    :rtype: int or None
    NzIllegal mode "z". Supported values: r  )
r   MODE_MAPPINGvalueslowerr  r  r!   r  r  r  )r1   r   r   r   r$     s   
r$   c                 C   s   | du rdS zt | } W n ty   td|  dw |r3d|   kr(dk s1n td|  d| S d|   kr=dk sFn td|  d| S )	a      Normalizes the (user specified) mask.

    :param mask: A mask constant
    :type mask: int or None
    :param bool is_micro: Indicates if the mask is meant to be used for a
            Micro QR Code.
    :raises: :py:exc:`ValueError` in case of an invalid mask.
    :rtype: int
    NzInvalid data mask "zD". Must be an integer or a string which represents an integer value.r   rM   z," for Micro QR Code. Must be in range 0 .. 3rN   z". Must be in range 0 .. 7)ri   r!   )r2   r7   r   r   r   r,     s   r,   c              	   C   sb   | du r|s
t d| S ztj|   W S  ttfy0   | tj v r(|  Y S t d|  dw )a4      Returns a constant for the provided error level.

    This function returns ``None`` if the provided parameter is ``None`` and
    `accept_none` is set to ``True`` (default: ``False``). If `error` is ``None``
    and `accept_none` is ``False`` or if the provided parameter cannot be
    mapped to a valid QR Code error level, a :py:exc:`ValueError` is raised.

    :param error: String or ``None``.
    :param bool accept_none: Indicates if ``None`` is accepted as error level.
    :raises: :py:exc:`ValueError` in case of an invalid mode.
    :rtype: int
    Nz The error level must be providedz!Illegal error correction level: "z". Supported levels: L, M, Q, H)r!   r   ERROR_MAPPINGr  r  r  r  )r/   r   r   r   r   r#     s   r#   c                 C   4   t j D ]\}}|| kr|  S qtd|  d)z    Returns the mode name for the provided mode constant.

    :param int mode_const: The mode constant (see :py:module:`segno.consts`)
    :raises: :py:exc:`ValueError` in case of an unknown mode constant.
    :rtype: str
    Unknown mode "r   )r   r  itemsr!   )
mode_constnamer   r   r   r   r&     
   r&   c                 C   r  )z    Returns the error name for the provided error constant.

    :param int error_const: The error constant (see :py:module:`segno.consts`)
    :raises: :py:exc:`ValueError` in case of an unknown error correction level.
    :rtype: str
    zUnknown error level "r   )r   r  r  r!   )error_constr  r   r   r   r   get_error_name  r  r  c                 C   sL   d|   k rdk r| S  t j D ]\}}|| kr|  S qtd|  d)a      Returns the version name.

    For version 1 .. 40 it returns the version as integer, for Micro QR Codes
    it returns a string like ``M1`` etc.

    :raises: :py:exc:`VersionError`: In case the `version_constant` is unknown.
    :rtype: str or int
    r   r  zUnknown version constant "r   )r   r  r  r!   )version_constr  vr   r   r   r"     s   
r"   s   ^[s   ]+\Zc                 C   s
   t | S )z    Returns if the provided `data` can be encoded in "alphanumeric" mode.

    :param bytes data: The data to check.
    :rtype: bool
    )_ALPHANUMERIC_PATTERNmatchrD   r   r   r   is_alphanumeric   s   
r  c                 C   s   t | }|r
|d rdS t| }td|dD ]'}t|d> t|B }d|  kr,dks=n d|  kr9dks= dS   dS qd	S )
zz    Returns if the `data` can be encoded in "kanji" mode.

    :param bytes data: The data to check.
    :rtype: bool
    rS   Fr   rN   rm  rn  ro  rp  T)rI   iterrJ   next)rD   data_len	data_iterrB   r~  r   r   r   is_kanji*  s   (r  c                 C   s0   |   rtjS t| rtjS t| rtjS tjS )z    Returns the appropriate QR Code mode (an integer constant) for the
    provided `data`.

    :param bytes data: Data to check.
    :rtype: int
    )isdigitr   rZ   r  r[   r  r\   rX   r  r   r   r   rs  <  s   rs  c           
   	   C   s   |r|rJ |p|du }|rt jnd}|rt jnd}|dk r(tdd | jD }|dur1|r1t j}t||d D ],}|du rF|t jkrFt j}zt j| | | 	|||krZ|W   S W q8 t
yd   Y q8w d}	|du rnd}	n|rrd}	td	|	 d
)a)      Returns the minimal (Micro) QR Code version constant for the provided input.

    :param segments: Iterable of Segment instances.
    :param error: The error correction level constant.
    :type error: int or None
    :param bool eci: Indicates if the ECI mode should be used.
    :param micro: Boolean value if a Micro QR Code should be created or ``None``
    :type micro: bool or None
    :param bool is_sa: Indicator if Structured Append is used.
    :raises: :py:exc:`ValueError` if the content does not fit into a QR Code.
    :rtype: int
    Nr   r-  c                 S   s   g | ]}t |qS r   )find_minimum_version_for_moderA   r1   r   r   r   rG   `  s    z find_version.<locals>.<listcomp> z(Micro) zMicro zData too large. No z$QR Code can handle the provided data)r   r*   r   ry   rv   
VERSION_M2rJ   r+   rh   r   r  r   )
r5   r/   r   r   ra   micro_allowedmin_versionmax_versionr0   help_txtr   r   r   r)   M  s0   
r)   c                 C   s$   | dkr
| d d S | d d d S )a.      Returns the matrix size according to the provided `version`.

    Note: This function does not check if `version` is actually a valid
    (Micro) QR Code version. Invalid versions like ``41`` may return a
    size as well.

    :param int ver: (Micro) QR Code version constant.
    :rtype: int
    r   rM   r   rS   r  r   )r   r   r   r   r   s  r   r   c                 C   sn   t | ts	t| } z| d}W n! ty1   z| d}W n ttfy.   | d}Y nw Y nw tt|S )zy    Calculates the parity data for the Structured Append mode.

    :param str content: The content.
    :rtype: int
    z
iso-8859-1z	shift-jisrf  )r`  rw   r   rg  LookupErrorr   r   )r.   rD   r   r   r   rx     s   

rx   c                 C   s@   |dkrdn|}z|t j|  v W S  ty   td|  dw )aR      Returns if `mode` is supported by `version`.

    Note: This function does not check if `version` is actually a valid
    (Micro) QR Code version. Invalid versions like ``41`` may return an illegal
    value.

    :param int mode: Canonicalized mode.
    :param int or None ver: (Micro) QR Code version constant.
    :rtype: bool
    r   Nr  r   )r   SUPPORTED_MODESr  r!   )r1   r   r   r   r   r%     s   r%   c                 C   s"   t jD ]}t| |r|  S qdS )z    Returns the minimum Micro QR Code version which supports the provided mode.

    :param int mode: Canonicalized mode.
    :rtype: int
    r   )r   r    r%   )r1   r  r   r   r   r    s
   

r  c                 C   s^   d|   k rdk rt jS  d|   k rdk rt jS  d|   k r&dk r't jS  td|  d)	z    Returns the version range for the provided version. This applies to QR Code
    versions, only.

    :param int version: The QR Code version (1 .. 40)
    :rtype: int
    r   rQ   r  r   r   r  zUnknown version "r   )r   VERSION_RANGE_01_09VERSION_RANGE_10_26VERSION_RANGE_27_40r!   )r0   r   r   r   rg     s   
rg   c                 C   s4   z
t jt| j W S  ty   td|  dw )z    Returns the ECI number for the provided encoding.

    :param str encoding: A encoding name
    :return str: The ECI number.
    z,Unknown ECI assignment number for encoding "z".)r   ECI_ASSIGNMENT_NUMcodecslookupr  r  r!   r=   r   r   r   r     s
   r   c           	      C   sd   dd }dd }dd }dd }d	d
 }dd }dd }dd }| r(||||fS ||||||||fS )u  
    Returns the data mask functions.

    ISO/IEC 18004:2015(E) -- 7.8.2 Data mask patterns (page 50)
    Table 10 — Data mask pattern generation conditions (page 50)

    ===============     =====================   =====================================
    QR Code Pattern     Micro QR Code Pattern￼  Condition
    ===============     =====================   =====================================
    000                                         (i + j) mod 2 = 0
    001                 00                      i mod 2 = 0
    010                                         j mod 3 = 0
    011                                         (i + j) mod 3 = 0
    100                 01                      ((i div 2) + (j div 3)) mod 2 = 0
    101                                         (i j) mod 2 + (i j) mod 3 = 0
    110                 10                      ((i j) mod 2 + (i j) mod 3) mod 2 = 0
    111                 11                      ((i+j) mod 2 + (i j) mod 3) mod 2 = 0
    ===============     =====================   =====================================

    :param is_micro: Indicates if data mask functions for a Micro QR Code
            should be returned
    :return: A tuple of functions
    c                 S   s   | | d@ dkS Nr   r   r   r  r   r   r   fn0  r  z$get_data_mask_functions.<locals>.fn0c                 S   s   | d@ dkS r  r   r  r   r   r   fn1     z$get_data_mask_functions.<locals>.fn1c                 S   s   |d dkS NrP   r   r   r  r   r   r   fn2  r  z$get_data_mask_functions.<locals>.fn2c                 S   s   | | d dkS r  r   r  r   r   r   fn3  r  z$get_data_mask_functions.<locals>.fn3c                 S   s   | d |d  d@ dkS )NrS   rP   r   r   r   r  r   r   r   fn4  s   z$get_data_mask_functions.<locals>.fn4c                 S   s   | | }|d@ |d  dkS Nr   rP   r   r   rB   r   tmpr   r   r   fn5  s   z$get_data_mask_functions.<locals>.fn5c                 S   s    | | }|d@ |d  d@ dkS r  r   r  r   r   r   fn6   s   z$get_data_mask_functions.<locals>.fn6c                 S   s    | | d@ | | d  d@ dkS r  r   r  r   r   r   fn7  s    z$get_data_mask_functions.<locals>.fn7r   )	r7   r  r  r  r  r  r  r  r  r   r   r   r    s   r  c                   @   sF   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dddZ
dS )r8   z    Represents a sequence of `Segment` instances.

    Note: len(segments) returns the number of Segments and not the data length;
    use segments.data_length
    )rm   rv   r5   c                 C   s   g | _ d| _g | _d S )Nr   )r5   rm   rv   selfr   r   r   __init__  s   
zSegments.__init__c                 C   s   | j r7| j d }|j|jkr7|j|jkr7t|j|j |j|j |j|j}|  jt|j8  _| j d= | jd= | j 	| |  jt|j7  _| j	|j dS )z:
        :param _Segment segment: Segment to add.
        r   N)
r5   r1   r3   ru  rc   r^   rm   rI   rv   r   )r  r   prev_segr   r   r   r9     s   

zSegments.add_segmentc                 C   
   t | jS r   )rI   r5   r  r   r   r   __len__-     
zSegments.__len__c                 C   
   | j | S r   )r5   r  rb  r   r   r   __getitem__0  r  zSegments.__getitem__c                 C   r  r   )r  r5   r  r   r   r   __iter__3  r  zSegments.__iter__Fc                    s   d}|rt dd | jD }||d 7 }||d 7 }|r |d7 }|dkr.|t| jd 7 }n|tjkr>|t| j|d  7 }|dkrFt|n| |t  fdd| jD 7 }|| j S )	Nr   c                 s   s,    | ]}|j tjkr|jtjkrd V  qdS r   )r1   r   rX   r3   rY   )rA   r   r   r   r   r   :  s    z4Segments.bit_length_with_overhead.<locals>.<genexpr>rM   rN   rO   rP   c                 3   s    | ]
}t j|   V  qd S r   )r   rW   r  r_   r   r   r   J  s    )r+  r5   rI   rv   r   r*   rg   rm   )r  r0   r   ra   rb   no_eci_indicatorsr   r  r   r   6  s   

z!Segments.bit_length_with_overheadNF)r   r   r   r   	__slots__r  r9   r  r  r  r   r   r   r   r   r8     s    r8   c                   @   sN   e Zd ZdZdZd
ddZeedZeedZ	eedZ
eed	ZdS )ru  aN      Represents a data segment.

    A segment provides the (encoding specific) byte data, the data length,
    the QR Code mode, and the used encoding. The latter is ``None`` iff mode
    is not "byte".

    Note that `data_length` may not be equal to len(data)!

    See also ISO/IEC 18004:2015(E) - 7.4.7 Mixing modes (page 30)
    r   Nc                 C   s   t | ||||fS r   )r  __new__)clsrc   r^   r1   r3   r   r   r   r  \  s   z_Segment.__new__r   r   rS   rP   r   )r   r   r   r   r  r  propertyr   rc   r^   r1   r3   r   r   r   r   ru  N  s    
ru  c                   @   sP   e Zd ZdZdgZdddZdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdS )r   zO    Wraps a :cls:`bytearray` and provides some useful methods to add bits.
    _datar   c                 C   s   t || _d S r   )r  r  r  iterabler   r   r   r  k  s   zBuffer.__init__c                 C   s   | j | d S r   )r  r   r  r   r   r   r   n  r  zBuffer.extendc                    s&   | j  fddtt|D  d S )Nc                 3   r   r   r   r@   r   r   r   r   r  r   z%Buffer.append_bits.<locals>.<genexpr>)r  r   r   rJ   )r  r   rl   r   r   r   r   q  s   &zBuffer.append_bitsc                 C   s   | j S r   r  r  r   r   r   rv  t  s   zBuffer.getbitsc                 C   s$   dd t t| jgd ddiD S )z        Returns an iterable of integers interpreting the content of `seq`
        as sequence of binary numbers of length 8.
        c                 s   s&    | ]}t d tt|dV  qdS )r  rS   N)ri   r  r   rw   )rA   gr   r   r   r   |  s   $ z Buffer.toints.<locals>.<genexpr>rN   	fillvaluer   )r   r  r  r  r   r   r   r   w  s   $zBuffer.tointsc                 C   r  r   )rI   r  r  r   r   r   r  ~  r  zBuffer.__len__c                 C   r  r   r  r  r   r   r   r    r  zBuffer.__getitem__N)r   )r   r   r   r   r  r  r   r   rv  r   r  r  r   r   r   r   r   e  s    
r   c                       sT   e Zd ZdZdZ fddZeedZeedZ	eedZ
eedZ  ZS )	rz   z    Represents Structured Append information.

    Note: This class provides the Structured Append header information in
    correct order (incl. Structured Append mode indicator); cf.
    ISO/IEC 18004:2015(E) -- 8 Structured Append (page 59).
    r   c                    s   t  | tj|||fS )z        :param int number: Symbol number ``[0 .. 15]``
        :param int total: Total symbol count ``[2 .. 15]``
        :param int parity: Parity data.
        )superr  r   MODE_STRUCTURED_APPEND)r  numberrs   rt   	__class__r   r   r    s   z_StructuredAppendInfo.__new__r   r   rS   rP   )r   r   r   r   r  r  r  r   r1   r  rs   rt   __classcell__r   r   r  r   rz     s    rz   )NNNNNFNT)NNNNNFTNr   r  )TT)Rr   operatorr   r   r   r   	functoolsr   r   	itertoolsr   r	   r
   rerj   r  collectionsr   r  r   r   sysmaxsizer  __all__r!   r   r   r   r   r-   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r,  r  rR  r   r   r(   ri  r:   r   r   r$   r,   r#   r&   r  r"   compileescapert  r  r  r  rs  r)   r   rx   r%   r  rg   r   r  r8   r  ru  r   rz   r   r   r   r   <module>   s   

-

p
6
"+/
">l>'!
&
e+#


&8B 