コード例 #1
0
def test_cli_copy_from(temp_file):
    # test copy from source file
    from osxmetadata import OSXMetaData, Tag
    from osxmetadata.__main__ import cli

    TESTDIR = None
    source_file = NamedTemporaryFile(dir=TESTDIR)
    source_filename = source_file.name

    meta_source = OSXMetaData(source_filename)
    meta_source.tags = [Tag("bar")]
    meta_source.keywords = ["foo"]
    meta_source.findercomment = "Bar"
    meta_source.comment = "Foo"

    runner = CliRunner()
    result = runner.invoke(cli, ["--copyfrom", source_filename, temp_file])
    assert result.exit_code == 0
    meta = OSXMetaData(temp_file)
    assert meta.tags == [Tag("bar")]
    assert meta.keywords == ["foo"]
    assert meta.findercomment == "Bar"
    assert meta.comment == "Foo"

    source_file.close()
コード例 #2
0
def test_tags_assign(temp_file):
    """ test assigning one tag object to another """
    from osxmetadata import OSXMetaData, Tag

    # update tags
    meta = OSXMetaData(temp_file)
    tagset = [Tag("Test"), Tag("Green")]
    meta.tags = tagset
    assert meta.tags == tagset

    # create second temp file
    TESTDIR = None
    tempfile2 = NamedTemporaryFile(dir=TESTDIR)
    temp_file_2 = tempfile2.name

    meta2 = OSXMetaData(temp_file_2)
    tagset2 = [Tag("test2"), Tag("Blue")]
    meta2.tags = tagset2
    assert meta2.tags == tagset2

    meta.tags = meta2.tags
    assert meta.tags == tagset2

    # close second tempfile, first gets closed automatically
    tempfile2.close()
コード例 #3
0
def test_cli_backup_restore_2(temp_file):
    # test set during restore
    import pathlib
    from osxmetadata import OSXMetaData, ATTRIBUTES, Tag
    from osxmetadata.constants import _BACKUP_FILENAME
    from osxmetadata.__main__ import cli

    runner = CliRunner()
    result = runner.invoke(
        cli,
        [
            "--set",
            "tags",
            "Foo",
            "--set",
            "tags",
            "Bar",
            "--set",
            "comment",
            "Hello World!",
            "--list",
            temp_file,
        ],
    )
    assert result.exit_code == 0
    output = parse_cli_output(result.stdout)
    assert output["tags"] == "['Foo', 'Bar']"
    meta = OSXMetaData(temp_file)
    assert meta.get_attribute("tags") == [Tag("Foo"), Tag("Bar")]
    assert meta.get_attribute("comment") == "Hello World!"

    # test creation of backup file
    result = runner.invoke(cli, ["--backup", temp_file])
    assert result.exit_code == 0
    backup_file = pathlib.Path(pathlib.Path(temp_file).parent) / _BACKUP_FILENAME
    assert backup_file.exists()

    # clear the attributes to see if they can be restored
    meta.clear_attribute("tags")
    meta.clear_attribute("comment")
    assert meta.tags == []
    assert meta.comment == None

    result = runner.invoke(
        cli,
        [
            "--restore",
            "--append",
            "tags",
            "Flooz",
            "--set",
            "keywords",
            "FooBar",
            temp_file,
        ],
    )
    assert result.exit_code == 0
    assert meta.tags == [Tag("Foo"), Tag("Bar"), Tag("Flooz")]
    assert meta.comment == "Hello World!"
    assert meta.keywords == ["FooBar"]
