-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
185 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[MASTER] | ||
unsafe-load-any-extension=y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,80 @@ | ||
from ._bgcode import * | ||
|
||
"""Prusa Block & Binary G-code reader / writer / converter""" | ||
__version__ = _bgcode.version() | ||
# pylint: disable=redefined-builtin | ||
from ._bgcode import ( # type: ignore | ||
BlockHeader, | ||
CompressionType, | ||
EBlockType, | ||
EResult, | ||
EThumbnailFormat, | ||
FileHeader, | ||
FILEWrapper, | ||
ThumbnailBlock, | ||
close, | ||
from_ascii_to_binary, | ||
from_binary_to_ascii, | ||
get_config, | ||
is_open, | ||
open, | ||
read_header, | ||
read_next_block_header, | ||
rewind, | ||
translate_result, | ||
version, | ||
) | ||
|
||
__version__ = version() | ||
|
||
__all__ = [ | ||
"BlockHeader", | ||
"CompressionType", | ||
"EBlockType", | ||
"EResult", | ||
"EThumbnailFormat", | ||
"FileHeader", | ||
"ThumbnailBlock", | ||
"close", | ||
"from_ascii_to_binary", | ||
"from_binary_to_ascii", | ||
"get_config", | ||
"is_open", | ||
"open", | ||
"read_header", | ||
"read_next_block_header", | ||
"rewind", | ||
"translate_result"] | ||
|
||
|
||
class ResultError(Exception): | ||
"""Exception based on EResult.""" | ||
|
||
def __init__(self, res: EResult): | ||
super().__init__(translate_result(res)) | ||
|
||
|
||
def read_thumbnails(gcodefile: FILEWrapper): | ||
"""Read thumbnails from binary gcode file.""" | ||
assert is_open(gcodefile) | ||
rewind(gcodefile) | ||
|
||
header = FileHeader() | ||
res = read_header(gcodefile, header) | ||
if res != EResult.Success: | ||
raise ResultError(res) | ||
|
||
block = BlockHeader() | ||
thumbnails = [] | ||
while res == EResult.Success: | ||
res = read_next_block_header(gcodefile, header, block, | ||
EBlockType.Thumbnail) | ||
if res != EResult.Success: | ||
break | ||
|
||
thumbnail_block = ThumbnailBlock() | ||
res = thumbnail_block.read_data(gcodefile, header, block) | ||
if res != EResult.Success: | ||
raise ResultError(res) | ||
|
||
thumbnails.append({"meta": thumbnail_block.params, | ||
"bytes": thumbnail_block.data()}) | ||
|
||
return thumbnails |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
select = [ | ||
"F", # pyflakes | ||
"E", # pycodestyle | ||
"W", # pycodestyle | ||
"C90", # mccabe | ||
"I", # isort | ||
"N", # pep8-naming | ||
# "D", # pydocstyle | ||
# "UP", # pyupgrade | ||
"YTT", # flake8-2020 | ||
# "ANN", # flake8-annotations | ||
"S", # flake8-bandit | ||
"BLE", # flake8-blind-except | ||
# "FBT", # flake8-boolean-trap | ||
"B", # flake8-bugbear | ||
"A", # flake8-builtins | ||
"COM", # flake8-commas | ||
"C4", # flake8-comprehensions | ||
# "DTZ", # flake8-datetimez | ||
"T10", # flake8-debugger | ||
"DJ", # flake8-django | ||
# "EM", # flake8-errmsg | ||
"EXE", # flake8-executable | ||
"ISC", # flake8-implicit-str-concat | ||
"ICN", # flake8-import-conventions | ||
"G", # flake8-logging-format | ||
"INP", # flake8-no-pep420 | ||
"PIE", # flake8-pie | ||
"T20", # flake8-print | ||
"PYI", # flake8-pyi | ||
"PT", # flake8-pytest-style | ||
"Q", # flake8-quotes | ||
"RSE", # flake8-raise | ||
"RET", # flake8-return | ||
"SLF", # flake8-self | ||
"SIM", # flake8-simplify | ||
# "TID", # flake8-tidy-imports | ||
"TCH", # flake8-type-checking | ||
"INT", # flake8-gettext | ||
"ARG", # flake8-unused-arguments | ||
# "PTH", # flake8-use-pathlib | ||
"ERA", # eradicate | ||
"PD", # pandas-vet | ||
"PGH", # pygrep-hooks | ||
"PL", # Pylint | ||
"TRY", # tryceratops | ||
"FLY", # flynt | ||
"NPY", # NumPy-specific rules | ||
"RUF", # Ruff-specific rules | ||
] | ||
ignore = [ | ||
"PGH003", # Use specific rule codes when ignoring type issues | ||
"S101", # Use of `assert` detected | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,78 @@ | ||
import pybgcode | ||
"""Convert test functions.""" | ||
import filecmp | ||
import io | ||
|
||
import pytest | ||
|
||
import pybgcode | ||
from pybgcode import ( | ||
EResult, | ||
EThumbnailFormat, | ||
read_thumbnails, | ||
) | ||
|
||
# pylint: disable=missing-function-docstring | ||
TEST_GCODE = "test.gcode" | ||
TEST_REVERSE_GCODE = "test_reverse.gcode" | ||
TEST_BGCODE = "test.bgcode" | ||
TEST_THUMBNAILS = 2 | ||
|
||
|
||
def get_thumbnail_extension(format_id): | ||
ret = "unknown" | ||
if format_id == int(pybgcode.EThumbnailFormat.PNG): | ||
if format_id == int(EThumbnailFormat.PNG): | ||
ret = "png" | ||
elif format_id == int(pybgcode.EThumbnailFormat.JPG): | ||
elif format_id == int(EThumbnailFormat.JPG): | ||
ret = "jpg" | ||
elif format_id == int(pybgcode.EThumbnailFormat.QOI): | ||
elif format_id == int(EThumbnailFormat.QOI): | ||
ret = "qoi" | ||
|
||
return ret | ||
|
||
def read_thumbnails(gcodefile): | ||
assert(pybgcode.is_open(gcodefile)) | ||
pybgcode.rewind(gcodefile) | ||
|
||
header = pybgcode.FileHeader() | ||
res = pybgcode.read_header(gcodefile, header) | ||
if res != pybgcode.EResult.Success: | ||
raise Exception(pybgcode.translate_result(res)) | ||
|
||
block = pybgcode.BlockHeader() | ||
thumbnails = [] | ||
while res == pybgcode.EResult.Success: | ||
res = pybgcode.read_next_block_header(gcodefile, header, block, pybgcode.EBlockType.Thumbnail) | ||
if res != pybgcode.EResult.Success: | ||
break | ||
|
||
thumbnail_block = pybgcode.ThumbnailBlock() | ||
res = thumbnail_block.read_data(gcodefile, header, block) | ||
if res != pybgcode.EResult.Success: | ||
raise Exception(pybgcode.translate_result(res)) | ||
|
||
thumbnails.append({"meta": thumbnail_block.params, "bytes": thumbnail_block.data()}) | ||
|
||
return thumbnails | ||
|
||
|
||
def test_main(): | ||
assert(pybgcode.__version__ == pybgcode.version()) | ||
assert pybgcode.__version__ == pybgcode.version() | ||
|
||
in_f = pybgcode.open("test.gcode", "r"); | ||
out_f = pybgcode.open("test.bgcode", "wb"); | ||
in_f = pybgcode.open(TEST_GCODE, "r") | ||
out_f = pybgcode.open(TEST_BGCODE, "wb") | ||
|
||
assert(in_f) | ||
assert(out_f) | ||
assert in_f | ||
assert out_f | ||
|
||
assert(pybgcode.is_open(in_f)) | ||
assert(pybgcode.is_open(out_f)) | ||
assert pybgcode.is_open(in_f) | ||
assert pybgcode.is_open(out_f) | ||
|
||
cfg = pybgcode.get_config(); | ||
cfg = pybgcode.get_config() | ||
cfg.compression.file_metadata = pybgcode.CompressionType.Heatshrink_11_4 | ||
res = pybgcode.from_ascii_to_binary(in_f, out_f, cfg); | ||
res = pybgcode.from_ascii_to_binary(in_f, out_f, cfg) | ||
|
||
assert(res == pybgcode.EResult.Success) | ||
assert res == EResult.Success | ||
|
||
pybgcode.close(out_f) | ||
pybgcode.close(in_f) | ||
|
||
thumb_f = pybgcode.open("test.bgcode", "rb") | ||
thumb_f = pybgcode.open(TEST_BGCODE, "rb") | ||
thumbnails = read_thumbnails(thumb_f) | ||
assert(len(thumbnails) == 2) | ||
assert len(thumbnails) == TEST_THUMBNAILS | ||
pybgcode.close(thumb_f) | ||
|
||
# write thumbnails to png files | ||
thumcnt = 0; | ||
thumcnt = 0 | ||
for thumb in thumbnails: | ||
with open("thumb" + str(thumcnt) + "." + get_thumbnail_extension(thumb["meta"].format), "wb") as outpng: | ||
outpng.write(thumb["bytes"]) | ||
with open("thumb" + str(thumcnt) + "." + | ||
get_thumbnail_extension(thumb["meta"].format), "wb") as out: | ||
out.write(thumb["bytes"]) | ||
thumcnt = thumcnt + 1 | ||
|
||
in_f = pybgcode.open("test.bgcode", "rb"); | ||
out_f = pybgcode.open("test_reverse.gcode", "w"); | ||
in_f = pybgcode.open(TEST_BGCODE, "rb") | ||
out_f = pybgcode.open(TEST_REVERSE_GCODE, "w") | ||
|
||
res = pybgcode.from_binary_to_ascii(in_f, out_f, True) | ||
assert(res == pybgcode.EResult.Success) | ||
assert res == EResult.Success | ||
|
||
pybgcode.close(out_f) | ||
|
||
assert(filecmp.cmp("test.gcode", "test_reverse.gcode", shallow=False)) | ||
assert filecmp.cmp(TEST_GCODE, TEST_REVERSE_GCODE, shallow=False) | ||
|
||
|
||
if __name__ == '__main__': | ||
test_main() | ||
if __name__ == "__main__": | ||
pytest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters