Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove usage of six & __future__ #72

Merged
merged 1 commit into from
Sep 23, 2023
Merged
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
61 changes: 29 additions & 32 deletions mediafile.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
data from the tags. In turn ``MediaField`` uses a number of
``StorageStyle`` strategies to handle format specific logic.
"""
from __future__ import division, absolute_import, print_function

import mutagen
import mutagen.id3
import mutagen.mp3
Expand All @@ -54,7 +52,6 @@
import math
import os
import re
import six
import struct
import traceback

Expand Down Expand Up @@ -136,8 +133,8 @@ def mutagen_call(action, filename, func, *args, **kwargs):
try:
return func(*args, **kwargs)
except mutagen.MutagenError as exc:
log.debug(u'%s failed: %s', action, six.text_type(exc))
raise UnreadableFileError(filename, six.text_type(exc))
log.debug(u'%s failed: %s', action, str(exc))
raise UnreadableFileError(filename, str(exc))
except UnreadableFileError:
# Reraise our errors without changes.
# Used in case of decorating functions (e.g. by `loadfile`).
Expand Down Expand Up @@ -202,8 +199,8 @@ def _safe_cast(out_type, val):
# Process any other type as a string.
if isinstance(val, bytes):
val = val.decode('utf-8', 'ignore')
elif not isinstance(val, six.string_types):
val = six.text_type(val)
elif not isinstance(val, str):
val = str(val)
# Get a number from the front of the string.
match = re.match(r'[\+-]?[0-9]+', val.strip())
return int(match.group(0)) if match else 0
Expand All @@ -215,13 +212,13 @@ def _safe_cast(out_type, val):
except ValueError:
return False

elif out_type == six.text_type:
elif out_type == str:
if isinstance(val, bytes):
return val.decode('utf-8', 'ignore')
elif isinstance(val, six.text_type):
elif isinstance(val, str):
return val
else:
return six.text_type(val)
return str(val)

elif out_type == float:
if isinstance(val, int) or isinstance(val, float):
Expand All @@ -230,7 +227,7 @@ def _safe_cast(out_type, val):
if isinstance(val, bytes):
val = val.decode('utf-8', 'ignore')
else:
val = six.text_type(val)
val = str(val)
match = re.match(r'[\+-]?([0-9]+\.?[0-9]*|[0-9]*\.[0-9]+)',
val.strip())
if match:
Expand Down Expand Up @@ -289,7 +286,7 @@ def _sc_decode(soundcheck):
"""
# We decode binary data. If one of the formats gives us a text
# string, interpret it as UTF-8.
if isinstance(soundcheck, six.text_type):
if isinstance(soundcheck, str):
soundcheck = soundcheck.encode('utf-8')

# SoundCheck tags consist of 10 numbers, each represented by 8
Expand Down Expand Up @@ -437,7 +434,7 @@ class Image(object):
def __init__(self, data, desc=None, type=None):
assert isinstance(data, bytes)
if desc is not None:
assert isinstance(desc, six.text_type)
assert isinstance(desc, str)
self.data = data
self.desc = desc
if isinstance(type, int):
Expand Down Expand Up @@ -495,7 +492,7 @@ class StorageStyle(object):
"""List of mutagen classes the StorageStyle can handle.
"""

