Skip to content

Commit 6db9a71

Browse files
make --recordings accept paths to recordings even if they do not exist locally
1 parent 506b278 commit 6db9a71

File tree

3 files changed

+52
-15
lines changed

3 files changed

+52
-15
lines changed

ChildProject/pipelines/processors.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ def process_recording(self, recording):
7878
pass
7979

8080
def process(self, parameters):
81-
recordings = self.project.get_recordings_from_list(self.recordings)
81+
recordings = self.project.get_recordings_from_list(
82+
self.recordings, self.input_profile
83+
)
8284

8385
os.makedirs(name=self.output_directory(), exist_ok=True)
8486

@@ -94,10 +96,9 @@ def process(self, parameters):
9496

9597
if not len(self.converted):
9698
return
97-
99+
98100
self.converted.set_index(
99-
["original_filename", "converted_filename"],
100-
inplace = True
101+
["original_filename", "converted_filename"], inplace=True
101102
)
102103
self.converted = self.converted.assign(parameters=parameters)
103104

@@ -207,7 +208,7 @@ def process_recording(self, recording):
207208
success = proc.returncode == 0
208209

209210
if not success:
210-
print(stderr, file = sys.stderr)
211+
print(stderr, file=sys.stderr)
211212

212213
return pd.DataFrame(
213214
[

ChildProject/projects.py

+36-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import datetime
2+
from functools import partial
23
import glob
34
import numpy as np
45
import os
@@ -7,7 +8,7 @@
78
import subprocess
89

910
from .tables import IndexTable, IndexColumn, is_boolean
10-
from .utils import get_audio_duration
11+
from .utils import get_audio_duration, path_is_parent
1112

1213

1314
class ChildProject:
@@ -384,7 +385,10 @@ def validate(self, ignore_files: bool = False) -> tuple:
384385
self.warnings.append(message)
385386

386387
# child id refers to an existing child in the children table
387-
if str(row["child_id"]) not in self.children["child_id"].astype(str).tolist():
388+
if (
389+
str(row["child_id"])
390+
not in self.children["child_id"].astype(str).tolist()
391+
):
388392
self.errors.append(
389393
"child_id '{}' in recordings on line {} cannot be found in the children table.".format(
390394
row["child_id"], index
@@ -483,19 +487,41 @@ def get_converted_recording_filename(
483487
self.converted_recordings_hashtable[key] = None
484488
return None
485489

486-
def get_recordings_from_list(self, recordings: list):
490+
def recording_from_path(self, path: str, profile: str = None) -> str:
491+
if profile:
492+
media_path = os.path.join(self.path, self.CONVERTED_RECORDINGS, profile)
493+
else:
494+
media_path = os.path.join(self.path, self.RAW_RECORDINGS)
495+
496+
if not path_is_parent(media_path, path):
497+
return None
498+
499+
recording = os.path.relpath(
500+
path, media_path
501+
)
502+
503+
return recording
504+
505+
506+
def get_recordings_from_list(self, recordings: list, profile: str = None) -> pd.DataFrame:
507+
"""Recover recordings metadata from a list of recordings or path to recordings.
508+
509+
:param recordings: list of recording names or paths
510+
:type recordings: list
511+
:return: matching recordings
512+
:rtype: pd.DataFrame
513+
"""
487514
_recordings = self.recordings.copy()
488515

489516
if recordings is not None:
490517
# if the user provided paths,
491518
# transform those paths into recording_filename values
492-
if all(map(os.path.exists, recordings)):
493-
recordings = [
494-
os.path.relpath(
495-
recording, os.path.join(self.path, self.RAW_RECORDINGS)
496-
)
497-
for recording in recordings
498-
]
519+
recordings_from_paths = [
520+
self.recording_from_path(recording, profile) for recording in recordings
521+
]
522+
523+
if None not in recordings_from_paths:
524+
recordings = recordings_from_paths
499525

500526
_recordings = _recordings[
501527
_recordings["recording_filename"].isin(recordings)

ChildProject/utils.py

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import os
22

3+
def path_is_parent(parent_path: str, child_path: str):
4+
# Smooth out relative path names, note: if you are concerned about symbolic links, you should use os.path.realpath too
5+
parent_path = os.path.abspath(parent_path)
6+
child_path = os.path.abspath(child_path)
7+
8+
# Compare the common path of the parent and child path with the common path of just the parent path. Using the commonpath method on just the parent path will regularise the path name in the same way as the comparison that deals with both paths, removing any trailing path separator
9+
return os.path.commonpath([parent_path]) == os.path.commonpath(
10+
[parent_path, child_path]
11+
)
12+
313

414
class Segment:
515
def __init__(self, start, stop):

0 commit comments

Comments
 (0)