Skip to content

Commit

Permalink
Merge pull request #7 from gisce/IMP_options_to_barcode
Browse files Browse the repository at this point in the history
Add pyBarcode common options to save or export
  • Loading branch information
guilleJB committed Oct 26, 2015
2 parents 2ffd069 + 46ba604 commit 879f6c8
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions bankbarcode/bankbarcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class BankBarcode(object):
Base class for barcodes
"""

defaults = {
'font_size': 6
}

def _check_length(self, name, value, expected_length, description):
"""
Check length of a value.
Expand Down Expand Up @@ -38,29 +42,38 @@ def code(self):
"""
raise NotImplementedError('This method is not implemented!')

def save(self, path):
def save(self, path, writer_options=None):
"""
Save barcode in SVG format.
:param path: path to SVG file with or without ".svg" extension
:param writer_options: Common options from pyBarcode \
http://pythonhosted.org/pyBarcode/writers/index.html?#common-options
:return: a string with the name of the file generated
"""
path = self._strip_dotsvg(path)

writer_options = {'font_size': 6}
if writer_options is None:
writer_options = self.defaults.copy()
else:
for k, v in self.defaults.items():
writer_options.setdefault(k, v)

return generate(
'code128',
self.code(),
output=path,
writer_options=writer_options
)

def svg(self):
def svg(self, writer_options=None):
"""
Generate a SVG with the barcode.
:param writer_options: Common options from pyBarcode \
http://pythonhosted.org/pyBarcode/writers/index.html?#common-options
:return: a string with the barcode in SVG format
"""
f = StringIO()
self.save(f)
self.save(f, writer_options)
return f.getvalue()

0 comments on commit 879f6c8

Please sign in to comment.