コード例 #4
0
ファイル: test_cli.py プロジェクト: RhetTbull/osxmetadata
def test_cli_backup_restore_all(temp_file):
    """Test --backup/--restore with --all"""
    import pathlib

    from osxmetadata import ATTRIBUTES, OSXMetaData, Tag
    from osxmetadata.__main__ import cli
    from osxmetadata.constants import _BACKUP_FILENAME

    runner = CliRunner()
    result = runner.invoke(
        cli,
        [
            "--set",
            "tags",
            "Foo",
            "--set",
            "tags",
            "Bar",
            "--set",
            "comment",
            "Hello World!",
            "--list",
            temp_file,
        ],
    )
    assert result.exit_code == 0
    meta = OSXMetaData(temp_file)
    # set a value osxmetadata doesn't know about
    meta._attrs.set("com.foo.bar", b"FOOBAR")

    # backup
    result = runner.invoke(cli, ["--backup", "--all", temp_file])
    assert result.exit_code == 0

    # clear the attributes to see if they can be restored
    meta.clear_attribute("tags")
    meta.clear_attribute("comment")
    meta._attrs.remove("com.foo.bar")
    assert meta.tags == []
    assert meta.comment is None
    assert "com.foo.bar" not in meta._list_attributes()

    # first run restore without --all
    result = runner.invoke(cli, ["--restore", temp_file])
    assert result.exit_code == 0
    assert meta.tags == [Tag("Foo"), Tag("Bar")]
    assert meta.comment == "Hello World!"

    with pytest.raises(KeyError):
        assert meta._attrs["com.foo.bar"] == b"FOOBAR"

    # next run restore with --all
    result = runner.invoke(cli, ["--restore", "--all", temp_file])
    assert result.exit_code == 0
    assert meta.tags == [Tag("Foo"), Tag("Bar")]
    assert meta.comment == "Hello World!"
    assert meta._attrs["com.foo.bar"] == b"FOOBAR"
コード例 #5
0
def test_tag_on_directory():
    import tempfile
    from osxmetadata import OSXMetaData, Tag

    tempdir = tempfile.TemporaryDirectory(prefix="osxmetadata")

    md = OSXMetaData(tempdir.name)
    md.tags = [Tag("Red")]
    assert md.get_attribute("tags") == [Tag("Red")]

    md.tags.remove(Tag("Red"))
    assert md.get_attribute("tags") == []
コード例 #6
0
ファイル: test_cli.py プロジェクト: RhetTbull/osxmetadata
def test_cli_mirror(temp_file):
    import datetime

    from osxmetadata import OSXMetaData, Tag
    from osxmetadata.__main__ import cli

    runner = CliRunner()
    result = runner.invoke(
        cli,
        [
            "--set",
            "comment",
            "Foo",
            "--set",
            "findercomment",
            "Bar",
            "--set",
            "keywords",
            "foo",
            "--set",
            "tags",
            "bar",
            "--list",
            temp_file,
        ],
    )
    assert result.exit_code == 0
    meta = OSXMetaData(temp_file)
    assert meta.tags == [Tag("bar")]
    assert meta.keywords == ["foo"]
    assert meta.findercomment == "Bar"
    assert meta.comment == "Foo"

    result = runner.invoke(
        cli,
        [
            "--mirror",
            "keywords",
            "tags",
            "--mirror",
            "comment",
            "findercomment",
            temp_file,
        ],
    )

    assert result.exit_code == 0
    assert meta.keywords == ["bar", "foo"]
    assert meta.tags == [Tag("bar"), Tag("foo")]
    assert meta.findercomment == "Bar"
    assert meta.comment == "Bar"
コード例 #7
0
ファイル: test_cli.py プロジェクト: RhetTbull/osxmetadata
def test_cli_walk_pattern(temp_dir):
    """test --walk with --pattern"""
    import os
    import pathlib

    from osxmetadata import OSXMetaData, Tag
    from osxmetadata.__main__ import cli

    dirname = pathlib.Path(temp_dir)
    os.makedirs(dirname / "temp" / "subfolder1")
    os.makedirs(dirname / "temp" / "subfolder2")
    create_file(dirname / "temp" / "temp1.txt")
    create_file(dirname / "temp" / "subfolder1" / "sub1.txt")
    create_file(dirname / "temp" / "subfolder1" / "sub1.pdf")

    runner = CliRunner()
    result = runner.invoke(
        cli,
        ["--set", "tags", "FOO", "--walk", "--pattern", "*.pdf", temp_dir])
    assert result.exit_code == 0

    md = OSXMetaData(dirname / "temp" / "subfolder1" / "sub1.pdf")
    assert md.tags == [Tag("FOO")]

    md = OSXMetaData(dirname / "temp" / "subfolder1" / "sub1.txt")
    assert not md.tags

    md = OSXMetaData(dirname / "temp" / "subfolder2")
    assert not md.tags
