Changed AlbumInfo and FolderInfo interface to maintain backwards compatibility with PhotosDB.albums

This commit is contained in:
Rhet Turnbull
2020-04-12 09:01:16 -07:00
parent b749681c6d
commit e09f0b40f1
17 changed files with 126 additions and 100 deletions

View File

@@ -556,46 +556,48 @@ keywords = photosdb.keywords
Returns a list of the keywords found in the Photos library
#### `album_info`
```python
# assumes photosdb is a PhotosDB object (see above)
albums = photosdb.album_info
```
Returns a list of [AlbumInfo](#AlbumInfo) objects representing albums in the database or empty list if there are no albums. See also [albums](#albums).
#### `albums`
```python
# assumes photosdb is a PhotosDB object (see above)
albums = photosdb.albums
```
Returns a list of [AlbumInfo](#AlbumInfo) objects representing albums in the database or empty list if there are no albums. See also [album_names](#album_names).
#### `album_names`
```python
# assumes photosdb is a PhotosDB object (see above)
album_names = photosdb.album_names
album_names = photosdb.albums
```
Returns a list of the album names found in the Photos library.
**Note**: In Photos 5.0 (MacOS 10.15/Catalina), It is possible to have more than one album with the same name in Photos. Albums with duplicate names are treated as a single album and the photos in each are combined. For example, if you have two albums named "Wedding" and each has 2 photos, osxphotos will treat this as a single album named "Wedding" with 4 photos in it.
#### `album_names_shared`
See also [album_info](#album_info.)
#### `albums_shared`
Returns list of shared album names found in photos database (e.g. albums shared via iCloud photo sharing)
**Note**: *Only valid for Photos 5 / MacOS 10.15*; on Photos <= 4, prints warning and returns empty list.
#### `folder_info`
```python
# assumes photosdb is a PhotosDB object (see above)
folders = photosdb.folder_info
```
Returns a list of [FolderInfo](#FolderInfo) objects representing top level folders in the database or empty list if there are no folders. See also [folders](#folders).
**Note**: Currently folder_info is only implemented for Photos 5 (Catalina); will return empty list and output warning if called on earlier database versions.
#### `folders`
```python
# assumes photosdb is a PhotosDB object (see above)
folders = photosdb.folders
```
Returns a list of [FolderInfo](#FolderInfo) objects representing top level folders in the database or empty list if there are no folders. See also [folder_names](#folder_names).
**Note**: Currently folders is only implemented for Photos 5 (Catalina); will return empty list and output warning if called on earlier database versions.
#### `folder_names`
```python
# assumes photosdb is a PhotosDB object (see above)
folder_names = photosdb.folder_names
```
Returns a list names of top level folder names in the database.
**Note**: Currently folders is only implemented for Photos 5 (Catalina); will return empty list and output warning if called on earlier database versions.
@@ -809,7 +811,10 @@ Returns the title of the photo
Returns a list of keywords (e.g. tags) applied to the photo
#### `albums`
Returns a list of albums the photo is contained in
Returns a list of albums the photo is contained in. See also [album_info](#album_info).
#### `album_info`
Returns a list of [AlbumInfo](#AlbumInfo) objects representing the albums the photo is contained in. See also [albums](#albums).
#### `persons`
Returns a list of the names of the persons in the photo
@@ -956,10 +961,10 @@ Then
If overwrite=False and increment=False, export will fail if destination file already exists
**Implementation Note**: Because the usual python file copy methods don't preserve all the metadata available on MacOS, export uses /usr/bin/ditto to do the copy for export. ditto preserves most metadata such as extended attributes, permissions, ACLs, etc.
**Implementation Note**: Because the usual python file copy methods don't preserve all the metadata available on MacOS, export uses `/usr/bin/ditto` to do the copy for export. ditto preserves most metadata such as extended attributes, permissions, ACLs, etc.
### AlbumInfo
PhotosDB.albums returns a list of AlbumInfo objects. Each AlbumInfo object represents a single album in the Photos library.
### AlbumInfo
PhotosDB.album_info and PhotoInfo.album_info return a list of AlbumInfo objects. Each AlbumInfo object represents a single album in the Photos library.
#### `uuid`
Returns the universally unique identifier (uuid) of the album. This is how Photos keeps track of individual objects within the database.
@@ -971,7 +976,7 @@ Returns the title or name of the album.
Returns a list of [PhotoInfo](#PhotoInfo) objects representing each photo contained in the album.
#### `folder_list`
Returns a hierarchical list of [FolderInfo](#FolderInfo) objects representing the folders the album is contained in. For example, if album "AlbumInFolder" is in SubFolder1 of Folder1 as illustrated below, would return a list of `FolderInfo` objects representing ["Folder1", "SubFolder2"]
Returns a hierarchical list of [FolderInfo](#FolderInfo) objects representing the folders the album is contained in. For example, if album "AlbumInFolder" is in SubFolder2 of Folder1 as illustrated below, would return a list of `FolderInfo` objects representing ["Folder1", "SubFolder2"]
```txt
Photos Library
@@ -1005,31 +1010,31 @@ Returns the universally unique identifier (uuid) of the folder. This is how Pho
#### `title`
Returns the title or name of the folder.
#### `albums`
#### `album_info`
Returns a list of [AlbumInfo](#AlbumInfo) objects representing each album contained in the folder.
#### `folders`
#### `subfolders`
Returns a list of [FolderInfo](#FolderInfo) objects representing the sub-folders of the folder.
#### `parent`
Returns a [FolderInfo](#FolderInfo) object representing the folder's parent folder or `None` if album is not a in a folder.
**Note**: FolderInfo and AlbumInfo objects effectively work as a linked list. The children of a folder are contained in `folders` and `albums` and the parent object of both `AlbumInfo` and `FolderInfo` is represented by `parent`. For example:
**Note**: FolderInfo and AlbumInfo objects effectively work as a linked list. The children of a folder are contained in `subfolders` and `album_info` and the parent object of both `AlbumInfo` and `FolderInfo` is represented by `parent`. For example:
```python
>>> import osxphotos
>>> photosdb = osxphotos.PhotosDB()
>>> photosdb.folders
>>> photosdb.subfolders
[<osxphotos.albuminfo.FolderInfo object at 0x10fcc0160>]
>>> photosdb.folders[0].title
>>> photosdb.folder_info[0].title
'Folder1'
>>> photosdb.folders[0].folders[1].title
>>> photosdb.folder_info[0].subfolders[1].title
'SubFolder2'
>>> photosdb.folders[0].folders[1].albums[0].title
>>> photosdb.folder_info[0].subfolders[1].album_info[0].title
'AlbumInFolder'
>>> photosdb.folders[0].folders[1].albums[0].parent.title
>>> photosdb.folder_info[0].subfolders[1].album_info[0].parent.title
'SubFolder2'
>>> photosdb.folders[0].folders[1].albums[0].parent.albums[0].title
>>> photosdb.folder_info[0].subfolders[1].album_info[0].parent.album_info[0].title
'AlbumInFolder'
```

View File

@@ -1,3 +1,3 @@
""" version info """
__version__ = "0.27.0"
__version__ = "0.27.1"

View File

@@ -126,7 +126,7 @@ class FolderInfo:
return self._uuid
@property
def albums(self):
def album_info(self):
""" return list of albums (as AlbumInfo objects) contained in the folder """
try:
return self._albums
@@ -156,7 +156,7 @@ class FolderInfo:
return self._parent
@property
def folders(self):
def subfolders(self):
""" return list of folders (as FolderInfo objects) contained in the folder """
try:
return self._folders
@@ -173,4 +173,4 @@ class FolderInfo:
def __len__(self):
""" returns count of folders + albums contained in the folder """
return len(self.folders) + len(self.albums)
return len(self.subfolders) + len(self.album_info)

View File

@@ -28,6 +28,7 @@ from ._constants import (
)
from .exiftool import ExifTool
from .placeinfo import PlaceInfo4, PlaceInfo5
from .albuminfo import AlbumInfo
from .utils import (
_copy_file,
_export_photo_uuid_applescript,
@@ -256,6 +257,15 @@ class PhotoInfo:
albums.append(self._db._dbalbum_details[album]["title"])
return albums
@property
def album_info(self):
""" list of AlbumInfo objects representing albums the photos is contained in """
albums = []
for album in self._info["albums"]:
albums.append(AlbumInfo(db=self._db, uuid=album))
return albums
@property
def keywords(self):
""" list of keywords for picture """

View File

@@ -340,8 +340,8 @@ class PhotosDB:
return list(persons)
@property
def folders(self):
""" return list of top-level folders in the photos database """
def folder_info(self):
""" return list FolderInfo objects representing top-level folders in the photos database """
if self._db_version < _PHOTOS_5_VERSION:
logging.warning("Folders not yet implemented for this DB version")
return []
@@ -356,7 +356,7 @@ class PhotosDB:
return folders
@property
def folder_names(self):
def folders(self):
""" return list of top-level folder names in the photos database """
if self._db_version < _PHOTOS_5_VERSION:
logging.warning("Folders not yet implemented for this DB version")
@@ -371,9 +371,8 @@ class PhotosDB:
]
return folder_names
@property
def albums(self):
def album_info(self):
""" return list of AlbumInfo objects for each album in the photos database """
albums = [
@@ -385,7 +384,7 @@ class PhotosDB:
return albums
@property
def albums_shared(self):
def album_info_shared(self):
""" return list of AlbumInfo objects for each shared album in the photos database
only valid for Photos 5; on Photos <= 4, prints warning and returns empty list """
# if _dbalbum_details[key]["cloudownerhashedpersonid"] is not None, then it's a shared album
@@ -405,7 +404,7 @@ class PhotosDB:
return albums_shared
@property
def album_names(self):
def albums(self):
""" return list of albums found in photos database """
# Could be more than one album with same name
@@ -420,7 +419,7 @@ class PhotosDB:
return list(albums)
@property
def album_names_shared(self):
def albums_shared(self):
""" return list of shared albums found in photos database
only valid for Photos 5; on Photos <= 4, prints warning and returns empty list """

View File

@@ -55,6 +55,7 @@ TEMPLATE_SUBSTITUTIONS = {
# Permitted multi-value substitutions (each of these returns None or 1 or more values)
TEMPLATE_SUBSTITUTIONS_MULTI_VALUED = {
"{album}": "Album(s) photo is contained in",
# "{folder}": "Folder path + album photo is contained in. e.g. Folder/Subfolder/Album",
"{keyword}": "Keyword(s) assigned to photo",
"{person}": "Person(s) / face(s) in a photo",
}
@@ -321,6 +322,13 @@ def render_filepath_template(template, photo, none_str="_"):
values = photo.persons
# remove any _UNKNOWN_PERSON values
values = [val for val in values if val != _UNKNOWN_PERSON]
# elif field == "folder":
# folders = []
# # photos must be in an album to be in a folder
# albums = photo.albums
# for album in albums:
# zzz
else:
raise ValueError(f"Unhandleded template value: {field}")

View File

@@ -77,8 +77,8 @@ def test_album_names():
import collections
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
assert "Pumpkin Farm" in photosdb.album_names
assert collections.Counter(ALBUMS) == collections.Counter(photosdb.album_names)
assert "Pumpkin Farm" in photosdb.albums
assert collections.Counter(ALBUMS) == collections.Counter(photosdb.albums)
def test_keywords_dict():

View File

@@ -24,11 +24,7 @@ ALBUM_FOLDER_NAMES_DICT = {
"Test Album": [],
}
ALBUM_LEN_DICT = {
"Pumpkin Farm": 3,
"AlbumInFolder": 2,
"Test Album": 1,
}
ALBUM_LEN_DICT = {"Pumpkin Farm": 3, "AlbumInFolder": 2, "Test Album": 1}
ALBUM_PHOTO_UUID_DICT = {
"Pumpkin Farm": [
@@ -46,7 +42,8 @@ ALBUM_PHOTO_UUID_DICT = {
],
}
######### Test FolderInfo ##########
######### Test FolderInfo ##########
def test_folders_1():
import osxphotos
@@ -54,20 +51,21 @@ def test_folders_1():
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
# top level folders
folders = photosdb.folders
folders = photosdb.folder_info
assert len(folders) == 1
# check folder names
folder_names = [f.title for f in folders]
assert sorted(folder_names) == sorted(TOP_LEVEL_FOLDERS)
def test_folder_names():
import osxphotos
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
# check folder names
folder_names = photosdb.folder_names
folder_names = photosdb.folders
assert sorted(folder_names) == sorted(TOP_LEVEL_FOLDERS)
@@ -77,7 +75,7 @@ def test_folders_len():
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
# top level folders
folders = photosdb.folders
folders = photosdb.folder_info
assert len(folders[0]) == len(TOP_LEVEL_CHILDREN)
@@ -87,14 +85,14 @@ def test_folders_children():
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
# top level folders
folders = photosdb.folders
folders = photosdb.folder_info
# children of top level folder
children = folders[0].folders
children = folders[0].subfolders
children_names = [f.title for f in children]
assert sorted(children_names) == sorted(TOP_LEVEL_CHILDREN)
for child in folders[0].folders:
for child in folders[0].subfolders:
# check valid children FolderInfo
assert child.parent
assert child.parent.uuid == folders[0].uuid
@@ -110,12 +108,12 @@ def test_folders_parent():
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
# top level folders
folders = photosdb.folders
folders = photosdb.folder_info
# parent of top level folder should be none
for folder in folders:
assert folder.parent is None
for child in folder.folders:
for child in folder.subfolders:
# children's parent uuid should match folder uuid
assert child.parent
assert child.parent.uuid == folder.uuid
@@ -127,25 +125,27 @@ def test_folders_albums():
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
# top level folders
folders = photosdb.folders
folders = photosdb.folder_info
for folder in folders:
name = folder.title
albums = [a.title for a in folder.albums]
albums = [a.title for a in folder.album_info]
assert sorted(albums) == sorted(FOLDER_ALBUM_DICT[name])
for child in folder.folders:
for child in folder.subfolders:
name = child.title
albums = [a.title for a in child.albums]
albums = [a.title for a in child.album_info]
assert sorted(albums) == sorted(FOLDER_ALBUM_DICT[name])
########## Test AlbumInfo ##########
########## Test AlbumInfo ##########
def test_albums_1():
import osxphotos
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
albums = photosdb.albums
albums = photosdb.album_info
assert len(albums) == 4
# check names
@@ -158,7 +158,7 @@ def test_albums_parent():
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
albums = photosdb.albums
albums = photosdb.album_info
for album in albums:
parent = album.parent.title if album.parent else None
@@ -170,7 +170,7 @@ def test_albums_folder_names():
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
albums = photosdb.albums
albums = photosdb.album_info
for album in albums:
folder_names = album.folder_names
@@ -182,7 +182,7 @@ def test_albums_folders():
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
albums = photosdb.albums
albums = photosdb.album_info
for album in albums:
folders = album.folder_list
folder_names = [f.title for f in folders]
@@ -194,7 +194,7 @@ def test_albums_len():
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
albums = photosdb.albums
albums = photosdb.album_info
for album in albums:
assert len(album) == ALBUM_LEN_DICT[album.title]
@@ -205,7 +205,7 @@ def test_albums_photos():
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
albums = photosdb.albums
albums = photosdb.album_info
for album in albums:
photos = album.photos
@@ -213,5 +213,3 @@ def test_albums_photos():
assert len(photos) == len(album)
for photo in photos:
assert photo.uuid in ALBUM_PHOTO_UUID_DICT[album.title]

View File

@@ -33,7 +33,11 @@ ALBUM_LEN_DICT = {
}
ALBUM_PHOTO_UUID_DICT = {
"Pumpkin Farm": ["HrK3ZQdlQ7qpDA0FgOYXLA","15uNd7%8RguTEgNPKHfTWw","8SOE9s0XQVGsuq4ONohTng"],
"Pumpkin Farm": [
"HrK3ZQdlQ7qpDA0FgOYXLA",
"15uNd7%8RguTEgNPKHfTWw",
"8SOE9s0XQVGsuq4ONohTng",
],
"Test Album": ["8SOE9s0XQVGsuq4ONohTng"],
"Test Album (1)": ["15uNd7%8RguTEgNPKHfTWw"],
# "AlbumInFolder": [
@@ -62,17 +66,19 @@ def test_folders_1(caplog):
# folder_names = [f.title for f in folders]
# assert sorted(folder_names) == sorted(TOP_LEVEL_FOLDERS)
def test_folder_names(caplog):
import osxphotos
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
# check folder names
folder_names = photosdb.folder_names
folder_names = photosdb.folders
assert folder_names == []
assert "Folders not yet implemented for this DB version" in caplog.text
# assert sorted(folder_names) == sorted(TOP_LEVEL_FOLDERS)
@pytest.mark.skip(reason="Folders not yet impleted in Photos < 5")
def test_folders_len():
import osxphotos
@@ -137,11 +143,11 @@ def test_folders_albums():
for folder in folders:
name = folder.title
albums = [a.title for a in folder.albums]
albums = [a.title for a in folder.album_info]
assert sorted(albums) == sorted(FOLDER_ALBUM_DICT[name])
for child in folder.folders:
name = child.title
albums = [a.title for a in child.albums]
albums = [a.title for a in child.album_info]
assert sorted(albums) == sorted(FOLDER_ALBUM_DICT[name])
@@ -153,7 +159,7 @@ def test_albums_1():
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
albums = photosdb.albums
albums = photosdb.album_info
assert len(albums) == 3
# check names
@@ -166,7 +172,7 @@ def test_albums_parent(caplog):
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
albums = photosdb.albums
albums = photosdb.album_info
for album in albums:
parent = album.parent.title if album.parent else None
@@ -179,7 +185,7 @@ def test_albums_folder_names(caplog):
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
albums = photosdb.albums
albums = photosdb.album_info
for album in albums:
folder_names = album.folder_names
@@ -192,7 +198,7 @@ def test_albums_folders(caplog):
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
albums = photosdb.albums
albums = photosdb.album_info
for album in albums:
folders = album.folder_list
@@ -206,7 +212,7 @@ def test_albums_len():
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
albums = photosdb.albums
albums = photosdb.album_info
for album in albums:
assert len(album) == ALBUM_LEN_DICT[album.title]
@@ -217,7 +223,7 @@ def test_albums_photos():
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
albums = photosdb.albums
albums = photosdb.album_info
for album in albums:
photos = album.photos

View File

@@ -159,8 +159,8 @@ def test_album_names():
import collections
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
assert "Pumpkin Farm" in photosdb.album_names
assert collections.Counter(ALBUMS) == collections.Counter(photosdb.album_names)
assert "Pumpkin Farm" in photosdb.albums
assert collections.Counter(ALBUMS) == collections.Counter(photosdb.albums)
def test_keywords_dict():

View File

@@ -157,8 +157,8 @@ def test_album_names():
import collections
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
assert "Pumpkin Farm" in photosdb.album_names
assert collections.Counter(ALBUMS) == collections.Counter(photosdb.album_names)
assert "Pumpkin Farm" in photosdb.albums
assert collections.Counter(ALBUMS) == collections.Counter(photosdb.albums)
def test_keywords_dict():

View File

@@ -57,7 +57,7 @@ def test_album_names():
import collections
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
assert photosdb.album_names == []
assert photosdb.albums == []
def test_keywords_dict():

View File

@@ -76,8 +76,8 @@ def test_album_names():
import collections
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
assert "Pumpkin Farm" in photosdb.album_names
assert collections.Counter(ALBUMS) == collections.Counter(photosdb.album_names)
assert "Pumpkin Farm" in photosdb.albums
assert collections.Counter(ALBUMS) == collections.Counter(photosdb.albums)
def test_keywords_dict():

View File

@@ -76,8 +76,8 @@ def test_album_names():
import collections
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
assert "Pumpkin Farm" in photosdb.album_names
assert collections.Counter(ALBUMS) == collections.Counter(photosdb.album_names)
assert "Pumpkin Farm" in photosdb.albums
assert collections.Counter(ALBUMS) == collections.Counter(photosdb.albums)
def test_keywords_dict():

View File

@@ -84,8 +84,8 @@ def test_album_names():
import collections
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
assert "Pumpkin Farm" in photosdb.album_names
assert collections.Counter(ALBUMS) == collections.Counter(photosdb.album_names)
assert "Pumpkin Farm" in photosdb.albums
assert collections.Counter(ALBUMS) == collections.Counter(photosdb.albums)
def test_keywords_dict():

View File

@@ -30,7 +30,7 @@ def test_album_names():
import osxphotos
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
albums = photosdb.album_names
albums = photosdb.albums
assert len(albums) == 1
assert albums[0] == ALBUMS[0]
@@ -40,7 +40,7 @@ def test_albums_shared():
import osxphotos
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
albums_shared = photosdb.album_names_shared
albums_shared = photosdb.albums_shared
assert len(albums_shared) == 1
assert albums_shared[0] == ALBUMS_SHARED[0]

View File

@@ -15,7 +15,7 @@ def test_album_names():
import osxphotos
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
albums = photosdb.album_names
albums = photosdb.albums
assert len(albums) == len(ALBUMS)
for album in albums:
@@ -26,7 +26,7 @@ def test_albums_names_shared():
import osxphotos
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
albums_shared = photosdb.album_names_shared
albums_shared = photosdb.albums_shared
assert len(albums_shared) == 0