o
    fik                     @  sh   d Z ddlmZ dZddlmZ ddlmZ ddlm	Z
 ddlmZ ddlmZ G d	d
 d
eZeZdS )z/Module: barcode.upc

:Provided barcodes: UPC-A
    )annotationszrestructuredtext en)reduce)Barcode)upc)IllegalCharacterError)NumberOfDigitsErrorc                      sd   e Zd ZdZdZdZdddd	ZdddZdd Zdd Z	dddZ
dddZd fdd	Z  ZS )UniversalProductCodeAzTUniversal Product Code (UPC) barcode.

    UPC-A consists of 12 numeric digits.
    zUPC-A   NFreturnNonec                 C  sx   || _ |d| j }| stdt|| jkr'td| j dt| d|| _| |   | _|p8|  | _	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 z digits, not .)
eandigitsisdigitr   lenr   r   calculate_checksumdefault_writerwriter)selfr   r   make_ean r   ?/home/ubuntu/.local/lib/python3.10/site-packages/barcode/upc.py__init__   s   
zUniversalProductCodeA.__init__strc                 C     | j rd| j S | jS N0r   r   r   r   r   r   __str__1      
zUniversalProductCodeA.__str__c                 C  r   r   r   r   r   r   r   get_fullcode7   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_D   s   z6UniversalProductCodeA.calculate_checksum.<locals>.sum_r   N         
   )r   r   r   )r   r%   r   oddsumevensumcheckr   r   r   r   =   s   z(UniversalProductCodeA.calculate_checksum	list[str]c                 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 )zBuilds the barcode pattern from 'self.upc'

        :return: The pattern as string
        :rtype: List containing the string as a single element
        Nr      LR)_upcEDGE	enumerater   CODESr"   MIDDLE)r   code_inumberr   r   r   buildP   s   

zUniversalProductCodeA.buildc                 C  s8   |   }t|dkrtd|d }|ddddS )zMReturns an ascii representation of the barcode.

        :rtype: str
        r'   z(Code list must contain a single element.r   1|r   _)r9   r   RuntimeErrorreplace)r   	code_listr6   r   r   r   to_asciid   s
   zUniversalProductCodeA.to_asciic                   s$   ddi}| |p	i  t ||S )Nmodule_widthgQ?)updatesuperrender)r   writer_optionstextoptions	__class__r   r   rD   p   s   zUniversalProductCodeA.render)NF)r
   r   )r
   r   )r
   r-   )NN)__name__
__module____qualname____doc__namer   r   r   r!   r   r9   r@   rD   __classcell__r   r   rH   r   r      s    


r   N)rM   
__future__r   __docformat__	functoolsr   barcode.baser   barcode.charsetsr   r1   barcode.errorsr   r   r   UPCAr   r   r   r   <module>   s    e