Skip to content

Commit

Permalink
Fix some new os.path instances in qt files
Browse files Browse the repository at this point in the history
  • Loading branch information
Icosahunter committed May 23, 2024
1 parent b1cd39e commit a53b515
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 64 deletions.
12 changes: 11 additions & 1 deletion tagstudio/src/core/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,17 @@
".ogg",
".aiff",
]
DOC_TYPES: list[str] = [".txt", ".rtf", ".md", ".doc", ".docx", ".pdf", ".tex", ".odt", ".pages"]
DOC_TYPES: list[str] = [
".txt",
".rtf",
".md",
".doc",
".docx",
".pdf",
".tex",
".odt",
".pages",
]
PLAINTEXT_TYPES: list[str] = [
".txt",
".md",
Expand Down
19 changes: 5 additions & 14 deletions tagstudio/src/core/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ def create_library(self, path) -> int:
0: Library Successfully Created\n
2: File creation error
"""

path = self._fix_lib_path(path)

try:
Expand All @@ -454,12 +454,8 @@ def verify_ts_folders(self) -> None:
"""Verifies/creates folders required by TagStudio."""

full_ts_path = self.library_dir / TS_FOLDER_NAME
full_backup_path = (
self.library_dir / TS_FOLDER_NAME / BACKUP_FOLDER_NAME
)
full_collage_path = (
self.library_dir / TS_FOLDER_NAME / COLLAGE_FOLDER_NAME
)
full_backup_path = self.library_dir / TS_FOLDER_NAME / BACKUP_FOLDER_NAME
full_collage_path = self.library_dir / TS_FOLDER_NAME / COLLAGE_FOLDER_NAME

if not os.path.isdir(full_ts_path):
os.mkdir(full_ts_path)
Expand Down Expand Up @@ -707,9 +703,7 @@ def open_library(self, path: str | Path) -> int:

# If the Library is loaded, continue other processes.
if return_code == 1:
(self.library_dir / TS_FOLDER_NAME).mkdir(
parents=True, exist_ok=True
)
(self.library_dir / TS_FOLDER_NAME).mkdir(parents=True, exist_ok=True)
self._map_filenames_to_entry_ids()

return return_code
Expand Down Expand Up @@ -807,10 +801,7 @@ def save_library_backup_to_disk(self) -> str:

self.verify_ts_folders()
with open(
self.library_dir
/ TS_FOLDER_NAME
/ BACKUP_FOLDER_NAME
/ filename,
self.library_dir / TS_FOLDER_NAME / BACKUP_FOLDER_NAME / filename,
"w",
encoding="utf-8",
) as outfile:
Expand Down
19 changes: 8 additions & 11 deletions tagstudio/src/qt/widgets/collage_icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ def render(
keep_aspect,
):
entry = self.lib.get_entry(entry_id)
filepath = os.path.normpath(
f"{self.lib.library_dir}/{entry.path}/{entry.filename}"
)
filepath = self.lib.library_dir / entry.path / entry.filename
file_type = os.path.splitext(filepath)[1].lower()[1:]
color: str = ""

Expand Down Expand Up @@ -91,16 +89,14 @@ def render(
self.rendered.emit(pic)
if not data_only_mode:
logging.info(
f"\r{INFO} Combining [ID:{entry_id}/{len(self.lib.entries)}]: {self.get_file_color(file_type)}{entry.path}{os.sep}{entry.filename}\033[0m"
f"\r{INFO} Combining [ID:{entry_id}/{len(self.lib.entries)}]: {self.get_file_color(filepath.suffix)}{entry.path}{os.sep}{entry.filename}\033[0m"
)
# sys.stdout.write(f'\r{INFO} Combining [{i+1}/{len(self.lib.entries)}]: {self.get_file_color(file_type)}{entry.path}{os.sep}{entry.filename}{RESET}')
# sys.stdout.flush()
if file_type in IMAGE_TYPES:
if filepath.suffix in IMAGE_TYPES:
try:
with Image.open(
os.path.normpath(
f"{self.lib.library_dir}/{entry.path}/{entry.filename}"
)
str(self.lib.library_dir / entry.path / entry.filename)
) as pic:
if keep_aspect:
pic.thumbnail(size)
Expand All @@ -115,7 +111,7 @@ def render(
self.rendered.emit(pic)
except DecompressionBombError as e:
logging.info(f"[ERROR] One of the images was too big ({e})")
elif file_type in VIDEO_TYPES:
elif filepath.suffix in VIDEO_TYPES:
video = cv2.VideoCapture(filepath)
video.set(
cv2.CAP_PROP_POS_FRAMES,
Expand Down Expand Up @@ -145,8 +141,9 @@ def render(
f"\n{ERROR} Couldn't read {entry.path}{os.sep}{entry.filename}"
)
with Image.open(
os.path.normpath(
f"{Path(__file__).parents[2]}/resources/qt/images/thumb_broken_512.png"
str(
Path(__file__).parents[2]
/ "resources/qt/images/thumb_broken_512.png"
)
) as pic:
pic.thumbnail(size)
Expand Down
12 changes: 3 additions & 9 deletions tagstudio/src/qt/widgets/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,17 @@
class FieldContainer(QWidget):
# TODO: reference a resources folder rather than path.parents[3]?
clipboard_icon_128: Image.Image = Image.open(
os.path.normpath(
f"{Path(__file__).parents[3]}/resources/qt/images/clipboard_icon_128.png"
)
str(Path(__file__).parents[3] / "resources/qt/images/clipboard_icon_128.png")
).resize((math.floor(24 * 1.25), math.floor(24 * 1.25)))
clipboard_icon_128.load()

edit_icon_128: Image.Image = Image.open(
os.path.normpath(
f"{Path(__file__).parents[3]}/resources/qt/images/edit_icon_128.png"
)
str(Path(__file__).parents[3] / "resources/qt/images/edit_icon_128.png")
).resize((math.floor(24 * 1.25), math.floor(24 * 1.25)))
edit_icon_128.load()

trash_icon_128: Image.Image = Image.open(
os.path.normpath(
f"{Path(__file__).parents[3]}/resources/qt/images/trash_icon_128.png"
)
str(Path(__file__).parents[3] / "resources/qt/images/trash_icon_128.png")
).resize((math.floor(24 * 1.25), math.floor(24 * 1.25)))
trash_icon_128.load()

Expand Down
12 changes: 3 additions & 9 deletions tagstudio/src/qt/widgets/item_thumb.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,12 @@ class ItemThumb(FlowWidget):
update_cutoff: float = time.time()

collation_icon_128: Image.Image = Image.open(
os.path.normpath(
f"{Path(__file__).parents[3]}/resources/qt/images/collation_icon_128.png"
)
str(Path(__file__).parents[3] / "resources/qt/images/collation_icon_128.png")
)
collation_icon_128.load()

tag_group_icon_128: Image.Image = Image.open(
os.path.normpath(
f"{Path(__file__).parents[3]}/resources/qt/images/tag_group_icon_128.png"
)
str(Path(__file__).parents[3] / "resources/qt/images/tag_group_icon_128.png")
)
tag_group_icon_128.load()

Expand Down Expand Up @@ -414,9 +410,7 @@ def set_item_id(self, id: int):
if id == -1:
return
entry = self.lib.get_entry(self.item_id)
filepath = os.path.normpath(
f"{self.lib.library_dir}/{entry.path}/{entry.filename}"
)
filepath = self.lib.library_dir / entry.path / entry.filename
self.opener.set_filepath(filepath)

def assign_favorite(self, value: bool):
Expand Down
25 changes: 12 additions & 13 deletions tagstudio/src/qt/widgets/preview_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,7 @@ def update_widgets(self):
item: Entry = self.lib.get_entry(self.driver.selected[0][1])
# If a new selection is made, update the thumbnail and filepath.
if not self.selected or self.selected != self.driver.selected:
filepath = os.path.normpath(
f"{self.lib.library_dir}/{item.path}/{item.filename}"
)
filepath = self.lib.library_dir / item.path / item.filename
self.file_label.setFilePath(filepath)
window_title = filepath
ratio: float = self.devicePixelRatio()
Expand All @@ -484,7 +482,7 @@ def update_widgets(self):
ratio,
update_on_ratio_change=True,
)
self.file_label.setText("\u200b".join(filepath))
self.file_label.setText("\u200b".join(str(filepath)))
self.file_label.setCursor(Qt.CursorShape.PointingHandCursor)

self.preview_img.setContextMenuPolicy(
Expand All @@ -499,31 +497,32 @@ def update_widgets(self):
)

# TODO: Do this somewhere else, this is just here temporarily.
extension = os.path.splitext(filepath)[1][1:].lower()
try:
image = None
if extension in IMAGE_TYPES:
image = Image.open(filepath)
elif extension in RAW_IMAGE_TYPES:
if filepath.suffix in IMAGE_TYPES:
image = Image.open(str(filepath))
elif filepath.suffix in RAW_IMAGE_TYPES:
with rawpy.imread(filepath) as raw:
rgb = raw.postprocess()
image = Image.new(
"L", (rgb.shape[1], rgb.shape[0]), color="black"
)
elif extension in VIDEO_TYPES:
elif filepath.suffix in VIDEO_TYPES:
video = cv2.VideoCapture(filepath)
video.set(cv2.CAP_PROP_POS_FRAMES, 0)
success, frame = video.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image = Image.fromarray(frame)

# Stats for specific file types are displayed here.
if extension in (IMAGE_TYPES + VIDEO_TYPES + RAW_IMAGE_TYPES):
if filepath.suffix in (
IMAGE_TYPES + VIDEO_TYPES + RAW_IMAGE_TYPES
):
self.dimensions_label.setText(
f"{extension.upper()}{format_size(os.stat(filepath).st_size)}\n{image.width} x {image.height} px"
f"{filepath.suffix.upper()}{format_size(os.stat(filepath).st_size)}\n{image.width} x {image.height} px"
)
else:
self.dimensions_label.setText(f"{extension.upper()}")
self.dimensions_label.setText(f"{filepath.suffix.upper()}")

if not image:
raise UnidentifiedImageError
Expand All @@ -535,7 +534,7 @@ def update_widgets(self):
DecompressionBombError,
) as e:
self.dimensions_label.setText(
f"{extension.upper()}{format_size(os.stat(filepath).st_size)}"
f"{filepath.suffix.upper()}{format_size(os.stat(filepath).st_size)}"
)
logging.info(
f"[PreviewPanel][ERROR] Couldn't Render thumbnail for {filepath} (because of {e})"
Expand Down
4 changes: 1 addition & 3 deletions tagstudio/src/qt/widgets/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@

class TagWidget(QWidget):
edit_icon_128: Image.Image = Image.open(
os.path.normpath(
f"{Path(__file__).parents[3]}/resources/qt/images/edit_icon_128.png"
)
str(Path(__file__).parents[3] / "resources/qt/images/edit_icon_128.png")
).resize((math.floor(14 * 1.25), math.floor(14 * 1.25)))
edit_icon_128.load()
on_remove = Signal()
Expand Down
5 changes: 1 addition & 4 deletions tagstudio/src/qt/widgets/thumb_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class ThumbRenderer(QObject):
def render(
self,
timestamp: float,
filepath: str|Path,
filepath: str | Path,
base_size: tuple[int, int],
pixel_ratio: float,
is_loading=False,
Expand Down Expand Up @@ -119,7 +119,6 @@ def render(
if update_on_ratio_change:
self.updated_ratio.emit(1)
elif filepath:

try:
# Images =======================================================
if filepath.suffix in IMAGE_TYPES:
Expand Down Expand Up @@ -274,7 +273,6 @@ def render(
(adj_size, adj_size), resample=resampling_method
)
qim = ImageQt.ImageQt(final)
final.save(f'/home/icosahunter/Documents/test/{filepath.stem}.png')
if image:
image.close()
pixmap = QPixmap.fromImage(qim)
Expand All @@ -292,5 +290,4 @@ def render(
)

else:

self.updated.emit(timestamp, QPixmap(), QSize(*base_size), filepath.suffix)

0 comments on commit a53b515

Please sign in to comment.