Spaces:
Runtime error
Runtime error
File size: 1,974 Bytes
013c2d8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
"""Module containing common function.
"""
import os
import traceback
from pathlib import Path
import config as cfg
def collect_audio_files(path: str):
"""Collects all audio files in the given directory.
Args:
path: The directory to be searched.
Returns:
A sorted list of all audio files in the directory.
"""
# Get all files in directory with os.walk
files = []
for root, _, flist in os.walk(path):
for f in flist:
if not f.startswith(".") and f.rsplit(".", 1)[-1].lower() in cfg.ALLOWED_FILETYPES:
files.append(os.path.join(root, f))
return sorted(files)
def readLines(path: str):
"""Reads the lines into a list.
Opens the file and reads its contents into a list.
It is expected to have one line for each species or label.
Args:
path: Absolute path to the species file.
Returns:
A list of all species inside the file.
"""
return Path(path).read_text(encoding="utf-8").splitlines() if path else []
def list_subdirectories(path: str):
"""Lists all directories inside a path.
Retrieves all the subdirectories in a given path without recursion.
Args:
path: Directory to be searched.
Returns:
A filter sequence containing the absolute paths to all directories.
"""
return filter(lambda el: os.path.isdir(os.path.join(path, el)), os.listdir(path))
def clearErrorLog():
"""Clears the error log file.
For debugging purposes.
"""
if os.path.isfile(cfg.ERROR_LOG_FILE):
os.remove(cfg.ERROR_LOG_FILE)
def writeErrorLog(ex: Exception):
"""Writes an exception to the error log.
Formats the stacktrace and writes it in the error log file configured in the config.
Args:
ex: An exception that occurred.
"""
with open(cfg.ERROR_LOG_FILE, "a") as elog:
elog.write("".join(traceback.TracebackException.from_exception(ex).format()) + "\n")
|