Ejemplo n.º 1
0
def generate(
    name: str,
    code: str,
    writer=None,
    output: Union[str, os.PathLike, BinaryIO] = None,
    writer_options: Dict = None,
    text: str = None,
):
    """Shortcut to generate a barcode in one line.

    :param name: Name of the type of barcode to use.
    :param code: Data to encode into the barcode.
    :param writer: A writer to use (e.g.: ImageWriter or SVGWriter).
    :param output: Destination file-like or path-like where to save the generated
     barcode.
    :param writer_options: Options to pass on to the writer instance.
    :param text: Text to render under the barcode.
    """
    from barcode.base import Barcode

    writer = writer or Barcode.default_writer()
    writer.set_options(writer_options or {})

    barcode = get(name, code, writer)

    if isinstance(output, str):
        fullname = barcode.save(output, writer_options, text)
        return fullname
    elif output:
        barcode.write(output, writer_options, text)
    else:
        raise TypeError("'output' cannot be None")
Ejemplo n.º 2
0
 def render(self, writer_options, text=None):
     options = {
         'module_width': MIN_SIZE / self.narrow,
         'quiet_zone': MIN_QUIET_ZONE,
     }
     options.update(writer_options or {})
     return Barcode.render(self, options, text)
Ejemplo n.º 3
0
 def __init__(self, ean, writer=None):
     ean = ean[:self.digits]
     if not ean.isdigit():
         raise IllegalCharacterError('Code can only contain numbers.')
     self.ean = ean
     self.ean = '{0}{1}'.format(ean, self.calculate_checksum())
     self.writer = writer or Barcode.default_writer()
Ejemplo n.º 4
0
 def __init__(self, ean, writer=None):
     ean = ean[:self.digits]
     if not ean.isdigit():
         raise IllegalCharacterError('Code can only contain numbers.')
     self.ean = ean
     self.ean = '{0}{1}'.format(ean, self.calculate_checksum())
     self.writer = writer or Barcode.default_writer()
Ejemplo n.º 5
0
 def __init__(self, i2of5, writer=None):
     if not i2of5.isdigit():
         raise IllegalCharacterError('Code can only contain numbers.')
     if not len(i2of5)%2 == 0:
         raise NumberOfDigitsError('Code must have an even number of digits')
     self.i2of5 = i2of5
     self.writer = writer or Barcode.default_writer()
Ejemplo n.º 6
0
 def __init__(self, i2of5, writer=None):
     if not i2of5.isdigit():
         raise IllegalCharacterError('Code can only contain numbers.')
     if not len(i2of5) % 2 == 0:
         raise NumberOfDigitsError(
             'Code must have an even number of digits')
     self.i2of5 = i2of5
     self.writer = writer or Barcode.default_writer()
Ejemplo n.º 7
0
 def __init__(self, code, writer=None, narrow=2, wide=5):
     if not code.isdigit():
         raise IllegalCharacterError('ITF code can only contain numbers.')
     #Length must be even, prepend 0 if necessary
     if len(code) % 2 != 0:
         code = '0' + code
     self.code = code
     self.writer = writer or Barcode.default_writer()
     self.narrow = narrow
     self.wide = wide
 def __init__(self, ean, writer=None):
     ean = ean[:self.digits]
     if not ean.isdigit():
         raise IllegalCharacterError('EAN code can only contain numbers.')
     if len(ean) != self.digits:
         raise NumberOfDigitsError('EAN must have {0} digits, not '
                                   '{1}.'.format(self.digits, len(ean)))
     self.ean = ean
     self.ean = '{0}{1}'.format(ean, self.calculate_checksum())
     self.writer = writer or Barcode.default_writer()
Ejemplo n.º 9
0
 def __init__(self, code, writer=None, narrow=2, wide=5):
     if not code.isdigit():
         raise IllegalCharacterError('ITF code can only contain numbers.')
     #Length must be even, prepend 0 if necessary
     if len(code) % 2 != 0:
         code = '0' + code
     self.code = code
     self.writer = writer or Barcode.default_writer()
     self.narrow = narrow
     self.wide = wide
Ejemplo n.º 10
0
 def render(self, writer_options=None, text=None):
     options = dict(module_width=MIN_SIZE, quiet_zone=MIN_QUIET_ZONE)
     # moved check sum logic here
     add_checksum = writer_options.get('add_checksum',
                                       self.add_checksum_default)
     if add_checksum and not self.checksum_added:
         self.code += self.calculate_checksum()
         self.checksum_added = True
     options.update(writer_options or {})
     return Barcode.render(self, options, text)
Ejemplo n.º 11
0
 def __init__(self, code, writer=None, add_checksum=True):
     code = code.upper()
     for char in code:
         if char not in REF:
             raise IllegalCharacterError('Character {0!r} not valid for '
                                         'Code 39.'.format(char))
     self.code = code
     if add_checksum:
         self.code += self.calculate_checksum()
     self.writer = writer or Barcode.default_writer()
