Skip to content

Commit

Permalink
truncate album correctly if tracknumber has >2 digits
Browse files Browse the repository at this point in the history
  • Loading branch information
hejops committed Mar 23, 2024
1 parent cfd8669 commit af92835
Showing 1 changed file with 22 additions and 45 deletions.
67 changes: 22 additions & 45 deletions dita/file/mover.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,48 +129,27 @@ def validate(self):
if self.targets.empty:
print("emptied")
return
# raise ValueError

# 1. determine full abspath
self.targets = self.targets.join(
self.targets.apply(self.get_dest_filename, axis=1)
)
self.targets["dest"] = self.targets.apply(self.get_dest_filename, axis=1)

# print(self.targets)
# raise ValueError
if not (hundred := self.targets[self.targets.tracknumber.str.len() > 2]).empty:
# if dir has >99 files, all files must have tracknumber of len 3
# tags don't need to be updated
hundred = self.targets.album.isin(hundred.album)
self.targets.loc[hundred, "tracknumber"] = self.targets[
hundred
].tracknumber.apply(lambda x: str(int(x)).zfill(3))
self.targets["dest"] = self.targets.apply(self.get_dest_filename, axis=1)

# 2. truncate fnames (before ext)
# TODO: apply() to affected rows only
too_long = self.targets[self.targets.dest.str.len() > 255]
# longest = self.targets.sort_values("dest", key=lambda x: x.str.len()).iloc[-1]
# trunc = truncate_filename(longest)
# print(trunc)
# raise ValueError
if not too_long.empty:
# if len(sorted(self.targets.dest.to_list(), key=len)[-1]) > 255:
# print(too_long)
# raise Exception

# self.targets.dest = self.targets.apply(
# truncate_filename,
# axis=1,
# )

self.targets.dest = self.targets.dest.apply(truncate_filename)

# if dir has >99 files, all files must have tracknumber of len 3
# TODO: tags are not updated (not strictly necessary)

self.targets.tracknumber = self.targets.tracknumber.apply(front_int)

hundred = self.targets.album.isin(
self.targets[self.targets.tracknumber.str.len() > 2].album
)
if not hundred.empty:
self.targets.loc[hundred, "tracknumber"] = self.targets[
hundred
].tracknumber.apply(lambda x: str(int(x)).zfill(3))

# 3. src_to_dest.compilation == 1 -> prepare make_va_symlinks
# don't actually make them yet
if "compilation" in self.targets:
Expand Down Expand Up @@ -282,7 +261,7 @@ def move(
# set_tag(_tags, "date", date)

@staticmethod
def get_dest_filename(row: pd.Series) -> pd.Series:
def get_dest_filename(row: pd.Series) -> str:
"""Construct destination filename from metadata in a row, then
add/update 'dest' field to/in the row. Format is fixed to:
Expand Down Expand Up @@ -314,15 +293,13 @@ def sanitize_filename(parts: list[str]) -> list[str]:
]
assert all(dest)

# print(self.targets.dest.iloc[0])
# raise ValueError

row = pd.Series(
os.path.join(TARGET_DIR, *sanitize_filename(dest)),
index=["dest"],
)
return os.path.join(TARGET_DIR, *sanitize_filename(dest))

return row
# row = pd.Series(
# os.path.join(TARGET_DIR, *sanitize_filename(dest)),
# index=["dest"],
# )
# return row

def queue_new_albums(self) -> None:
"""Add new relpaths to library and queue files"""
Expand Down Expand Up @@ -420,9 +397,9 @@ def regen_tag_columns(self) -> None:
# print(self.targets.columns)
right = self.targets.tags.apply(pd.Series)
self.targets = self.targets[self.targets.columns.difference(right.columns)]
self.targets = self.targets.merge(
right, left_index=True, right_index=True
).applymap(dita.tag.fix.tags_to_columns)
self.targets = self.targets.merge(right, left_index=True, right_index=True).map(
dita.tag.fix.tags_to_columns
)

def dry_run(self):
"""Perform a dry-run of the move, then show the resulting relpaths.
Expand Down Expand Up @@ -647,7 +624,7 @@ def generate_symlinks(

def truncate_filename(
# row: pd.Series,
src_filename: str,
dest_filename: str,
max_artist_len: int = 160, # https://www.discogs.com/master/2152342
maxlen: int = 255,
) -> str:
Expand All @@ -657,12 +634,12 @@ def truncate_filename(
unit testing.
"""

dest_filename = src_filename # .dest
# dest_filename = src_filename # .dest
excess = len(dest_filename) - maxlen

# print(excess)

assert "." in dest_filename, src_filename
assert "." in dest_filename
fullpath, ext = dest_filename.rsplit(".", maxsplit=1)

root, artist, album, fname = fullpath.rsplit("/", maxsplit=3)
Expand Down

0 comments on commit af92835

Please sign in to comment.