Skip to content

Commit

Permalink
skip in chunk parse
Browse files Browse the repository at this point in the history
  • Loading branch information
sarkafa committed Jul 28, 2023
1 parent c0cb61a commit 151f323
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 10 additions & 1 deletion gcode_metadata/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def __init__(self, path: str):
self.data = {}
# buffer which keeps last unfinished line from previous chunk
self.chunk_buffer = b''
self.position = 0

@property
def cache_name(self):
Expand Down Expand Up @@ -526,9 +527,17 @@ def quick_parse(self, file_descriptor):
position += len(line)
self.process_line(line)

def load_from_chunk(self, data: bytes):
def load_from_chunk(self, data: bytes, size: int):
"""Process given chunk array of data."""
data = self.chunk_buffer + data
close_to_start = self.position < self.METADATA_START_OFFSET
close_to_end = self.position > size - self.METADATA_END_OFFSET
self.position += len(data)
if not close_to_start and not close_to_end:
return
log.info(f"debdeb line splitted {size, self.position} "
f"{self.METADATA_START_OFFSET, close_to_start, self.position}"
f"{self.METADATA_END_OFFSET, close_to_end}")
lines = data.split(b"\n")
# last line was cut in middle, save it to buffer to process it
# with next chunk of data
Expand Down
5 changes: 4 additions & 1 deletion tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,14 @@ def chunk_read_file(self, meta_class, filepath):
on the blocks of data from given meta_class."""
chunk_size = 10 * 1024 # 10 KiB test on small chunks
with open(filepath, 'rb') as file:
# find 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)
meta_class.load_from_chunk(chunk, file_size)

def test_full(self):
"""Both the file and filename contain metadata. There are thumbnails.
Expand Down

0 comments on commit 151f323

Please sign in to comment.