Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions stagger/frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,19 @@

import abc
import collections
import imghdr
from abc import abstractmethod
from warnings import warn

from stagger.errors import *
from stagger.specs import *
from stagger.util import imghdr_what

try:
from collections import Container
from collections import Iterable
except ImportError:
from collections.abc import Container
from collections.abc import Iterable

class Frame(metaclass=abc.ABCMeta):
_framespec = tuple()
Expand Down Expand Up @@ -114,7 +121,7 @@ def _in_version(self, *versions):
"Returns true if this frame is in any of the specified versions of ID3."
for version in versions:
if (self._version == version
or (isinstance(self._version, collections.Container)
or (isinstance(self._version, Container)
and version in self._version)):
return True
return False
Expand Down Expand Up @@ -244,7 +251,7 @@ def extract_strs(values):
return
if isinstance(values, str):
yield values
elif isinstance(values, collections.Iterable):
elif isinstance(values, Iterable):
for val in values:
for v in extract_strs(val):
yield v
Expand Down Expand Up @@ -313,7 +320,7 @@ def __init__(self, value=None, frameid=None, flags=None, frameno=None, **kwargs)
self.data = file.read()
self.type = 0
self.desc = ""
format = imghdr.what(None, self.data[:32])
format = imghdr_what(None, self.data[:32])
if not format:
format = value.rpartition(".")[2]
self._set_format(format)
Expand Down
9 changes: 4 additions & 5 deletions stagger/id3.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@
"""List of frames defined in the various ID3 versions.
"""

import imghdr

import stagger.tags as tags
from stagger.frames import *
from stagger.specs import *
from stagger.tags import frameclass
from stagger.util import imghdr_what


# ID3v2.4
Expand Down Expand Up @@ -422,7 +421,7 @@ def _to_version(self, version):

def _str_fields(self):
img = "{0} bytes of {1} data".format(len(self.data),
imghdr.what(None, self.data[:32]))
imghdr_what(None, self.data[:32]))
return ("type={0}, desc={1}, mime={2}: {3}"
.format(repr(self._spec("type").to_str(self.type)),
repr(self.desc),
Expand Down Expand Up @@ -819,15 +818,15 @@ def _to_version(self, version):
elif self.format.upper() == "JPG":
mime = "image/jpeg"
else:
mime = imghdr.what(io.StringIO(self.data))
mime = imghdr_what(io.StringIO(self.data))
if mime is None:
raise ValueError("Unknown image format")
mime = "image/" + mime.lower()
return APIC(mime=mime, type=self.type, desc=self.desc, data=self.data)

def _str_fields(self):
img = "{0} bytes of {1} data".format(len(self.data),
imghdr.what(None, self.data[:32]))
imghdr_what(None, self.data[:32]))
return ("type={0}, desc={1}, format={2}: {3}"
.format(repr(self._spec("type").to_str(self.type)),
repr(self.desc),
Expand Down
13 changes: 10 additions & 3 deletions stagger/specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
from stagger.conversion import *
from stagger.errors import *

try:
from collections import ByteString
from collections import Sequence
except ImportError:
from collections.abc import ByteString
from collections.abc import Sequence

# The idea for the Spec system comes from Mutagen.

def optionalspec(spec):
Expand Down Expand Up @@ -228,7 +235,7 @@ def write(self, frame, value):
def validate(self, frame, value):
if value is None:
return bytes()
if not isinstance(value, collections.ByteString):
if not isinstance(value, ByteString):
raise TypeError("Not a byte sequence")
return value
def to_str(self, value):
Expand Down Expand Up @@ -423,7 +430,7 @@ def validate(self, frame, values):
return []
res = []
for v in values:
if not isinstance(v, collections.Sequence) or isinstance(v, str):
if not isinstance(v, Sequence) or isinstance(v, str):
raise TypeError("Records must be sequences")
if len(v) != len(self.specs):
raise ValueError("Invalid record length")
Expand Down Expand Up @@ -453,7 +460,7 @@ def write(self, frame, values):
def validate(self, frame, values):
if values is None:
return []
if not isinstance(values, collections.Sequence) or isinstance(values, str):
if not isinstance(values, Sequence) or isinstance(values, str):
raise TypeError("ASPISpec needs a sequence of integers")
if len(values) != frame.N:
raise ValueError("ASPISpec needs {0} integers".format(frame.N))
Expand Down
19 changes: 13 additions & 6 deletions stagger/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import re
import collections
import io
import imghdr
import zlib

from abc import abstractmethod, abstractproperty
Expand All @@ -47,6 +46,14 @@

import stagger.frames as Frames
import stagger.fileutil as fileutil
from stagger.util import imghdr_what

try:
from collections import MutableMapping
from collections import Iterable
except ImportError:
from collections.abc import MutableMapping
from collections.abc import Iterable

_FRAME23_FORMAT_COMPRESSED = 0x0080
_FRAME23_FORMAT_ENCRYPTED = 0x0040
Expand Down Expand Up @@ -219,7 +226,7 @@ def __repr__(self):
return "<FrameOrder: {0}>".format(", ".join(pair[0] for pair in order))


class Tag(collections.MutableMapping, metaclass=abc.ABCMeta):
class Tag(MutableMapping, metaclass=abc.ABCMeta):
known_frames = { } # Maps known frameids to Frame class objects

frame_order = None # Initialized by stagger.id3
Expand Down Expand Up @@ -316,7 +323,7 @@ def __setitem__(self, key, value):
self._frames[key] = [value]
return
if self.known_frames[key]._allow_duplicates:
if not isinstance(value, collections.Iterable) or isinstance(value, str):
if not isinstance(value, Iterable) or isinstance(value, str):
raise ValueError("{0} requires a list of frame values".format(key))
self._frames[key] = [val if isinstance(val, self.known_frames[key])
else self.known_frames[key](val)
Expand Down Expand Up @@ -484,8 +491,8 @@ def _get_date(self, yearframe, dateframe, timeframe):
try:
time = self.__friendly_text_collect(timeframe)[0]
m = re.match(r"\s*(?P<hour>[0-2][0-9])\s*:?\s*"
"(?P<minute>[0-5][0-9])\s*:?\s*"
"(?P<second>[0-5][0-9])?\s*$", time)
r"(?P<minute>[0-5][0-9])\s*:?\s*"
r"(?P<second>[0-5][0-9])?\s*$", time)
if m is not None:
hour = int(m.group("hour"))
minute = int(m.group("minute"))
Expand Down Expand Up @@ -516,7 +523,7 @@ def getter(self):
.format(f._spec("type").to_str(f.type),
f.desc,
len(f.data),
imghdr.what(None, f.data[:32]))
imghdr_what(None, f.data[:32]))
for f in self[frameid])
def setter(self, value):
if len(value) > 0:
Expand Down
5 changes: 5 additions & 0 deletions stagger/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,8 @@ def print_warnings(filename, options):
print(filename + ":warning: " + str(w.message),
file=sys.stderr)
sys.stderr.flush()

def imghdr_what(file, h):
# imhdr.what was deprecated in PEP-0594
# See https://peps.python.org/pep-0594/#imghdr
return None

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This leaves 2 tests failing:

FAILED test/friendly.py::FriendlyTestCase::testPicture22 - AssertionError: 'Other(0)::<60511 bytes of None data>' != 'Other(0)::<60511...
FAILED test/friendly.py::FriendlyTestCase::testPicture23_24 - AssertionError: 'Other(0)::<60511 bytes of None data>' != 'Other(0)::<60511...

The full messages:

________________________ FriendlyTestCase.testPicture22 ________________________
[…]
E       AssertionError: 'Other(0)::<60511 bytes of None data>' != 'Other(0)::<60511 bytes of jpeg data>'
E       - Other(0)::<60511 bytes of None data>
E       ?                           ^^^
E       + Other(0)::<60511 bytes of jpeg data>
E       ?                           ^^ +

test/friendly.py:292: AssertionError
______________________ FriendlyTestCase.testPicture23_24 _______________________
[…]
E           AssertionError: 'Other(0)::<60511 bytes of None data>' != 'Other(0)::<60511 bytes of jpeg data>'
E           - Other(0)::<60511 bytes of None data>
E           ?                           ^^^
E           + Other(0)::<60511 bytes of jpeg data>
E           ?                           ^^ +

test/friendly.py:318: AssertionError

One solution is to provide a fallback implementation of imghdr.what, as I did in macports/macports-ports 26385.