Skip to content

Commit

Permalink
Refactor rename
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef-Friedrich committed Jan 16, 2024
1 parent f4d9146 commit 75f42f1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
4 changes: 2 additions & 2 deletions mscxyz/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import mscxyz.export
from mscxyz import utils
from mscxyz.meta import Combined, Interface, InterfaceReadWrite, Metatag, Vbox
from mscxyz.rename import rename_filename
from mscxyz.rename import rename
from mscxyz.score import Score
from mscxyz.settings import parse_args
from mscxyz.utils import Dimension
Expand Down Expand Up @@ -825,7 +825,7 @@ def list_styles(version: int) -> None:
# score.save(mscore=args.general_mscore)

if args.rename_rename:
rename_filename(str(score.path))
rename(score)

if args.export_extension:
score.export.to_extension(args.export_extension)
Expand Down
4 changes: 2 additions & 2 deletions mscxyz/cli_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import mscxyz.settings as settings
from mscxyz import Score, cli_legacy, utils
from mscxyz.meta import Interface, InterfaceReadWrite
from mscxyz.rename import rename_filename
from mscxyz.rename import rename


def list_fields(
Expand Down Expand Up @@ -661,7 +661,7 @@ def execute(cli_args: typing.Sequence[str] | None = None) -> None:
score.save(mscore=args.general_mscore)

elif args.subcommand == "rename":
score = rename_filename(file)
score = rename(Score(file))

elif args.subcommand == "export":
score = Score(file)
Expand Down
11 changes: 5 additions & 6 deletions mscxyz/rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ def get_checksum(filename: str) -> str:
return hasher.hexdigest()


def rename_filename(source: str) -> Score:
def rename(score: Score) -> Score:
args = get_args()

score = Score(source)
meta_values: dict[str, str] = score.meta.interface.export_to_dict()
target_filename: str = apply_format_string(meta_values)

Expand All @@ -90,12 +89,12 @@ def rename_filename(source: str) -> Score:
counter_format: str = ""
while os.path.exists(target_format.format(counter_format)):
target = target_format.format(counter_format)
if get_checksum(source) == get_checksum(target):
if get_checksum(str(score.path)) == get_checksum(target):
print(
color(
"The file “{}” with the same checksum (sha1) "
"already exists in the target path “{}”!".format(
source, target
str(score.path), target
),
"red",
)
Expand All @@ -109,12 +108,12 @@ def rename_filename(source: str) -> Score:

target = target_format.format(counter_format)

show(source, target)
show(str(score.path), target)

if not args.general_dry_run:
create_dir(target)
# Invalid cross-device link:
# os.rename(source, target)
shutil.move(source, target)
shutil.move(score.path, target)

return score

0 comments on commit 75f42f1

Please sign in to comment.