More PhotoInfo.albums refactoring, closes #169

This commit is contained in:
Rhet Turnbull
2020-06-21 08:18:11 -07:00
parent 32806c8459
commit 538bac7ade
8 changed files with 52 additions and 31 deletions

View File

@@ -1,3 +1,3 @@
""" version info """ """ version info """
__version__ = "0.29.24" __version__ = "0.29.25"

View File

@@ -22,6 +22,7 @@ from .._constants import (
_MOVIE_TYPE, _MOVIE_TYPE,
_PHOTO_TYPE, _PHOTO_TYPE,
_PHOTOS_4_ALBUM_KIND, _PHOTOS_4_ALBUM_KIND,
_PHOTOS_4_ROOT_FOLDER,
_PHOTOS_4_VERSION, _PHOTOS_4_VERSION,
_PHOTOS_5_ALBUM_KIND, _PHOTOS_5_ALBUM_KIND,
_PHOTOS_5_SHARED_ALBUM_KIND, _PHOTOS_5_SHARED_ALBUM_KIND,
@@ -347,17 +348,10 @@ class PhotoInfo:
try: try:
return self._albums return self._albums
except AttributeError: except AttributeError:
self._albums = [] album_uuids = self._get_album_uuids()
album_kinds = ( self._albums = list(
[_PHOTOS_4_ALBUM_KIND] {self._db._dbalbum_details[album]["title"] for album in album_uuids}
if self._db._db_version <= _PHOTOS_4_VERSION
else [_PHOTOS_5_ALBUM_KIND, _PHOTOS_5_SHARED_ALBUM_KIND]
) )
for album in self._info["albums"]:
detail = self._db._dbalbum_details[album]
if detail["kind"] in album_kinds and not detail["intrash"]:
self._albums.append(detail["title"])
return self._albums return self._albums
@property @property
@@ -366,17 +360,10 @@ class PhotoInfo:
try: try:
return self._album_info return self._album_info
except AttributeError: except AttributeError:
self._album_info = [] album_uuids = self._get_album_uuids()
album_kinds = ( self._album_info = [
[_PHOTOS_4_ALBUM_KIND] AlbumInfo(db=self._db, uuid=album) for album in album_uuids
if self._db._db_version <= _PHOTOS_4_VERSION ]
else [_PHOTOS_5_ALBUM_KIND, _PHOTOS_5_SHARED_ALBUM_KIND]
)
for album in self._info["albums"]:
detail = self._db._dbalbum_details[album]
if detail["kind"] in album_kinds and not detail["intrash"]:
self._album_info.append(AlbumInfo(db=self._db, uuid=album))
return self._album_info return self._album_info
@property @property
@@ -677,6 +664,37 @@ class PhotoInfo:
""" Returns latitude, in degrees """ """ Returns latitude, in degrees """
return self._info["latitude"] return self._info["latitude"]
def _get_album_uuids(self):
""" Return list of album UUIDs this photo is found in
Filters out albums in the trash and any special album types
Returns: list of album UUIDs
"""
if self._db._db_version <= _PHOTOS_4_VERSION:
version4 = True
album_kind = [_PHOTOS_4_ALBUM_KIND]
else:
version4 = False
album_kind = [_PHOTOS_5_SHARED_ALBUM_KIND, _PHOTOS_5_ALBUM_KIND]
album_list = []
for album in self._info["albums"]:
detail = self._db._dbalbum_details[album]
if (
detail["kind"] in album_kind
and not detail["intrash"]
and (
not version4
# in Photos <= 4, special albums like "printAlbum" have kind _PHOTOS_4_ALBUM_KIND
# but should not be listed here; they can be distinguished by looking
# for folderUuid of _PHOTOS_4_ROOT_FOLDER as opposed to _PHOTOS_4_TOP_LEVEL_ALBUM
or (version4 and detail["folderUuid"] != _PHOTOS_4_ROOT_FOLDER)
)
):
album_list.append(album)
return album_list
def __repr__(self): def __repr__(self):
return f"osxphotos.{self.__class__.__name__}(db={self._db}, uuid='{self._uuid}', info={self._info})" return f"osxphotos.{self.__class__.__name__}(db={self._db}, uuid='{self._uuid}', info={self._info})"

View File

@@ -2084,7 +2084,7 @@ class PhotosDB:
Args: Args:
shared: boolean; if True, returns shared albums, else normal albums shared: boolean; if True, returns shared albums, else normal albums
Returns: list of album names Returns: list of album UUIDs
""" """
if self._db_version <= _PHOTOS_4_VERSION: if self._db_version <= _PHOTOS_4_VERSION:
version4 = True version4 = True
@@ -2110,11 +2110,11 @@ class PhotosDB:
or (not shared and detail["cloudownerhashedpersonid"] is None) or (not shared and detail["cloudownerhashedpersonid"] is None)
) )
and ( and (
not version4
# in Photos 4, special albums like "printAlbum" have kind _PHOTOS_4_ALBUM_KIND # in Photos 4, special albums like "printAlbum" have kind _PHOTOS_4_ALBUM_KIND
# but should not be listed here; they can be distinguished by looking # but should not be listed here; they can be distinguished by looking
# for folderUuid of _PHOTOS_4_ROOT_FOLDER as opposed to _PHOTOS_4_TOP_LEVEL_ALBUM # for folderUuid of _PHOTOS_4_ROOT_FOLDER as opposed to _PHOTOS_4_TOP_LEVEL_ALBUM
(version4 and detail["folderUuid"] != _PHOTOS_4_ROOT_FOLDER) or (version4 and detail["folderUuid"] != _PHOTOS_4_ROOT_FOLDER)
or not version4
) )
): ):
album_list.append(album) album_list.append(album)

