Skip to content

Commit

Permalink
Added PhotosLibrary.open(); alpha release
Browse files Browse the repository at this point in the history
  • Loading branch information
RhetTbull committed Nov 14, 2020
1 parent 6166fc7 commit 7b22dbe
Show file tree
Hide file tree
Showing 208 changed files with 399 additions and 208 deletions.
Binary file modified photoscript.pdf
Binary file not shown.
38 changes: 32 additions & 6 deletions photoscript/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
import pathlib
import random
import string
from subprocess import run
import tempfile
from subprocess import run

from applescript import kMissingValue

from applescript import AppleScript, kMissingValue
from photoscript.utils import ditto, findfiles

from .script_loader import run_script
Expand Down Expand Up @@ -42,6 +41,33 @@ def quit(self):
""" quit Photos.app """
run_script("_photoslibrary_quit")

def open(self, library_path):
""" open a library """
# Note: Unlike the other AppleScript scripts, this one is not included in photoscript.applescript
# because, for reasons I cannot explain, it fails to run if included there
if not pathlib.Path(library_path).is_dir():
raise ValueError(f"{library_path} does not appear to be a Photos library")
self.activate()
script = AppleScript(
f"""
set tries to 0
repeat while tries < 5
try
tell application "Photos"
activate
delay 3
open POSIX file "{library_path}"
delay 1
end tell
set tries to 5
on error
set tries to tries + 1
end try
end repeat
"""
)
script.run()

@property
def running(self):
""" True if Photos is running, otherwise False """
Expand Down Expand Up @@ -556,7 +582,7 @@ def uuid(self):
def name(self):
""" name of album (read/write) """
name = run_script("_album_name", self.id)
return name if name != kMissingValue else ""
return name if name != kMissingValue else ""

@name.setter
def name(self, name):
Expand Down Expand Up @@ -765,7 +791,7 @@ def uuid(self):
def name(self):
""" name of folder (read/write) """
name = run_script("_folder_name", self.id)
return name if name != kMissingValue else ""
return name if name != kMissingValue else ""

@name.setter
def name(self, name):
Expand Down Expand Up @@ -1079,7 +1105,7 @@ def export(
original=original,
overwrite=overwrite,
timeout=timeout,
reveal_in_finder=reveal_in_finder
reveal_in_finder=reveal_in_finder,
)

def duplicate(self):
Expand Down
2 changes: 1 addition & 1 deletion photoscript/_version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
""" version info """

__version__ = "0.0.27"
__version__ = "0.1.0"
1 change: 0 additions & 1 deletion photoscript/photoscript.applescript
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ on _photoslibrary_waitforphotos(timeoutDurationInSeconds)
return true
end _photoslibrary_waitforphotos


