Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: Add linter #13

Merged
merged 11 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,11 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
pip install .
pip install .[dev]
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
- name: Lint
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
make lint
- name: Test with pytest
run: |
python -m pytest
8 changes: 6 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# Contributing to rmqrcode-python

Thank you for interesting in contributing to rmqrcode-python! Any suggestions are welcome.

## Style Guides
### Git Commit Message

Consider starting commit message with one of the following prefixes.
- `feat:` : New feature
- `fix:` : Bug fix
- `refactor:` : Refactoring
- `chore:` : Little things
- `doc:` : Documentation

## Pull Requests
Before make a pull request, please do the following.
1. `make format`
2. `make lint`
3. `python -m pytest` and make sure all tests are passed.
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.PHONY: lint
lint:
flake8 src
isort --check --diff src
black --check src

.PHONY: format
format:
isort src
black src
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
build-backend = "setuptools.build_meta"

[tool.black]
line-length = 119
exclude = "generator_polynomials.py"
13 changes: 12 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,21 @@ install_requires =
[options.extras_require]
dev =
pytest
flake8
isort
black

[options.packages.find]
where = src

[options.entry_points]
console_scripts =
rmqr = rmqrcode.console:main
rmqr = rmqrcode.console:main

[flake8]
max-line-length = 119
extend-ignore = E203
exclude = src/rmqrcode/format/generator_polynomials.py

[isort]
profile=black
9 changes: 4 additions & 5 deletions src/rmqrcode/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from .rmqrcode import rMQR
from .rmqrcode import FitStrategy
from .rmqrcode import DataTooLongError
from .rmqrcode import IllegalVersionError
from .format.error_correction_level import ErrorCorrectionLevel
from .qr_image import QRImage
from .format.error_correction_level import ErrorCorrectionLevel
from .rmqrcode import DataTooLongError, FitStrategy, IllegalVersionError, rMQR

__all__ = ("rMQR", "DataTooLongError", "FitStrategy", "IllegalVersionError", "QRImage", "ErrorCorrectionLevel")
54 changes: 28 additions & 26 deletions src/rmqrcode/console.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
#!/usr/bin/env python
import rmqrcode
from rmqrcode import rMQR
from rmqrcode import QRImage
from rmqrcode import ErrorCorrectionLevel
from rmqrcode import FitStrategy
from rmqrcode import DataTooLongError
from rmqrcode import IllegalVersionError

import argparse
import sys

from rmqrcode import (
DataTooLongError,
ErrorCorrectionLevel,
FitStrategy,
IllegalVersionError,
QRImage,
rMQR,
)


def _show_error_and_exit(msg):
print(msg, file=sys.stderr)
sys.exit(1)


def _make_qr(data, ecc, version, fit_strategy):
if version == None:
if version is None:
qr = rMQR.fit(data, ecc=ecc, fit_strategy=fit_strategy)
else:
try:
Expand All @@ -29,7 +30,6 @@ def _make_qr(data, ecc, version, fit_strategy):
return qr



def _save_image(qr, output):
image = QRImage(qr)
try:
Expand All @@ -42,24 +42,19 @@ def main():
parser = _init_argparser()
args = parser.parse_args()

if args.ecc == 'M':
if args.ecc == "M":
ecc = ErrorCorrectionLevel.M
elif args.ecc == 'H':
elif args.ecc == "H":
ecc = ErrorCorrectionLevel.H

fit_strategy = FitStrategy.BALANCED
if args.fit_strategy == 'min_width':
if args.fit_strategy == "min_width":
fit_strategy = FitStrategy.MINIMIZE_WIDTH
elif args.fit_strategy == 'min_height':
elif args.fit_strategy == "min_height":
fit_strategy = FitStrategy.MINIMIZE_HEIGHT

try:
qr = _make_qr(
args.DATA,
ecc=ecc,
version=args.version,
fit_strategy=fit_strategy
)
qr = _make_qr(args.DATA, ecc=ecc, version=args.version, fit_strategy=fit_strategy)
except DataTooLongError:
_show_error_and_exit("Error: The data is too long.")

Expand All @@ -68,13 +63,20 @@ def main():

def _init_argparser():
parser = argparse.ArgumentParser()
parser.add_argument('DATA', type=str, help="Data to encode.")
parser.add_argument('OUTPUT', type=str, help="Output file path")
parser.add_argument('--ecc', help="Error correction level. (default: M)", type=str, choices=["M", "H"], default='M')
parser.add_argument('--version', help="rMQR Code version like 'R11x139'.")
parser.add_argument('--fit-strategy', choices=["min_width", "min_height", "balanced"], help="Strategy how to determine rMQR Code size.", dest="fit_strategy")
parser.add_argument("DATA", type=str, help="Data to encode.")
parser.add_argument("OUTPUT", type=str, help="Output file path")
parser.add_argument(
"--ecc", help="Error correction level. (default: M)", type=str, choices=["M", "H"], default="M"
)
parser.add_argument("--version", help="rMQR Code version like 'R11x139'.")
parser.add_argument(
"--fit-strategy",
choices=["min_width", "min_height", "balanced"],
help="Strategy how to determine rMQR Code size.",
dest="fit_strategy",
)
return parser


if __name__ == "__main__":
main()
main()
6 changes: 2 additions & 4 deletions src/rmqrcode/encoder/byte_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ class ByteEncoder:
@staticmethod
def _encoded_bits(s):
res = ""
encoded = s.encode('utf-8')
encoded = s.encode("utf-8")
for byte in encoded:
res += bin(byte)[2:].zfill(8)
return res


@staticmethod
def encode(data, character_count_length):
res = ByteEncoder.MODE_INDICATOR
res += bin(len(data))[2:].zfill(character_count_length)
res += ByteEncoder._encoded_bits(data)
return res


@staticmethod
def length(data):
return len(data.encode('utf-8'))
return len(data.encode("utf-8"))
3 changes: 2 additions & 1 deletion src/rmqrcode/enums/color.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from enum import Enum


class Color(Enum):
UNDEFINED = -1
WHITE = 0
BLACK = 1
BLACK = 1
3 changes: 2 additions & 1 deletion src/rmqrcode/enums/fit_strategy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from enum import Enum


class FitStrategy(Enum):
MINIMIZE_WIDTH = 0
MINIMIZE_HEIGHT = 1
BALANCED = 2
BALANCED = 2
2 changes: 1 addition & 1 deletion src/rmqrcode/format/alignment_pattern_coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
77: [25, 51],
99: [23, 49, 75],
139: [27, 55, 83, 111],
}
}
Loading