Skip to content

Commit 77b5a4e

Browse files
author
Hugo Osvaldo Barrera
committed
Fix bug when calling generate
1 parent a5ed505 commit 77b5a4e

2 files changed

Lines changed: 70 additions & 4 deletions

File tree

barcode/__init__.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
created as SVG objects. If Pillow is installed, the barcodes can also be
44
rendered as images (all formats supported by Pillow).
55
"""
6+
import os
7+
from typing import BinaryIO
8+
from typing import Dict
9+
from typing import Union
10+
611
from barcode.codex import Code128
712
from barcode.codex import Code39
813
from barcode.codex import Gs1_128
@@ -76,15 +81,37 @@ def get_class(name):
7681

7782

7883
def generate(
79-
name, code, writer=None, output=None, writer_options=None, text=None,
84+
name: str,
85+
code: str,
86+
writer=None,
87+
output: Union[str, os.PathLike, BinaryIO] = None,
88+
writer_options: Dict = None,
89+
text: str = None,
8090
):
81-
writer_options = writer_options or {}
82-
barcode = get(name, code, writer, writer_options=writer_options)
91+
"""Shortcut to generate a barcode in one line.
92+
93+
:param name: Name of the type of barcode to use.
94+
:param code: Data to encode into the barcode.
95+
:param writer: A writer to use (e.g.: ImageWriter or SVGWriter).
96+
:param output: Destination file-like or path-like where to save the generated
97+
barcode.
98+
:param writer_options: Options to pass on to the writer instance.
99+
:param text: Text to render under the barcode.
100+
"""
101+
from barcode.base import Barcode
102+
103+
writer = writer or Barcode.default_writer()
104+
writer.set_options(writer_options or {})
105+
106+
barcode = get(name, code, writer)
107+
83108
if isinstance(output, str):
84109
fullname = barcode.save(output, writer_options, text)
85110
return fullname
86-
else:
111+
elif output:
87112
barcode.write(output, writer_options, text)
113+
else:
114+
raise TypeError("'output' cannot be None")
88115

89116

90117
get_barcode = get

tests/test_init.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import os
2+
from io import BytesIO
3+
4+
import pytest
5+
6+
import barcode
7+
from barcode.writer import SVGWriter
8+
9+
PATH = os.path.dirname(os.path.abspath(__file__))
10+
TESTPATH = os.path.join(PATH, "test_outputs")
11+
12+
13+
def test_generate_without_output():
14+
with pytest.raises(TypeError, match="'output' cannot be None"):
15+
barcode.generate("ean13", "123455559121112")
16+
17+
18+
def test_generate_with_file():
19+
with open(f"{TESTPATH}/generate_with_file.jpeg", "wb") as f:
20+
barcode.generate("ean13", "123455559121112", output=f)
21+
22+
23+
def test_generate_with_filepath():
24+
# FIXME: extension is added to the filepath even if you include it.
25+
rv = barcode.generate(
26+
"ean13", "123455559121112", output=f"{TESTPATH}/generate_with_filepath"
27+
)
28+
assert rv == os.path.abspath(f"{TESTPATH}/generate_with_filepath.svg")
29+
30+
31+
def test_generate_with_file_and_writer():
32+
with open(f"{TESTPATH}/generate_with_file_and_writer.jpeg", "wb") as f:
33+
barcode.generate("ean13", "123455559121112", output=f, writer=SVGWriter())
34+
35+
36+
def test_generate_with_bytesio():
37+
bio = BytesIO()
38+
barcode.generate("ean13", "123455559121112", output=bio)
39+
assert len(bio.getvalue()) == 6127

0 commit comments

Comments
 (0)