Compare commits

...

22 Commits

Author SHA1 Message Date
Rhet Turnbull
062d8eb206 Updated CHANGELOG.md [skip ci] 2021-08-29 12:21:59 -07:00
Rhet Turnbull
f0d7496bc6 Fix for newlines in exif tags, #513 2021-08-29 12:18:20 -07:00
allcontributors[bot]
8e2b768236 docs: add dssinger as a contributor for bug (#514)
* docs: update README.md [skip ci]

* docs: update .all-contributorsrc [skip ci]

Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
2021-08-29 07:07:51 -07:00
Rhet Turnbull
48bf326994 Updated CHANGELOG.md [skip ci] 2021-08-28 09:21:07 -07:00
Rhet Turnbull
159d1102aa Added {strip} template 2021-08-28 08:14:26 -07:00
Rhet Turnbull
dbb4dbc0a7 Fixed --strip behavior, #511 2021-08-28 08:01:08 -07:00
Rhet Turnbull
777e768243 Added selected and quit to repl 2021-08-28 07:23:17 -07:00
Rhet Turnbull
70999a70b8 Updated tutorial template 2021-08-27 23:52:14 -07:00
Rhet Turnbull
3a6b2c2c35 Update test_cli.py 2021-08-23 18:36:35 -07:00
Rhet Turnbull
dfb80ba8d6 Update test_cli.py 2021-08-23 18:30:34 -07:00
Rhet Turnbull
94b818b156 Update test_cli.py 2021-08-23 18:09:36 -07:00
Rhet Turnbull
f1cea1498b Update test for #506 2021-08-23 17:57:28 -07:00
Rhet Turnbull
345678577a Updated test for #506 2021-08-23 17:29:38 -07:00
Rhet Turnbull
fb4138cfe6 Updated README [skip ci] 2021-08-23 14:25:13 -07:00
Rhet Turnbull
db5b34d589 Fix for #506 2021-08-23 14:23:39 -07:00
Rhet Turnbull
8963af9229 Updated CHANGELOG.md [skip ci] 2021-08-15 14:14:51 -07:00
Rhet Turnbull
2041789ff4 Updated README.md [skip ci] 2021-08-15 14:12:15 -07:00
Rhet Turnbull
aec86f93ea Added inspect() to repl, closes #501 2021-08-15 13:50:37 -07:00
Rhet Turnbull
57bfb03e05 Updated CHANGELOG.md [skip ci] 2021-08-02 05:55:19 -07:00
Rhet Turnbull
c2b2476e38 Updated docs for Text Detection [skip ci] 2021-08-02 05:52:48 -07:00
Rhet Turnbull
fa2027d453 Improved caching of detected_text results 2021-08-02 05:10:26 -07:00
Rhet Turnbull
9d980e4917 Updated CHANGELOG.md [skip ci] 2021-07-29 21:27:51 -07:00
18 changed files with 433 additions and 106 deletions

View File

@@ -241,6 +241,15 @@
"contributions": [
"data"
]
},
{
"login": "dssinger",
"name": "David Singer",
"avatar_url": "https://avatars.githubusercontent.com/u/1817903?v=4",
"profile": "https://github.com/dssinger",
"contributions": [
"bug"
]
}
],
"contributorsPerLine": 7,

View File

