Skip to content

Commit

Permalink
refactor: makes changes to comply with ruff linter recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydrennan committed Jan 24, 2024
1 parent 504a6d8 commit f1079ac
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
9 changes: 5 additions & 4 deletions imaginairy/cli/upscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import logging
import os.path
from datetime import datetime
from datetime import datetime, timezone

import click

Expand Down Expand Up @@ -38,6 +38,7 @@
)
@click.option(
"--format",
"format_template",
default="{file_sequence_number:06}_{algorithm}_{original_filename}_UPSCALED{file_extension}",
type=str,
help="Formats the file name. {original_filename}: original name without the extension; "
Expand All @@ -51,7 +52,7 @@ def upscale_cmd(
fix_faces,
fix_faces_fidelity,
upscale_model,
format,
format_template,
):
"""
Upscale an image 4x using AI.
Expand Down Expand Up @@ -85,7 +86,7 @@ def upscale_cmd(
file_base_name, extension = os.path.splitext(os.path.basename(p))
base_count = len(os.listdir(outdir))

now = datetime.now()
now = datetime.now(timezone.utc)

if model.startswith(("https://", "http://")):
model_name = get_url_file_name(model)
Expand All @@ -99,7 +100,7 @@ def upscale_cmd(
"now": now,
"file_extension": extension,
}
new_file_name = format_filename(format, new_file_name_data)
new_file_name = format_filename(format_template, new_file_name_data)
new_file_path = os.path.join(outdir, new_file_name)
img.save(new_file_path)
print(f"Saved to {new_file_path}")
6 changes: 3 additions & 3 deletions imaginairy/utils/format_file_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
from urllib.parse import urlparse


def format_filename(format: str, data: dict) -> str:
def format_filename(format_template: str, data: dict) -> str:
"""
Formats the filename based on the provided template and variables.
"""
if not isinstance(format, str):
if not isinstance(format_template, str):
raise TypeError("format argument must be a string")

filename = format.format(**data)
filename = format_template.format(**data)
return filename


Expand Down
27 changes: 12 additions & 15 deletions tests/test_utils/test_format_file_name.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import datetime
from datetime import datetime, timezone

import pytest


def format_filename(format: str, data: dict) -> str:
def format_filename(format_template: str, data: dict) -> str:
"""
Formats the filename based on the provided template and variables.
"""
if not isinstance(format, str):
if not isinstance(format_template, str):
raise TypeError("format argument must be a string")

filename = format.format(**data)
filename = format_template.format(**data)
filename += data["ext"]
return filename

Expand All @@ -19,18 +19,17 @@ def format_filename(format: str, data: dict) -> str:
"original": "file",
"number": 1,
"algorithm": "alg",
"date": datetime.datetime(2023, 1, 23, 12, 30, 45),
"time": datetime.time(12, 30, 45),
"now": datetime(2023, 1, 23, 12, 30, 45, tzinfo=timezone.utc),
"ext": ".jpg",
}


# Tests
@pytest.mark.parametrize(
"format_str, data, expected",
("format_str", "data", "expected"),
[
("{original}_{algorithm}", base_data, "file_alg.jpg"),
("{original}_{number}_{date}", base_data, "file_1_2023-01-23 12:30:45.jpg"),
("{original}_{number}_{now}", base_data, "file_1_2023-01-23 12:30:45+00:00.jpg"),
("", base_data, ".jpg"),
("{original}", {}, KeyError),
("{nonexistent_key}", base_data, KeyError),
Expand All @@ -43,9 +42,9 @@ def format_filename(format: str, data: dict) -> str:
"file_123.jpg",
),
(
"{date}_{time}",
{"date": "2023/01/23", "time": "12-00", "ext": ".log"},
"2023/01/23_12-00.log",
"{now}",
{"now": "2023/01/23", "ext": ".log"},
"2023/01/23.log",
),
("{original}", {"original": "file", "ext": ""}, "file"),
],
Expand All @@ -56,9 +55,7 @@ def test_format_filename(format_str, data, expected):
format_filename(format_str, data)
except expected:
assert True, f"Expected {expected} to be raised"
except Exception as e:
raise e
else:
assert False, f"Expected {expected} to be raised"
except Exception:
raise
else:
assert format_filename(format_str, data) == expected

0 comments on commit f1079ac

Please sign in to comment.