def test_tags_3(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.update_attribute(attribute, [Tag("Foo")]) tags = meta.get_attribute(attribute) assert tags == [Tag("Test"), Tag("Green"), Tag("Foo")] meta.remove_attribute(attribute, Tag("Foo")) tags = meta.get_attribute(attribute) assert tags == [Tag("Test"), Tag("Green")] meta.update_attribute(attribute, [Tag("Foo")]) tags = meta.get_attribute(attribute) assert tags == [Tag("Test"), Tag("Green"), Tag("Foo")] meta.clear_attribute(attribute) tags = meta.get_attribute(attribute) assert len(tags) == 0
def test_list_attribute(temp_file, attribute): """test set_attribute, get_attribute, etc for list items""" from osxmetadata import OSXMetaData meta = OSXMetaData(temp_file) expected = ["Foo"] meta.set_attribute(attribute, expected) got = meta.get_attribute(attribute) assert expected == got expected += ["Bar"] meta.append_attribute(attribute, ["Bar"]) assert meta.get_attribute(attribute) == expected expected.remove("Bar") meta.remove_attribute(attribute, "Bar") assert meta.get_attribute(attribute) == expected with pytest.raises(ValueError): meta.remove_attribute(attribute, "Bar") expected += ["Flooz"] meta.update_attribute(attribute, ["Flooz"]) assert meta.get_attribute(attribute) == expected meta.discard_attribute(attribute, "foo") assert meta.get_attribute(attribute) == expected meta.clear_attribute(attribute) assert meta.get_attribute(attribute) is None expected = ["Foo"] meta.set_attribute(attribute, expected) got = meta.get_attribute(attribute) assert expected == got
def test_str_attribute(temp_file, attribute): """test set_attribute, get_attribute, etc""" from osxmetadata import OSXMetaData from osxmetadata.constants import _FINDER_COMMENT_NAMES meta = OSXMetaData(temp_file) expected = 2 meta.set_attribute(attribute, expected) got = meta.get_attribute(attribute) assert expected == got expected += 1 meta.append_attribute(attribute, 1) assert meta.get_attribute(attribute) == expected meta.clear_attribute(attribute) assert meta.get_attribute(attribute) is None with pytest.raises(TypeError): meta.set_attribute(attribute, "") with pytest.raises(TypeError): meta.update_attribute(attribute, "foo") with pytest.raises(TypeError): meta.discard_attribute(attribute, "foo") with pytest.raises(TypeError): meta.remove_attribute(attribute, "foo")
def test_finderinfo_color_set_get_clear(temp_file): """Test ability to set_attribute, get_attribute, clear_attribute on finderinfo""" from osxmetadata import OSXMetaData, Tag from osxmetadata.constants import FINDER_COLOR_GREEN, FINDER_COLOR_BLUE from osxmetadata.findertags import get_tag_color_name meta = OSXMetaData(temp_file) assert meta.finderinfo.color == 0 meta.finderinfo.color = FINDER_COLOR_GREEN assert meta.finderinfo.color == FINDER_COLOR_GREEN assert {"color": FINDER_COLOR_GREEN}.items() <= meta.get_attribute( "finderinfo" ).items() meta.set_attribute("finderinfo", {"color": FINDER_COLOR_BLUE}) assert meta.finderinfo.color == FINDER_COLOR_BLUE meta.clear_attribute("finderinfo") assert meta.finderinfo.color == 0 meta.finderinfo.color = FINDER_COLOR_GREEN meta.finderinfo.color = None assert meta.finderinfo.color == 0 meta.finderinfo.color = FINDER_COLOR_GREEN meta.finderinfo = None assert meta.finderinfo.color == 0
def test_str_attribute(temp_file, attribute): """test set_attribute, get_attribute, etc""" from osxmetadata import OSXMetaData from osxmetadata.constants import _FINDER_COMMENT_NAMES meta = OSXMetaData(temp_file) expected = datetime.datetime(2021, 7, 8) meta.set_attribute(attribute, expected) got = meta.get_attribute(attribute) assert expected == got meta.clear_attribute(attribute) assert meta.get_attribute(attribute) == None # test setting empty string meta.set_attribute(attribute, "") assert meta.get_attribute(attribute) is None with pytest.raises(TypeError): meta.update_attribute(attribute, "foo") with pytest.raises(TypeError): meta.discard_attribute(attribute, "foo") with pytest.raises(TypeError): meta.remove_attribute(attribute, "foo")
def test_finder_comments_2(temp_file): """ test get/set attribute """ from osxmetadata import OSXMetaData from osxmetadata.constants import kMDItemFinderComment attribute = kMDItemFinderComment meta = OSXMetaData(temp_file) fc = "This is my new comment" meta.set_attribute(attribute, fc) assert meta.findercomment == fc meta.findercomment += ", foo" fc += ", foo" assert meta.findercomment == fc # set finder comment to "" results in null string but not deleted meta.set_attribute(attribute, "") assert meta.get_attribute(attribute) == "" meta.set_attribute(attribute, "foo") assert meta.get_attribute(attribute) == "foo" # set finder comment to None deletes it meta.set_attribute(attribute, "") assert meta.get_attribute(attribute) == "" meta.clear_attribute(attribute) assert meta.get_attribute(attribute) == None # can we set findercomment after is was set to None? meta.set_attribute(attribute, "bar") assert meta.get_attribute(attribute) == "bar"
def test_finder_comments_2(temp_file): """ test get/set attribute """ from osxmetadata import OSXMetaData from osxmetadata.constants import kMDItemFinderComment attribute = kMDItemFinderComment meta = OSXMetaData(temp_file) fc = "This is my new comment" meta.set_attribute(attribute, fc) assert meta.findercomment == fc meta.findercomment += ", foo" fc += ", foo" assert meta.findercomment == fc # set finder comment to "" deletes it as this mirrors mdls and Finder meta.set_attribute(attribute, "") assert meta.get_attribute(attribute) is None meta.set_attribute(attribute, "foo") assert meta.get_attribute(attribute) == "foo" # set finder comment to None deletes it meta.set_attribute(attribute, None) assert meta.get_attribute(attribute) is None meta.clear_attribute(attribute) assert meta.get_attribute(attribute) is None # can we set findercomment after is was set to None? meta.set_attribute(attribute, "bar") assert meta.get_attribute(attribute) == "bar"
def test_download_date_tz_1A(temp_file): """ set naive time but return tz_aware """ from osxmetadata import OSXMetaData from osxmetadata.datetime_utils import datetime_naive_to_local import datetime meta = OSXMetaData(temp_file, tz_aware=True) dt = datetime.datetime.now() meta.set_attribute("downloadeddate", dt) dt_tz = datetime_naive_to_local(dt) assert meta.downloadeddate == [dt_tz] assert meta.get_attribute("downloadeddate") == [dt_tz]
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_finder_comments_dir(): """ test get/set attribute but on a directory, not on a file""" from osxmetadata import OSXMetaData from osxmetadata.constants import kMDItemFinderComment with TemporaryDirectory() as temp_dir: attribute = kMDItemFinderComment meta = OSXMetaData(temp_dir) fc = "This is my new comment" meta.set_attribute(attribute, fc) assert meta.findercomment == fc meta.findercomment += ", foo" fc += ", foo" assert meta.findercomment == fc
def test_finderinfo_stationarypad_and_color(temp_file): """Test ability to modify color and stationary pad independently""" from osxmetadata import OSXMetaData, Tag from osxmetadata.constants import ( FINDER_COLOR_GREEN, FINDER_COLOR_BLUE, FINDER_COLOR_NONE, ) meta = OSXMetaData(temp_file) assert meta.finderinfo.stationarypad == 0 assert meta.finderinfo.color == FINDER_COLOR_NONE meta.finderinfo.stationarypad = True assert meta.finderinfo.stationarypad assert meta.finderinfo.color == FINDER_COLOR_NONE meta.finderinfo.color = FINDER_COLOR_GREEN assert meta.finderinfo.color == FINDER_COLOR_GREEN assert meta.finderinfo.stationarypad meta.finderinfo.stationarypad = False assert not meta.finderinfo.stationarypad assert meta.finderinfo.color == FINDER_COLOR_GREEN assert { "color": FINDER_COLOR_GREEN, "stationarypad": False, }.items() <= meta.get_attribute("finderinfo").items() meta.set_attribute("finderinfo", {"stationarypad": True}) assert meta.finderinfo.stationarypad assert meta.finderinfo.color == FINDER_COLOR_GREEN meta.set_attribute("finderinfo", {"color": FINDER_COLOR_BLUE}) assert meta.finderinfo.color == FINDER_COLOR_BLUE assert meta.finderinfo.stationarypad meta.finderinfo = {"color": FINDER_COLOR_GREEN} assert meta.finderinfo.color == FINDER_COLOR_GREEN assert meta.finderinfo.stationarypad meta.finderinfo = {"stationarypad": False} assert meta.finderinfo.color == FINDER_COLOR_GREEN assert not meta.finderinfo.stationarypad
def test_str_attribute(temp_file, attribute): """test set_attribute, get_attribute, etc""" from osxmetadata import OSXMetaData from osxmetadata.constants import _FINDER_COMMENT_NAMES meta = OSXMetaData(temp_file) expected = True meta.set_attribute(attribute, expected) got = meta.get_attribute(attribute) assert expected == got meta.set_attribute(attribute, not expected) assert not meta.get_attribute(attribute) meta.clear_attribute(attribute) assert meta.get_attribute(attribute) is None # test setting empty string # setting finder comment to empty string clears it # but other fields get set to empty string # this mirrors the way finder comments work in mdls meta.set_attribute(attribute, "") assert not meta.get_attribute(attribute) with pytest.raises(AttributeError): meta.update_attribute(attribute, "foo") with pytest.raises(TypeError): meta.discard_attribute(attribute, "foo") with pytest.raises(TypeError): meta.remove_attribute(attribute, "foo")
def test_stationarypad(temp_file): """Test stationarypad attribute""" from osxmetadata import OSXMetaData meta = OSXMetaData(temp_file) assert not meta.stationarypad meta.stationarypad = True assert meta.stationarypad meta.stationarypad = 0 assert not meta.stationarypad meta.stationarypad = 1 assert meta.stationarypad meta.stationarypad = False assert not meta.stationarypad assert not meta.get_attribute("stationarypad") meta.set_attribute("stationarypad", True) assert meta.get_attribute("stationarypad")
def test_finderinfo_stationarypad_set_get_clear(temp_file): """Test ability to set_attribute, get_attribute, clear_attribute on finderinfo""" from osxmetadata import OSXMetaData, Tag meta = OSXMetaData(temp_file) assert meta.finderinfo.stationarypad == 0 meta.finderinfo.stationarypad = True assert meta.finderinfo.stationarypad assert {"stationarypad": True}.items() <= meta.get_attribute("finderinfo").items() meta.set_attribute("finderinfo", {"stationarypad": True}) assert meta.finderinfo.stationarypad meta.clear_attribute("finderinfo") assert not meta.finderinfo.stationarypad meta.finderinfo.stationarypad = None assert not meta.finderinfo.stationarypad # test string to bool meta.finderinfo.stationarypad = "True" assert meta.finderinfo.stationarypad