@@ -4,6 +4,50 @@ All notable changes to this project will be documented in this file. Dates are d
Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
#### [v0.42.78](https://github.com/RhetTbull/osxphotos/compare/v0.42.77...v0.42.78)
> 29 August 2021
- docs: add dssinger as a contributor for bug [`#514`](https://github.com/RhetTbull/osxphotos/pull/514)
- Fix for newlines in exif tags, #513 [`f0d7496`](https://github.com/RhetTbull/osxphotos/commit/f0d7496bc66aae291337efc570a2e2c4b9b5529c)
#### [v0.42.77](https://github.com/RhetTbull/osxphotos/compare/v0.42.74...v0.42.77)
> 28 August 2021
- Fixed --strip behavior, #511 [`dbb4dbc`](https://github.com/RhetTbull/osxphotos/commit/dbb4dbc0a7f7cb590ab3b2ce532c5c618c7fc249)
- Update test for #506 [`f1cea14`](https://github.com/RhetTbull/osxphotos/commit/f1cea1498b3b973aa500d874126b9668a8743f1f)
- Added {strip} template [`159d110`](https://github.com/RhetTbull/osxphotos/commit/159d1102aabd56def2caf6754747f7a4caa7d374)
#### [v0.42.74](https://github.com/RhetTbull/osxphotos/compare/v0.42.73...v0.42.74)
> 23 August 2021
- Fix for #506 [`db5b34d`](https://github.com/RhetTbull/osxphotos/commit/db5b34d58950c65f95d22a0e81390b9d4fb7ccd7)
- Updated README [skip ci] [`fb4138c`](https://github.com/RhetTbull/osxphotos/commit/fb4138cfe6cfad02fead821b70b4b84d11b027e9)
#### [v0.42.73](https://github.com/RhetTbull/osxphotos/compare/v0.42.72...v0.42.73)
> 15 August 2021
- Added inspect() to repl, closes #501 [`#501`](https://github.com/RhetTbull/osxphotos/issues/501)
- Updated docs for Text Detection [skip ci] [`c2b2476`](https://github.com/RhetTbull/osxphotos/commit/c2b2476e385fcd3773bd8abb942e788be2af8169)
- Updated README.md [skip ci] [`2041789`](https://github.com/RhetTbull/osxphotos/commit/2041789ff4a3979a73712b27a51a77e8a880efb8)
#### [v0.42.72](https://github.com/RhetTbull/osxphotos/compare/v0.42.71...v0.42.72)
> 2 August 2021
- Improved caching of detected_text results [`fa2027d`](https://github.com/RhetTbull/osxphotos/commit/fa2027d45308738d2335d4b5a72c3ef5c478491a)
#### [v0.42.71](https://github.com/RhetTbull/osxphotos/compare/v0.42.70...v0.42.71)
> 29 July 2021
- Updated text_detection to detect macOS version [`7376223`](https://github.com/RhetTbull/osxphotos/commit/7376223eb87a4919fd54cc685a3f263e83626879)
- Updated detected_text docs to make it clear this only works on Catalina+ [`ecd0b8e`](https://github.com/RhetTbull/osxphotos/commit/ecd0b8e22f8bf1f8d1e98d64834bebf0394dd903)
- Fix for #500, check for macOS version before loading Vision [`673243c`](https://github.com/RhetTbull/osxphotos/commit/673243c6cd1c267b6b741b5429cdb63c062648d1)
#### [v0.42.70](https://github.com/RhetTbull/osxphotos/compare/v0.42.69...v0.42.70)
> 29 July 2021

View File

@@ -35,6 +35,7 @@ OSXPhotos provides the ability to interact with and query Apple's Photos.app lib
+ [Raw Photos](#raw-photos)
+ [Template System](#template-system)
+ [ExifTool](#exiftoolExifTool)
+ [Text Detection](#textdetection)
+ [Utility Functions](#utility-functions)
* [Examples](#examples)
* [Related Projects](#related-projects)
@@ -450,15 +451,15 @@ For example, to set Finder comment to the photo's title and description:
In the template string above, `{newline}` instructs osxphotos to insert a new line character ("\n") between the title and description. In this example, if `{title}` or `{descr}` is empty, you'll get "title\n" or "\ndescription" which may not be desired so you can use more advanced features of the template system to handle these cases:
`osxphotos export /path/to/export --xattr-template findercomment "{title}{title?{descr?{newline},},}{descr}"`
`osxphotos export /path/to/export --xattr-template findercomment "{title,}{title?{descr?{newline},},}{descr,}"`
Explanation of the template string:
```txt
{title}{title?{descr?{newline},},}{descr}
{title,}{title?{descr?{newline},},}{descr,}
│ │ │ │ │ │ │
│ │ │ │ │ │ │
└──> insert title │ │ │ │ │
└──> insert title (or nothing if no title)
│ │ │ │ │ │
└───> is there a title?
│ │ │ │ │
@@ -470,7 +471,8 @@ Explanation of the template string:
│ │
└───> if title is blank, insert nothing
└───> finally, insert description
└───> finally, insert description
(or nothing if no description)
```
In this example, `title?` demonstrates use of the boolean (True/False) feature of the template system. `title?` is read as "Is the title True (or not blank/empty)? If so, then the value immediately following the `?` is used in place of `title`. If `title` is blank, then the value immediately following the comma is used instead. The format for boolean fields is `field?value if true,value if false`. Either `value if true` or `value if false` may be blank, in which case a blank string ("") is used for the value and both may also be an entirely new template string as seen in the above example. Using this format, template strings may be nested inside each other to form complex `if-then-else` statements.
@@ -1700,7 +1702,7 @@ Substitution Description
{lf} A line feed: '\n', alias for {newline}
{cr} A carriage return: '\r'
{crlf} a carriage return + line feed: '\r\n'
{osxphotos_version} The osxphotos version, e.g. '0.42.71'
{osxphotos_version} The osxphotos version, e.g. '0.42.78'
{osxphotos_cmd_line} The full command line used to run osxphotos
The following substitutions may result in multiple values. Thus if specified for
@@ -1777,6 +1779,8 @@ Substitution Description
rendered TEMPLATE value(s) for safe usage in the
shell, e.g. My file.jpeg => 'My file.jpeg'; only adds
quotes if needed.
{strip} Use in form '{strip,TEMPLATE}'; strips whitespace
from begining and end of rendered TEMPLATE value(s).
{function} Execute a python function from an external file and
use return value as template substitution. Use in
format: {function:file.py::function_name} where
@@ -2788,7 +2792,8 @@ Some substitutions, notably `album`, `keyword`, and `person` could return multip
See [Template System](#template-system) for additional details.
#### `detected_text(confidence_threshold=TEXT_DETECTION_CONFIDENCE_THRESHOLD)`
#### <a name="detected_text_method">`detected_text(confidence_threshold=TEXT_DETECTION_CONFIDENCE_THRESHOLD)`</a>
Detects text in photo and returns lists of results as (detected text, confidence)
@@ -2800,6 +2805,8 @@ Returns: list of (detected text, confidence) tuples.
Note: This is *not* the same as Live Text in macOS Monterey. When using `detected_text()`, osxphotos will use Apple's [Vision framework](https://developer.apple.com/documentation/vision/recognizing_text_in_images?language=objc) to perform text detection on the image. On my circa 2013 MacBook Pro, this takes about 2 seconds per image. `detected_text()` does memoize the results for a given `confidence_threshold` so repeated calls will not re-process the photo. This works only on macOS Catalina (10.15) or later.
See also [Text Detection](#textdetection).
### ExifInfo
[PhotosInfo.exif_info](#exif-info) returns an `ExifInfo` object with some EXIF data about the photo (Photos 5 only). `ExifInfo` contains the following properties:
@@ -3554,7 +3561,7 @@ The following template field substitutions are availabe for use the templating s
|{lf}|A line feed: '\n', alias for {newline}|
|{cr}|A carriage return: '\r'|
|{crlf}|a carriage return + line feed: '\r\n'|
|{osxphotos_version}|The osxphotos version, e.g. '0.42.71'|
|{osxphotos_version}|The osxphotos version, e.g. '0.42.78'|
|{osxphotos_cmd_line}|The full command line used to run osxphotos|
|{album}|Album(s) photo is contained in|
|{folder_album}|Folder path + album photo is contained in. e.g. 'Folder/Subfolder/Album' or just 'Album' if no enclosing folder|
@@ -3571,6 +3578,7 @@ The following template field substitutions are availabe for use the templating s
|{photo}|Provides direct access to the PhotoInfo object for the photo. Must be used in format '{photo.property}' where 'property' represents a PhotoInfo property. For example: '{photo.favorite}' is the same as '{favorite}' and '{photo.place.name}' is the same as '{place.name}'. '{photo}' provides access to properties that are not available as separate template fields but it assumes some knowledge of the underlying PhotoInfo class. See https://rhettbull.github.io/osxphotos/ for additional documentation on the PhotoInfo class.|
|{detected_text}|List of text strings found in the image after performing text detection. Using '{detected_text}' will cause osxphotos to perform text detection on your photos using the built-in macOS text detection algorithms which will slow down your export. The results for each photo will be cached in the export database so that future exports with '--update' do not need to reprocess each photo. You may pass a confidence threshold value between 0.0 and 1.0 after a colon as in '{detected_text:0.5}'; The default confidence threshold is 0.75. '{detected_text}' works only on macOS Catalina (10.15) or later. Note: this feature is not the same thing as Live Text in macOS Monterey, which osxphotos does not yet support.|
|{shell_quote}|Use in form '{shell_quote,TEMPLATE}'; quotes the rendered TEMPLATE value(s) for safe usage in the shell, e.g. My file.jpeg => 'My file.jpeg'; only adds quotes if needed.|
|{strip}|Use in form '{strip,TEMPLATE}'; strips whitespace from begining and end of rendered TEMPLATE value(s).|
|{function}|Execute a python function from an external file and use return value as template substitution. Use in format: {function:file.py::function_name} where 'file.py' is the name of the python file and 'function_name' is the name of the function to call. The function will be passed the PhotoInfo object for the photo. See https://github.com/RhetTbull/osxphotos/blob/master/examples/template_function.py for an example of how to implement a template function.|
<!-- OSXPHOTOS-TEMPLATE-TABLE:END -->
@@ -3634,6 +3642,14 @@ osxphotos.exiftool also provides an `ExifToolCaching` class which caches all met
`ExifTool()` runs `exiftool` as a subprocess using the `-stay_open True` flag to keep the process running in the background. The subprocess will be cleaned up when your main script terminates. `ExifTool()` uses a singleton pattern to ensure that only one instance of `exiftool` is created. Multiple instances of `ExifTool()` will all use the same `exiftool` subprocess.
### <a name="textdetection">Text Detection</a>
The [PhotoInfo.detected_text()](#detected_text_method) and the `{detected_text}` template will perform text detection on the photos in your library. Text detection is a slow process so to avoid unnecessary re-processing of photos, osxphotos will cache the results of the text detection process as an extended attribute on the photo image file. Extended attributes do not modify the actual file. The extended attribute is named `osxphotos.metadata:detected_text` and can be viewed using the built-in [xattr](https://ss64.com/osx/xattr.html) command or my [osxmetadata](https://github.com/RhetTbull/osxmetadata) tool. If you want to remove the cached attribute, you can do so with osxmetadata as follows:
`osxmetadata --clear osxphotos.metadata:detected_text --walk ~/Pictures/Photos\ Library.photoslibrary/`
### Utility Functions
The following functions are located in osxphotos.utils
@@ -3773,6 +3789,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center"><a href="https://github.com/kaduskj"><img src="https://avatars.githubusercontent.com/u/983067?v=4?s=75" width="75px;" alt=""/><br /><sub><b>kaduskj</b></sub></a><br /><a href="https://github.com/RhetTbull/osxphotos/issues?q=author%3Akaduskj" title="Bug reports">🐛</a></td>
<td align="center"><a href="https://github.com/mkirkland4874"><img src="https://avatars.githubusercontent.com/u/36466711?v=4?s=75" width="75px;" alt=""/><br /><sub><b>mkirkland4874</b></sub></a><br /><a href="https://github.com/RhetTbull/osxphotos/issues?q=author%3Amkirkland4874" title="Bug reports">🐛</a> <a href="#example-mkirkland4874" title="Examples">💡</a></td>
<td align="center"><a href="https://github.com/jcommisso07"><img src="https://avatars.githubusercontent.com/u/3111054?v=4?s=75" width="75px;" alt=""/><br /><sub><b>Joseph Commisso</b></sub></a><br /><a href="#data-jcommisso07" title="Data">🔣</a></td>
<td align="center"><a href="https://github.com/dssinger"><img src="https://avatars.githubusercontent.com/u/1817903?v=4?s=75" width="75px;" alt=""/><br /><sub><b>David Singer</b></sub></a><br /><a href="https://github.com/RhetTbull/osxphotos/issues?q=author%3Adssinger" title="Bug reports">🐛</a></td>
</tr>
</table>

View File

@@ -1,3 +1,3 @@
""" version info """
__version__ = "0.42.71"
__version__ = "0.42.78"

View File

@@ -2784,9 +2784,7 @@ def _render_suffix_template(
return ""
try:
options = RenderOptions(
filename=True, strip=strip, export_dir=dest, exportdb=export_db
)
options = RenderOptions(filename=True, export_dir=dest, exportdb=export_db)
rendered_suffix, unmatched = photo.render_template(suffix_template, options)
except ValueError as e:
raise click.BadOptionUsage(
@@ -2803,6 +2801,10 @@ def _render_suffix_template(
var_name,
f"Invalid template for {option_name}: may not use multi-valued templates: '{suffix_template}': results={rendered_suffix}",
)
if strip:
rendered_suffix[0] = rendered_suffix[0].strip()
return rendered_suffix[0]
@@ -3033,7 +3035,6 @@ def get_filenames_from_template(
options = RenderOptions(
path_sep="_",
filename=True,
strip=strip,
edited_version=edited,
export_dir=export_dir,
dest_path=dest_path,
@@ -3057,7 +3058,10 @@ def get_filenames_from_template(
else [photo.filename]
)
if strip:
filenames = [filename.strip() for filename in filenames]
filenames = [sanitize_filename(filename) for filename in filenames]
return filenames
@@ -3101,7 +3105,7 @@ def get_dirnames_from_template(
# got a directory template, render it and check results are valid
try:
options = RenderOptions(
dirname=True, strip=strip, edited_version=edited, exportdb=export_db
dirname=True, edited_version=edited, exportdb=export_db
)
dirnames, unmatched = photo.render_template(directory, options)
except ValueError as e:
@@ -3116,6 +3120,8 @@ def get_dirnames_from_template(
dest_paths = []
for dirname in dirnames:
if strip:
dirname = dirname.strip()
dirname = sanitize_filepath(dirname)
dest_path = os.path.join(dest, dirname)
if not is_valid_filepath(dest_path):
@@ -3429,7 +3435,6 @@ def write_finder_tags(
options = RenderOptions(
none_str=_OSXPHOTOS_NONE_SENTINEL,
path_sep="/",
strip=strip,
export_dir=export_dir,
exportdb=export_db,
)
@@ -3451,6 +3456,9 @@ def write_finder_tags(
rendered_tags.extend(rendered)
# filter out any template values that didn't match by looking for sentinel
if strip:
rendered_tags = [value.strip() for value in rendered_tags]
rendered_tags = [
value.replace(_OSXPHOTOS_NONE_SENTINEL, "") for value in rendered_tags
]
@@ -3496,7 +3504,6 @@ def write_extended_attributes(
options = RenderOptions(
none_str=_OSXPHOTOS_NONE_SENTINEL,
path_sep="/",
strip=strip,
export_dir=export_dir,
exportdb=export_db,
)
@@ -3516,6 +3523,9 @@ def write_extended_attributes(
)
# filter out any template values that didn't match by looking for sentinel
if strip:
rendered = [value.strip() for value in rendered]
rendered = [value.replace(_OSXPHOTOS_NONE_SENTINEL, "") for value in rendered]
try:
@@ -4074,6 +4084,10 @@ def _get_selected(photosdb):
@click.pass_context
def repl(ctx, cli_obj, db):
"""Run interactive osxphotos shell"""
from osxphotos import PhotosDB, PhotoInfo, ExifTool
from rich import inspect as _inspect
pretty.install()
print(f"python version: {sys.version}")
print(f"osxphotos version: {osxphotos._version.__version__}")
@@ -4089,12 +4103,30 @@ def repl(ctx, cli_obj, db):
get_photo = photosdb.get_photo
show = _show_photo
get_selected = _get_selected(photosdb)
selected = get_selected()
def inspect(obj):
"""inspect object"""
return _inspect(obj, methods=True)
class ReprQuit:
def __repr__(self):
sys.exit(0)
def __call__(self):
sys.exit(0)
quit = ReprQuit()
q = ReprQuit()
print(f"Found {len(photos)} photos in {tictoc:0.2f} seconds")
print("The following variables are defined:")
print(f"- photosdb: PhotosDB() instance for {photosdb.library_path}")
print(
f"- photos: list of PhotoInfo objects for all photos in photosdb, including those in the trash"
f"- photos: list of PhotoInfo objects for all photos in photosdb, including those in the trash (len={len(photos)})"
)
print(
f"- selected: list of PhotoInfo objects for any photos selected in Photos (len={len(selected)})"
)
print(f"\nThe following functions may be helpful:")
print(f"- get_photo(uuid): return a PhotoInfo object for photo with uuid")
@@ -4105,5 +4137,8 @@ def repl(ctx, cli_obj, db):
print(
f"- help(object): print help text including list of methods for object; for example, help(PhotosDB)"
)
print(f"- quit(): exit this interactive shell\n")
print(
f"- inspect(object): print information about an object; for example inspect(photosdb)"
)
print(f"- q, quit, or quit(): exit this interactive shell\n")
code.interact(banner="", local=locals())

View File

@@ -7,6 +7,7 @@
pyexiftool: https://github.com/smarnach/pyexiftool which provides more functionality """
import atexit
import html
import json
import logging
import os
@@ -24,16 +25,34 @@ EXIFTOOL_STAYOPEN_EOF_LEN = len(EXIFTOOL_STAYOPEN_EOF)
EXIFTOOL_PROCESSES = []
def escape_str(s):
"""escape string for use with exiftool -E"""
if type(s) != str:
return s
s = html.escape(s)
s = s.replace("\n", "&#xa;")
s = s.replace("\t", "&#x9;")
s = s.replace("\r", "&#xd;")
return s
def unescape_str(s):
"""unescape an HTML string returned by exiftool -E"""
if type(s) != str:
return s
return html.unescape(s)
@atexit.register
def terminate_exiftool():
"""Terminate any running ExifTool subprocesses; call this to cleanup when done using ExifTool """
"""Terminate any running ExifTool subprocesses; call this to cleanup when done using ExifTool"""
for proc in EXIFTOOL_PROCESSES:
proc._stop_proc()
@lru_cache(maxsize=1)
def get_exiftool_path():
""" return path of exiftool, cache result """
"""return path of exiftool, cache result"""
exiftool_path = shutil.which("exiftool")
if exiftool_path:
return exiftool_path.rstrip()
@@ -49,7 +68,7 @@ class _ExifToolProc:
Creates a singleton object"""
def __new__(cls, *args, **kwargs):
""" create new object or return instance of already created singleton """
"""create new object or return instance of already created singleton"""
if not hasattr(cls, "instance") or not cls.instance:
cls.instance = super().__new__(cls)
@@ -74,7 +93,7 @@ class _ExifToolProc:
@property
def process(self):
""" return the exiftool subprocess """
"""return the exiftool subprocess"""
if self._process_running:
return self._process
else:
@@ -83,16 +102,16 @@ class _ExifToolProc:
@property
def pid(self):
""" return process id (PID) of the exiftool process """
"""return process id (PID) of the exiftool process"""
return self._process.pid
@property
def exiftool(self):
""" return path to exiftool process """
"""return path to exiftool process"""
return self._exiftool
def _start_proc(self):
""" start exiftool in batch mode """
"""start exiftool in batch mode"""
if self._process_running:
logging.warning("exiftool already running: {self._process}")
@@ -110,6 +129,7 @@ class _ExifToolProc:
"-n", # no print conversion (e.g. print tag values in machine readable format)
"-P", # Preserve file modification date/time
"-G", # print group name for each tag
"-E", # escape tag values for HTML (allows use of HTML &#xa; for newlines)
],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
@@ -120,7 +140,7 @@ class _ExifToolProc:
EXIFTOOL_PROCESSES.append(self)
def _stop_proc(self):
""" stop the exiftool process if it's running, otherwise, do nothing """
"""stop the exiftool process if it's running, otherwise, do nothing"""
if not self._process_running:
return
@@ -143,7 +163,7 @@ class _ExifToolProc:
class ExifTool:
""" Basic exiftool interface for reading and writing EXIF tags """
"""Basic exiftool interface for reading and writing EXIF tags"""
def __init__(self, filepath, exiftool=None, overwrite=True, flags=None):
"""Create ExifTool object
@@ -189,6 +209,7 @@ class ExifTool:
if value is None:
value = ""
value = escape_str(value)
command = [f"-{tag}={value}"]
if self.overwrite and not self._context_mgr:
command.append("-overwrite_original")
@@ -233,6 +254,7 @@ class ExifTool:
for value in values:
if value is None:
raise ValueError("Can't add None value to tag")
value = escape_str(value)
command.append(f"-{tag}+={value}")
if self.overwrite and not self._context_mgr:
@@ -315,12 +337,12 @@ class ExifTool:
@property
def pid(self):
""" return process id (PID) of the exiftool process """
"""return process id (PID) of the exiftool process"""
return self._process.pid
@property
def version(self):
""" returns exiftool version """
"""returns exiftool version"""
ver, _, _ = self.run_commands("-ver", no_file=True)
return ver.decode("utf-8")
@@ -335,6 +357,7 @@ class ExifTool:
json_str, _, _ = self.run_commands("-json")
if not json_str:
return dict()
json_str = unescape_str(json_str.decode("utf-8"))
try:
exifdict = json.loads(json_str)
@@ -342,7 +365,6 @@ class ExifTool:
# will fail with some commands, e.g --ext AVI which produces
# 'No file with specified extension' instead of json
return dict()
exifdict = exifdict[0]
if not tag_groups:
# strip tag groups
@@ -358,12 +380,13 @@ class ExifTool:
return exifdict
def json(self):
""" returns JSON string containing all EXIF tags and values from exiftool """
"""returns JSON string containing all EXIF tags and values from exiftool"""
json, _, _ = self.run_commands("-json")
json = unescape_str(json.decode("utf-8"))
return json
def _read_exif(self):
""" read exif data from file """
"""read exif data from file"""
data = self.asdict()
self.data = {k: v for k, v in data.items()}
@@ -384,15 +407,15 @@ class ExifTool:
class ExifToolCaching(ExifTool):
""" Basic exiftool interface for reading and writing EXIF tags, with caching.
Use this only when you know the file's EXIF data will not be changed by any external process.
Creates a singleton cached ExifTool instance """
"""Basic exiftool interface for reading and writing EXIF tags, with caching.
Use this only when you know the file's EXIF data will not be changed by any external process.
Creates a singleton cached ExifTool instance"""
_singletons = {}
def __new__(cls, filepath, exiftool=None):
""" create new object or return instance of already created singleton """
"""create new object or return instance of already created singleton"""
if filepath not in cls._singletons:
cls._singletons[filepath] = _ExifToolCaching(filepath, exiftool=exiftool)
return cls._singletons[filepath]
@@ -448,7 +471,6 @@ class _ExifToolCaching(ExifTool):
return self._asdict_cache[tag_groups][normalized]
def flush_cache(self):
""" Clear cached data so that calls to json or asdict return fresh data """
"""Clear cached data so that calls to json or asdict return fresh data"""
self._json_cache = None
self._asdict_cache = {}

View File

@@ -530,6 +530,7 @@ def export2(
preview=False,
preview_suffix=DEFAULT_PREVIEW_SUFFIX,
render_options: Optional[RenderOptions] = None,
strip=False,
):
"""export photo, like export but with update and dry_run options
dest: must be valid destination path or exception raised
@@ -588,6 +589,7 @@ def export2(
preview: if True, also exports preview image
preview_suffix: optional string to append to end of filename for preview images
render_options: optional osxphotos.phototemplate.RenderOptions instance to specify options for rendering templates
strip: if True, strip whitespace from rendered templates
Returns: ExportResults class
ExportResults has attributes:
@@ -969,6 +971,7 @@ def export2(
persons=persons,
location=location,
replace_keywords=replace_keywords,
strip=strip,
)
sidecars.append(
(
@@ -995,6 +998,7 @@ def export2(
persons=persons,
location=location,
replace_keywords=replace_keywords,
strip=strip,
)
sidecars.append(
(
@@ -1017,6 +1021,7 @@ def export2(
persons=persons,
location=location,
replace_keywords=replace_keywords,
strip=strip,
)
sidecars.append(
(
@@ -1087,6 +1092,7 @@ def export2(
persons=persons,
location=location,
replace_keywords=replace_keywords,
strip=strip,
)
)[0]
if old_data != current_data:
@@ -1110,6 +1116,7 @@ def export2(
persons=persons,
location=location,
replace_keywords=replace_keywords,
strip=strip,
)
if warning_:
all_results.exiftool_warning.append((exported_file, warning_))
@@ -1130,6 +1137,7 @@ def export2(
persons=persons,
location=location,
replace_keywords=replace_keywords,
strip=strip,
),
)
export_db.set_stat_exif_for_file(
@@ -1155,6 +1163,7 @@ def export2(
persons=persons,
location=location,
replace_keywords=replace_keywords,
strip=strip,
)
if warning_:
all_results.exiftool_warning.append((exported_file, warning_))
@@ -1175,6 +1184,7 @@ def export2(
persons=persons,
location=location,
replace_keywords=replace_keywords,
strip=strip,
),
)
export_db.set_stat_exif_for_file(
@@ -1580,6 +1590,7 @@ def _write_exif_data(
persons=True,
location=True,
replace_keywords=False,
strip=False,
):
"""write exif data to image file at filepath
@@ -1593,6 +1604,7 @@ def _write_exif_data(
persons: if True, write person data to metadata
location: if True, write location data to metadata
replace_keywords: if True, keyword_template replaces any keywords, otherwise it's additive
strip: if True, strip any leading or trailing whitespace from rendered templates
Returns:
(warning, error) of warning and error strings if exiftool produces warnings or errors
@@ -1610,6 +1622,7 @@ def _write_exif_data(
persons=persons,
location=location,
replace_keywords=replace_keywords,
strip=strip,
)
with ExifTool(filepath, flags=flags, exiftool=self._db._exiftool_path) as exiftool:
@@ -1635,6 +1648,7 @@ def _exiftool_dict(
persons=True,
location=True,
replace_keywords=False,
strip=False,
):
"""Return dict of EXIF details for building exiftool JSON sidecar or sending commands to ExifTool.
Does not include all the EXIF fields as those are likely already in the image.
@@ -1651,6 +1665,7 @@ def _exiftool_dict(
persons: if True, include person data
location: if True, include location data
replace_keywords: if True, keyword_template replaces any keywords, otherwise it's additive
strip: if True, strip any rendered templates
Returns: dict with exiftool tags / values
@@ -1698,6 +1713,8 @@ def _exiftool_dict(
)
rendered = self.render_template(description_template, options)[0]
description = " ".join(rendered) if rendered else ""
if strip:
description = description.strip()
exif["EXIF:ImageDescription"] = description
exif["XMP:Description"] = description
exif["IPTC:Caption-Abstract"] = description
@@ -1745,6 +1762,9 @@ def _exiftool_dict(
)
rendered_keywords.extend(rendered)
if strip:
rendered_keywords = [keyword.strip() for keyword in rendered_keywords]
# filter out any template values that didn't match by looking for sentinel
rendered_keywords = [
keyword
@@ -1851,12 +1871,6 @@ def _exiftool_dict(
self.date_modified
).strftime("%Y:%m:%d %H:%M:%S")
# remove any new lines in any fields
for field, val in exif.items():
if type(val) == str:
exif[field] = val.replace("\n", " ")
elif type(val) == list:
exif[field] = [str(v).replace("\n", " ") for v in val if v is not None]
return exif
@@ -1909,6 +1923,7 @@ def _exiftool_json_sidecar(
persons=True,
location=True,
replace_keywords=False,
strip=False,
):
"""Return dict of EXIF details for building exiftool JSON sidecar or sending commands to ExifTool.
Does not include all the EXIF fields as those are likely already in the image.
@@ -1926,6 +1941,7 @@ def _exiftool_json_sidecar(
persons: if True, include person data
location: if True, include location data
replace_keywords: if True, keyword_template replaces any keywords, otherwise it's additive
strip: if True, strip whitespace from rendered templates
Returns: dict with exiftool tags / values
@@ -1965,6 +1981,7 @@ def _exiftool_json_sidecar(
persons=persons,
location=location,
replace_keywords=replace_keywords,
strip=strip,
)
if not tag_groups:
@@ -1990,6 +2007,7 @@ def _xmp_sidecar(
persons=True,
location=True,
replace_keywords=False,
strip=False,
):
"""returns string for XMP sidecar
use_albums_as_keywords: treat album names as keywords
@@ -2002,6 +2020,7 @@ def _xmp_sidecar(
persons: if True, include person data
location: if True, include location data
replace_keywords: if True, keyword_template replaces any keywords, otherwise it's additive
strip: if True, strip whitespace from rendered templates
"""
xmp_template_file = (
@@ -2019,6 +2038,8 @@ def _xmp_sidecar(
)
rendered = self.render_template(description_template, options)[0]
description = " ".join(rendered) if rendered else ""
if strip:
description = description.strip()
else:
description = self.description if self.description is not None else ""
@@ -2060,6 +2081,9 @@ def _xmp_sidecar(
)
rendered_keywords.extend(rendered)
if strip:
rendered_keywords = [keyword.strip() for keyword in rendered_keywords]
# filter out any template values that didn't match by looking for sentinel
rendered_keywords = [
keyword

View File

@@ -14,6 +14,7 @@ from datetime import timedelta, timezone
from typing import Optional
import yaml
from osxmetadata import OSXMetaData
from .._constants import (
_MOVIE_TYPE,
@@ -1118,6 +1119,28 @@ class PhotoInfo:
Returns: list of (detected text, confidence) tuples
"""
try:
return self._detected_text_cache[confidence_threshold]
except (AttributeError, KeyError) as e:
if isinstance(e, AttributeError):
self._detected_text_cache = {}
try:
detected_text = self._detected_text()
except Exception as e:
logging.warning(f"Error detecting text in photo {self.uuid}: {e}")
detected_text = []
self._detected_text_cache[confidence_threshold] = [
(text, confidence)
for text, confidence in detected_text
if confidence >= confidence_threshold
]
return self._detected_text_cache[confidence_threshold]
def _detected_text(self):
"""detect text in photo, either from cached extended attribute or by attempting text detection"""
path = (
self.path_edited if self.hasadjustments and self.path_edited else self.path
)
@@ -1125,24 +1148,12 @@ class PhotoInfo:
if not path:
return []
try:
return self._detected_text[(path, confidence_threshold)]
except (AttributeError, KeyError) as e:
if isinstance(e, AttributeError):
self._detected_text = {}
try:
detected_text = detect_text(path)
except Exception as e:
logging.warning(f"Error detecting text in photo {self.uuid} at {path}: {e}")
detected_text = []
self._detected_text[(path, confidence_threshold)] = [
(text, confidence)
for text, confidence in detected_text
if confidence >= confidence_threshold
]
return self._detected_text[(path, confidence_threshold)]
md = OSXMetaData(path)
detected_text = md.get_attribute("osxphotos_detected_text")
if detected_text is None:
detected_text = detect_text(path)
md.set_attribute("osxphotos_detected_text", detected_text)
return detected_text
@property
def _longitude(self):

View File

@@ -209,6 +209,7 @@ TEMPLATE_SUBSTITUTIONS_MULTI_VALUED = {
+ "'{detected_text}' works only on macOS Catalina (10.15) or later. "
+ "Note: this feature is not the same thing as Live Text in macOS Monterey, which osxphotos does not yet support.",
"{shell_quote}": "Use in form '{shell_quote,TEMPLATE}'; quotes the rendered TEMPLATE value(s) for safe usage in the shell, e.g. My file.jpeg => 'My file.jpeg'; only adds quotes if needed.",
"{strip}": "Use in form '{strip,TEMPLATE}'; strips whitespace from begining and end of rendered TEMPLATE value(s).",
"{function}": "Execute a python function from an external file and use return value as template substitution. "
+ "Use in format: {function:file.py::function_name} where 'file.py' is the name of the python file and 'function_name' is the name of the function to call. "
+ "The function will be passed the PhotoInfo object for the photo. "
@@ -574,7 +575,7 @@ class PhotoTemplate:
if self.expand_inplace or delim is not None:
sep = delim if delim is not None else self.inplace_sep
vals = [sep.join(sorted(vals))]
vals = [sep.join(sorted(vals))] if vals else []
for filter_ in filters:
vals = self.get_template_value_filter(filter_, vals)
@@ -1162,6 +1163,8 @@ class PhotoTemplate:
)
elif field == "shell_quote":
values = [shlex.quote(v) for v in default if v]
elif field == "strip":
values = [v.strip() for v in default]
elif field.startswith("photo"):
# provide access to PhotoInfo object
properties = field.split(".")
@@ -1445,25 +1448,8 @@ def _get_detected_text(photo, exportdb, confidence=TEXT_DETECTION_CONFIDENCE_THR
else TEXT_DETECTION_CONFIDENCE_THRESHOLD
)
detected_text = exportdb.get_detected_text_for_uuid(photo.uuid)
if detected_text is not None:
detected_text = json.loads(detected_text)
else:
path = (
photo.path_edited
if photo.hasadjustments and photo.path_edited
else photo.path
)
path = path or photo.path_derivatives[0] if photo.path_derivatives else None
if not path:
detected_text = []
else:
try:
detected_text = detect_text(path)
except Exception as e:
logging.warning(
f"Error detecting text in image {photo.uuid} at {path}: {e}"
)
return []
exportdb.set_detected_text_for_uuid(photo.uuid, json.dumps(detected_text))
# _detected_text caches the text detection results in an extended attribute
# so the first time this gets called is slow but repeated accesses are fast
detected_text = photo._detected_text()
exportdb.set_detected_text_for_uuid(photo.uuid, json.dumps(detected_text))
return [text for text, conf in detected_text if conf >= confidence]

View File

@@ -52,6 +52,9 @@ def detect_text(img_path: str) -> List:
vision_request.dealloc()
vision_handler.dealloc()
for result in results:
result[0] = str(result[0])
return results

View File

@@ -278,15 +278,15 @@ For example, to set Finder comment to the photo's title and description:
In the template string above, `{newline}` instructs osxphotos to insert a new line character ("\n") between the title and description. In this example, if `{title}` or `{descr}` is empty, you'll get "title\n" or "\ndescription" which may not be desired so you can use more advanced features of the template system to handle these cases:
`osxphotos export /path/to/export --xattr-template findercomment "{title}{title?{descr?{newline},},}{descr}"`
`osxphotos export /path/to/export --xattr-template findercomment "{title,}{title?{descr?{newline},},}{descr,}"`
Explanation of the template string:
```txt
{title}{title?{descr?{newline},},}{descr}
{title,}{title?{descr?{newline},},}{descr,}
│ │ │ │ │ │ │
│ │ │ │ │ │ │
└──> insert title │ │ │ │ │
└──> insert title (or nothing if no title)
│ │ │ │ │ │
└───> is there a title?
│ │ │ │ │
@@ -298,7 +298,8 @@ Explanation of the template string:
│ │
└───> if title is blank, insert nothing
└───> finally, insert description
└───> finally, insert description
(or nothing if no description)
```
In this example, `title?` demonstrates use of the boolean (True/False) feature of the template system. `title?` is read as "Is the title True (or not blank/empty)? If so, then the value immediately following the `?` is used in place of `title`. If `title` is blank, then the value immediately following the comma is used instead. The format for boolean fields is `field?value if true,value if false`. Either `value if true` or `value if false` may be blank, in which case a blank string ("") is used for the value and both may also be an entirely new template string as seen in the above example. Using this format, template strings may be nested inside each other to form complex `if-then-else` statements.

View File

@@ -16,7 +16,7 @@ dataclasses==0.7;python_version<'3.7'
wurlitzer==2.1.0
photoscript==0.1.4
toml==0.10.2
osxmetadata==0.99.26
osxmetadata==0.99.31
textx==2.3.0
rich==10.6.0
bitmath==1.3.3.1

View File

@@ -91,7 +91,7 @@ setup(
"wurlitzer==2.1.0",
"photoscript==0.1.4",
"toml==0.10.2",
"osxmetadata==0.99.26",
"osxmetadata==0.99.31",
"textx==2.3.0",
"rich==10.6.0",
"bitmath==1.3.3.1",

View File

@@ -1376,12 +1376,12 @@ def test_no_adjustments(photosdb):
def test_exiftool_newlines_in_description(photosdb):
"""Test that exiftool code removes newlines embedded in description, issue #393"""
"""Test that exiftool handles newlines embedded in description, issue #393"""
photo = photosdb.get_photo(UUID_DICT["description_newlines"])
exif = photo._exiftool_dict()
assert photo.description.find("\n") > 0
assert exif["EXIF:ImageDescription"].find("\n") == -1
assert exif["EXIF:ImageDescription"].find("\n") > 0
@pytest.mark.skip(SKIP_TEST, reason="Not yet implemented")

View File

@@ -793,6 +793,12 @@ UUID_DICT_FOLDER_ALBUM_SEQ = {
},
}
UUID_EMPTY_TITLE = "7783E8E6-9CAC-40F3-BE22-81FB7051C266" # IMG_3092.heic
FILENAME_EMPTY_TITLE = "IMG_3092.heic"
DESCRIPTION_TEMPLATE_EMPTY_TITLE = "{title,No Title} and {descr,No Descr}"
DESCRIPTION_VALUE_EMPTY_TITLE = "No Title and No Descr"
DESCRIPTION_TEMPLATE_TITLE_CONDITIONAL = "{title?true,false}"
DESCRIPTION_VALUE_TITLE_CONDITIONAL = "false"
def modify_file(filename):
"""appends data to a file to modify it"""
@@ -7121,3 +7127,76 @@ def test_export_album_seq():
f"{UUID_DICT_FOLDER_ALBUM_SEQ[uuid]['album']}/{UUID_DICT_FOLDER_ALBUM_SEQ[uuid]['result']}"
in files
)
@pytest.mark.skipif(exiftool is None, reason="exiftool not installed")
def test_export_description_template():
"""Test for issue #506"""
import json
import os
import os.path
import osxphotos
from osxphotos.cli import cli
from osxphotos.exiftool import ExifTool
runner = CliRunner()
cwd = os.getcwd()
# pylint: disable=not-context-manager
with runner.isolated_filesystem():
result = runner.invoke(
cli,
[
"export",
"--db",
os.path.join(cwd, PHOTOS_DB_15_7),
".",
"--sidecar=json",
f"--uuid={UUID_EMPTY_TITLE}",
"-V",
"--description-template",
DESCRIPTION_TEMPLATE_EMPTY_TITLE,
"--exiftool"
],
)
assert result.exit_code == 0
exif = ExifTool(FILENAME_EMPTY_TITLE).asdict()
assert exif["EXIF:ImageDescription"] == DESCRIPTION_VALUE_EMPTY_TITLE
def test_export_description_template_conditional():
"""Test for issue #506"""
import json
import os
import os.path
import osxphotos
from osxphotos.cli import cli
from osxphotos.exiftool import ExifTool
import json
runner = CliRunner()
cwd = os.getcwd()
# pylint: disable=not-context-manager
with runner.isolated_filesystem():
result = runner.invoke(
cli,
[
"export",
"--db",
os.path.join(cwd, PHOTOS_DB_15_7),
".",
"--sidecar=json",
f"--uuid={UUID_EMPTY_TITLE}",
"-V",
"--description-template",
DESCRIPTION_TEMPLATE_TITLE_CONDITIONAL,
"--sidecar",
"JSON"
],
)
assert result.exit_code == 0
with open(f"{FILENAME_EMPTY_TITLE}.json","r") as fp:
json_got = json.load(fp)[0]
assert json_got["EXIF:ImageDescription"] == DESCRIPTION_VALUE_TITLE_CONDITIONAL

View File

@@ -141,6 +141,44 @@ def test_setvalue_1():
assert exif.data["IPTC:Keywords"] == "test"
def test_setvalue_multiline():
# test setting a tag value with embedded newline
import os.path
import tempfile
import osxphotos.exiftool
from osxphotos.fileutil import FileUtil
tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_")
tempfile = os.path.join(tempdir.name, os.path.basename(TEST_FILE_ONE_KEYWORD))
FileUtil.copy(TEST_FILE_ONE_KEYWORD, tempfile)
exif = osxphotos.exiftool.ExifTool(tempfile)
exif.setvalue("EXIF:ImageDescription", "multi\nline")
assert not exif.error
exif._read_exif()
assert exif.data["EXIF:ImageDescription"] == "multi\nline"
def test_setvalue_non_alphanumeric_chars():
# test setting a tag value non-alphanumeric characters
import os.path
import tempfile
import osxphotos.exiftool
from osxphotos.fileutil import FileUtil
tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_")
tempfile = os.path.join(tempdir.name, os.path.basename(TEST_FILE_ONE_KEYWORD))
FileUtil.copy(TEST_FILE_ONE_KEYWORD, tempfile)
exif = osxphotos.exiftool.ExifTool(tempfile)
exif.setvalue("EXIF:ImageDescription", "<hello>{world}$bye#foo%bar")
assert not exif.error
exif._read_exif()
assert exif.data["EXIF:ImageDescription"] == "<hello>{world}$bye#foo%bar"
def test_setvalue_warning():
# test setting illegal tag value generates warning
import os.path
@@ -311,6 +349,45 @@ def test_addvalues_2():
assert sorted(exif.data["IPTC:Keywords"]) == sorted(test_multi)
def test_addvalues_non_alphanumeric_multiline():
# test setting a tag value
import os.path
import tempfile
import osxphotos.exiftool
from osxphotos.fileutil import FileUtil
tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_")
tempfile = os.path.join(tempdir.name, os.path.basename(TEST_FILE_ONE_KEYWORD))
FileUtil.copy(TEST_FILE_ONE_KEYWORD, tempfile)
exif = osxphotos.exiftool.ExifTool(tempfile)
exif.addvalues("IPTC:Keywords", "multi\nline", "<Foo>\t{bar}")
assert not exif.error
exif._read_exif()
assert sorted(exif.data["IPTC:Keywords"]) == sorted(
["wedding", "multi\nline", "<Foo>\t{bar}"]
)
def test_addvalues_unicode():
# test setting a tag value with unicode
import os.path
import tempfile
import osxphotos.exiftool
from osxphotos.fileutil import FileUtil
tempdir = tempfile.TemporaryDirectory(prefix="osxphotos_")
tempfile = os.path.join(tempdir.name, os.path.basename(TEST_FILE_ONE_KEYWORD))
FileUtil.copy(TEST_FILE_ONE_KEYWORD, tempfile)
exif = osxphotos.exiftool.ExifTool(tempfile)
exif.setvalue("IPTC:Keywords", None)
exif.addvalues("IPTC:Keywords", "ǂ", "Ƕ")
assert not exif.error
exif._read_exif()
assert sorted(exif.data["IPTC:Keywords"]) == sorted(["ǂ", "Ƕ"])
def test_singleton():
import osxphotos.exiftool
@@ -384,7 +461,7 @@ def test_str():
def test_photoinfo_exiftool():
""" test PhotoInfo.exiftool which returns ExifTool object for photo """
"""test PhotoInfo.exiftool which returns ExifTool object for photo"""
import osxphotos
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
@@ -397,7 +474,7 @@ def test_photoinfo_exiftool():
def test_photoinfo_exiftool_no_groups():
""" test PhotoInfo.exiftool which returns ExifTool object for photo without tag group names"""
"""test PhotoInfo.exiftool which returns ExifTool object for photo without tag group names"""
import osxphotos
photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB)
@@ -420,7 +497,7 @@ def test_photoinfo_exiftool_none():
def test_exiftool_terminate():
""" Test that exiftool process is terminated when exiftool.terminate() is called """
"""Test that exiftool process is terminated when exiftool.terminate() is called"""
import osxphotos.exiftool
import subprocess
@@ -435,7 +512,7 @@ def test_exiftool_terminate():
ps = subprocess.run(["ps"], capture_output=True)
stdout = ps.stdout.decode("utf-8")
assert "exiftool -stay_open" not in stdout
# verify we can create a new instance after termination
exif2 = osxphotos.exiftool.ExifTool(TEST_FILE_ONE_KEYWORD)
assert exif2.asdict()["IPTC:Keywords"] == "wedding"

View File

@@ -1346,12 +1346,12 @@ def test_no_adjustments(photosdb):
def test_exiftool_newlines_in_description(photosdb):
"""Test that exiftool code removes newlines embedded in description, issue #393"""
"""Test that exiftool handles newlines embedded in description, issue #393"""
photo = photosdb.get_photo(UUID_DICT["description_newlines"])
exif = photo._exiftool_dict()
assert photo.description.find("\n") > 0
assert exif["EXIF:ImageDescription"].find("\n") == -1
assert exif["EXIF:ImageDescription"].find("\n") > 0
@pytest.mark.skip(SKIP_TEST, reason="Not yet implemented")

View File

@@ -393,6 +393,19 @@ UUID_ALBUM_SEQ = {
},
}
UUID_EMPTY_TITLE = "7783E8E6-9CAC-40F3-BE22-81FB7051C266" # IMG_3092.heic
UUID_EMPTY_TITLE_HAS_DESCRIPTION = "E9BC5C36-7CD1-40A1-A72B-8B8FAC227D51" # wedding.jpg
TEMPLATE_VALUES_EMPTY_TITLE = {
"{title,No Title} and {descr,No Descr}": "No Title and No Descr",
"{title?true,false}": "false",
}
TEMPLATE_VALUES_EMPTY_TITLE_HAS_DESCRIPTION = {
"{title,} {descr} ": " Bride Wedding day ",
"{strip,{title,} {descr} }": "Bride Wedding day",
}
@pytest.fixture(scope="module")
def photosdb_places():
@@ -1181,11 +1194,17 @@ def test_detected_text(photosdb):
assert value in "".join(rendered)
def test_detected_text_caching(photosdb):
"""Test {detected_text} template caches values"""
exportdb = ExportDBInMemory(None)
exportdb.set_detected_text_for_uuid(UUID_DETECTED_TEXT, json.dumps([["foo", 0.9]]))
photo = photosdb.get_photo(UUID_DETECTED_TEXT)
options = RenderOptions(exportdb=exportdb)
rendered, _ = photo.render_template("{detected_text}", options=options)
assert rendered[0] == "foo"
def test_empty_title(photosdb):
"""Test for issue #506"""
photo = photosdb.get_photo(UUID_EMPTY_TITLE)
for template, value in TEMPLATE_VALUES_EMPTY_TITLE.items():
rendered, _ = photo.render_template(template)
assert value in "".join(rendered)
def test_strip(photosdb):
"""Test {strip} template"""
photo = photosdb.get_photo(UUID_EMPTY_TITLE_HAS_DESCRIPTION)
for template, value in TEMPLATE_VALUES_EMPTY_TITLE_HAS_DESCRIPTION.items():
rendered, _ = photo.render_template(template)
assert value in "".join(rendered)