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

Reformat all python files with black #480

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 1 addition & 5 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,4 @@

from pathlib import Path

from .zcbor.zcbor import (
CddlValidationError,
DataTranslator,
main
)
from .zcbor.zcbor import CddlValidationError, DataTranslator, main
8 changes: 4 additions & 4 deletions scripts/add_helptext.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from sys import argv

p_root = Path(__file__).absolute().parents[1]
p_README = Path(p_root, 'README.md')
p_README = Path(p_root, "README.md")

pattern = r"""
Command line documentation
Expand Down Expand Up @@ -42,13 +42,13 @@
```
"""

with open(p_README, 'r', encoding="utf-8") as f:
with open(p_README, "r", encoding="utf-8") as f:
readme_contents = f.read()
new_readme_contents = sub(pattern + r'.*', output, readme_contents, flags=S)
new_readme_contents = sub(pattern + r".*", output, readme_contents, flags=S)
if len(argv) > 1 and argv[1] == "--check":
if new_readme_contents != readme_contents:
print("Check failed")
exit(9)
else:
with open(p_README, 'w', encoding="utf-8") as f:
with open(p_README, "w", encoding="utf-8") as f:
f.write(new_readme_contents)
1 change: 1 addition & 0 deletions scripts/black.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
black $(dirname "$0")/.. -l 100
18 changes: 10 additions & 8 deletions scripts/regenerate_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,24 @@
from tempfile import mkdtemp

p_root = Path(__file__).absolute().parents[1]
p_build = p_root / 'build'
p_pet_sample = p_root / 'samples' / 'pet'
p_pet_cmake = p_pet_sample / 'pet.cmake'
p_pet_include = p_pet_sample / 'include'
p_pet_src = p_pet_sample / 'src'
p_build = p_root / "build"
p_pet_sample = p_root / "samples" / "pet"
p_pet_cmake = p_pet_sample / "pet.cmake"
p_pet_include = p_pet_sample / "include"
p_pet_src = p_pet_sample / "src"


def regenerate():
tmpdir = Path(mkdtemp())
run(['cmake', p_pet_sample, "-DREGENERATE_ZCBOR=Y", "-DCMAKE_MESSAGE_LOG_LEVEL=WARNING"],
cwd=tmpdir)
run(
["cmake", p_pet_sample, "-DREGENERATE_ZCBOR=Y", "-DCMAKE_MESSAGE_LOG_LEVEL=WARNING"],
cwd=tmpdir,
)
rmtree(tmpdir)


def check():
files = (list(p_pet_include.iterdir()) + list(p_pet_src.iterdir()) + [p_pet_cmake])
files = list(p_pet_include.iterdir()) + list(p_pet_src.iterdir()) + [p_pet_cmake]
contents = "".join(p.read_text(encoding="utf-8") for p in files)
tmpdir = Path(mkdtemp())
list(makedirs(tmpdir / f.relative_to(p_pet_sample).parent, exist_ok=True) for f in files)
Expand Down
2 changes: 1 addition & 1 deletion scripts/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pyelftools
pycodestyle
black
west
ecdsa
26 changes: 16 additions & 10 deletions scripts/update_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
from datetime import datetime

p_root = Path(__file__).absolute().parents[1]
p_VERSION = Path(p_root, 'zcbor', 'VERSION')
p_RELEASE_NOTES = Path(p_root, 'RELEASE_NOTES.md')
p_MIGRATION_GUIDE = Path(p_root, 'MIGRATION_GUIDE.md')
p_common_h = Path(p_root, 'include', 'zcbor_common.h')
p_VERSION = Path(p_root, "zcbor", "VERSION")
p_RELEASE_NOTES = Path(p_root, "RELEASE_NOTES.md")
p_MIGRATION_GUIDE = Path(p_root, "MIGRATION_GUIDE.md")
p_common_h = Path(p_root, "include", "zcbor_common.h")