Ejemplo n.º 12
0
 def __init__(self, ean, writer=None):
     ean = ean[:self.digits]
     if not ean.isdigit():
         raise IllegalCharacterError('EAN code can only contain numbers.')
     if len(ean) != self.digits:
         raise NumberOfDigitsError('EAN must have {0} digits, not '
                                   '{1}.'.format(self.digits, len(ean)))
     self.ean = ean
     self.ean = '{0}{1}'.format(ean, self.calculate_checksum())
     self.writer = writer or Barcode.default_writer()
Ejemplo n.º 13
0
 def __init__(self, code, writer=None, add_checksum=True):
     code = code.upper()
     for char in code:
         if char not in REF:
             raise IllegalCharacterError('Character {0!r} not valid for '
                                         'Code 39.'.format(char))
     self.code = code
     if add_checksum:
         self.code += self.calculate_checksum()
     self.writer = writer or Barcode.default_writer()
Ejemplo n.º 14
0
 def __init__(self, upc, writer=None, make_ean=False):
     self.ean = make_ean
     upc = upc[:self.digits]
     if not upc.isdigit():
         raise IllegalCharacterError('UPC code can only contain numbers.')
     if len(upc) != self.digits:
         raise NumberOfDigitsError('UPC must have {0} digits, not '
                                   '{1}.'.format(self.digits, len(upc)))
     self.upc = upc
     self.upc = '{}{}'.format(upc, self.calculate_checksum())
     self.writer = writer or Barcode.default_writer()
Ejemplo n.º 15
0
 def __init__(self, upc, writer=None, make_ean=False):
     self.ean = make_ean
     upc = upc[:self.digits]
     if not upc.isdigit():
         raise IllegalCharacterError('UPC code can only contain numbers.')
     if len(upc) != self.digits:
         raise NumberOfDigitsError('UPC must have {0} digits, not '
                                   '{1}.'.format(self.digits, len(upc)))
     self.upc = upc
     self.upc = '{}{}'.format(upc, self.calculate_checksum())
     self.writer = writer or Barcode.default_writer()
Ejemplo n.º 16
0
    def __init__(self, code: str, writer=None, add_checksum: bool = True):
        r"""
        :param code: Code 39 string without \* and without checksum.
        :param writer: A ``barcode.writer`` instance used to render the barcode
            (default: SVGWriter).
        :param add_checksum: Add the checksum to code or not
        """

        self.code = code.upper()
        if add_checksum:
            self.code += self.calculate_checksum()
        self.writer = writer or Barcode.default_writer()
        check_code(self.code, self.name, code39.REF)
Ejemplo n.º 17
0
    def __init__(self, ean, writer=None):
        """Initializes EAN13 object.

        :parameters:
            ean : String
                The ean number as string.
            writer : barcode.writer Instance
                The writer to render the barcode (default: SVGWriter).
        """
        ean = ean[:self.digits]
        if not ean.isdigit():
            raise IllegalCharacterError('Code can only contain numbers.')
        self.ean = ean
        self.ean = '{0}{1}'.format(ean, self.calculate_checksum())
        self.writer = writer or Barcode.default_writer()
Ejemplo n.º 18
0
 def __init__(self, ean, writer=None, **kwargs):
     no_checksum = False
     if "no_checksum" in kwargs.keys():
         no_checksum = kwargs["no_checksum"]
     ean = ean[:self.digits]
     if not ean.isdigit():
         raise IllegalCharacterError('EAN code can only contain numbers.')
     if len(ean) != self.digits:
         raise NumberOfDigitsError('EAN must have {0} digits, not '
                                   '{1}.'.format(self.digits, len(ean)))
     self.ean = ean
     # If no checksum 
     if no_checksum:
         # Add a thirteen char if given in parameter, otherwise pad with zero
         self.ean = '{0}{1}'.format(ean, ean[self.digits] if len(ean) > self.digits else 0)
     else:
         self.ean = '{0}{1}'.format(ean, self.calculate_checksum())
     self.writer = writer or Barcode.default_writer()
Ejemplo n.º 19
0
 def __init__(self, ean, writer=None, no_checksum=False):
     ean = ean[:self.digits]
     if not ean.isdigit():
         raise IllegalCharacterError("EAN code can only contain numbers.")
     if len(ean) != self.digits:
         raise NumberOfDigitsError(
             "EAN must have {} digits, not {}.".format(
                 self.digits,
                 len(ean),
             ))
     self.ean = ean
     # If no checksum
     if no_checksum:
         # Add a thirteen char if given in parameter,
         # otherwise pad with zero
         self.ean = "{}{}".format(
             ean, ean[self.digits] if len(ean) > self.digits else 0)
     else:
         self.ean = "{}{}".format(ean, self.calculate_checksum())
     self.writer = writer or Barcode.default_writer()
