From c77bc9a7c6ce6ef3bc64eb3a1413de568923514c Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Sat, 1 Jun 2024 20:47:56 +0200 Subject: [PATCH] Update addition_processed.py --- .../rootfs/helpers/addition_processed.py | 82 ++++++------------- 1 file changed, 23 insertions(+), 59 deletions(-) diff --git a/birdnet-pi/rootfs/helpers/addition_processed.py b/birdnet-pi/rootfs/helpers/addition_processed.py index d06efad9c..4bbc08a06 100644 --- a/birdnet-pi/rootfs/helpers/addition_processed.py +++ b/birdnet-pi/rootfs/helpers/addition_processed.py @@ -1,66 +1,30 @@ # Code to modify the birdnet_analysis.py and restore the Processed folder -# The last 15 wav files are stored in a dynamic manner in the Processed folder +# The last 30 wav files are stored in a dynamic manner in the Processed folder -def read_file(file_path): - with open(file_path, 'r') as file: - return file.readlines() +import re -def write_file(file_path, content): - with open(file_path, 'w') as file: - file.writelines(content) +def modify_code(original_file, modified_file): + with open(original_file, 'r') as f: + lines = f.readlines() -def process_code(lines): - processed_lines = [] + # Define the modifications + modifications = { + 'os.remove(file.file_name)': 'move_to_processed(file.file_name)', + 'def handle_reporting_queue(queue):': 'def handle_reporting_queue(queue):\n conf = get_settings()\n processed_dir = os.path.join(conf[\'RECS_DIR\'], \'Processed\')\n os.makedirs(processed_dir, exist_ok=True)\n user_id = pwd.getpwnam(os.getenv(\'USER\')).pw_uid\n os.chown(processed_dir, user_id, user_id)', + 'import threading\nfrom queue import Queue\nfrom subprocess import CalledProcessError': 'import threading\nfrom queue import Queue\nfrom subprocess import CalledProcessError\nimport glob\nimport time\nimport pwd', + 'def move_to_processed(file_name):': 'def move_to_processed(file_name):\n conf = get_settings()\n processed_dir = os.path.join(conf[\'RECS_DIR\'], \'Processed\')\n os.rename(file_name, os.path.join(processed_dir, os.path.basename(file_name)))\n files = glob.glob(os.path.join(processed_dir, \'*\'))\n files.sort(key=os.path.getmtime)\n buffer_size = int(os.getenv(\'Processed_Buffer\', 30))\n while len(files) > buffer_size:\n os.remove(files.pop(0))' + } + + # Apply the modifications + modified_lines = [] for line in lines: - # Add the import statement for shutil if it's not already there - if 'import os.path' in line and 'import shutil' not in lines: - processed_lines.append('import shutil\n') - processed_lines.append(line) + for original, modified in modifications.items(): + line = line.replace(original, modified) + modified_lines.append(line) - # Modify the process_file function to include the conf parameter - if 'def process_file(file_name, report_queue):' in line: - processed_lines[-1] = 'def process_file(file_name, report_queue, conf):\n' + # Write the modified code to the new file + with open(modified_file, 'w') as f: + f.writelines(modified_lines) - # Modify the handle_reporting_queue function to include the conf parameter and the new logic - if 'def handle_reporting_queue(queue):' in line: - processed_lines[-1] = 'def handle_reporting_queue(queue, conf):\n' - if 'os.remove(file.file_name)' in line: - indent = ' ' * (len(line) - len(line.lstrip())) - processed_lines.append(f"{indent}# Move the file to the 'Processed' folder\n") - processed_lines.append(f"{indent}processed_dir = os.path.join(conf['RECS_DIR'], 'Processed')\n") - processed_lines.append(f"{indent}if not os.path.exists(processed_dir):\n") - processed_lines.append(f"{indent} os.makedirs(processed_dir)\n") - processed_lines.append(f"{indent}shutil.move(file.file_name, processed_dir)\n") - processed_lines.append(f"{indent}\n") - processed_lines.append(f"{indent}# Maintain the file count in the 'Processed' folder\n") - processed_lines.append(f"{indent}maintain_file_count(processed_dir)\n") - processed_lines.append(f"{indent}\n") - continue # Skip the original line that removes the file - - # Add the new maintain_file_count function at the end of the file - if 'if __name__' in line and not any('def maintain_file_count' in l for l in processed_lines): - processed_lines.append('\n') - processed_lines.append('def maintain_file_count(directory, max_files=15):\n') - processed_lines.append(' files = [os.path.join(directory, f) for f in os.listdir(directory) if f.endswith(\'.wav\')]\n') - processed_lines.append(' files.sort(key=lambda x: os.path.getmtime(x))\n') - processed_lines.append('\n') - processed_lines.append(' while len(files) > max_files:\n') - processed_lines.append(' os.remove(files.pop(0))\n') - processed_lines.append('\n') - - return processed_lines - -# Paths to the original and new code files -original_code_path = '/home/pi/BirdNET-Pi/scripts/birdnet_analysis.py' -new_code_path = '/home/pi/BirdNET-Pi/scripts/birdnet_analysis.py' - -# Read the original code -original_lines = read_file(original_code_path) - -# Process the code -modified_lines = process_code(original_lines) - -# Write the modified code to a new file -write_file(new_code_path, modified_lines) - -print(f"The code has been modified and saved to {new_code_path}") +# Use the function +modify_code('original.py', 'modified.py')