Added grep command to CLI
This commit is contained in:
@@ -20,7 +20,7 @@ import osxmetadata
|
|||||||
import photoscript
|
import photoscript
|
||||||
import rich.traceback
|
import rich.traceback
|
||||||
import yaml
|
import yaml
|
||||||
from rich import pretty
|
from rich import pretty, print
|
||||||
|
|
||||||
import osxphotos
|
import osxphotos
|
||||||
|
|
||||||
@@ -60,9 +60,11 @@ from .photoexporter import ExportResults, PhotoExporter
|
|||||||
from .photoinfo import PhotoInfo
|
from .photoinfo import PhotoInfo
|
||||||
from .photokit import check_photokit_authorization, request_photokit_authorization
|
from .photokit import check_photokit_authorization, request_photokit_authorization
|
||||||
from .photosalbum import PhotosAlbum
|
from .photosalbum import PhotosAlbum
|
||||||
|
from .photosdb.photosdb_utils import get_photos_library_version
|
||||||
from .phototemplate import PhotoTemplate, RenderOptions
|
from .phototemplate import PhotoTemplate, RenderOptions
|
||||||
from .pyrepl import embed_repl
|
from .pyrepl import embed_repl
|
||||||
from .queryoptions import QueryOptions
|
from .queryoptions import QueryOptions
|
||||||
|
from .sqlgrep import sqlgrep
|
||||||
from .uti import get_preferred_uti_extension
|
from .uti import get_preferred_uti_extension
|
||||||
from .utils import expand_and_validate_filepath, load_function, normalize_fs_path
|
from .utils import expand_and_validate_filepath, load_function, normalize_fs_path
|
||||||
|
|
||||||
@@ -4283,3 +4285,49 @@ def repl(ctx, cli_obj, db, emacs):
|
|||||||
quit_words=["q", "quit", "exit"],
|
quit_words=["q", "quit", "exit"],
|
||||||
vi_mode=not emacs,
|
vi_mode=not emacs,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@cli.command(hidden=True)
|
||||||
|
@DB_OPTION
|
||||||
|
@click.pass_obj
|
||||||
|
@click.pass_context
|
||||||
|
@click.option(
|
||||||
|
"--ignore-case",
|
||||||
|
"-i",
|
||||||
|
required=False,
|
||||||
|
is_flag=True,
|
||||||
|
default=False,
|
||||||
|
help="Ignore case when searching (default is case-sensitive)",
|
||||||
|
)
|
||||||
|
@click.option(
|
||||||
|
"--print-filename",
|
||||||
|
"-p",
|
||||||
|
required=False,
|
||||||
|
is_flag=True,
|
||||||
|
default=False,
|
||||||
|
help="Print name of database file when printing results",
|
||||||
|
)
|
||||||
|
@click.argument("pattern", metavar="PATTERN", required=True)
|
||||||
|
def grep(ctx, cli_obj, db, ignore_case, print_filename, pattern):
|
||||||
|
"""Search for PATTERN in the Photos sqlite database file"""
|
||||||
|
db = db or get_photos_db()
|
||||||
|
db = pathlib.Path(db)
|
||||||
|
if db.is_file():
|
||||||
|
# if passed the actual database, really want the parent of the database directory
|
||||||
|
db = db.parent.parent
|
||||||
|
photos_ver = get_photos_library_version(str(db))
|
||||||
|
if photos_ver < 5:
|
||||||
|
db_file = db / "database" / "photos.db"
|
||||||
|
else:
|
||||||
|
db_file = db / "database" / "Photos.sqlite"
|
||||||
|
|
||||||
|
if not db_file.is_file():
|
||||||
|
click.secho(f"Could not find database file {db_file}", fg="red")
|
||||||
|
ctx.exit(2)
|
||||||
|
|
||||||
|
db_file = str(db_file)
|
||||||
|
|
||||||
|
for table, column, row_id, value in sqlgrep(
|
||||||
|
db_file, pattern, ignore_case, print_filename, rich_markup=True
|
||||||
|
):
|
||||||
|
print(", ".join([table, column, row_id, value]))
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
""" utility functions used by PhotosDB """
|
""" utility functions used by PhotosDB """
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import pathlib
|
||||||
import plistlib
|
import plistlib
|
||||||
|
|
||||||
from .._constants import (
|
from .._constants import (
|
||||||
@@ -17,7 +18,7 @@ from ..utils import _open_sql_file
|
|||||||
|
|
||||||
|
|
||||||
def get_db_version(db_file):
|
def get_db_version(db_file):
|
||||||
""" Gets the Photos DB version from LiGlobals table
|
"""Gets the Photos DB version from LiGlobals table
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
db_file: path to photos.db database file containing LiGlobals table
|
db_file: path to photos.db database file containing LiGlobals table
|
||||||
@@ -44,7 +45,7 @@ def get_db_version(db_file):
|
|||||||
|
|
||||||
|
|
||||||
def get_model_version(db_file):
|
def get_model_version(db_file):
|
||||||
""" Returns the database model version from Z_METADATA
|
"""Returns the database model version from Z_METADATA
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
db_file: path to Photos.sqlite database file containing Z_METADATA table
|
db_file: path to Photos.sqlite database file containing Z_METADATA table
|
||||||
@@ -67,7 +68,7 @@ def get_model_version(db_file):
|
|||||||
|
|
||||||
|
|
||||||
def get_db_model_version(db_file):
|
def get_db_model_version(db_file):
|
||||||
""" Returns Photos version based on model version found in db_file
|
"""Returns Photos version based on model version found in db_file
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
db_file: path to Photos.sqlite file
|
db_file: path to Photos.sqlite file
|
||||||
@@ -94,7 +95,7 @@ class UnknownLibraryVersion(Exception):
|
|||||||
|
|
||||||
|
|
||||||
def get_photos_library_version(library_path):
|
def get_photos_library_version(library_path):
|
||||||
"""Return int indicating which Photos version a library was created with """
|
"""Return int indicating which Photos version a library was created with"""
|
||||||
library_path = pathlib.Path(library_path)
|
library_path = pathlib.Path(library_path)
|
||||||
db_ver = get_db_version(str(library_path / "database" / "photos.db"))
|
db_ver = get_db_version(str(library_path / "database" / "photos.db"))
|
||||||
db_ver = int(db_ver)
|
db_ver = int(db_ver)
|
||||||
|
|||||||
Reference in New Issue
Block a user