def __init__(self, key, as_type=six.text_type, suffix=None,
def __init__(self, key, as_type=str, suffix=None,
float_places=2, read_only=False):
"""Create a basic storage strategy. Parameters:

Expand All @@ -520,8 +517,8 @@ def __init__(self, key, as_type=six.text_type, suffix=None,
self.read_only = read_only

# Convert suffix to correct string type.
if self.suffix and self.as_type is six.text_type \
and not isinstance(self.suffix, six.text_type):
if self.suffix and self.as_type is str \
and not isinstance(self.suffix, str):
self.suffix = self.suffix.decode('utf-8')

# Getter.
Expand All @@ -544,7 +541,7 @@ def deserialize(self, mutagen_value):
"""Given a raw value stored on a Mutagen object, decode and
return the represented value.
"""
if self.suffix and isinstance(mutagen_value, six.text_type) \
if self.suffix and isinstance(mutagen_value, str) \
and mutagen_value.endswith(self.suffix):
return mutagen_value[:-len(self.suffix)]
else:
Expand All @@ -566,17 +563,17 @@ def serialize(self, value):
"""Convert the external Python value to a type that is suitable for
storing in a Mutagen file object.
"""
if isinstance(value, float) and self.as_type is six.text_type:
if isinstance(value, float) and self.as_type is str:
value = u'{0:.{1}f}'.format(value, self.float_places)
value = self.as_type(value)
elif self.as_type is six.text_type:
elif self.as_type is str:
if isinstance(value, bool):
# Store bools as 1/0 instead of True/False.
value = six.text_type(int(bool(value)))
value = str(int(bool(value)))
elif isinstance(value, bytes):
value = value.decode('utf-8', 'ignore')
else:
value = six.text_type(value)
value = str(value)
else:
value = self.as_type(value)

Expand Down Expand Up @@ -702,7 +699,7 @@ class MP4StorageStyle(StorageStyle):

def serialize(self, value):
value = super(MP4StorageStyle, self).serialize(value)
if self.key.startswith('----:') and isinstance(value, six.text_type):
if self.key.startswith('----:') and isinstance(value, str):
value = value.encode('utf-8')
return value

Expand Down Expand Up @@ -881,7 +878,7 @@ def fetch(self, mutagen_file):

def store(self, mutagen_file, value):
# This field type stores text data as encoded data.
assert isinstance(value, six.text_type)
assert isinstance(value, str)
value = value.encode('utf-8')

frames = mutagen_file.tags.getall(self.key)
Expand All @@ -905,7 +902,7 @@ class MP3DescStorageStyle(MP3StorageStyle):
"""
def __init__(self, desc=u'', key='TXXX', attr='text', multispec=True,
**kwargs):
assert isinstance(desc, six.text_type)
assert isinstance(desc, str)
self.description = desc
self.attr = attr
self.multispec = multispec
Expand Down Expand Up @@ -994,7 +991,7 @@ def __init__(self, key, pack_pos=0, **kwargs):
def _fetch_unpacked(self, mutagen_file):
data = self.fetch(mutagen_file)
if data:
items = six.text_type(data).split('/')
items = str(data).split('/')
else:
items = []
packing_length = 2
Expand All @@ -1010,7 +1007,7 @@ def set(self, mutagen_file, value):
items[0] = ''
if items[1] is None:
items.pop() # Do not store last value
self.store(mutagen_file, '/'.join(map(six.text_type, items)))
self.store(mutagen_file, '/'.join(map(str, items)))

def delete(self, mutagen_file):
if self.pack_pos == 0:
Expand Down Expand Up @@ -1277,7 +1274,7 @@ def __init__(self, *styles, **kwargs):
getting this property.

"""
self.out_type = kwargs.get('out_type', six.text_type)
self.out_type = kwargs.get('out_type', str)
self._styles = styles

def styles(self, mutagen_file):
Expand Down Expand Up @@ -1317,7 +1314,7 @@ def _none_value(self):
return 0.0
elif self.out_type == bool:
return False
elif self.out_type == six.text_type:
elif self.out_type == str:
return u''


Expand Down Expand Up @@ -1400,9 +1397,9 @@ def _get_date_tuple(self, mediafile):
"""
# Get the underlying data and split on hyphens and slashes.
datestring = super(DateField, self).__get__(mediafile, None)
if isinstance(datestring, six.string_types):
datestring = re.sub(r'[Tt ].*$', '', six.text_type(datestring))
items = re.split('[-/]', six.text_type(datestring))
if isinstance(datestring, str):
datestring = re.sub(r'[Tt ].*$', '', str(datestring))
items = re.split('[-/]', str(datestring))
else:
items = []

Expand Down Expand Up @@ -1439,7 +1436,7 @@ def _set_date_tuple(self, mediafile, year, month=None, day=None):
date.append(u'{0:02d}'.format(int(month)))
if month and day:
date.append(u'{0:02d}'.format(int(day)))
date = map(six.text_type, date)
date = map(str, date)
super(DateField, self).__set__(mediafile, u'-'.join(date))

if hasattr(self, '_year_field'):
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ author-email = "adrian@radbox.org"
home-page = "https://github.com/beetbox/mediafile"
description-file = "README.rst"
requires = [
"six>=1.9",
"mutagen>=1.46",
]
requires-python = ">=3.7"
Expand Down
9 changes: 1 addition & 8 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,4 @@ min-version=2.7
# - E221: multiple spaces before operator (used to align visually)
# - E731: do not assign a lambda expression, use a def
# - C901: function/method complexity
# `flake8-future-import` errors we ignore:
# - FI50: `__future__` import "division" present
# - FI51: `__future__` import "absolute_import" present
# - FI12: `__future__` import "with_statement" missing
# - FI53: `__future__` import "print_function" present
# - FI14: `__future__` import "unicode_literals" missing
# - FI15: `__future__` import "generator_stop" missing
ignore=C901,E241,E221,E731,FI50,FI51,FI12,FI53,FI14,FI15
ignore=C901,E241,E221,E731
2 changes: 0 additions & 2 deletions test/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
# -*- coding: utf-8 -*-

from __future__ import division, absolute_import, print_function
2 changes: 0 additions & 2 deletions test/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
# included in all copies or substantial portions of the Software.

"""Some common functionality for mediafile tests."""
from __future__ import division, absolute_import, print_function

import os
import tempfile
import shutil
Expand Down
11 changes: 4 additions & 7 deletions test/test_mediafile.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@
"""Automatically-generated blanket testing for the MediaFile metadata
layer.
"""
from __future__ import division, absolute_import, print_function

import os
import shutil
import datetime
import time
import unittest
from six import assertCountEqual

from test import _common
from mediafile import MediaFile, Image, \
Expand Down Expand Up @@ -269,15 +266,15 @@ class GenreListTestMixin(object):

def test_read_genre_list(self):
mediafile = self._mediafile_fixture('full')
assertCountEqual(self, mediafile.genres, ['the genre'])
self.assertCountEqual(mediafile.genres, ['the genre'])

def test_write_genre_list(self):
mediafile = self._mediafile_fixture('empty')
mediafile.genres = [u'one', u'two']
mediafile.save()

mediafile = MediaFile(mediafile.filename)
assertCountEqual(self, mediafile.genres, [u'one', u'two'])
self.assertCountEqual(mediafile.genres, [u'one', u'two'])

def test_write_genre_list_get_first(self):
mediafile = self._mediafile_fixture('empty')
Expand All @@ -294,7 +291,7 @@ def test_append_genre_list(self):
mediafile.save()

mediafile = MediaFile(mediafile.filename)
assertCountEqual(self, mediafile.genres, [u'the genre', u'another'])
self.assertCountEqual(mediafile.genres, [u'the genre', u'another'])


class ReadWriteTestBase(ArtTestMixin, GenreListTestMixin,
Expand Down Expand Up @@ -1116,7 +1113,7 @@ def test_known_fields(self):
'albumtypes', 'catalognums', 'languages', 'artists_credit',
'artists_sort', 'albumartists_credit', 'albumartists_sort')
)
assertCountEqual(self, MediaFile.fields(), fields)
self.assertCountEqual(MediaFile.fields(), fields)

