o
    tBh                     @   sT   d Z dZddlmZ ddlmZ ddlmZ ddl	m
Z
mZ G dd deZeZd	S )
z/Module: barcode.upc

:Provided barcodes: UPC-A
zrestructuredtext en    )reduce)Barcode)upc)IllegalCharacterErrorNumberOfDigitsErrorc                   @   sX   e Zd ZdZdZdZdddZdd	 ZeZd
d Z	dd Z
dd Zdd ZdddZdS )UniversalProductCodeAzTUniversal Product Code (UPC) barcode.

    UPC-A consists of 12 numeric digits.
    zUPC-A   NFc                 C   sr   || _ |d| j }| stdt|| jkr$td| jt||| _d||  | _|p5t	
 | _dS )a  Initializes new UPC-A barcode.

        :param str upc: The upc number as string.
        :param writer: barcode.writer instance. The writer to render the
            barcode (default: SVGWriter).
        :param bool make_ean: Indicates if a leading zero should be added to
            the barcode. This converts the UPC into a valid European Article
            Number (EAN).
        Nz"UPC code can only contain numbers.z UPC must have {} digits, not {}.z{}{})eandigitsisdigitr   lenr   formatr   calculate_checksumr   default_writerwriter)selfr   r   make_ean r   b/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/barcode/upc.py__init__   s   
zUniversalProductCodeA.__init__c                 C      | j rd| j S | jS N0r	   r   r   r   r   r   __unicode__.      
z!UniversalProductCodeA.__unicode__c                 C   r   r   r   r   r   r   r   get_fullcode6   r   z"UniversalProductCodeA.get_fullcodec                 C   sd   dd }| j d| j }t||ddd }t||ddd }||d  d }|dkr.dS d| S )	zuCalculates the checksum for UPCA/UPC codes

        :return: The checksum for 'self.upc'
        :rtype: int
        c                 S   s   t | t | S )N)int)xyr   r   r   sum_C   s   z6UniversalProductCodeA.calculate_checksum.<locals>.sum_r   N         
   )r   r
   r   )r   r!   r   oddsumevensumcheckr   r   r   r   <   s   z(UniversalProductCodeA.calculate_checksumc                 C   s   t jdd }t| jdd D ]\}}|t jd t| 7 }q|t j7 }| jdd D ]}|t jd t| 7 }q,|t j7 }|gS )zoBuilds the barcode pattern from 'self.upc'

        :return: The pattern as string
        :rtype: str
        Nr      LR)_upcEDGE	enumerater   CODESr   MIDDLE)r   code_inumberr   r   r   buildO   s   

zUniversalProductCodeA.buildc                 C   s<   |   }t|D ]\}}|dddd||< qd|S )zMReturns an ascii representation of the barcode.

        :rtype: str
        1|r   _
)r4   r.   replacejoin)r   r1   iliner   r   r   to_asciic   s   
zUniversalProductCodeA.to_asciic                 C   s$   ddi}| |p	i  t| ||S )Nmodule_widthgQ?)updater   render)r   writer_optionstextoptionsr   r   r   r@   n   s   zUniversalProductCodeA.render)NF)NN)__name__
__module____qualname____doc__namer
   r   r   __str__r   r   r4   r=   r@   r   r   r   r   r      s    
r   N)rG   __docformat__	functoolsr   barcode.baser   barcode.charsetsr   r,   barcode.errorsr   r   r   UPCAr   r   r   r   <module>   s    f