Ejemplo n.º 20
0
    def __init__(self, code, writer=None, add_checksum=True):
        """Initializes a new Code39 instance.

        :parameters:
            code : String
                Code39 string without \* and checksum (added automatically if
                `add_checksum` is True).
            writer : barcode.writer Instance
                The writer to render the barcode (default: SVGWriter).
            add_checksum : Boolean
                Add the checksum to code or not.
        """
        code = code.upper()
        for char in code:
            if char not in REF:
                raise IllegalCharacterError('Character {0!r} not valid for '
                                            'Code 39.'.format(char))
        self.code = code
        if add_checksum:
            self.code += self.calculate_checksum()
        self.writer = writer or Barcode.default_writer()
Ejemplo n.º 21
0
    def __init__(self, upc, writer=None, make_ean=False):
        """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).
        """
        self.ean = make_ean
        upc = upc[:self.digits]
        if not upc.isdigit():
            raise IllegalCharacterError("UPC code can only contain numbers.")
        if len(upc) != self.digits:
            raise NumberOfDigitsError(
                "UPC must have {} digits, not {}.".format(
                    self.digits, len(upc)))
        self.upc = upc
        self.upc = "{}{}".format(upc, self.calculate_checksum())
        self.writer = writer or Barcode.default_writer()
Ejemplo n.º 22
0
 def render(self, writer_options=None, text=None):
     options = {'module_width': 0.33}
     options.update(writer_options or {})
     return Barcode.render(self, options, text)
Ejemplo n.º 23
0
 def render(self, writer_options=None):
     options = dict(module_width=SIZES['SC2'])
     options.update(writer_options or {})
     return Barcode.render(self, options)
Ejemplo n.º 24
0
 def render(self, writer_options=None, text=None):
     options = dict(module_width=0.33)
     options.update(writer_options or {})
     return Barcode.render(self, options, text)
Ejemplo n.º 25
0
 def __init__(self, code, writer=None):
     self.add_checksum_default = True
     self.checksum_added = False
     self.code = code.upper()
     self.writer = writer or Barcode.default_writer()
     check_code(self.code, self.name, code39.REF)
Ejemplo n.º 26
0
 def render(self, writer_options=None):
     options = dict(module_width=SIZES['SC2'])
     options.update(writer_options or {})
     return Barcode.render(self, options)
Ejemplo n.º 27
0
 def render(self, write_text=True, **writer_options):
     options = dict(module_width=MIN_SIZE, quiet_zone=MIN_QUIET_ZONE)
     options.update(writer_options)
     return Barcode.render(self, write_text, **options)
Ejemplo n.º 28
0
 def render(self, writer_options=None, text=None):
     options = {"module_width": SIZES["SC2"]}
     options.update(writer_options or {})
     return Barcode.render(self, options, text)
Ejemplo n.º 29
0
 def render(self, write_text=True, **writer_options):
     options = dict(module_width=SIZES['SC2'])
     options.update(writer_options)
     return Barcode.render(self, write_text, **options)
Ejemplo n.º 30
0
 def __init__(self, code, writer=None):
     self.code = code
     self.writer = writer or Barcode.default_writer()
     self._charset = 'B'
     self._buffer = ''
     check_code(self.code, self.name, code128.ALL)
Ejemplo n.º 31
0
 def render(self, writer_options):
     options = dict(module_width=MIN_SIZE, quiet_zone=MIN_QUIET_ZONE)
     options.update(writer_options or {})
     return Barcode.render(self, options)
Ejemplo n.º 32
0
 def __init__(self, code, writer=None, add_checksum=True):
     self.code = code.upper()
     if add_checksum:
         self.code += self.calculate_checksum()
     self.writer = writer or Barcode.default_writer()
     check_code(self.code, self.name, REF)
Ejemplo n.º 33
0
 def __init__(self, code, writer=None, add_checksum=True):
     self.code = code.upper()
     if add_checksum:
         self.code += self.calculate_checksum()
     self.writer = writer or Barcode.default_writer()
     check_code(self.code, self.name, REF)
Ejemplo n.º 34
0
 def render(self, writer_options=None, text=None):
     options = {"module_width": MIN_SIZE, "quiet_zone": MIN_QUIET_ZONE}
     options.update(writer_options or {})
     return Barcode.render(self, options, text)
Ejemplo n.º 35
0
 def __init__(self, code, writer=None):
     self.code = code
     self.writer = writer or Barcode.default_writer()
     self._charset = 'B'
     self._buffer = ''
     check_code(self.code, self.name, code128.ALL)
Ejemplo n.º 36
0
 def render(self, writer_options):
     options = dict(module_width=MIN_SIZE/self.narrow, quiet_zone=MIN_QUIET_ZONE)
     options.update(writer_options or {})
     return Barcode.render(self, options)