Skip to content

Commit

Permalink
Add forward-compatibility with Pillow 8.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Osvaldo Barrera committed Aug 14, 2020
1 parent 23a4388 commit 261984c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
5 changes: 1 addition & 4 deletions barcode/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ def write(self, fp, options=None, text=None):
Text to render under the barcode.
"""
output = self.render(options, text)
if hasattr(output, "tostring"):
output.save(fp, format=self.writer.format)
else:
fp.write(output)
self.writer.write(output, fp)

def render(self, writer_options=None, text=None):
"""Renders the barcode using `self.writer`.
Expand Down
15 changes: 15 additions & 0 deletions barcode/writer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import gzip
import os
import xml.dom
from typing.io import BinaryIO

from barcode.version import version

Expand Down Expand Up @@ -318,6 +319,13 @@ def save(self, filename, output):
f.write(output)
return _filename

def write(self, content, fp: BinaryIO):
"""Write `content` into a file-like object.
Content should be a barcode rendered by this writer.
"""
fp.write(content)


if Image is None:
ImageWriter = None
Expand Down Expand Up @@ -367,3 +375,10 @@ def save(self, filename, output):
filename = "{}.{}".format(filename, self.format.lower())
output.save(filename, self.format.upper())
return filename

def write(self, content, fp: BinaryIO):
"""Write `content` into a file-like object.
Content should be a barcode rendered by this writer.
"""
content.save(fp, format=self.format)
27 changes: 27 additions & 0 deletions tests/test_writers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
from io import BytesIO

from barcode import EAN13
from barcode.writer import ImageWriter
from barcode.writer import SVGWriter


PATH = os.path.dirname(os.path.abspath(__file__))
TESTPATH = os.path.join(PATH, "test_outputs")

if ImageWriter:

def test_saving_image_to_byteio():
rv = BytesIO()
EAN13(str(100000902922), writer=ImageWriter()).write(rv)

with open(f"{TESTPATH}/somefile.jpeg", "wb") as f:
EAN13("100000011111", writer=ImageWriter()).write(f)


def test_saving_svg_to_byteio():
rv = BytesIO()
EAN13(str(100000902922), writer=SVGWriter()).write(rv)

with open(f"{TESTPATH}/somefile.svg", "wb") as f:
EAN13("100000011111", writer=SVGWriter()).write(f)

0 comments on commit 261984c

Please sign in to comment.