Repository: yt-dlp/yt-dlp
File: yt_dlp/postprocessor/xattrpp.py (Python)

Code before:
        # 'user.xdg.comment': 'description',
    }

    APPLE_PLIST_TEMPLATE = '''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
\t<string>%s</string>
</array>
</plist>'''

    def format_value(self, xattrname, infoname, value):
        if infoname == 'upload_date':
            return hyphenate_date(value)
        if xattrname == 'com.apple.metadata:kMDItemWhereFroms':
            return self.APPLE_PLIST_TEMPLATE % value
        return value

    def run(self, info):
        mtime = os.stat(info['filepath']).st_mtime
        self.to_screen('Writing metadata to file\'s xattrs')
        for xattrname, infoname in self.XATTR_MAPPING.items():
            try:
                value = info.get(infoname)
                if value:
                    value = self.format_value(xattrname, infoname, value)
                    write_xattr(info['filepath'], xattrname, value.encode())

            except XAttrUnavailableError as e:
                raise PostProcessingError(str(e))
            except XAttrMetadataError as e:
                if e.reason == 'NO_SPACE':
                    self.report_warning(
                        'There\'s no disk space left, disk quota exceeded or filesystem xattr limit exceeded. '
                        f'Extended attribute "{xattrname}" was not written.')
                elif e.reason == 'VALUE_TOO_LONG':
                    self.report_warning(f'Unable to write extended attribute "{xattrname}" due to too long values.')
                else:
                    tip = ('You need to use NTFS' if os.name == 'nt'
                           else 'You may have to enable them in your "/etc/fstab"')
                    raise PostProcessingError(f'This filesystem doesn\'t support extended attributes. {tip}')

        self.try_utime(info['filepath'], mtime, mtime)
        return [], info


Review comment (suggestion):
I would probably still inline the function call, since its just two `if`s but I think its fine like this as well

Code after:
        'com.apple.metadata:kMDItemWhereFroms': 'webpage_url',
    }

    APPLE_PLIST_TEMPLATE = '''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
\t<string>%s</string>
</array>
</plist>'''

    def run(self, info):
        mtime = os.stat(info['filepath']).st_mtime
        self.to_screen('Writing metadata to file\'s xattrs')
        for xattrname, infoname in self.XATTR_MAPPING.items():
            try:
                value = info.get(infoname)
                if value:
                    if infoname == 'upload_date':
                        value = hyphenate_date(value)
                    elif xattrname == 'com.apple.metadata:kMDItemWhereFroms':
                        value = self.APPLE_PLIST_TEMPLATE % value
                    write_xattr(info['filepath'], xattrname, value.encode())

            except XAttrUnavailableError as e:
                raise PostProcessingError(str(e))
            except XAttrMetadataError as e:
                if e.reason == 'NO_SPACE':
                    self.report_warning(
                        'There\'s no disk space left, disk quota exceeded or filesystem xattr limit exceeded. '
                        f'Extended attribute "{xattrname}" was not written.')
                elif e.reason == 'VALUE_TOO_LONG':
                    self.report_warning(f'Unable to write extended attribute "{xattrname}" due to too long values.')
                else:
                    tip = ('You need to use NTFS' if os.name == 'nt'
                           else 'You may have to enable them in your "/etc/fstab"')
                    raise PostProcessingError(f'This filesystem doesn\'t support extended attributes. {tip}')

        self.try_utime(info['filepath'], mtime, mtime)
        return [], info