当前位置: 首页>>代码示例>>Python>>正文


Python File.__setitem__方法代码示例

本文整理汇总了Python中mutagen.File.__setitem__方法的典型用法代码示例。如果您正苦于以下问题:Python File.__setitem__方法的具体用法?Python File.__setitem__怎么用?Python File.__setitem__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mutagen.File的用法示例。


在下文中一共展示了File.__setitem__方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: AudioFile

# 需要导入模块: from mutagen import File [as 别名]
# 或者: from mutagen.File import __setitem__ [as 别名]
class AudioFile(MutableMapping):
    """A simple class just for tag editing.

    No internal mutagen tags are exposed, or filenames or anything. So
    calling clear() won't destroy the filename field or things like
    that. Use it like a dict, then .write() it to commit the changes.
    When saving, tags that cannot be saved by the file format will be
    skipped with a debug message, since this is a common occurrance
    with MP3/M4A.

    Optional argument blacklist is a list of regexps matching
    non-transferrable tags. They will effectively be hidden, nether
    settable nor gettable.

    Or grab the actual underlying mutagen format object from the
    .data field and get your hands dirty.

    """
    def __init__(self, filename: str, blacklist: List[Pattern[str]]=[],
                 easy: bool=True) -> None:
        self.filename = filename
        self.data = MusicFile(self.filename, easy=easy)
        if self.data is None:
            raise ValueError("Unable to identify %s as a music file" % (repr(filename)))
        # Also exclude mutagen's internal tags
        self.blacklist = [ re.compile("^~") ] + blacklist
    def __getitem__(self, item: str) -> Any:
        if self.blacklisted(item):
            logger.debug("Attempted to get blacklisted key: %s." % repr(item))
        else:
            return self.data.__getitem__(item)
    def __setitem__(self, item: str, value: Any) -> None:
        if self.blacklisted(item):
            logger.debug("Attempted to set blacklisted key: %s." % repr(item))
        else:
            try:
                return self.data.__setitem__(item, value)
            except KeyError:
                logger.debug("Skipping unsupported tag %s for file type %s",
                             item, type(self.data))
    def __delitem__(self, item: str):
        if self.blacklisted(item):
            logger.debug("Attempted to del blacklisted key: %s." % repr(item))
        else:
            return self.data.__delitem__(item)
    def __len__(self) -> int:
        return len(list(self.keys()))
    def __iter__(self) -> Iterable[Any]:
        return iter(self.keys())

    def blacklisted(self, item: str) -> bool:
        """Return True if tag is blacklisted.

        Blacklist automatically includes internal mutagen tags (those
        beginning with a tilde)."""
        for regex in self.blacklist:
            if re.search(regex, item):
                return True
        else:
            return False
    def keys(self) -> Iterable[str]:
        return [ key for key in self.data.keys() if not self.blacklisted(key) ]
    def write(self) -> None:
        return self.data.save()
开发者ID:DarwinAwardWinner,项目名称:transfercoder,代码行数:66,代码来源:__init__.py


注:本文中的mutagen.File.__setitem__方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。