Skip to content

Commit

Permalink
Alignments: Remove support for legacy .json files
Browse files Browse the repository at this point in the history
  • Loading branch information
torzdf committed Apr 25, 2024
1 parent 0c68712 commit f266121
Showing 1 changed file with 2 additions and 62 deletions.
64 changes: 2 additions & 62 deletions lib/align/alignments.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import numpy as np

from lib.serializer import get_serializer, get_serializer_from_filename
from lib.serializer import get_serializer
from lib.utils import FaceswapError

from .thumbnails import Thumbnails
Expand Down Expand Up @@ -644,61 +644,9 @@ def have_alignments_file(self) -> bool:
logger.trace(retval) # type:ignore[attr-defined]
return retval

def _update_file_format(self, folder: str, filename: str) -> str:
""" Convert old style serialized alignments to new ``.fsa`` format.
Parameters
----------
folder: str
The folder that the legacy alignments exist in
filename: str
The file name of the legacy alignments
Returns
-------
str
The full path to the newly created ``.fsa`` alignments file
"""
logger.info("Reformatting legacy alignments file...")
old_location = os.path.join(str(folder), filename)
new_location = f"{os.path.splitext(old_location)[0]}.{self._serializer.file_extension}"
if os.path.exists(old_location):
if os.path.exists(new_location):
logger.info("Using existing updated alignments file found at '%s'. If you do not "
"wish to use this existing file then you should delete or rename it.",
new_location)
else:
logger.info("Old location: '%s', New location: '%s'", old_location, new_location)
load_serializer = get_serializer_from_filename(old_location)
data = load_serializer.load(old_location)
self._serializer.save(new_location, data)
return os.path.basename(new_location)

def _test_for_legacy(self, location: str) -> None:
""" For alignments filenames passed in without an extension, test for legacy
serialization formats and update to current ``.fsa`` format if any are found.
Parameters
----------
location: str
The folder location to check for legacy alignments
"""
logger.debug("Checking for legacy alignments file formats: '%s'", location)
filename = os.path.splitext(location)[0]
for ext in (".json", ".p", ".pickle", ".yaml"):
legacy_filename = f"{filename}{ext}"
if os.path.exists(legacy_filename):
logger.debug("Legacy alignments file exists: '%s'", legacy_filename)
_ = self._update_file_format(*os.path.split(legacy_filename))
break
logger.debug("Legacy alignments file does not exist: '%s'", legacy_filename)

def _get_location(self, folder: str, filename: str) -> str:
""" Obtains the location of an alignments file.
If a legacy alignments file is provided/discovered, then the alignments file will be
updated to the custom ``.fsa`` format and saved.
Parameters
----------
folder: str
Expand All @@ -713,22 +661,14 @@ def _get_location(self, folder: str, filename: str) -> str:
"""
logger.debug("Getting location: (folder: '%s', filename: '%s')", folder, filename)
noext_name, extension = os.path.splitext(filename)
if extension in (".json", ".p", ".pickle", ".yaml", ".yml"):
# Reformat legacy alignments file
filename = self._update_file_format(folder, filename)
logger.debug("Updated legacy alignments. New filename: '%s'", filename)
if extension[1:] == self._serializer.file_extension:
logger.debug("Valid Alignments filename provided: '%s'", filename)
else:
filename = f"{noext_name}.{self._serializer.file_extension}"
logger.debug("File extension set from serializer: '%s'",
self._serializer.file_extension)
location = os.path.join(str(folder), filename)
if not os.path.exists(location):
# Test for old format alignments files and reformat if they exist. This will be
# executed if an alignments file has not been explicitly provided therefore it will not
# have been picked up in the extension test
self._test_for_legacy(location)

logger.verbose("Alignments filepath: '%s'", location) # type:ignore[attr-defined]
return location

Expand Down

0 comments on commit f266121

Please sign in to comment.