Skip to content

Commit 4d6500d

Browse files
authored
Fix for unknown UTI #1643 (#1646)
1 parent c74e863 commit 4d6500d

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

osxphotos/uti.py

+27-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
""" get UTI for a given file extension and the preferred extension for a given UTI
22
3-
On macOS <= 11 (Big Sur), uses objective C CoreServices methods
4-
UTTypeCopyPreferredTagWithClass and UTTypeCreatePreferredIdentifierForTag to retrieve the
3+
On macOS <= 11 (Big Sur), uses objective C CoreServices methods
4+
UTTypeCopyPreferredTagWithClass and UTTypeCreatePreferredIdentifierForTag to retrieve the
55
UTI and the extension. These are deprecated in 10.15 (Catalina) and no longer supported on Monterey.
66
7-
On Monterey, these calls are replaced with Swift methods that I can't call from python so
8-
this code uses a cached dict of UTI values. The code first checks to see if the extension or UTI
9-
is available in the cache and if so, returns it. If not, it performs a subprocess call to `mdls` to
10-
retrieve the UTI (by creating a temp file with the correct extension) and returns the UTI. This only
11-
works for the extension -> UTI lookup. On Monterey, if there is no cached value for UTI -> extension lookup,
7+
On Monterey, these calls are replaced with Swift methods that I can't call from python so
8+
this code uses a cached dict of UTI values. The code first checks to see if the extension or UTI
9+
is available in the cache and if so, returns it. If not, it performs a subprocess call to `mdls` to
10+
retrieve the UTI (by creating a temp file with the correct extension) and returns the UTI. This only
11+
works for the extension -> UTI lookup. On Monterey, if there is no cached value for UTI -> extension lookup,
1212
returns None.
1313
1414
Outside of macOS uses only the hardcoded list of UTIs.
@@ -19,6 +19,7 @@
1919
from __future__ import annotations
2020

2121
import csv
22+
import logging
2223
import os
2324
import pathlib
2425
import re
@@ -31,6 +32,8 @@
3132
import CoreServices
3233
import objc
3334

35+
logger = logging.getLogger("osxphotos")
36+
3437
__all__ = ["get_preferred_uti_extension", "get_uti_for_extension", "get_uti_for_path"]
3538

3639
# cached values of all the UTIs (< 6 chars long) known to my Mac running macOS 10.15.7
@@ -229,6 +232,7 @@
229232
odt,org.oasis-open.opendocument.text,odt,application/vnd.oasis.opendocument.text
230233
omf,com.avid.open-media-framework,omf,None
231234
orf,com.olympus.raw-image,orf,None
235+
orf,com.olympus.or-raw-image,orf,None
232236
otc,public.opentype-collection-font,otc,None
233237
otf,public.opentype-font,otf,None
234238
otg,org.oasis-open.opendocument.graphics-template,otg,application/vnd.oasis.opendocument.graphics-template
@@ -576,10 +580,16 @@ def _get_ext_from_uti_dict(uti):
576580
return None
577581

578582

579-
def get_preferred_uti_extension(uti: str) -> str | None:
580-
"""get preferred extension for a UTI type
581-
uti: UTI str, e.g. 'public.jpeg'
582-
returns: preferred extension as str or None if cannot be determined"""
583+
def get_preferred_uti_extension(uti: str) -> str:
584+
"""Get preferred extension for a UTI type
585+
586+
Args:
587+
uti: UTI str, e.g. 'public.jpeg'
588+
589+
Returns: preferred extension as str or empty string if extension cannot be determined
590+
591+
Note: Logs a warning if extension cannot be determined.
592+
"""
583593

584594
if is_macos and (OS_VER, OS_MAJOR) <= (10, 16):
585595
# reference: https://developer.apple.com/documentation/coreservices/1442744-uttypecopypreferredtagwithclass?language=objc
@@ -595,9 +605,13 @@ def get_preferred_uti_extension(uti: str) -> str | None:
595605
if uti == "public.heic":
596606
return "heic"
597607

598-
return None
608+
logger.warning(f"Could not determine extension for UTI: {uti}")
609+
return ""
599610

600-
return _get_ext_from_uti_dict(uti)
611+
ext = _get_ext_from_uti_dict(uti) or ""
612+
if not ext:
613+
logger.warning(f"Could not determine extension for UTI: {uti}")
614+
return ext
601615

602616

603617
def get_uti_for_extension(extension: str) -> str | None:

tests/test_uti.py

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
""" test uti.py """
22

3+
import logging
4+
35
import pytest
46

57
import osxphotos.uti
@@ -20,6 +22,13 @@ def test_get_preferred_uti_extension():
2022
assert get_preferred_uti_extension(uti) == UTI_DICT[uti]
2123

2224

25+
def test_get_preferred_uti_extension_unknown(caplog):
26+
"""test get_preferred_uti_extension for unknown UTI #1643"""
27+
caplog.set_level(logging.WARNING)
28+
assert get_preferred_uti_extension("com.OSXPHOTOS.UNKNOWN.FOOBAR") == ""
29+
assert "Could not determine extension for UTI" in caplog.text
30+
31+
2332
def test_get_uti_for_extension():
2433
"""get get_uti_for_extension"""
2534
for ext in EXT_DICT:

0 commit comments

Comments
 (0)