View File

@@ -124,7 +124,7 @@ def test_attributes():
) )
assert p.description == "Girl holding pumpkin" assert p.description == "Girl holding pumpkin"
assert p.title == "I found one!" assert p.title == "I found one!"
assert p.albums == ["Pumpkin Farm", "AlbumInFolder"] assert sorted(p.albums) == ["AlbumInFolder", "Pumpkin Farm"]
assert p.persons == ["Katie"] assert p.persons == ["Katie"]
assert p.path.endswith( assert p.path.endswith(
"/tests/Test-10.12.6.photoslibrary/Masters/2019/08/24/20190824-030824/Pumkins2.jpg" "/tests/Test-10.12.6.photoslibrary/Masters/2019/08/24/20190824-030824/Pumkins2.jpg"

View File

@@ -106,6 +106,7 @@ def test_init4():
except: except:
pass pass
def test_init5(mocker): def test_init5(mocker):
# test failed get_last_library_path # test failed get_last_library_path
import osxphotos import osxphotos
@@ -117,7 +118,6 @@ def test_init5(mocker):
# because of the layout of photosdb/ need to patch it this way...don't really understand why, but it works # because of the layout of photosdb/ need to patch it this way...don't really understand why, but it works
mocker.patch("osxphotos.photosdb.photosdb.get_last_library_path", new=bad_library) mocker.patch("osxphotos.photosdb.photosdb.get_last_library_path", new=bad_library)
with pytest.raises(Exception): with pytest.raises(Exception):
assert osxphotos.PhotosDB() assert osxphotos.PhotosDB()
@@ -207,7 +207,7 @@ def test_attributes():
) )
assert p.description == "Girl holding pumpkin" assert p.description == "Girl holding pumpkin"
assert p.title == "I found one!" assert p.title == "I found one!"
assert p.albums == ["Pumpkin Farm", "Test Album", "Multi Keyword"] assert sorted(p.albums) == ["Multi Keyword", "Pumpkin Farm", "Test Album"]
assert p.persons == ["Katie"] assert p.persons == ["Katie"]
assert p.path.endswith( assert p.path.endswith(
"tests/Test-10.15.1.photoslibrary/originals/D/D79B8D77-BFFC-460B-9312-034F2877D35B.jpeg" "tests/Test-10.15.1.photoslibrary/originals/D/D79B8D77-BFFC-460B-9312-034F2877D35B.jpeg"

View File

@@ -215,7 +215,7 @@ def test_attributes():
) )
assert p.description == "Girl holding pumpkin" assert p.description == "Girl holding pumpkin"
assert p.title == "I found one!" assert p.title == "I found one!"
assert p.albums == ["Pumpkin Farm", "Test Album"] assert sorted(p.albums) == ["Pumpkin Farm", "Test Album"]
assert p.persons == ["Katie"] assert p.persons == ["Katie"]
assert p.path.endswith( assert p.path.endswith(
"tests/Test-10.15.4.photoslibrary/originals/D/D79B8D77-BFFC-460B-9312-034F2877D35B.jpeg" "tests/Test-10.15.4.photoslibrary/originals/D/D79B8D77-BFFC-460B-9312-034F2877D35B.jpeg"

View File

@@ -228,7 +228,7 @@ def test_attributes():
) )
assert p.description == "Girl holding pumpkin" assert p.description == "Girl holding pumpkin"
assert p.title == "I found one!" assert p.title == "I found one!"
assert p.albums == ["Pumpkin Farm", "Test Album"] assert sorted(p.albums) == ["Pumpkin Farm", "Test Album"]
assert p.persons == ["Katie"] assert p.persons == ["Katie"]
assert p.path.endswith( assert p.path.endswith(
"tests/Test-10.15.5.photoslibrary/originals/D/D79B8D77-BFFC-460B-9312-034F2877D35B.jpeg" "tests/Test-10.15.5.photoslibrary/originals/D/D79B8D77-BFFC-460B-9312-034F2877D35B.jpeg"

View File

@@ -355,7 +355,10 @@ def test_query_uuid():
for key_ in json_expected: for key_ in json_expected:
assert key_ in json_got assert key_ in json_got
if key_ != "path": if key_ != "path":
assert json_expected[key_] == json_got[key_] if isinstance(json_expected[key_], list):
assert sorted(json_expected[key_]) == sorted(json_got[key_])
else:
assert json_expected[key_] == json_got[key_]
else: else:
assert json_expected[key_] in json_got[key_] assert json_expected[key_] in json_got[key_]