Пример #1
0
def test_setvalue_context_manager():
    # test setting a tag value as context manager
    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)

    with osxphotos.exiftool.ExifTool(tempfile) as exif:
        exif.setvalue("IPTC:Keywords", "test1")
        exif.setvalue("IPTC:Keywords", "test2")
        exif.setvalue("XMP:Title", "title")
        exif.setvalue("XMP:Subject", "subject")

    assert exif.error is None

    exif2 = osxphotos.exiftool.ExifTool(tempfile)
    exif2._read_exif()
    assert sorted(exif2.data["IPTC:Keywords"]) == ["test1", "test2"]
    assert exif2.data["XMP:Title"] == "title"
    assert exif2.data["XMP:Subject"] == "subject"
Пример #2
0
def copy_photos_library(photos_library, destination=None):
    """ copy the test library, returns path to copied library """
    photoslib = photoscript.PhotosLibrary()
    photoslib.quit()
    picture_folder = (pathlib.Path(str(destination))
                      or pathlib.Path("~/Pictures").expanduser())
    if not picture_folder.is_dir():
        pytest.exit(f"Invalid picture folder: '{picture_folder}'")
    src = pathlib.Path(os.getcwd()) / f"tests/test_libraries/{photos_library}"
    dest = picture_folder / photos_library
    FileUtil.copy(src, picture_folder)
    return dest
Пример #3
0
def test_unlink_file():
    import os.path
    import tempfile
    from osxphotos.fileutil import FileUtil

    temp_dir = tempfile.TemporaryDirectory(prefix="osxphotos_")
    src = "tests/test-images/wedding.jpg"
    dest = os.path.join(temp_dir.name, "wedding.jpg")
    result = FileUtil.copy(src, temp_dir.name)
    assert os.path.isfile(dest)
    FileUtil.unlink(dest)
    assert not os.path.isfile(dest)
Пример #4
0
def test_hardlink_file_valid():
    # hardlink file with valid src, dest
    import os.path
    import tempfile
    from osxphotos.fileutil import FileUtil

    temp_dir = tempfile.TemporaryDirectory(prefix="osxphotos_")
    src = "tests/test-images/wedding.jpg"
    dest = os.path.join(temp_dir.name, "wedding.jpg")
    FileUtil.hardlink(src, dest)
    assert os.path.isfile(dest)
    assert os.path.samefile(src, dest)
Пример #5
0
def test_setvalue_context_manager_error():
    # test setting a tag value as context manager when error generated
    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)

    with osxphotos.exiftool.ExifTool(tempfile) as exif:
        exif.setvalue("Foo:Bar", "test1")
    assert exif.error
Пример #6
0
def test_setvalue_error():
    # test setting illegal tag value generates error
    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:Foo", "test")
    assert exif.error
Пример #7
0
def test_addvalues_1():
    # 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", "test")
    exif._read_exif()
    assert sorted(exif.data["IPTC:Keywords"]) == sorted(["wedding", "test"])
Пример #8
0
def test_setvalue_1():
    # 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.setvalue("IPTC:Keywords", "test")
    assert not exif.error

    exif._read_exif()
    assert exif.data["IPTC:Keywords"] == "test"
Пример #9
0
def test_clear_value():
    # test clearing 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)
    assert "IPTC:Keywords" in exif.data

    exif.setvalue("IPTC:Keywords", None)
    exif._read_exif()
    assert "IPTC:Keywords" not in exif.data
Пример #10
0
def test_copy_file_invalid():
    # copy file with invalid src
    import tempfile
    from osxphotos.fileutil import FileUtil

    temp_dir = tempfile.TemporaryDirectory(prefix="osxphotos_")
    src = "tests/test-images/wedding_DOES_NOT_EXIST.jpg"
    with pytest.raises(Exception) as e:
        assert FileUtil.copy(src, temp_dir.name)
    assert e.type == FileNotFoundError
Пример #11
0
def test_copy_file_valid():
    # copy file with valid src, dest
    import os.path
    import tempfile
    from osxphotos.fileutil import FileUtil

    temp_dir = tempfile.TemporaryDirectory(prefix="osxphotos_")
    src = "tests/test-images/wedding.jpg"
    result = FileUtil.copy(src, temp_dir.name)
    assert result == 0
    assert os.path.isfile(os.path.join(temp_dir.name, "wedding.jpg"))
Пример #12
0
def test_addvalues_2():
    # test setting a tag value where multiple values already exist
    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_MULTI_KEYWORD))
    FileUtil.copy(TEST_FILE_MULTI_KEYWORD, tempfile)

    exif = osxphotos.exiftool.ExifTool(tempfile)
    assert sorted(exif.data["IPTC:Keywords"]) == sorted(TEST_MULTI_KEYWORDS)
    exif.addvalues("IPTC:Keywords", "test")
    exif._read_exif()
    assert "IPTC:Keywords" in exif.data
    test_multi = TEST_MULTI_KEYWORDS.copy()
    test_multi.append("test")
    assert sorted(exif.data["IPTC:Keywords"]) == sorted(test_multi)
Пример #13
0
def test_convert_to_jpeg():
    """ test convert_to_jpeg """
    import pathlib
    import tempfile
    from osxphotos.fileutil import FileUtil

    temp_dir = tempfile.TemporaryDirectory(prefix="osxphotos_")
    with temp_dir:
        imgfile = pathlib.Path(TEST_HEIC)
        outfile = pathlib.Path(temp_dir.name) / f"{imgfile.stem}.jpeg"
        assert FileUtil.convert_to_jpeg(imgfile, outfile)
        assert outfile.is_file()
Пример #14
0
def test_convert_to_jpeg_quality():
    """ test convert_to_jpeg with compression_quality """
    import pathlib
    import tempfile
    from osxphotos.fileutil import FileUtil

    temp_dir = tempfile.TemporaryDirectory(prefix="osxphotos_")
    with temp_dir:
        imgfile = pathlib.Path(TEST_RAW)
        outfile = pathlib.Path(temp_dir.name) / f"{imgfile.stem}.jpeg"
        assert FileUtil.convert_to_jpeg(imgfile,
                                        outfile,
                                        compression_quality=0.1)
        assert outfile.is_file()
        assert outfile.stat().st_size < 1000000