コード例 #8
0
def test_cli_verbose_short_opts(temp_file):
    from osxmetadata import OSXMetaData, Tag
    from osxmetadata.__main__ import cli

    TESTDIR = None
    source_file = NamedTemporaryFile(dir=TESTDIR)
    source_filename = source_file.name

    meta_source = OSXMetaData(source_filename)
    meta_source.tags = [Tag("bar")]
    meta_source.keywords = ["foo"]
    meta_source.findercomment = "Bar"
    meta_source.comment = "Foo"

    runner = CliRunner()
    result = runner.invoke(
        cli,
        [
            "-X",
            "-s",
            "keywords",
            "test",
            "-l",
            "-g",
            "keywords",
            "-c",
            "keywords",
            "-r",
            "keywords",
            "test",
            "-u",
            "keywords",
            "foo",
            "-m",
            "keywords",
            "tags",
            "-V",
            "-B",
            "-f",
            source_filename,
            temp_file,
        ],
    )
    assert result.exit_code == 0
    output = result.output
    assert "Processing file" in output
    assert "No metadata to wipe from" in output
    assert "Copying attributes from" in output
    assert "Copying com.apple.metadata:_kMDItemUserTags" in output
    assert "Copying com.apple.metadata:kMDItemComment" in output
    assert "Copying com.apple.metadata:kMDItemKeywords" in output
    assert "Copying com.apple.metadata:kMDItemFinderComment" in output
    assert "Clearing keywords" in output
    assert "Setting keywords=test" in output
    assert "Updating keywords=foo" in output
    assert "Removing keywords" in output
    assert "Mirroring keywords tags" in output
    assert "Backing up attribute data" in output

    source_file.close()
コード例 #9
0
def test_restore(temp_file):
    from osxmetadata import OSXMetaData, Tag, kMDItemComment

    meta = OSXMetaData(temp_file)
    meta.tags = [Tag("foo"), Tag("bar")]
    meta.set_attribute(kMDItemComment, "Hello World!")
    attr_dict = meta._to_dict()
    meta.tags = []
    meta.clear_attribute(kMDItemComment)

    assert meta.tags == []
    assert meta.comment is None

    meta._restore_attributes(attr_dict)
    assert meta.tags == [Tag("foo"), Tag("bar")]
    assert meta.get_attribute(kMDItemComment) == "Hello World!"
コード例 #10
0
def test_to_json(temp_file):
    import json
    import pathlib
    from osxmetadata import OSXMetaData, Tag, __version__
    from osxmetadata.constants import FINDER_COLOR_NONE

    meta = OSXMetaData(temp_file)
    meta.tags = [Tag("foo"), Tag("bar")]
    json_ = json.loads(meta.to_json())

    assert json_["com.apple.metadata:_kMDItemUserTags"] == [
        ["foo", FINDER_COLOR_NONE],
        ["bar", FINDER_COLOR_NONE],
    ]
    assert json_["_version"] == __version__
    assert json_["_filename"] == pathlib.Path(temp_file).name
コード例 #11
0
ファイル: test_cli.py プロジェクト: RhetTbull/osxmetadata
def test_cli_files_only(temp_dir):
    """test --files-only without --walk"""
    import glob
    import os
    import pathlib

    from osxmetadata import OSXMetaData, Tag
    from osxmetadata.__main__ import cli

    dirname = pathlib.Path(temp_dir)
    os.makedirs(dirname / "temp" / "subfolder1")
    os.makedirs(dirname / "temp" / "subfolder2")
    create_file(dirname / "temp" / "temp1.txt")
    create_file(dirname / "temp" / "subfolder1" / "sub1.txt")

    files = glob.glob(str(dirname / "temp" / "*"))

    runner = CliRunner()
    result = runner.invoke(cli,
                           ["--set", "tags", "FOO", "--files-only", *files])
    assert result.exit_code == 0

    md = OSXMetaData(dirname / "temp" / "temp1.txt")
    assert md.tags == [Tag("FOO")]

    md = OSXMetaData(dirname / "temp" / "subfolder1")
    assert not md.tags

    md = OSXMetaData(dirname / "temp" / "subfolder1" / "sub1.txt")
    assert not md.tags
