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()
def test_tags_exception(temp_file): from osxmetadata import OSXMetaData, Tag # update tags meta = OSXMetaData(temp_file) with pytest.raises(TypeError): meta.tags = ["Foo"] with pytest.raises(TypeError): meta.tags = "Foo"
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!"
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()
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()
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")]
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") == []
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
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