Added OSXPhotosDB.get_db_connection()

This commit is contained in:
Rhet Turnbull
2020-06-14 12:52:23 -07:00
parent 00bc50490e
commit 43d28e78f3
4 changed files with 57 additions and 8 deletions

View File

@@ -829,6 +829,23 @@ photosdb.db_version
Returns the version number for Photos library database. You likely won't need this but it's provided in case needed for debugging. PhotosDB will print a warning to `sys.stderr` if you open a database version that has not been tested. Returns the version number for Photos library database. You likely won't need this but it's provided in case needed for debugging. PhotosDB will print a warning to `sys.stderr` if you open a database version that has not been tested.
#### `get_db_connection()`
Returns tuple of (connection, cursor) for the working copy of the Photos database. This is useful for debugging or prototyping new features.
```python
photosdb = osxphotos.PhotosDB()
conn, cursor = photosdb.get_db_connection()
results = conn.execute(
"SELECT ZUUID FROM ZGENERICASSET WHERE ZFAVORITE = 1;"
).fetchall()
for row in results:
# do something
pass
conn.close()
```
#### ` photos(keywords=None, uuid=None, persons=None, albums=None, images=True, movies=False, from_date=None, to_date=None)` #### ` photos(keywords=None, uuid=None, persons=None, albums=None, images=True, movies=False, from_date=None, to_date=None)`

View File

@@ -1,3 +1,3 @@
""" version info """ """ version info """
__version__ = "0.29.19" __version__ = "0.29.20"

View File

@@ -46,8 +46,6 @@ from ..utils import (
# TODO: Add test for imageTimeZoneOffsetSeconds = None # TODO: Add test for imageTimeZoneOffsetSeconds = None
# TODO: Fix command line so multiple --keyword, etc. are AND (instead of OR as they are in .photos())
# Or fix the help text to match behavior
# TODO: Add test for __str__ # TODO: Add test for __str__
# TODO: Add special albums and magic albums # TODO: Add special albums and magic albums
@@ -255,7 +253,7 @@ class PhotosDB:
if _db_is_locked(self._dbfile): if _db_is_locked(self._dbfile):
self._tmp_db = self._copy_db_file(self._dbfile) self._tmp_db = self._copy_db_file(self._dbfile)
self._db_version = self._get_db_version() self._db_version = self._get_db_version(self._tmp_db)
# If Photos >= 5, actual data isn't in photos.db but in Photos.sqlite # If Photos >= 5, actual data isn't in photos.db but in Photos.sqlite
if int(self._db_version) > int(_PHOTOS_4_VERSION): if int(self._db_version) > int(_PHOTOS_4_VERSION):
@@ -493,6 +491,14 @@ class PhotosDB:
""" returns path to the Photos library PhotosDB was initialized with """ """ returns path to the Photos library PhotosDB was initialized with """
return self._library_path return self._library_path
def get_db_connection(self):
""" Get connection to the working copy of the Photos database
Returns:
tuple of (connection, cursor) to sqlite3 database
"""
return _open_sql_file(self._tmp_db)
def _copy_db_file(self, fname): def _copy_db_file(self, fname):
""" copies the sqlite database file to a temp file """ """ copies the sqlite database file to a temp file """
""" returns the name of the temp file """ """ returns the name of the temp file """
@@ -517,12 +523,18 @@ class PhotosDB:
return dest_path return dest_path
def _get_db_version(self): def _get_db_version(self, db_file):
""" gets the Photos DB version from LiGlobals table """ """ Gets the Photos DB version from LiGlobals table
""" returns the version as str"""
Args:
db_file: path to database file containing LiGlobals table
Returns: version as str
"""
version = None version = None
(conn, c) = _open_sql_file(self._tmp_db) (conn, c) = _open_sql_file(db_file)
# get database version # get database version
c.execute( c.execute(

View File

@@ -439,6 +439,26 @@ def test_get_library_path():
assert lib_path.endswith(PHOTOS_LIBRARY_PATH) assert lib_path.endswith(PHOTOS_LIBRARY_PATH)
def test_get_db_connection():
""" Test PhotosDB.get_db_connection """
import osxphotos
import sqlite3
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
conn, cursor = photosdb.get_db_connection()
assert isinstance(conn, sqlite3.Connection)
assert isinstance(cursor, sqlite3.Cursor)
results = conn.execute(
"SELECT ZUUID FROM ZGENERICASSET WHERE ZFAVORITE = 1;"
).fetchall()
assert len(results) == 1
assert results[0][0] == "E9BC5C36-7CD1-40A1-A72B-8B8FAC227D51" # uuid
conn.close()
def test_export_1(): def test_export_1():
# test basic export # test basic export
# get an unedited image and export it using default filename # get an unedited image and export it using default filename