コード例 #12
0
def test_tags_clear(temp_file):

    from osxmetadata import OSXMetaData, Tag
    from osxmetadata.constants import kMDItemUserTags

    attribute = kMDItemUserTags

    # update tags
    meta = OSXMetaData(temp_file)
    tagset = [Tag("Test"), Tag("Green")]

    meta.set_attribute(attribute, tagset)
    tags = meta.get_attribute(attribute)
    assert tags == tagset

    meta.clear_attribute(attribute)
    tags = meta.get_attribute(attribute)
    assert tags == []
コード例 #13
0
ファイル: test_cli.py プロジェクト: RhetTbull/osxmetadata
def test_list_attributes(temp_file, attribute):
    from osxmetadata import ATTRIBUTES, OSXMetaData, Tag
    from osxmetadata.__main__ import cli
    from osxmetadata.constants import _TAGS_NAMES

    runner = CliRunner()
    result = runner.invoke(cli, [
        "--set", attribute, "Foo", "--set", attribute, "Bar", "--list",
        temp_file
    ])
    assert result.exit_code == 0
    output = parse_cli_output(result.stdout)
    attr_short_name = ATTRIBUTES[attribute].name
    assert output[attr_short_name] == "['Foo', 'Bar']"
    meta = OSXMetaData(temp_file)
    if attribute in [*_TAGS_NAMES]:
        assert meta.get_attribute(attribute) == [Tag("Foo"), Tag("Bar")]
    else:
        assert meta.get_attribute(attribute) == ["Foo", "Bar"]

    result = runner.invoke(
        cli,
        [
            "--append",
            attribute,
            "Green",
            "--remove",
            attribute,
            "Foo",
            "--list",
            temp_file,
        ],
    )
    assert result.exit_code == 0
    output = parse_cli_output(result.stdout)
    attr_short_name = ATTRIBUTES[attribute].name
    if attr_short_name == "tags":
        assert output[attr_short_name] == "['Bar', 'Green,Green']"
    else:
        assert output[attr_short_name] == "['Bar', 'Green']"
コード例 #14
0
def test_tag_factory():
    from osxmetadata import Tag, FINDER_COLOR_BLUE, FINDER_COLOR_GREEN
    from osxmetadata.findertags import tag_factory

    tag_ = tag_factory("Foo")
    assert isinstance(tag_, Tag)
    assert tag_ == Tag("Foo")

    tag_ = tag_factory("Foo,Blue")
    assert isinstance(tag_, Tag)
    assert tag_ == Tag("Foo", FINDER_COLOR_BLUE)

    tag_ = tag_factory("Foo,blue")
    assert isinstance(tag_, Tag)
    assert tag_ == Tag("Foo", FINDER_COLOR_BLUE)

    tag_ = tag_factory(f"Foo,{FINDER_COLOR_BLUE}")
    assert isinstance(tag_, Tag)
    assert tag_ == Tag("Foo", FINDER_COLOR_BLUE)

    tag_ = tag_factory("green")
    assert isinstance(tag_, Tag)
    assert tag_ == Tag("Green", FINDER_COLOR_GREEN)
コード例 #15
0
def test_findercolor_colorid(temp_file):

    from osxmetadata import OSXMetaData, Tag
    from osxmetadata.constants import _MAX_FINDER_COLOR
    from osxmetadata.findertags import get_tag_color_name

    meta = OSXMetaData(temp_file)

    # check each color combo.  Setting 0 doesn't work -- the attribute gets deleted
    for color_id in range(_MAX_FINDER_COLOR + 1, _MAX_FINDER_COLOR + 1):
        meta.findercolor = color_id
        color_got = meta.findercolor
        assert color_got == color_id
        color_name = get_tag_color_name(color_id)
        assert meta.tags == [Tag(color_name, color_id)]
コード例 #16
0
def test_tags_2(temp_file):

    from osxmetadata import OSXMetaData, Tag

    # update tags
    meta = OSXMetaData(temp_file)
    tagset = [Tag("Test"), Tag("Green")]
    meta.tags = tagset
    assert meta.tags == tagset

    assert Tag("Test") in meta.tags
    assert Tag("Foo") not in meta.tags

    meta.tags.remove(Tag("Test"))
    assert Tag("Test") not in meta.tags
コード例 #17
0
def test_cli_wipe_2(temp_file):
    # test wipe then set
    from osxmetadata import OSXMetaData, Tag
    from osxmetadata.__main__ import cli

    runner = CliRunner()
    result = runner.invoke(
        cli,
        [
            "--set",
            "comment",
            "Foo",
            "--set",
            "findercomment",
            "Bar",
            "--set",
            "keywords",
            "foo",
            "--set",
            "tags",
            "bar",
            "--list",
            temp_file,
        ],
    )
    assert result.exit_code == 0
    meta = OSXMetaData(temp_file)
    assert meta.tags == [Tag("bar")]
    assert meta.keywords == ["foo"]
    assert meta.findercomment == "Bar"
    assert meta.comment == "Foo"

    result = runner.invoke(
        cli, ["--wipe", "--set", "comment", "Hello World!", temp_file]
    )
    assert result.exit_code == 0
    assert meta.tags == []
    assert meta.keywords == []
    assert meta.findercomment is None
    assert meta.comment == "Hello World!"
コード例 #18
0
def test_tags(temp_file):

    from osxmetadata import OSXMetaData, Tag

    # update tags
    meta = OSXMetaData(temp_file)
    tagset = [Tag("Test"), Tag("Green")]
    meta.tags.extend(tagset)
    assert meta.tags == tagset

    # extend
    meta.tags.extend([Tag("Foo")])
    assert meta.tags == [Tag("Test"), Tag("Green"), Tag("Foo")]

    # add tags
    meta.tags.append(Tag("Hello"))
    assert meta.tags != tagset
    assert meta.tags == [Tag("Test"), Tag("Green"), Tag("Foo"), Tag("Hello")]

    # __iadd__
    meta.tags += [Tag("Bar")]
    assert meta.tags == [
        Tag("Test"),
        Tag("Green"),
        Tag("Foo"),
        Tag("Hello"),
        Tag("Bar"),
    ]

    # __repr__
    # TODO

    # remove tags
    meta.tags.remove(Tag("Test"))
    assert meta.tags == [Tag("Green"), Tag("Foo"), Tag("Hello"), Tag("Bar")]

    # remove tag that doesn't exist, raises KeyError
    with pytest.raises(ValueError):
        assert meta.tags.remove(Tag("FooBar"))

    # len
    assert len(meta.tags) == 4

    # clear tags
    meta.tags.clear()
    assert meta.tags == []
コード例 #19
0
def test_tags_4(temp_file):
    """ Test various list methods """
    from osxmetadata import OSXMetaData, Tag
    from osxmetadata.constants import _kMDItemUserTags

    attribute = _kMDItemUserTags

    # update tags
    meta = OSXMetaData(temp_file)
    tagset = [Tag("Test"), Tag("Green"), Tag("Foo")]

    meta.tags = tagset
    assert meta.tags == tagset
    assert meta.get_attribute(attribute) == tagset

    idx = meta.tags.index(Tag("Green"))
    assert idx == 1

    count = meta.tags.count(Tag("Test"))
    assert count == 1

    count = meta.tags.count(Tag("Bar"))
    assert count == 0

    meta.tags.sort()
    assert meta.tags == [Tag("Foo"), Tag("Green"), Tag("Test")]
    assert meta.get_attribute(attribute) == [
        Tag("Foo"), Tag("Green"), Tag("Test")
    ]

    meta.tags.sort(reverse=True)
    assert meta.tags == [Tag("Test"), Tag("Green"), Tag("Foo")]
    assert meta.get_attribute(attribute) == [
        Tag("Test"), Tag("Green"), Tag("Foo")
    ]

    # sort by key
    meta.tags.sort(key=lambda tag: len(tag))
    assert meta.tags == [Tag("Foo"), Tag("Test"), Tag("Green")]
    assert meta.get_attribute(attribute) == [
        Tag("Foo"), Tag("Test"), Tag("Green")
    ]

    # todo: reverse not working
    # meta.tags.reverse()
    # assert meta.tags == [Tag("Green"), Tag("Test"), Tag("Foo")]
    # assert meta.get_attribute(attribute) == [Tag("Green"), Tag("Test"), Tag("Foo")]

    # tag_expected = Tag("Test")
    # tag_got = meta.tags.pop(1)
    # assert tag_got == tag_expected
    # assert meta.tags == [Tag("Green"), Tag("Foo")]
    # assert meta.get_attribute(attribute) == [Tag("Green"), Tag("Foo")]

    tag_expected = Tag("Test")
    tag_got = meta.tags.pop(1)
    assert tag_got == tag_expected
    assert meta.tags == [Tag("Foo"), Tag("Green")]
    assert meta.get_attribute(attribute) == [Tag("Foo"), Tag("Green")]
コード例 #20
0
ファイル: test_cli.py プロジェクト: RhetTbull/osxmetadata
def test_cli_backup_restore(temp_file):
    import pathlib

    from osxmetadata import ATTRIBUTES, OSXMetaData, Tag
    from osxmetadata.__main__ import cli
    from osxmetadata.constants import (
        _BACKUP_FILENAME,
        FINDER_COLOR_GREEN,
        FINDER_COLOR_NONE,
    )

    runner = CliRunner()
    result = runner.invoke(
        cli,
        [
            "--set",
            "tags",
            "Foo",
            "--set",
            "tags",
            "Bar",
            "--set",
            "comment",
            "Hello World!",
            "--list",
            "--set",
            "finderinfo",
            "color:green",
            temp_file,
        ],
    )
    assert result.exit_code == 0
    output = parse_cli_output(result.stdout)
    assert output["tags"] == "['Foo', 'Bar', 'Green,Green']"
    meta = OSXMetaData(temp_file)
    assert meta.get_attribute("tags") == [
        Tag("Foo"),
        Tag("Bar"),
        Tag("Green", FINDER_COLOR_GREEN),
    ]
    assert meta.get_attribute("comment") == "Hello World!"
    assert meta.finderinfo.color == FINDER_COLOR_GREEN

    # test creation of backup file
    result = runner.invoke(cli, ["--backup", temp_file])
    assert result.exit_code == 0
    backup_file = pathlib.Path(
        pathlib.Path(temp_file).parent) / _BACKUP_FILENAME
    assert backup_file.exists()

    # clear the attributes to see if they can be restored
    meta.clear_attribute("tags")
    meta.clear_attribute("comment")
    meta.clear_attribute("finderinfo")
    assert meta.tags == []
    assert meta.comment is None
    assert meta.finderinfo.color == FINDER_COLOR_NONE

    result = runner.invoke(cli, ["--restore", temp_file])
    assert result.exit_code == 0
    assert meta.tags == [
        Tag("Foo"), Tag("Bar"),
        Tag("Green", FINDER_COLOR_GREEN)
    ]
    assert meta.comment == "Hello World!"
    assert meta.finderinfo.color == FINDER_COLOR_GREEN
コード例 #21
0
import json
import logging
from osxmetadata import OSXMetaData, Tag
import subprocess

# Getting the settings from json file
with open("settings.json", "r") as read_file:
    settings = json.load(read_file)

root = settings['dstPath']
iTunesPath = settings['itunesAutoPath']

# declaring tags object
c = settings['tagIsBeingConverted']
i = settings['tagHasBeenCopiedToLibrary']
hasBeenConverted = [Tag(c)]
hasBeenAdded = [Tag(i)]


# Iterate through files in all folders
def convert():
    for subdir, dirs, files in os.walk(root, topdown=False):
        for filename in files:
            inputFilePath = subdir + os.sep + filename

            # convert to mp3
            if inputFilePath.endswith(".flac"):
                logging.info('starting to convert ' + inputFilePath)
                filePath = inputFilePath.replace(".flac", "")
                outputFilePath = filePath + ".mp3"
コード例 #22
0
import osxmetadata
import json
import subprocess

# Getting the settings from json file
with open("settings.json", "r") as read_file:
    settings = json.load(read_file)

# Declaring source and destination folder
srcPath = settings['srcPath']
dstPath = settings['dstPath']
iTunesPath = settings['itunesAutoPath']

#declaring tags object
m = settings['tagHasBeenMoved']
hasBeenMoved = [Tag(m)]


#Copying files
def copyFiles():
    subprocess.run(["clear"])
    logging.info("starting the routine check")
    for root, dirs, files in os.walk(srcPath, topdown=False):
        for name in dirs:

            folder = (os.path.join(root, name))

            md = osxmetadata.OSXMetaData(folder)

            #next line is for testing purposes only, remove for build version
            md.clear_attribute("tags")