Beispiel #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")
Beispiel #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)
Beispiel #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()
Beispiel #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()
Beispiel #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()
Beispiel #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()
Beispiel #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()
Beispiel #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
Beispiel #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)
Beispiel #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()
Beispiel #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()
Beispiel #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()
Beispiel #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()
Beispiel #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()
Beispiel #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)
Beispiel #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()
Beispiel #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()
Beispiel #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()
Beispiel #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()
Beispiel #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()
Beispiel #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)
Beispiel #23
0
 def render(self, writer_options=None):
     options = dict(module_width=SIZES['SC2'])
     options.update(writer_options or {})
     return Barcode.render(self, options)
Beispiel #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)
Beispiel #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)
Beispiel #26
0
 def render(self, writer_options=None):
     options = dict(module_width=SIZES['SC2'])
     options.update(writer_options or {})
     return Barcode.render(self, options)
Beispiel #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)
Beispiel #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)
Beispiel #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)
Beispiel #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)
Beispiel #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)
Beispiel #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)
Beispiel #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)
Beispiel #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)
Beispiel #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)
Beispiel #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)