Skip to content

Commit

Permalink
fix: PRIV data type checking, fixed examples, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicfit committed Dec 30, 2019
1 parent 8472634 commit 865b75e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 38 deletions.
24 changes: 12 additions & 12 deletions examples/tag_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,33 @@
log.setLevel(logging.DEBUG)

t = Tag()
t.artist = u"M.O.P."
t.title = u"How About Some Hardcore"
t.album = u"To The Death"
t.genre = u"Hip-Hop"
t.track_num = (3,5)
t.disc_num = (1,1)
t.artist = "M.O.P."
t.title = "How About Some Hardcore"
t.album = "To The Death"
t.genre = "Hip-Hop"
t.track_num = (3, 5)
t.disc_num = (1, 1)

t.original_release_date = "1994-04-07"
t.release_date = "1994-04-07"
t.encoding_date = "2002-03"
t.recording_date = 1996
t.tagging_date = "2012-2-5"

t.comments.set(u"Gritty, yo!")
t.comments.set(u"Brownsville, Brooklyn", u"Origin")
t.comments.set("Gritty, yo!")
t.comments.set("Brownsville, Brooklyn", "Origin")

t.user_text_frames.set(u"****", u"Rating")
t.user_text_frames.set("****", "Rating")
t.artist_url = b"http://allmusic.com/artist/mop-p194909"
t.user_url_frames.set(b"http://eyed3.nicfit.net/")

t.bpm = 187
t.play_count = 125
t.unique_file_ids.set(b"43e888e067ea107f964916af6259cbe7", "md5sum")
t.cd_id = b"\x3c\x33\x4d\x41\x43\x59\x3c\x33"
t.privates.set("Secrets", "Billy Danzenie")
t.terms_of_use = u"Blunted"
t.lyrics.set(u"""
t.privates.set(b"Secrets", b"Billy Danzenie")
t.terms_of_use = "Blunted"
t.lyrics.set("""
[ Billy Danzenie ]
How about some hardcore?
(Yeah, we like it raw!) (4x)
Expand Down
8 changes: 6 additions & 2 deletions eyed3/id3/frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,8 +851,12 @@ class PrivateFrame(Frame):
"""PRIV"""

def __init__(self, id=PRIVATE_FID, owner_id=b"", owner_data=b""):
super(PrivateFrame, self).__init__(id)
assert(id == PRIVATE_FID)
super().__init__(id)
assert id == PRIVATE_FID
for arg in (owner_id, owner_data):
if type(arg) is not bytes:
raise ValueError("PRIV owner fields require bytes type")

self.owner_id = owner_id
self.owner_data = owner_data

Expand Down
29 changes: 15 additions & 14 deletions eyed3/id3/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def clear(self):
self.file_info = None

def parse(self, fileobj, version=ID3_ANY_VERSION):
assert(fileobj)
assert fileobj
self.clear()
version = version or ID3_ANY_VERSION

Expand Down Expand Up @@ -100,10 +100,10 @@ def parse(self, fileobj, version=ID3_ANY_VERSION):

def _loadV2Tag(self, fp):
"""Returns (tag_found, padding_len)"""
padding = 0

# Look for a tag and if found load it.
if not self.header.parse(fp):
return (False, 0)
return False, 0

# Read the extended header if present.
if self.header.extended:
Expand All @@ -114,7 +114,7 @@ def _loadV2Tag(self, fp):
self.extended_header)

log.debug("Tag contains %d bytes of padding." % padding)
return (True, padding)
return True, padding

def _loadV1Tag(self, fp):
v1_enc = "latin1"
Expand All @@ -123,12 +123,12 @@ def _loadV1Tag(self, fp):
# v1.x tags are 128 bytes min and max
fp.seek(0, 2)
if fp.tell() < 128:
return (False, 0)
return False, 0
fp.seek(-128, 2)
tag_data = fp.read(128)

if tag_data[0:3] != b"TAG":
return (False, 0)
return False, 0

log.debug("Located ID3 v1 tag")
# v1.0 is implied until a v1.1 feature is recognized.
Expand Down Expand Up @@ -190,7 +190,7 @@ def _loadV1Tag(self, fp):
log.warning(ex)
self.genre = None

return (True, 0)
return True, 0

@property
def version(self):
Expand Down Expand Up @@ -295,7 +295,7 @@ def _splitNum(self, fid):
second = int(n[1]) if len(n) == 2 else None
except ValueError as ex:
log.warning(str(ex))
return (first, second)
return first, second

def _setNum(self, fid, val):
if type(val) is tuple:
Expand All @@ -321,7 +321,7 @@ def _setNum(self, fid, val):

