From 208af67a50589ea54a5cc2798b3a1105a852492b Mon Sep 17 00:00:00 2001 From: SpudGunMan Date: Mon, 7 Oct 2024 01:43:19 -0700 Subject: [PATCH] refactoringCHUNKER @Nestpebble FYI --- modules/system.py | 73 ++++++++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/modules/system.py b/modules/system.py index 8486227..b8e6297 100644 --- a/modules/system.py +++ b/modules/system.py @@ -514,44 +514,57 @@ def get_closest_nodes(nodeInt=1,returnCount=3): else: logger.warning(f"System: No nodes found in closest_nodes on interface {nodeInt}") return ERROR_FETCHING_DATA - + def messageChunker(message): - parts = message.split('\n') - current_chunk = '' message_list = [] - if len(message) > MESSAGE_CHUNK_SIZE: + parts = message.split('\n') for part in parts: part = part.strip() + # remove empty parts if not part: continue - sentences = part.split('. ') - for sentence in sentences: - sentence = sentence.strip() - sentence = sentence.replace(' ', ' ') - if not sentence: - continue - while len(sentence) > MESSAGE_CHUNK_SIZE: - # Split the sentence if it's too long - message_list.append(sentence[:MESSAGE_CHUNK_SIZE]) - sentence = sentence[MESSAGE_CHUNK_SIZE:] - if len(current_chunk) + len(sentence) <= MESSAGE_CHUNK_SIZE: - current_chunk += sentence - else: - if current_chunk: - message_list.append(current_chunk.strip()) - current_chunk = sentence - if current_chunk: - # Add the last chunk - message_list.append(current_chunk.strip()) - - # Calculate the total length of the message - total_length = sum(len(chunk) for chunk in message_list) - num_chunks = len(message_list) - logger.debug(f"System: Splitting #chunks: {num_chunks}, Total length: {total_length}") - return message_list + # if part is under the MESSAGE_CHUNK_SIZE, add it to the list + if len(part) < MESSAGE_CHUNK_SIZE: + message_list.append(part) + else: + # split the part into chunks + current_chunk = '' + sentences = part.split('. ') + for sentence in sentences: + sentence = sentence.strip() + sentence = sentence.replace(' ', ' ') + # remove empty sentences + if not sentence: + continue - return message + # if sentence is too long, split it by words + if len(current_chunk) + len(sentence) > MESSAGE_CHUNK_SIZE: + message_list.append(current_chunk) + current_chunk = sentence + else: + if current_chunk: + current_chunk += ' ' + current_chunk += sentence + if current_chunk: + message_list.append(current_chunk) + + # Ensure no chunk exceeds MESSAGE_CHUNK_SIZE + final_message_list = [] + for chunk in message_list: + while len(chunk) > MESSAGE_CHUNK_SIZE: + final_message_list.append(chunk[:MESSAGE_CHUNK_SIZE]) + chunk = chunk[MESSAGE_CHUNK_SIZE:] + if chunk: + final_message_list.append(chunk) + + # Calculate the total length of the message + total_length = sum(len(chunk) for chunk in final_message_list) + num_chunks = len(final_message_list) + logger.debug(f"System: Splitting #chunks: {num_chunks}, Total length: {total_length}") + return final_message_list + + return [message] def send_message(message, ch, nodeid=0, nodeInt=1): if message == "" or message == None or len(message) == 0: