Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
removed moved function for template formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
iLLiCiTiT committed Mar 18, 2022
1 parent 64491fb commit 03fcacc
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 151 deletions.
6 changes: 0 additions & 6 deletions avalon/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@
deregister_plugin_path,

HOST_WORKFILE_EXTENSIONS,
format_template_with_optional_keys,
last_workfile_with_version,
last_workfile
)


Expand Down Expand Up @@ -97,7 +94,4 @@
"deregister_plugin_path",

"HOST_WORKFILE_EXTENSIONS",
"format_template_with_optional_keys",
"last_workfile_with_version",
"last_workfile",
]
145 changes: 0 additions & 145 deletions avalon/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,31 +740,6 @@ def ls():
return host


def format_template_with_optional_keys(data, template):
# Remove optional missing keys
pattern = re.compile(r"(<.*?[^{0]*>)[^0-9]*?")
invalid_optionals = []
for group in pattern.findall(template):
try:
group.format(**data)
except KeyError:
invalid_optionals.append(group)

for group in invalid_optionals:
template = template.replace(group, "")

work_file = template.format(**data)

# Remove optional symbols
work_file = work_file.replace("<", "")
work_file = work_file.replace(">", "")

# Remove double dots when dot for extension is in template
work_file = work_file.replace("..", ".")

return work_file


def get_thumbnail_binary(thumbnail_entity, thumbnail_type, dbcon=None):
if not thumbnail_entity:
return
Expand Down Expand Up @@ -796,123 +771,3 @@ def get_thumbnail_binary(thumbnail_entity, thumbnail_type, dbcon=None):
Resolver.__class__.__name__
))
traceback.print_exception(*sys.exc_info())


def last_workfile_with_version(workdir, file_template, fill_data, extensions):
"""Return last workfile version.
Args:
workdir(str): Path to dir where workfiles are stored.
file_template(str): Template of file name.
fill_data(dict): Data for filling template.
extensions(list, tuple): All allowed file extensions of workfile.
Returns:
tuple: Last workfile<str> with version<int> if there is any otherwise
returns (None, None).
"""
if not os.path.exists(workdir):
return None, None

# Fast match on extension
filenames = [
filename
for filename in os.listdir(workdir)
if os.path.splitext(filename)[1] in extensions
]

# Build template without optionals, version to digits only regex
# and comment to any definable value.
_ext = []
for ext in extensions:
if not ext.startswith("."):
ext = "." + ext
# Escape dot for regex
ext = "\\" + ext
_ext.append(ext)
ext_expression = "(?:" + "|".join(_ext) + ")"

# Replace `.{ext}` with `{ext}` so we are sure there is not dot at the end
file_template = re.sub(r"\.?{ext}", ext_expression, file_template)
# Replace optional keys with optional content regex
file_template = re.sub(r"<.*?>", r".*?", file_template)
# Replace `{version}` with group regex
file_template = re.sub(r"{version.*?}", r"([0-9]+)", file_template)
file_template = re.sub(r"{comment.*?}", r".+?", file_template)
file_template = format_template_with_optional_keys(
fill_data,
file_template
)

# Match with ignore case on Windows due to the Windows
# OS not being case-sensitive. This avoids later running
# into the error that the file did exist if it existed
# with a different upper/lower-case.
kwargs = {}
if platform.system().lower() == "windows":
kwargs["flags"] = re.IGNORECASE

# Get highest version among existing matching files
version = None
output_filenames = []
for filename in sorted(filenames):
match = re.match(file_template, filename, **kwargs)
if not match:
continue

file_version = int(match.group(1))
if version is None or file_version > version:
output_filenames[:] = []
version = file_version

if file_version == version:
output_filenames.append(filename)

output_filename = None
if output_filenames:
if len(output_filenames) == 1:
output_filename = output_filenames[0]
else:
last_time = None
for _output_filename in output_filenames:
full_path = os.path.join(workdir, _output_filename)
mod_time = os.path.getmtime(full_path)
if last_time is None or last_time < mod_time:
output_filename = _output_filename
last_time = mod_time

return output_filename, version


def last_workfile(
workdir, file_template, fill_data, extensions, full_path=False
):
"""Return last workfile filename.
Returns file with version 1 if there is not workfile yet.
Args:
workdir(str): Path to dir where workfiles are stored.
file_template(str): Template of file name.
fill_data(dict): Data for filling template.
extensions(list, tuple): All allowed file extensions of workfile.
full_path(bool): Full path to file is returned if set to True.
Returns:
str: Last or first workfile as filename of full path to filename.
"""
filename, version = last_workfile_with_version(
workdir, file_template, fill_data, extensions
)
if filename is None:
data = copy.deepcopy(fill_data)
data["version"] = 1
data.pop("comment", None)
if not data.get("ext"):
data["ext"] = extensions[0]
filename = format_template_with_optional_keys(data, file_template)

if full_path:
return os.path.normpath(os.path.join(workdir, filename))

return filename

0 comments on commit 03fcacc

Please sign in to comment.