Skip to content

Commit 6722d8d

Browse files
author
Hugo Barrera
authored
Merge pull request #86 from WhyNotHugo/fix-84
Fix bug when calling `generate` Fixes #84
2 parents a5ed505 + c65123b commit 6722d8d

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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(os.path.join(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",
27+
"123455559121112",
28+
output=os.path.join(TESTPATH, "generate_with_filepath"),
29+
)
30+
assert rv == os.path.abspath(os.path.join(TESTPATH, "generate_with_filepath.svg"))
31+
32+
33+
def test_generate_with_file_and_writer():
34+
with open(os.path.join(TESTPATH, "generate_with_file_and_writer.jpeg"), "wb") as f:
35+
barcode.generate("ean13", "123455559121112", output=f, writer=SVGWriter())
36+
37+
38+
def test_generate_with_bytesio():
39+
bio = BytesIO()
40+
barcode.generate("ean13", "123455559121112", output=bio)
41+
# XXX: File is not 100% deterministic; needs to be addressed at some point.
42+
# assert len(bio.getvalue()) == 6127

tests/test_outputs/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# This directory contains all the tests outputs.
2+
*

0 commit comments

Comments
 (0)