Skip to content

Commit

Permalink
Add a flag is_pasted to all pasted images
Browse files Browse the repository at this point in the history
This prevents polluting the `pasted_images` when the `Image` instances
are used for failure reports or copying images.
  • Loading branch information
thanhph111 committed Aug 5, 2021
1 parent e25fe8d commit e79cda7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
5 changes: 3 additions & 2 deletions imagepaste/clipboard/darwin/darwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@ def push(cls, save_directory: str) -> DarwinClipboard:
urls = pb.get_file_urls()
if urls is not None:
filepaths = list(urls)
images = [Image(filepath) for filepath in filepaths]
images = [Image(filepath, is_pasted=True) for filepath in filepaths]
return cls(Report(6, f"Pasted {len(images)} image files: {images}"), images)

# Save an image if it is in the clipboard
contents = pb.get_contents(type=pasteboard.TIFF)
if contents is not None:
filename = cls.get_timestamp_filename()
filepath = join(save_directory, filename)
image = Image(filepath, filename)
commands = [
"set pastedImage to "
f'(open for access POSIX file "{filepath}" with write permission)',
Expand All @@ -59,7 +58,9 @@ def push(cls, save_directory: str) -> DarwinClipboard:
]
process = Process.execute(cls.get_osascript_args(commands))
if not isfile(filepath):
image = Image(filepath, filename)
return cls(Report(3, f"Cannot save image: {image} ({process.stderr})"))
image = Image(filepath, filename, is_pasted=True)
if process.stderr:
report = Report(6, f"Saved 1 image: {image} (WARN: {process.stderr})")
return cls(report, [image])
Expand Down
5 changes: 3 additions & 2 deletions imagepaste/clipboard/linux/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,21 @@ def push(cls, save_directory: str) -> LinuxClipboard:
if XclipTarget.IMAGE.value in process.stdout:
filename = cls.get_timestamp_filename()
filepath = join(save_directory, filename)
image = Image(filepath, filename)
process = Process.execute(
cls.get_xclip_args(XclipTarget.IMAGE.value), outpath=filepath
)
if process.stderr:
image = Image(filepath, filename)
return cls(Report(3, f"Cannot save image: {image} ({process.stderr})"))
image = Image(filepath, filename, is_pasted=True)
return cls(Report(6, f"Saved and pasted 1 image: {image}"), [image])

# If copying from files, just send their paths
if XclipTarget.URI.value in process.stdout:
uris = Process.execute(cls.get_xclip_args(XclipTarget.URI.value)).stdout
filepaths = [url2pathname((urlparse(uri).path)) for uri in uris]
# TODO: Check if files are images
images = [Image(filepath) for filepath in filepaths]
images = [Image(filepath, is_pasted=True) for filepath in filepaths]
return cls(Report(6, f"Pasted {len(images)} image files: {images}"), images)
return cls(Report(2))

Expand Down
3 changes: 2 additions & 1 deletion imagepaste/clipboard/windows/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@ def push(cls, save_directory: str) -> WindowsClipboard:

filename = cls.get_timestamp_filename()
filepath = join(save_directory, filename)
image = Image(filepath, filename)

image_script = (
"$image = Get-Clipboard -Format Image\n"
f'if ($image) {{ $image.Save("{filepath}"); Write-Output 0 }}'
)
process = Process.execute(cls.get_powershell_args(image_script), split=False)
if process.stderr:
image = Image(filepath, filename)
return cls(Report(3, f"Cannot save image: {image} ({process.stderr})"))
if process.stdout == "0":
image = Image(filepath, filename, is_pasted=True)
return cls(Report(6, f"Saved and pasted 1 image: {image}"), [image])

file_script = (
Expand Down
10 changes: 7 additions & 3 deletions imagepaste/image.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
class Image:
"""A class to represent an image file."""

filepath_to_image = {}
pasted_images = {}

def __init__(self, filepath: str, filename: str = None) -> None:
def __init__(
self, filepath: str, filename: str = None, is_pasted: bool = False
) -> None:
"""Constructor for the Image class.
Args:
filepath (str): The path to the image file.
filename (str, optional): The name of the image file.
is_pasted (bool, optional): Whether the image is pasted.
"""
from os.path import basename
from os.path import dirname

self.filepath = filepath
self.filename = filename or basename(filepath)
self.filebase = dirname(filepath)
Image.filepath_to_image[self.filepath] = self
if is_pasted:
Image.pasted_images[self.filepath] = self

def __repr__(self) -> str:
"""Return a string representation of the Image class.
Expand Down
14 changes: 7 additions & 7 deletions imagepaste/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,21 +219,21 @@ def __init__(self) -> None:
def execute(self, _context):
from .image import Image

filepath_to_image = Image.filepath_to_image
pasted_images = Image.pasted_images
orphaned_image_filepaths = []
# Change the paths the images refers to with the new one
for orphaned_image in self.orphaned_images:
old_filepath = self.get_abspath(orphaned_image.filepath)
self.change_image_directory(orphaned_image, self.saved_directory)
new_filepath = self.get_abspath(orphaned_image.filepath)
# Also change in the dictionary
if old_filepath in filepath_to_image:
filepath_to_image[new_filepath] = filepath_to_image.pop(old_filepath)
if old_filepath in pasted_images:
pasted_images[new_filepath] = pasted_images.pop(old_filepath)
orphaned_image_filepaths.append(old_filepath)
# Remove pasted images which are not in `.blend` file (pasted but then undone)
for filepath in list(filepath_to_image.keys()):
for filepath in list(pasted_images.keys()):
if filepath in orphaned_image_filepaths:
del filepath_to_image[filepath]
del pasted_images[filepath]
return {"FINISHED"}

def invoke(self, context, _event):
Expand Down Expand Up @@ -274,7 +274,7 @@ def get_orphaned_images(self, saved_directory) -> list[bpy.types.Image]:
preferences = get_addon_preferences()
if preferences.image_type_to_move == "no_moving":
return []
filepath_to_image = Image.filepath_to_image
pasted_images = Image.pasted_images
existing_images = bpy.data.images
orphaned_images = []
for image in existing_images:
Expand All @@ -287,7 +287,7 @@ def get_orphaned_images(self, saved_directory) -> list[bpy.types.Image]:
if preferences.image_type_to_move == "all_images":
orphaned_images.append(image)
continue
if filepath in filepath_to_image:
if filepath in pasted_images:
orphaned_images.append(image)
return orphaned_images

Expand Down

0 comments on commit e79cda7

Please sign in to comment.