Skip to content

Commit

Permalink
Bug fixed for broken directory support
Browse files Browse the repository at this point in the history
  • Loading branch information
RhetTbull committed May 3, 2020
1 parent 4f0d9d5 commit 37abc74
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
2 changes: 1 addition & 1 deletion osxmetadata/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.99.1"
__version__ = "0.99.2"
64 changes: 41 additions & 23 deletions osxmetadata/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import collections.abc
import datetime
import logging
import os
import plistlib
import sys
from plistlib import FMT_BINARY # pylint: disable=E0611
Expand Down Expand Up @@ -36,6 +37,9 @@ def __init__(self, attribute, xattr_, osxmetadata_obj):
self._attrs = xattr_
self._md = osxmetadata_obj

# some attributes don't work on directories
self._isdir = os.path.isdir(self._md._fname)

self._constant = attribute.constant

self.data = []
Expand Down Expand Up @@ -177,12 +181,13 @@ def _load_data(self):
else:
self.data = []

# check color tag stored in com.apple.FinderInfo
color = get_finderinfo_color(str(self._md._fname))
# logging.debug(f"color = {color}")
if color and (self._tags is None or color not in self._tags.values()):
# have a FinderInfo color that's not in _kMDItemUserTag
self.data.append(Tag(_COLORIDS[color], color))
if not self._isdir:
# check color tag stored in com.apple.FinderInfo
color = get_finderinfo_color(str(self._md._fname))
# logging.debug(f"color = {color}")
if color and (self._tags is None or color not in self._tags.values()):
# have a FinderInfo color that's not in _kMDItemUserTag
self.data.append(Tag(_COLORIDS[color], color))

def _write_data(self):
# Overwrites the existing attribute values with the iterable of values provided.
Expand All @@ -199,21 +204,22 @@ def _write_data(self):
# also write FinderInfo if required
# if findercolor in tag set being written, do nothing
# if findercolor not in tag set being written, overwrite findercolor with first color tag
finder_color = get_finderinfo_color(str(self._md._fname))
tag_colors = [tag.color for tag in self.data]
logging.debug(f"write_data: finder {finder_color}, tag: {tag_colors}")
if finder_color not in tag_colors:
# overwrite FinderInfo color with new color
# get first non-zero color in tag if there is one
try:
color = tag_colors[
tag_colors.index(
next(filter(lambda x: x != FINDER_COLOR_NONE, tag_colors))
)
]
except StopIteration:
color = FINDER_COLOR_NONE
set_finderinfo_color(str(self._md._fname), color)
if not self._isdir:
finder_color = get_finderinfo_color(str(self._md._fname))
tag_colors = [tag.color for tag in self.data]
logging.debug(f"write_data: finder {finder_color}, tag: {tag_colors}")
if finder_color not in tag_colors:
# overwrite FinderInfo color with new color
# get first non-zero color in tag if there is one
try:
color = tag_colors[
tag_colors.index(
next(filter(lambda x: x != FINDER_COLOR_NONE, tag_colors))
)
]
except StopIteration:
color = FINDER_COLOR_NONE
set_finderinfo_color(str(self._md._fname), color)


class _AttributeFinderInfo:
Expand All @@ -228,6 +234,9 @@ def __init__(self, attribute, xattr_, osxmetadata_obj):
self._attrs = xattr_
self._md = osxmetadata_obj

# FinderInfo doesn't work on directories
self._isdir = os.path.isdir(self._md._fname)

self._constant = attribute.constant

self.data = None
Expand All @@ -245,9 +254,13 @@ def set_value(self, value):

def _load_data(self):
self._tags = {}

# check color tag stored in com.apple.FinderInfo
color = get_finderinfo_color(str(self._md._fname))
logging.debug(f"AttributeFinderInfo: color = {color}")
color = None
if not self._isdir:
color = get_finderinfo_color(str(self._md._fname))
logging.debug(f"AttributeFinderInfo: color = {color}")

if color is not None:
# have a FinderInfo color
self.data = Tag(_COLORIDS[color], color)
Expand All @@ -261,6 +274,11 @@ def _write_data(self):
# e.g. md.tags += [Tag("foo", 0)] will result in
# append --> insert --> __setattr__ --> set_attribute --> set_value
# which results in _write_data being called twice in a row

if self._isdir:
# can't write this attribute on a directory
return

if self.data is None:
# nothing to do
logging.debug(f"No data to write")
Expand Down
11 changes: 11 additions & 0 deletions tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,14 @@ def test_tag_factory():
tag_ = tag_factory("green")
assert isinstance(tag_, Tag)
assert tag_ == Tag("Green", FINDER_COLOR_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")]

0 comments on commit 37abc74

Please sign in to comment.