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