def test_fields_in_readable_fields(self):
readable = MediaFile.readable_fields()
Expand Down
11 changes: 4 additions & 7 deletions test/test_mediafile_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

"""Specific, edge-case tests for the MediaFile metadata layer.
"""
from __future__ import division, absolute_import, print_function

import os
import shutil
import unittest
Expand All @@ -25,7 +23,6 @@
from test import _common

import mediafile
import six


_sc = mediafile._safe_cast
Expand Down Expand Up @@ -130,8 +127,8 @@ def test_safe_cast_negative_string_to_float(self):
self.assertAlmostEqual(_sc(float, u'-1.234'), -1.234)

def test_safe_cast_special_chars_to_unicode(self):
us = _sc(six.text_type, 'caf\xc3\xa9')
self.assertTrue(isinstance(us, six.text_type))
us = _sc(str, 'caf\xc3\xa9')
self.assertTrue(isinstance(us, str))
self.assertTrue(us.startswith(u'caf'))

def test_safe_cast_float_with_no_numbers(self):
Expand Down Expand Up @@ -359,7 +356,7 @@ def test_v24_year_tag(self):
mf.year = 2013
mf.save()
frame = mf.mgfile['TDRC']
self.assertTrue('2013' in six.text_type(frame))
self.assertTrue('2013' in str(frame))
self.assertTrue('TYER' not in mf.mgfile)
finally:
self._delete_test()
Expand All @@ -370,7 +367,7 @@ def test_v23_year_tag(self):
mf.year = 2013
mf.save()
frame = mf.mgfile['TYER']
self.assertTrue('2013' in six.text_type(frame))
self.assertTrue('2013' in str(frame))
self.assertTrue('TDRC' not in mf.mgfile)
finally:
self._delete_test()
Expand Down
Loading