From 46d3c7dbdaf848d5c340ce8a362ff296a36c552d Mon Sep 17 00:00:00 2001 From: Rhet Turnbull Date: Sat, 1 Feb 2020 08:16:20 -0800 Subject: [PATCH] Added PhotosDB() behavior to open last library if no args passed but also added cautionary note to README --- README.md | 3 +++ osxphotos/photosdb.py | 9 ++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fdcb721a..cbdfc76e 100644 --- a/README.md +++ b/README.md @@ -268,6 +268,7 @@ if __name__ == "__main__": #### Read a Photos library database ```python +osxphotos.PhotosDB() # not recommended, see Note below osxphotos.PhotosDB(path) osxphotos.PhotosDB(dbfile=path) ``` @@ -278,6 +279,8 @@ Pass the path to a Photos library or to a specific database file (e.g. "/Users/s If an invalid path is passed, PhotosDB will raise `ValueError` exception. +**Note**: If neither path or dbfile is passed, PhotosDB will use get_last_library_path to open the last opened Photos library. This usually works but is not 100% reliable. It can also lead to loading a different library than expected if the user has held down *option* key when opening Photos to switch libraries. It is therefore recommended you explicitely pass the path to `PhotosDB()`. + Open the default (last opened) Photos library. (E.g. this is the library that would open if the user opened Photos.app) ```python diff --git a/osxphotos/photosdb.py b/osxphotos/photosdb.py index 8a66cbe7..192f6152 100644 --- a/osxphotos/photosdb.py +++ b/osxphotos/photosdb.py @@ -165,11 +165,10 @@ class PhotosDB: dbfile_, ) elif dbfile is None: - raise TypeError( - f"photos database path must be specified as argument or named parameter dbfile", - dbfile_, - dbfile, - ) + dbfile = get_last_library_path() + if dbfile is None: + # get_last_library_path must have failed to find library + raise FileNotFoundError("Could not get path to photo library database") if os.path.isdir(dbfile): # passed a directory, assume it's a photoslibrary