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
(
name: str,
code: str,
writer: BaseWriter | None = None,
output: str | os.PathLike | BinaryIO | None = None,
writer_options: dict | None = None,
text: str | None = None,
)
| 113 | |
| 114 | |
| 115 | def generate( |
| 116 | name: str, |
| 117 | code: str, |
| 118 | writer: BaseWriter | None = None, |
| 119 | output: str | os.PathLike | BinaryIO | None = None, |
| 120 | writer_options: dict | None = None, |
| 121 | text: str | None = None, |
| 122 | ) -> str | None: |
| 123 | """Shortcut to generate a barcode in one line. |
| 124 | |
| 125 | :param name: Name of the type of barcode to use. |
| 126 | :param code: Data to encode into the barcode. |
| 127 | :param writer: A writer to use (e.g.: ImageWriter or SVGWriter). |
| 128 | :param output: Destination file-like or path-like where to save the generated |
| 129 | barcode. |
| 130 | :param writer_options: Options to pass on to the writer instance. |
| 131 | :param text: Text to render under the barcode. |
| 132 | """ |
| 133 | from barcode.base import Barcode |
| 134 | |
| 135 | if output is None: |
| 136 | raise TypeError("'output' cannot be None") |
| 137 | |
| 138 | writer = writer or Barcode.default_writer() |
| 139 | writer.set_options(writer_options or {}) |
| 140 | |
| 141 | barcode = get(name, code, writer) |
| 142 | |
| 143 | if isinstance(output, str): |
| 144 | return barcode.save(output, writer_options, text) |
| 145 | if isinstance(output, os.PathLike): |
| 146 | with open(output, "wb") as fp: |
| 147 | barcode.write(fp, writer_options, text) |
| 148 | return None |
| 149 | barcode.write(output, writer_options, text) |
| 150 | return None |
| 151 | |
| 152 | |
| 153 | get_barcode = get |
nothing calls this directly
no test coverage detected