RELEASE_NOTES_boilerplate = """
Any new bugs, requests, or missing features should be reported as [Github issues](/~https://github.com/NordicSemiconductor/zcbor/issues).
Expand All @@ -31,23 +31,29 @@ def update_relnotes(p_relnotes, version, boilerplate="", include_date=True):
new_date = f" ({datetime.today().strftime('%Y-%m-%d')})" if include_date else ""
relnotes_new_header = f"# zcbor v. {version}{new_date}\n"
if ".99" not in relnotes_lines[0]:
relnotes_contents = relnotes_new_header + boilerplate + '\n\n' + relnotes_contents
relnotes_contents = relnotes_new_header + boilerplate + "\n\n" + relnotes_contents
relnotes_contents = sub(r".*?\n", relnotes_new_header, relnotes_contents, count=1)
p_relnotes.write_text(relnotes_contents, encoding="utf-8")


if __name__ == "__main__":
if len(argv) != 2 or match(r'\d+\.\d+\.\d+', argv[1]) is None:
if len(argv) != 2 or match(r"\d+\.\d+\.\d+", argv[1]) is None:
print(f"Usage: {argv[0]} <new zcbor version>")
exit(1)
version = argv[1]
(major, minor, bugfix) = version.split('.')
(major, minor, bugfix) = version.split(".")

p_VERSION.write_text(version, encoding="utf-8")
update_relnotes(p_RELEASE_NOTES, version, boilerplate=RELEASE_NOTES_boilerplate)
update_relnotes(p_MIGRATION_GUIDE, version, include_date=False)
p_common_h_contents = p_common_h.read_text(encoding="utf-8")
common_h_new_contents = sub(r"(#define ZCBOR_VERSION_MAJOR )\d+", f"\\g<1>{major}", p_common_h_contents)
common_h_new_contents = sub(r"(#define ZCBOR_VERSION_MINOR )\d+", f"\\g<1>{minor}", common_h_new_contents)
common_h_new_contents = sub(r"(#define ZCBOR_VERSION_BUGFIX )\d+", f"\\g<1>{bugfix}", common_h_new_contents)
common_h_new_contents = sub(
r"(#define ZCBOR_VERSION_MAJOR )\d+", f"\\g<1>{major}", p_common_h_contents
)
common_h_new_contents = sub(
r"(#define ZCBOR_VERSION_MINOR )\d+", f"\\g<1>{minor}", common_h_new_contents
)
common_h_new_contents = sub(
r"(#define ZCBOR_VERSION_BUGFIX )\d+", f"\\g<1>{bugfix}", common_h_new_contents
)
p_common_h.write_text(common_h_new_contents, encoding="utf-8")
39 changes: 23 additions & 16 deletions tests/decode/test5_corner_cases/floats.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,35 @@
import math
import cbor2


def print_var(val1, val2, bytestr):
var_str = ""
for b in bytestr:
var_str += hex(b) + ", "
print(str(val1) + ":", val2, bytestr.hex(), var_str)

def print_32_64(str_val, val = None):
val = val or float(str_val)
print_var(str_val, val, struct.pack("!e", numpy.float16(val)))
print_var(str_val, val, struct.pack("!f", struct.unpack("!e", struct.pack("!e", numpy.float16(val)))[0]))
print (numpy.float32(struct.unpack("!e", struct.pack("!e", numpy.float16(val)))[0]))
print_var(str_val, val, struct.pack("!f", val))
print_var(str_val, val, struct.pack("!d", val))
print_var(str_val, val, struct.pack("!d", struct.unpack("!f", struct.pack("!f", val))[0]))
print()
var_str = ""
for b in bytestr:
var_str += hex(b) + ", "
print(str(val1) + ":", val2, bytestr.hex(), var_str)


def print_32_64(str_val, val=None):
val = val or float(str_val)
print_var(str_val, val, struct.pack("!e", numpy.float16(val)))
print_var(
str_val,
val,
struct.pack("!f", struct.unpack("!e", struct.pack("!e", numpy.float16(val)))[0]),
)
print(numpy.float32(struct.unpack("!e", struct.pack("!e", numpy.float16(val)))[0]))
print_var(str_val, val, struct.pack("!f", val))
print_var(str_val, val, struct.pack("!d", val))
print_var(str_val, val, struct.pack("!d", struct.unpack("!f", struct.pack("!f", val))[0]))
print()


print_32_64("3.1415")
print_32_64("2.71828")
print_32_64("1234567.89")
print_32_64("-98765.4321")
print_32_64("123/456789", 123/456789)
print_32_64("-2^(-42)", -1/(2**(42)))
print_32_64("123/456789", 123 / 456789)
print_32_64("-2^(-42)", -1 / (2 ** (42)))
print_32_64("1.0")
print_32_64("-10000.0")
print_32_64("0.0")
Expand Down
110 changes: 51 additions & 59 deletions tests/scripts/test_repo_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from urllib.error import HTTPError
from argparse import ArgumentParser
from subprocess import Popen, check_output, PIPE, run
from pycodestyle import StyleGuide
from shutil import rmtree, copy2
from platform import python_version_tuple
from sys import platform
Expand All @@ -21,77 +20,63 @@


p_root = Path(__file__).absolute().parents[2]
p_tests = p_root / 'tests'
p_tests = p_root / "tests"
p_readme = p_root / "README.md"
p_pypi_readme = p_root / "pypi_README.md"
p_architecture = p_root / "ARCHITECTURE.md"
p_release_notes = p_root / "RELEASE_NOTES.md"
p_init_py = p_root / '__init__.py'
p_zcbor_py = p_root / 'zcbor' / 'zcbor.py'
p_add_helptext = p_root / 'scripts' / 'add_helptext.py'
p_regenerate_samples = p_root / 'scripts' / 'regenerate_samples.py'
p_test_zcbor_py = p_tests / 'scripts' / 'test_zcbor.py'
p_test_versions_py = p_tests / 'scripts' / 'test_versions.py'
p_test_repo_files_py = p_tests / 'scripts' / 'test_repo_files.py'
p_hello_world_sample = p_root / 'samples' / 'hello_world'
p_hello_world_build = p_hello_world_sample / 'build'
p_pet_sample = p_root / 'samples' / 'pet'
p_pet_cmake = p_pet_sample / 'pet.cmake'
p_pet_include = p_pet_sample / 'include'
p_pet_src = p_pet_sample / 'src'
p_pet_build = p_pet_sample / 'build'
p_add_helptext = p_root / "scripts" / "add_helptext.py"
p_regenerate_samples = p_root / "scripts" / "regenerate_samples.py"
p_hello_world_sample = p_root / "samples" / "hello_world"
p_hello_world_build = p_hello_world_sample / "build"
p_pet_sample = p_root / "samples" / "pet"
p_pet_cmake = p_pet_sample / "pet.cmake"
p_pet_include = p_pet_sample / "include"
p_pet_src = p_pet_sample / "src"
p_pet_build = p_pet_sample / "build"


class TestCodestyle(TestCase):
def do_codestyle(self, files, **kwargs):
style = StyleGuide(max_line_length=100, **kwargs)
result = style.check_files([str(f) for f in files])
result.print_statistics()
self.assertEqual(result.total_errors, 0,
f"Found {result.total_errors} style errors")

def test_codestyle(self):
"""Run codestyle tests on all Python scripts in the repo."""
self.do_codestyle([p_init_py, p_test_versions_py, p_test_repo_files_py, p_add_helptext,
p_regenerate_samples])
self.do_codestyle([p_zcbor_py], ignore=['W191', 'E101', 'W503'])
self.do_codestyle([p_test_zcbor_py], ignore=['E402', 'E501', 'W503'])
black_res = Popen(["black", "--check", p_root, "-l", "100"], stdout=PIPE, stderr=PIPE)
_, stderr = black_res.communicate()
self.assertEqual(0, black_res.returncode, "black failed:\n" + stderr.decode("utf-8"))


def version_int(in_str):
return int(search(r'\A\d+', in_str)[0]) # e.g. '0rc' -> '0'
return int(search(r"\A\d+", in_str)[0]) # e.g. '0rc' -> '0'


class TestSamples(TestCase):
def popen_test(self, args, input='', exp_retcode=0, **kwargs):
def popen_test(self, args, input="", exp_retcode=0, **kwargs):
call0 = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, **kwargs)
stdout0, stderr0 = call0.communicate(input)
self.assertEqual(exp_retcode, call0.returncode, stderr0.decode('utf-8'))
self.assertEqual(exp_retcode, call0.returncode, stderr0.decode("utf-8"))
return stdout0, stderr0

def cmake_build_run(self, path, build_path):
if build_path.exists():
rmtree(build_path)
with open(path / 'README.md', 'r', encoding="utf-8") as f:
with open(path / "README.md", "r", encoding="utf-8") as f:
contents = f.read()

to_build_patt = r'### To build:.*?```(?P<to_build>.*?)```'
to_run_patt = r'### To run:.*?```(?P<to_run>.*?)```'
exp_out_patt = r'### Expected output:.*?(?P<exp_out>(\n>[^\n]*)+)'
to_build = search(to_build_patt, contents, flags=S)['to_build'].strip()
to_run = search(to_run_patt, contents, flags=S)['to_run'].strip()
exp_out = search(exp_out_patt, contents, flags=S)['exp_out'].replace("\n> ", "\n").strip()
to_build_patt = r"### To build:.*?```(?P<to_build>.*?)```"
to_run_patt = r"### To run:.*?```(?P<to_run>.*?)```"
exp_out_patt = r"### Expected output:.*?(?P<exp_out>(\n>[^\n]*)+)"
to_build = search(to_build_patt, contents, flags=S)["to_build"].strip()
to_run = search(to_run_patt, contents, flags=S)["to_run"].strip()
exp_out = search(exp_out_patt, contents, flags=S)["exp_out"].replace("\n> ", "\n").strip()

os.chdir(path)
commands_build = [(line.split(' ')) for line in to_build.split('\n')]
assert '\n' not in to_run, "The 'to run' section should only have one command."
commands_run = to_run.split(' ')
commands_build = [(line.split(" ")) for line in to_build.split("\n")]
assert "\n" not in to_run, "The 'to run' section should only have one command."
commands_run = to_run.split(" ")
for c in commands_build:
self.popen_test(c)
output_run = ""
for c in commands_run:
output, _ = self.popen_test(c)
output_run += output.decode('utf-8')
output_run += output.decode("utf-8")
self.assertEqual(exp_out, output_run.strip())

@skipIf(platform.startswith("win"), "Skip on Windows because requires a Unix shell.")
Expand All @@ -109,17 +94,16 @@ def test_pet_regenerate(self):
self.assertEqual(0, regenerate.returncode)

def test_pet_file_header(self):
files = (list(p_pet_include.iterdir()) + list(p_pet_src.iterdir()) + [p_pet_cmake])
files = list(p_pet_include.iterdir()) + list(p_pet_src.iterdir()) + [p_pet_cmake]
for p in [f for f in files if "pet" in f.name]:
with p.open('r', encoding="utf-8") as f:
with p.open("r", encoding="utf-8") as f:
f.readline() # discard
self.assertEqual(
f.readline().strip(" *#\n"),
"Copyright (c) 2022 Nordic Semiconductor ASA")
"Copyright (c) 2022 Nordic Semiconductor ASA",
)
f.readline() # discard
self.assertEqual(
f.readline().strip(" *#\n"),
"SPDX-License-Identifier: Apache-2.0")
self.assertEqual(f.readline().strip(" *#\n"), "SPDX-License-Identifier: Apache-2.0")
f.readline() # discard
self.assertIn("Generated using zcbor version", f.readline())
self.assertIn("/~https://github.com/NordicSemiconductor/zcbor", f.readline())
Expand All @@ -130,15 +114,21 @@ class TestDocs(TestCase):
def __init__(self, *args, **kwargs):
"""Overridden to get base URL for relative links from remote tracking branch."""
super(TestDocs, self).__init__(*args, **kwargs)
remote_tr_args = ['git', 'rev-parse', '--abbrev-ref', '--symbolic-full-name', '@{u}']
remote_tracking = run(remote_tr_args, capture_output=True).stdout.decode('utf-8').strip()
remote_tr_args = [
"git",
"rev-parse",
"--abbrev-ref",
"--symbolic-full-name",
"@{u}",
]
remote_tracking = run(remote_tr_args, capture_output=True).stdout.decode("utf-8").strip()

if remote_tracking:
remote, remote_branch = remote_tracking.split('/', 1) # '1' to only split one time.
repo_url_args = ['git', 'remote', 'get-url', remote]
repo_url = check_output(repo_url_args).decode('utf-8').strip().strip('.git')
if 'github.com' in repo_url:
self.base_url = (repo_url + '/tree/' + remote_branch + '/')
remote, remote_branch = remote_tracking.split("/", 1) # '1' to only split one time.
repo_url_args = ["git", "remote", "get-url", remote]
repo_url = check_output(repo_url_args).decode("utf-8").strip().strip(".git")
if "github.com" in repo_url:
self.base_url = repo_url + "/tree/" + remote_branch + "/"
else:
# The URL is not in github.com, so we are not sure it is constructed correctly.
self.base_url = None
Expand All @@ -150,7 +140,7 @@ def __init__(self, *args, **kwargs):
# There is no remote tracking branch.
self.base_url = None

self.link_regex = compile(r'\[.*?\]\((?P<link>.*?)\)')
self.link_regex = compile(r"\[.*?\]\((?P<link>.*?)\)")

def check_code(self, link, codes):
"""Check the status code of a URL link. Assert if not 200 (OK)."""
Expand All @@ -164,7 +154,7 @@ def check_code(self, link, codes):
def do_test_links(self, path, allow_local=True):
"""Get all Markdown links in the file at <path> and check that they work."""
if allow_local and self.base_url is None:
raise SkipTest('This test requires the current branch to be pushed to Github.')
raise SkipTest("This test requires the current branch to be pushed to Github.")

text = path.read_text(encoding="utf-8")

Expand Down Expand Up @@ -211,8 +201,10 @@ def test_pet_readme(self):
def test_pypi_readme(self):
self.do_test_links(p_pypi_readme, allow_local=False)

@skipIf(list(map(version_int, python_version_tuple())) < [3, 10, 0],
"Skip on Python < 3.10 because of different wording in argparse output.")
@skipIf(
list(map(version_int, python_version_tuple())) < [3, 10, 0],
"Skip on Python < 3.10 because of different wording in argparse output.",
)
@skipIf(platform.startswith("win"), "Skip on Windows because of path/newline issues.")
def test_cli_doc(self):
"""Check the auto-generated CLI docs in the top level README.md file."""
Expand Down
Loading