Skip to content

Commit

Permalink
load_from_chunk common MetaData method (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarkafa authored Oct 25, 2023
1 parent d4343f8 commit ea77735
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
6 changes: 6 additions & 0 deletions gcode_metadata/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,12 @@ def load_from_path(self, path: str):
"""
# pylint: disable=unused-argument

def load_from_chunk(self, data: bytes, size: int):
"""Process given chunk array of data.
:data: data of a chunk.
:size: size of the file."""
# pylint: disable=unused-argument

def set_data(self, data: Dict):
"""Helper function to save all items from `data` that
match `self.Attr` in `self.data`.
Expand Down
46 changes: 26 additions & 20 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,26 @@ def tmp_dir():
del temp


def chunk_read_file(meta_class, filepath, chunk_size=None):
"""Util method which reads the file in chunk and call load_from_chunk
on the blocks of data from given meta_class."""
chunk_size = chunk_size or 10 * 1024 # 10 KiB test on small chunks
with open(filepath, 'rb') as file:
# find out file size
file_size = file.seek(0, os.SEEK_END)
file.seek(0)
while True:
chunk = file.read(chunk_size)
if not chunk:
break
meta_class.load_from_chunk(chunk, file_size)


def give_cache_version(path, version_to_give):
"""Modifies the cache file and adds a valid version number"""
with open(path, "r", encoding='utf-8') as cache_file:
cache = json.load(cache_file)
my_cache = {
"version": version_to_give
}
my_cache = {"version": version_to_give}
my_cache.update(cache)
with open(path, "w", encoding='utf-8') as cache_file:
json.dump(my_cache, cache_file)
Expand Down Expand Up @@ -137,20 +150,6 @@ def test_get_metadata_invalid_file():
class TestFDNMetaData:
"""Tests for standard gcode metadata."""

def chunk_read_file(self, meta_class, filepath, chunk_size=None):
"""Util method which reads the file in chunk and call load_from_chunk
on the blocks of data from given meta_class."""
chunk_size = chunk_size or 10 * 1024 # 10 KiB test on small chunks
with open(filepath, 'rb') as file:
# find out file size
file_size = file.seek(0, os.SEEK_END)
file.seek(0)
while True:
chunk = file.read(chunk_size)
if not chunk:
break
meta_class.load_from_chunk(chunk, file_size)

def test_full(self):
"""Both the file and filename contain metadata. There are thumbnails.
"""
Expand Down Expand Up @@ -255,7 +254,7 @@ def test_from_chunks(self, chunk_size):
fname = os.path.join(gcodes_dir,
"fdn_full_0.15mm_PETG_MK3S_2h6m.gcode")
chunk_meta = get_meta_class(fname)
self.chunk_read_file(chunk_meta, fname, chunk_size)
chunk_read_file(chunk_meta, fname, chunk_size)
# check that same result came from parsing metadata as a whole file
meta = get_metadata(fname, False)
assert meta.thumbnails == chunk_meta.thumbnails
Expand All @@ -276,7 +275,7 @@ def test_from_chunks_meta_only_path(self):
fname = os.path.join(gcodes_dir,
"fdn_only_filename_0.25mm_PETG_MK3S_2h9m.gcode")
chunk_meta = get_meta_class(fname)
self.chunk_read_file(chunk_meta, fname)
chunk_read_file(chunk_meta, fname)
assert chunk_meta.data == {}
chunk_meta.load_from_path(fname)
assert chunk_meta.data == {
Expand All @@ -291,7 +290,7 @@ def test_from_chunks_empty_file(self):
"""Test chunks from file with no metadata."""
fname = os.path.join(gcodes_dir, "fdn_all_empty.gcode")
chunk_meta = get_meta_class(fname)
self.chunk_read_file(chunk_meta, fname)
chunk_read_file(chunk_meta, fname)
assert chunk_meta.data == {}

def test_from_chunks_invalid_file(self):
Expand Down Expand Up @@ -334,3 +333,10 @@ def test_sl_empty_file(self):

assert not meta.data
assert not meta.thumbnails

def test_sl1_chunk_read(self):
"""TODO For now when chunk reading sl file metadata are not loaded"""
fname = os.path.join(gcodes_dir, "empty.sl1")
chunk_meta = get_meta_class(fname)
chunk_read_file(chunk_meta, fname)
assert chunk_meta.data == {}

0 comments on commit ea77735

Please sign in to comment.