on _photoslibrary_isrunning()
(* return true if Photos is running, otherwise false *)
set app_name_ to "Photos"
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
packages=find_packages(exclude=["tests", "examples"]),
license="License :: OSI Approved :: MIT License",
classifiers=[
"Development Status :: 3 - Alpha",
"Development Status :: 4 - Beta",
"Environment :: MacOS X",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
Expand Down
19 changes: 13 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import photoscript
from photoscript.utils import ditto

TEST_LIBRARY = "Test-PhotoScript-10.15.6.photoslibrary"


def get_os_version():
import platform
Expand All @@ -30,8 +28,16 @@ def get_os_version():
return (ver, major, minor)


def copy_photos_library(delay=0):
""" copy the test library and open Photos """
OS_VER = get_os_version()[1]
if OS_VER == "15":
from tests.photoscript_config_catalina import TEST_LIBRARY
else:
TEST_LIBRARY = None
pytest.exit("This test suite currently only runs on MacOS Catalina ")


def copy_photos_library(photos_library=TEST_LIBRARY, delay=0):
""" copy the test library and open Photos, returns path to copied library """
script = AppleScript(
"""
tell application "Photos"
Expand All @@ -40,7 +46,7 @@ def copy_photos_library(delay=0):
"""
)
script.run()
src = pathlib.Path(os.getcwd()) / f"tests/test_libraries/{TEST_LIBRARY}"
src = pathlib.Path(os.getcwd()) / f"tests/test_libraries/{photos_library}"
picture_folder = (
pathlib.Path(os.environ["PHOTOSCRIPT_PICTURES_FOLDER"])
if "PHOTOSCRIPT_PICTURES_FOLDER" in os.environ
Expand All @@ -49,7 +55,7 @@ def copy_photos_library(delay=0):
picture_folder = picture_folder.expanduser()
if not picture_folder.is_dir():
pytest.exit(f"Invalid picture folder: '{picture_folder}'")
dest = picture_folder / TEST_LIBRARY
dest = picture_folder / photos_library
ditto(src, dest)
script = AppleScript(
f"""
Expand All @@ -70,6 +76,7 @@ def copy_photos_library(delay=0):
"""
)
script.run()
return dest


@pytest.fixture(scope="session", autouse=True)
Expand Down
3 changes: 3 additions & 0 deletions tests/photoscript_config_catalina.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
""" Test data for Catalina/Photos 5 """

TEST_LIBRARY = "Test-PhotoScript-10.15.7.photoslibrary"
TEST_LIBRARY_OPEN = "Test-PhotoScript-10.15.7-Single-Photo.photoslibrary"

ALBUM_1_NAME = "San Juan Capistrano"
ALBUM_1_UUID = "01F18AB8-B0D7-4414-96A8-28D94AFE86BF/L0/040"
ALBUM_1_UUID_OSXPHOTOS = "01F18AB8-B0D7-4414-96A8-28D94AFE86BF"
Expand Down
34 changes: 30 additions & 4 deletions tests/test_0_interactive.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
""" Tests which require user interaction to run """

import pytest
from applescript import AppleScript
from tests.conftest import (
copy_photos_library,
get_os_version,
photoslib,
suspend_capture,
)
from tests.photoscript_config_catalina import (
ALBUM_1_PHOTO_EXPORT_FILENAMES,
PHOTO_EXPORT_FILENAME_ORIGINAL,
)
import pytest
from applescript import AppleScript
from tests.conftest import photoslib, suspend_capture, get_os_version

OS_VER = get_os_version()[1]
if OS_VER == "15":
Expand Down Expand Up @@ -37,13 +42,35 @@
PHOTOS_UUID,
PHOTOS_UUID_FILENAMES,
SELECTION_UUIDS,
TEST_LIBRARY,
TEST_LIBRARY_OPEN,
)
else:
pytest.exit("This test suite currently only runs on MacOS Catalina ")

########## Interactive tests run first ##########


def test_photoslibrary_open(photoslib, suspend_capture):
import os

test_library = copy_photos_library(photos_library=TEST_LIBRARY_OPEN)
prompt = "Click Switch in Photos after the drop down sheet appears."
os.system(f'say "{prompt}"')
with suspend_capture:
photoslib.open(test_library)
prompt = (
"Press 'y' if Photos Library contains a single image "
"of a kettlebell, otherwise press 'n' "
)
os.system(f'say "{prompt}"')
answer = input(f"\n{prompt}")
assert answer.lower() == "y"
# re-copy main test library
test_library = copy_photos_library(photos_library=TEST_LIBRARY)
photoslib.open(test_library)


def test_photoslibrary_import_photos_dup_check(photoslib):
""" Attempt to import a duplicate photo with skip_duplicate_check = False
This will cause Photos to display dialog box prompting user what to do """
Expand Down Expand Up @@ -187,4 +214,3 @@ def test_photo_export_reveal_in_finder(photoslib, suspend_capture):
os.system(f'say "{prompt}"')
answer = input(f"\n{prompt}")
assert answer.lower() == "y"

Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 7b22dbe

Please sign in to comment.