Skip to content

Commit 8980039

Browse files
author
Hugo Osvaldo Barrera
committed
Use pre-commit to keep things tidy
- Use black. Formatting is a mess. - Use mypy. Though more love is required here, especially since we have inconsistent APIs. This should help pick up on them. - Configure flake8 in a compatible way. - Reorder imports.
1 parent f150da7 commit 8980039

27 files changed

+724
-717
lines changed

.pre-commit-config.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
# vim: set nospell:
4+
repos:
5+
- repo: https://github.com/pre-commit/pre-commit-hooks
6+
rev: v3.2.0
7+
hooks:
8+
- id: trailing-whitespace
9+
args: [--markdown-linebreak-ext=md]
10+
- id: end-of-file-fixer
11+
- id: debug-statements
12+
- repo: https://github.com/asottile/reorder_python_imports
13+
rev: v2.3.5
14+
hooks:
15+
- id: reorder-python-imports
16+
- repo: https://github.com/psf/black
17+
rev: "master"
18+
hooks:
19+
- id: black
20+
- repo: https://gitlab.com/pycqa/flake8
21+
rev: "3.8.3" # pick a git hash / tag to point to
22+
hooks:
23+
- id: flake8
24+
additional_dependencies: [flake8-comprehensions, flake8-import-order, flake8-bugbear]
25+
- repo: https://github.com/pre-commit/mirrors-mypy
26+
rev: 'v0.782' # Use the sha / tag you want to point at
27+
hooks:
28+
- id: mypy

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Commandline::
125125

126126
`$ python-barcode create "123456789000" outfile -b ean --text "text to appear under barcode" `
127127
New barcode saved as outfile.svg.
128-
128+
129129
# The following will not work if Pillow is not installed (Pillow is required for exporting to images instead of SVG).
130130
$ python-barcode create -t png "My Text" outfile
131131
New barcode saved as outfile.png.

barcode/__init__.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,41 @@
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-
7-
from barcode.codex import Code128, Code39, Gs1_128, PZN
8-
from barcode.ean import EAN13, EAN14, EAN8, JAN
6+
from barcode.codex import Code128
7+
from barcode.codex import Code39
8+
from barcode.codex import Gs1_128
9+
from barcode.codex import PZN
10+
from barcode.ean import EAN13
11+
from barcode.ean import EAN14
12+
from barcode.ean import EAN8
13+
from barcode.ean import JAN
914
from barcode.errors import BarcodeNotFoundError
10-
from barcode.isxn import ISBN10, ISBN13, ISSN
15+
from barcode.isxn import ISBN10
16+
from barcode.isxn import ISBN13
17+
from barcode.isxn import ISSN
1118
from barcode.itf import ITF
1219
from barcode.upc import UPCA
1320
from barcode.version import version # noqa: F401
1421

1522
__BARCODE_MAP = {
16-
'ean8': EAN8,
17-
'ean13': EAN13,
18-
'ean': EAN13,
19-
'gtin': EAN14,
20-
'ean14': EAN14,
21-
'jan': JAN,
22-
'upc': UPCA,
23-
'upca': UPCA,
24-
'isbn': ISBN13,
25-
'isbn13': ISBN13,
26-
'gs1': ISBN13,
27-
'isbn10': ISBN10,
28-
'issn': ISSN,
29-
'code39': Code39,
30-
'pzn': PZN,
31-
'code128': Code128,
32-
'itf': ITF,
33-
'gs1_128': Gs1_128,
23+
"ean8": EAN8,
24+
"ean13": EAN13,
25+
"ean": EAN13,
26+
"gtin": EAN14,
27+
"ean14": EAN14,
28+
"jan": JAN,
29+
"upc": UPCA,
30+
"upca": UPCA,
31+
"isbn": ISBN13,
32+
"isbn13": ISBN13,
33+
"gs1": ISBN13,
34+
"isbn10": ISBN10,
35+
"issn": ISSN,
36+
"code39": Code39,
37+
"pzn": PZN,
38+
"code128": Code128,
39+
"itf": ITF,
40+
"gs1_128": Gs1_128,
3441
}
3542

3643
PROVIDED_BARCODES = list(__BARCODE_MAP)
@@ -55,7 +62,7 @@ def get(name, code=None, writer=None, options=None):
5562
barcode = __BARCODE_MAP[name.lower()]
5663
except KeyError:
5764
raise BarcodeNotFoundError(
58-
'The barcode {0!r} you requested is not known.'.format(name)
65+
"The barcode {0!r} you requested is not known.".format(name)
5966
)
6067

6168
if code is not None:
@@ -69,12 +76,7 @@ def get_class(name):
6976

7077

7178
def generate(
72-
name,
73-
code,
74-
writer=None,
75-
output=None,
76-
writer_options=None,
77-
text=None,
79+
name, code, writer=None, output=None, writer_options=None, text=None,
7880
):
7981
writer_options = writer_options or {}
8082
barcode = get(name, code, writer, writer_options=writer_options)

barcode/base.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
11
"""barcode.base
22
33
"""
4-
54
from barcode.writer import SVGWriter
65

76

87
class Barcode(object):
98

10-
name = ''
9+
name = ""
1110

1211
digits = 0
1312

1413
default_writer = SVGWriter
1514

1615
default_writer_options = {
17-
'module_width': 0.2,
18-
'module_height': 15.0,
19-
'quiet_zone': 6.5,
20-
'font_size': 10,
21-
'text_distance': 5.0,
22-
'background': 'white',
23-
'foreground': 'black',
24-
'write_text': True,
25-
'text': '',
16+
"module_width": 0.2,
17+
"module_height": 15.0,
18+
"quiet_zone": 6.5,
19+
"font_size": 10,
20+
"text_distance": 5.0,
21+
"background": "white",
22+
"foreground": "black",
23+
"write_text": True,
24+
"text": "",
2625
}
2726

2827
def to_ascii(self):
2928
code = self.build()
3029
for i, line in enumerate(code):
31-
code[i] = line.replace('1', 'X').replace('0', ' ')
32-
return '\n'.join(code)
30+
code[i] = line.replace("1", "X").replace("0", " ")
31+
return "\n".join(code)
3332

3433
def __repr__(self):
35-
return '<{0}({1!r})>'.format(
36-
self.__class__.__name__, self.get_fullcode()
37-
)
34+
return "<{0}({1!r})>".format(self.__class__.__name__, self.get_fullcode())
3835

3936
def build(self):
4037
raise NotImplementedError
@@ -83,7 +80,7 @@ def write(self, fp, options=None, text=None):
8380
Text to render under the barcode.
8481
"""
8582
output = self.render(options, text)
86-
if hasattr(output, 'tostring'):
83+
if hasattr(output, "tostring"):
8784
output.save(fp, format=self.writer.format)
8885
else:
8986
fp.write(output)
@@ -101,11 +98,11 @@ def render(self, writer_options=None, text=None):
10198
"""
10299
options = Barcode.default_writer_options.copy()
103100
options.update(writer_options or {})
104-
if options['write_text'] or text is not None:
101+
if options["write_text"] or text is not None:
105102
if text is not None:
106-
options['text'] = text
103+
options["text"] = text
107104
else:
108-
options['text'] = self.get_fullcode()
105+
options["text"] = self.get_fullcode()
109106
self.writer.set_options(options)
110107
code = self.build()
111108
raw = self.writer.render(code)

0 commit comments

Comments
 (0)