Skip to content
Snippets Groups Projects
Verified Commit e319e314 authored by kjk's avatar kjk
Browse files

hjd

parent db2e8f31
No related branches found
No related tags found
No related merge requests found
import os import os
import psycopg2 import psycopg2
import logging import logging
import concurrent.futures
import UltraStarSongFileParser import UltraStarSongFileParser
from UltraStarSongFile import UltraStarSongFile from UltraStarSongFile import UltraStarSongFile
...@@ -9,26 +11,29 @@ def add_directory(path, connection_string): ...@@ -9,26 +11,29 @@ def add_directory(path, connection_string):
""" """
Parse text files in each subdirectory of the given PATH and add the parsed data to the PostgreSQL database using CONNECTION_STRING. Parse text files in each subdirectory of the given PATH and add the parsed data to the PostgreSQL database using CONNECTION_STRING.
""" """
parser = UltraStarSongFileParser.UltraStarSongFileParser(strict_mode=False) with concurrent.futures.ThreadPoolExecutor() as executor:
futures = []
for subdir in os.listdir(path):
subdir_path = os.path.join(path, subdir)
# Iterate through each subdirectory in the specified path if os.path.isdir(subdir_path):
for subdir in os.listdir(path): text_files = [f for f in os.listdir(subdir_path) if f.endswith(".txt")]
subdir_path = os.path.join(path, subdir)
if os.path.isdir(subdir_path): if len(text_files) == 1:
# Find the text file within each subdirectory file_path = os.path.join(subdir_path, text_files[0])
text_files = [f for f in os.listdir(subdir_path) if f.endswith(".txt")] futures.append(executor.submit(process_directory, file_path, connection_string))
if len(text_files) == 1: # Wait for all tasks to complete
file_path = os.path.join(subdir_path, text_files[0]) concurrent.futures.wait(futures)
logging.debug(f"parsing: {file_path}")
# Parse the text file
parsed_data = parser.parse_file(file_path)
# Add the parsed data to the PostgreSQL database def process_directory(file_path, connection_string):
add_to_database(connection_string, parsed_data) parser = UltraStarSongFileParser(strict_mode=False)
logging.debug(f"parsing: {file_path}")
parsed_data = parser.parse_file(file_path)
add_to_database(connection_string, parsed_data)
def add_to_database(connection_string, song_instance: UltraStarSongFile): def add_to_database(connection_string, song_instance: UltraStarSongFile):
""" """
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment