diff --git a/gp2040ce_bintools/storage.py b/gp2040ce_bintools/storage.py index 0142c32..4ba667b 100644 --- a/gp2040ce_bintools/storage.py +++ b/gp2040ce_bintools/storage.py @@ -10,6 +10,28 @@ FOOTER_SIZE = 12 FOOTER_MAGIC = b'\x65\xe3\xf1\xd2' +############### +# LIB METHODS # +############### + + +def get_config(content: bytes) -> dict: + """Read the config from a GP2040-CE storage section. + + Args: + content: bytes from a GP2040-CE board's storage section + Returns: + the parsed configuration + """ + size, _, _ = get_config_footer(content) + + config_pb2 = get_config_pb2() + config = config_pb2.Config() + config.ParseFromString(content[-(size + FOOTER_SIZE):-FOOTER_SIZE]) + logger.debug("parsed: %s", config) + return config + + def get_config_footer(content: bytes) -> tuple[int, int, str]: """Confirm and retrieve the config footer from a series of bytes of GP2040-CE storage. @@ -40,21 +62,9 @@ def get_config_footer(content: bytes) -> tuple[int, int, str]: return config_size, config_crc, config_magic -def get_config(content: bytes) -> dict: - """Read the config from a GP2040-CE storage section. - - Args: - content: bytes from a GP2040-CE board's storage section - Returns: - the parsed configuration - """ - size, _, _ = get_config_footer(content) - - config_pb2 = get_config_pb2() - config = config_pb2.Config() - config.ParseFromString(content[-(size+FOOTER_SIZE):-FOOTER_SIZE]) - logger.debug("parsed: %s", config) - return config +############ +# COMMANDS # +############ def visualize(): diff --git a/pyproject.toml b/pyproject.toml index 509d0f5..eac8151 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,18 +14,25 @@ dependencies = ["grpcio-tools"] dynamic = ["version"] [project.optional-dependencies] -dev = ["decorator", "flake8", "flake8-blind-except", "flake8-builtins", "flake8-docstrings", "flake8-executable", - "flake8-fixme", "flake8-isort", "flake8-logging-format", "flake8-mutable", "flake8-pyproject", "mypy", - "pip-tools", "pytest", "setuptools-scm"] +dev = ["bandit", "decorator", "flake8", "flake8-blind-except", "flake8-builtins", "flake8-docstrings", + "flake8-executable", "flake8-fixme", "flake8-isort", "flake8-logging-format", "flake8-mutable", + "flake8-pyproject", "mypy", "pip-tools", "pytest", "pytest-cov", "setuptools-scm", "tox"] [project.scripts] visualize-storage = "gp2040ce_bintools.storage:visualize" [tool.flake8] +enable-extensions = "G,M" +exclude = [".tox/", "venv/", "_version.py", "tests/test-files/"] +extend-ignore = "T101" +max-complexity = 10 max-line-length = 120 [tool.mypy] ignore_missing_imports = true +[tool.pytest] +python_files = ["*_tests.py", "tests.py", "test_*.py"] + [tool.setuptools_scm] write_to = "gp2040ce_bintools/_version.py" diff --git a/requirements/requirements-dev.txt b/requirements/requirements-dev.txt index 73574c6..557af57 100644 --- a/requirements/requirements-dev.txt +++ b/requirements/requirements-dev.txt @@ -4,14 +4,30 @@ # # pip-compile --extra=dev --output-file=requirements/requirements-dev.txt # +bandit==1.7.5 + # via gp2040ce-binary-tools (pyproject.toml) build==0.10.0 # via pip-tools +cachetools==5.3.1 + # via tox +chardet==5.1.0 + # via tox click==8.1.3 # via pip-tools +colorama==0.4.6 + # via tox +coverage[toml]==7.2.7 + # via pytest-cov decorator==5.1.1 # via gp2040ce-binary-tools (pyproject.toml) +distlib==0.3.6 + # via virtualenv exceptiongroup==1.1.1 # via pytest +filelock==3.12.2 + # via + # tox + # virtualenv flake8==6.0.0 # via # flake8-builtins @@ -39,6 +55,10 @@ flake8-mutable==1.2.0 # via gp2040ce-binary-tools (pyproject.toml) flake8-pyproject==1.2.3 # via gp2040ce-binary-tools (pyproject.toml) +gitdb==4.0.10 + # via gitpython +gitpython==3.1.31 + # via bandit grpcio==1.54.2 # via grpcio-tools grpcio-tools==1.54.2 @@ -47,8 +67,12 @@ iniconfig==2.0.0 # via pytest isort==5.12.0 # via flake8-isort +markdown-it-py==3.0.0 + # via rich mccabe==0.7.0 # via flake8 +mdurl==0.1.2 + # via markdown-it-py mypy==1.4.0 # via gp2040ce-binary-tools (pyproject.toml) mypy-extensions==1.0.0 @@ -56,12 +80,22 @@ mypy-extensions==1.0.0 packaging==23.1 # via # build + # pyproject-api # pytest # setuptools-scm + # tox +pbr==5.11.1 + # via stevedore pip-tools==6.13.0 # via gp2040ce-binary-tools (pyproject.toml) +platformdirs==3.7.0 + # via + # tox + # virtualenv pluggy==1.0.0 - # via pytest + # via + # pytest + # tox protobuf==4.23.3 # via grpcio-tools pycodestyle==2.10.0 @@ -70,25 +104,49 @@ pydocstyle==6.3.0 # via flake8-docstrings pyflakes==3.0.1 # via flake8 +pygments==2.15.1 + # via rich +pyproject-api==1.5.2 + # via tox pyproject-hooks==1.0.0 # via build pytest==7.3.2 + # via + # gp2040ce-binary-tools (pyproject.toml) + # pytest-cov +pytest-cov==4.1.0 # via gp2040ce-binary-tools (pyproject.toml) +pyyaml==6.0 + # via bandit +rich==13.4.2 + # via bandit setuptools-scm==7.1.0 # via gp2040ce-binary-tools (pyproject.toml) +smmap==5.0.0 + # via gitdb snowballstemmer==2.2.0 # via pydocstyle +stevedore==5.1.0 + # via bandit tomli==2.0.1 # via # build + # coverage # flake8-pyproject # mypy + # pyproject-api + # pyproject-hooks # pytest # setuptools-scm + # tox +tox==4.6.3 + # via gp2040ce-binary-tools (pyproject.toml) typing-extensions==4.6.3 # via # mypy # setuptools-scm +virtualenv==20.23.1 + # via tox wheel==0.40.0 # via pip-tools diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..298634c --- /dev/null +++ b/tox.ini @@ -0,0 +1,68 @@ +# tox (https://tox.readthedocs.io/) is a tool for running tests +# in multiple virtualenvs. This configuration file will run the +# test suite on all supported python versions. To use it, "pip install tox" +# and then run "tox" from this directory. + +[tox] +isolated_build = true +envlist = begin,py39,py310,py311,coverage,bandit,lint + +[testenv] +allow_externals = pytest, coverage + +deps = + -rrequirements/requirements-dev.txt + +[testenv:begin] +# clean up potential previous coverage runs +skip_install = true +commands = coverage erase + +[testenv:py39] +# run pytest with coverage +commands = + pytest --cov-append --cov={envsitepackagesdir}/gp2040ce_bintools/ --cov-branch + +[testenv:py310] +# run pytest with coverage +commands = + pytest --cov-append --cov={envsitepackagesdir}/gp2040ce_bintools/ --cov-branch + +[testenv:py311] +# run pytest with coverage +commands = + pytest --cov-append --cov={envsitepackagesdir}/gp2040ce_bintools/ --cov-branch + +[testenv:coverage] +# report on coverage runs from above +skip_install = true +commands = + coverage report --fail-under=80 --show-missing + +[testenv:bandit] +commands = + bandit ./gp2040ce_bintools/ -r + +; dep hell, can't install safety at the moment +; [testenv:safety] +; skip_install = true +; isolated_build = false +; commands = +; safety check -r requirements/requirements-dev.txt + +[testenv:lint] +# run style checks +commands = + flake8 + - flake8 --disable-noqa --ignore= --select=E,W,F,C,D,A,G,B,I,T,M,DUO + +[coverage:paths] +source = + ./ + .tox/**/site-packages/ + +[coverage:run] +branch = True + +omit = + **/_version.py