total_str = ""
if n[1] is not None:
if n[1] >= 0 and n[1] <= 9:
if 0 <= n[1] <= 9:
total_str = "0" + str(n[1])
else:
total_str = str(n[1])
Expand Down Expand Up @@ -534,7 +534,7 @@ def _getV23OrignalReleaseDate(self):
date, date_str = None, None
try:
for fid in (b"XDOR", b"TORY"):
# Prefering XDOR over TORY since it can contain full date.
# Preferring XDOR over TORY since it can contain full date.
if fid in self.frame_set:
date_str = self.frame_set[fid][0].text.encode("latin1")
break
Expand Down Expand Up @@ -649,11 +649,12 @@ def _setGenre(self, g, id3_std=True):
elif not isinstance(g, Genre):
raise TypeError("Invalid genre data type: %s" % str(type(g)))
self.frame_set.setTextFrame(frames.GENRE_FID, str(g))

# genre property
genre = property(_getGenre, _setGenre)
"""genre property."""
# Non-standard genres.
non_std_genre = property(partial(_getGenre, id3_std=False),
partial(_setGenre, id3_std=False))
"""Non-standard genres."""

@property
def user_text_frames(self):
Expand Down Expand Up @@ -1144,8 +1145,8 @@ def fidHandled(fid):

# All other date frames have no conversion
for fid in date_frames:
log.warning("%s frame being dropped due to conversion to %s" %
(fid, versionToString(version)))
log.warning(f"{str(fid, 'ascii')} frame being dropped due to conversion to "
f"{versionToString(version)}")
flist.remove(date_frames[fid])

# Convert sort order frames 2.3 (XSO*) <-> 2.4 (TSO*)
Expand Down
20 changes: 10 additions & 10 deletions eyed3/plugins/classic.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,19 @@ def DescLangArg(arg):
vals = _splitArgs(arg, 2)
desc = vals[0]
lang = vals[1] if len(vals) > 1 else id3.DEFAULT_LANG
return (desc, b(lang)[:3] or id3.DEFAULT_LANG)
return desc, b(lang)[:3] or id3.DEFAULT_LANG

def DescTextArg(arg):
"""DESCRIPTION:TEXT"""
vals = _splitArgs(arg, 2)
desc = vals[0].strip()
text = FIELD_DELIM.join(vals[1:] if len(vals) > 1 else [])
return (desc or "", text or "")
return desc or "", text or ""
KeyValueArg = DescTextArg

def DescUrlArg(arg):
desc, url = DescTextArg(arg)
return (desc, url.encode("latin1"))
return desc, url.encode("latin1")

def FidArg(arg):
fid = arg.strip().encode("ascii")
Expand All @@ -140,12 +140,12 @@ def TextFrameArg(arg):
if not fid:
raise ArgumentTypeError("No frame ID")
text = vals[1] if len(vals) > 1 else ""
return (fid, text)
return fid, text

def UrlFrameArg(arg):
"""FID:TEXT"""
fid, url = TextFrameArg(arg)
return (fid, url.encode("latin1"))
return fid, url.encode("latin1")

def DateArg(date_str):
return core.Date.parse(date_str) if date_str else ""
Expand All @@ -160,7 +160,7 @@ def CommentArg(arg):
raise ArgumentTypeError("text required")
desc = vals[1] if len(vals) > 1 else ""
lang = vals[2] if len(vals) > 2 else id3.DEFAULT_LANG
return (text, desc, b(lang)[:3])
return text, desc, b(lang)[:3]

def LyricsArg(arg):
text, desc, lang = CommentArg(arg)
Expand All @@ -169,7 +169,7 @@ def LyricsArg(arg):
data = fp.read()
except Exception: # noqa: B901
raise ArgumentTypeError("Unable to read file")
return (data, desc, lang)
return data, desc, lang

def PlayCountArg(pc):
if not pc:
Expand All @@ -182,7 +182,7 @@ def PlayCountArg(pc):
pc = int(pc)
if pc < 0:
raise ArgumentTypeError("out of range")
return (increment, pc)
return increment, pc

def BpmArg(bpm):
bpm = int(float(bpm) + 0.5)
Expand Down Expand Up @@ -961,7 +961,7 @@ def _checkNumberedArgTuples(curr, new):

# --add-image
for img_path, img_type, img_mt, img_desc in self.args.images:
assert(img_path)
assert img_path
printWarning("Adding image %s" % img_path)
if img_mt not in ImageFrame.URL_MIME_TYPE_VALUES:
with open(img_path, "rb") as img_fp:
Expand All @@ -972,7 +972,7 @@ def _checkNumberedArgTuples(curr, new):

# --add-object
for obj_path, obj_mt, obj_desc, obj_fname in self.args.objects or []:
assert(obj_path)
assert obj_path
printWarning("Adding object %s" % obj_path)
with open(obj_path, "rb") as obj_fp:
tag.objects.set(obj_fp.read(), obj_mt, obj_desc, obj_fname)
Expand Down

0 comments on commit 865b75e

Please sign in to comment.