B
    	`C+                 @   s4   d dl Z d dlmZ ddlmZ G dd deZdS )    N)product   )Providerc                   s   e Zd ZdZededededZedZ	edZ
edZed	Zd fdd	Zdd ZdddZdddZdddZ  ZS )r   z{Implement barcode provider for ``en_US`` locale.

    Sources:

    - https://gs1.org/standards/id-keys/company-prefix
    )r   
   )      z^\d{6}$z^(?P<number_system_digit>[01])(?=\d{11}$)(?P<mfr_code>\d{2})(?:(?P<extra>[012])0{4})(?P<product_code>\d{3})(?P<check_digit>\d)$zt^(?P<number_system_digit>[01])(?=\d{11}$)(?P<mfr_code>\d{3,4}?)(?:0{5})(?P<product_code>\d{1,2})(?P<check_digit>\d)$zh^(?P<number_system_digit>[01])(?=\d{11}$)(?P<mfr_code>\d{5})(?:0{4}(?P<extra>[5-9]))(?P<check_digit>\d)$N c                s8   |s*|dkrd}n|dkr*|  ddff}t j|dS )a0  Generate an EAN-13 barcode.

        If ``leading_zero`` is ``True``, the leftmost digit of the barcode will
        be set to ``0``. If ``False``, the leftmost digit cannot be ``0``. If
        ``None`` (default), the leftmost digit can be any digit.

        If a value for ``prefixes`` is specified, the result will begin with one
        of the sequences in ``prefixes`` and will ignore ``leading_zero``.

        This method uses the standard barcode provider's |ean13| under the
        hood with the ``prefixes`` argument set to the correct value to attain
        the behavior described above.

        .. note::
           EAN-13 barcode that starts with a zero can be converted to UPC-A
           by dropping the leading zero. This may cause problems with readers
           that treat all of these code as UPC-A codes and drop the first digit
           when reading it.

           You can set the argument ``prefixes`` ( or ``leading_zero`` for
           convenience) explicitly to avoid or to force the generated barcode to
           start with a zero. You can also generate actual UPC-A barcode with
           |EnUsBarcodeProvider.upc_a|.

        :sample:
        :sample: leading_zero=False
        :sample: leading_zero=True
        :sample: prefixes=('00',)
        :sample: prefixes=('45', '49')
        T))r   Fr   	   )prefixes)
random_intsuperean13)selfleading_zeror
   )	__class__r   U/home/dcms/DCMS/lib/python3.7/site-packages/faker/providers/barcode/en_US/__init__.pyr   .   s    zProvider.ean13c             C   s   t |tstd| j|}| j|}| j|}t|||gsLtdd}|rf|j	f |
 }nL|r|
 }tt|d|d< |j	f |}n|
 }d|d< |j	f |}|S )zConvert a 12-digit UPC-A barcode to its 8-digit UPC-E equivalent.

        .. warning::
           Not all UPC-A barcodes can be converted.
        z`upc_a` is not a stringz`upc_a` has an invalid valuezA{number_system_digit}{mfr_code}{product_code}{extra}{check_digit}Zmfr_codeextra Zproduct_code)
isinstancestr	TypeErrorupc_ae_pattern1matchupc_ae_pattern2upc_ae_pattern3any
ValueErrorformat	groupdictlenget)r   upc_am1m2Zm3Zupc_e_templateupc_er   r   r   r   _convert_upc_a2eU   s$    
zProvider._convert_upc_a2ec                sR  t |tr& j|r&dd |D }n fddtdD }|dkrP dd}|d d	kr|d
d	 |dd
  dgd  |d	d  }nZ|d dkr|d
|d  dgd  ||d d  }n"|d
d dgd  |dd
  }|d| dddddddddddg}tdd t||D }d|d  d }|	| d
dd |D S )a  Create a 12-digit UPC-A barcode that can be converted to UPC-E.

        The expected value of ``base`` is a 6-digit string. If any other value
        is provided, this method will use a random 6-digit string instead.

        The expected value of ``number_system_digit`` is the integer ``0`` or
        ``1``. If any other value is provided, this method will randomly choose
        from the two.

        Please also view notes on |EnUsBarcodeProvider.upc_a| and
        |EnUsBarcodeProvider.upc_e| for more details.
        c             S   s   g | ]}t |qS r   )int).0xr   r   r   
<listcomp>}   s    z$Provider._upc_ae.<locals>.<listcomp>c                s   g | ]}  d dqS )r   r	   )r   )r'   _)r   r   r   r)      s       )r   r   r   r   r   Nr         c             s   s   | ]\}}|| V  qd S )Nr   )r'   r(   yr   r   r   	<genexpr>   s    z#Provider._upc_ae.<locals>.<genexpr>r   r   c             s   s   | ]}t |V  qd S )N)r   )r'   r(   r   r   r   r0      s    )r   r   upc_e_base_patternr   ranger   insertsumzipappendjoin)r   basenumber_system_digitcodeweightsZweighted_sumZcheck_digitr   )r   r   _upc_aeo   s     0,"
zProvider._upc_aeFc             C   s2   |dkr| j ||dS | jdd}|dd S dS )a  Generate a 12-digit UPC-A barcode.

        The value of ``upc_ae_mode`` controls how barcodes will be generated. If
        ``False`` (default), barcodes are not guaranteed to have a UPC-E
        equivalent. In this mode, the method uses |EnUsBarcodeProvider.ean13|
        under the hood, and the values of ``base`` and ``number_system_digit``
        will be ignored.

        If ``upc_ae_mode`` is ``True``, the resulting barcodes are guaranteed to
        have a UPC-E equivalent, and the values of ``base`` and
        ``number_system_digit`` will be used to control what is generated.

        Under this mode, ``base`` is expected to have a 6-digit string value. If
        any other value is supplied, a random 6-digit string will be used
        instead. As for ``number_system_digit``, the expected value is a ``0``
        or a ``1``. If any other value is provided, this method will randomly
        choose from the two.

        .. important::
           When ``upc_ae_mode`` is enabled, you might encounter instances where
           different values of ``base`` (e.g. ``'120003'`` and ``'120004'``)
           produce the same UPC-A barcode. This is normal, and the reason lies
           within the whole conversion process. To learn more about this and
           what ``base`` and ``number_system_digit`` actually represent, please
           refer to |EnUsBarcodeProvider.upc_e|.

        :sample:
        :sample: upc_ae_mode=True, number_system_digit=0
        :sample: upc_ae_mode=True, number_system_digit=1
        :sample: upc_ae_mode=True, base='123456', number_system_digit=0
        :sample: upc_ae_mode=True, base='120003', number_system_digit=0
        :sample: upc_ae_mode=True, base='120004', number_system_digit=0
        T)r8   r9   )r   r   N)r<   r   )r   Zupc_ae_moder8   r9   r   r   r   r   r!      s    "zProvider.upc_aTc             C   sV   |dk	r | j ||d}| |S | j ||d}|d ddd |D  |d  S dS )	a	  Generate an 8-digit UPC-E barcode.

        UPC-E barcodes can be expressed in 6, 7, or 8-digit formats, but this
        method uses the 8 digit format, since it is trivial to convert to the
        other two formats. The first digit (starting from the left) is
        controlled by ``number_system_digit``, and it can only be a ``0`` or a
        ``1``. The last digit is the check digit that is inherited from the
        UPC-E barcode's UPC-A equivalent. The middle six digits are collectively
        referred to as the ``base`` (for a lack of a better term).

        On that note, this method uses ``base`` and ``number_system_digit`` to
        first generate a UPC-A barcode for the check digit, and what happens
        next depends on the value of ``safe_mode``. The argument ``safe_mode``
        exists, because there are some UPC-E values that share the same UPC-A
        equivalent. For example, any UPC-E barcode of the form ``abc0000d``,
        ``abc0003d``, and ``abc0004d`` share the same UPC-A value
        ``abc00000000d``, but that UPC-A value will only convert to ``abc0000d``
        because of (a) how UPC-E is just a zero-suppressed version of UPC-A and
        (b) the rules around the conversion.

        If ``safe_mode`` is ``True`` (default), this method performs another set
        of conversions to guarantee that the UPC-E barcodes generated can be
        converted to UPC-A, and that UPC-A barcode can be converted back to the
        original UPC-E barcode. Using the example above, even if the bases
        ``120003`` or ``120004`` are used, the resulting UPC-E barcode will
        always use the base ``120000``.

        If ``safe_mode`` is ``False``, then the ``number_system_digit``,
        ``base``, and the computed check digit will just be concatenated
        together to produce the UPC-E barcode, and attempting to convert the
        barcode to UPC-A and back again to UPC-E will exhibit the behavior
        described above.

        :sample:
        :sample: base='123456'
        :sample: base='123456', number_system_digit=0
        :sample: base='123456', number_system_digit=1
        :sample: base='120000', number_system_digit=0
        :sample: base='120003', number_system_digit=0
        :sample: base='120004', number_system_digit=0
        :sample: base='120000', number_system_digit=0, safe_mode=False
        :sample: base='120003', number_system_digit=0, safe_mode=False
        :sample: base='120004', number_system_digit=0, safe_mode=False
        F)r8   r9   r   r   c             s   s   | ]}t |V  qd S )N)r   )r'   r(   r   r   r   r0      s    z!Provider.upc_e.<locals>.<genexpr>r,   N)r<   r%   r7   )r   r8   r9   Z	safe_modeZupc_aer   r   r   r$      s
    -
zProvider.upc_e)Nr   )NN)FNN)NNT)__name__
__module____qualname____doc__r   r2   Zlocal_prefixesrecompiler1   r   r   r   r   r%   r<   r!   r$   __classcell__r   r   )r   r   r      s   
'
"
(r   )rA   	itertoolsr   r   r   ZBarcodeProviderr   r   r   r   <module>   s   