# Copyright 2006 Joe wreschnig
#
# This program is free software; you can redistribute it and/or modify
# It under the terms of the GNU General Public License version 2
# Published by the Free Software Foundation.
#
# $ ID: _ util. py 4218 2007-12-02 06: 11: 20z Piman $
"Utility Classes for mutagen.
You shoshould not rely on the interfaces here being stable. They are
Intended for internal use in mutagen only.
"""
Import struct
From fnmatch import fnmatchcase
Class dictmixin (object ):
"Implement the dict API using keys () and _ * Item _ methods.
Similar to userdict. dictmixin, this takes a class that defines
_ Getitem __, _ setitem __, _ delitem __, and keys (), and turns it
Into a full dict-like object.
Userdict. dictmixin is not suitable for this purpose because it's
An old-style class.
This class is not optimized for very large dictionaries; optional
Functions have linear memory requirements. I recommend you
Override some of these functions if speed is required.
"""
Def _ ITER _ (Self ):
Return ITER (self. Keys ())
Def has_key (self, key ):
Try: Self [Key]
Failed t keyerror: Return false
Else: Return true
_ Contains _ = has_key
Iterkeys = Lambda self: ITER (self. Keys ())
Def values (Self ):
Return map (self. _ getitem __, self. Keys ())
Itervalues = Lambda self: ITER (self. Values ())
Def items (Self ):
Return zip (self. Keys (), self. Values ())
Iteritems = Lambda S: ITER (S. Items ())
Def clear (Self ):
Map (self. _ delitem __, self. Keys ())
Def POP (self, key, * ARGs ):
If Len (ARGs)> 1:
Raise typeerror ("pop takes at most two arguments ")
Try: value = self [Key]
Failed t keyerror:
If ARGs: Return ARGs [0]
Else: Raise
Del (Self [Key])
Return Value
Def popitem (Self ):
Try:
Key = self. Keys () [0]
Return key, self. Pop (key)
Failed t indexerror: Raise keyerror ("dictionary is empty ")
Def Update (self, other = none, ** kwargs ):
If other is none:
Self. Update (kwargs)
Other = {}
Try: Map (self. _ setitem __, other. Keys (), other. Values ())
T attributeerror:
For key, value in other:
Self [Key] = Value
Def setdefault (self, key, default = none ):
Try: return self [Key]
Failed t keyerror:
Self [Key] = default
Return default
Def get (self, key, default = none ):
Try: return self [Key]
Failed t keyerror: Return default
Def _ repr _ (Self ):
Return repr (dict (self. Items ()))
Def _ CMP _ (self, other ):
If other is none: return 1
Else: Return CMP (dict (self. Items (), other)
Def _ Len _ (Self ):
Return Len (self. Keys ())
Class dictproxy (dictmixin ):
Def _ init _ (self, * ARGs, ** kwargs ):
Self. _ dict = {}
Super (dictproxy, self). _ init _ (* ARGs, ** kwargs)
Def _ getitem _ (self, key ):
Return self. _ dict [Key]
Def _ setitem _ (self, key, value ):
Self. _ dict [Key] = Value
Def _ delitem _ (self, key ):
Del (self. _ dict [Key])
Def keys (Self ):
Return self. _ dict. Keys ()
Class CDATA (object ):
"C character buffer to Python numeric type conversions ."""
From struct import Error
Short_le = staticmethod (lambda data: struct. Unpack ('<H', data) [0])
Ushort_le = staticmethod (lambda data: struct. Unpack ('<H', data) [0])
Short_be = staticmethod (lambda data: struct. Unpack ('> H', data) [0])
Ushort_be = staticmethod (lambda data: struct. Unpack ('> H', data) [0])
Int_le = staticmethod (lambda data: struct. Unpack ('<I', data) [0])
Uint_le = staticmethod (lambda data: struct. Unpack ('<I', data) [0])
Int_be = staticmethod (lambda data: struct. Unpack ('> I', data) [0])
Uint_be = staticmethod (lambda data: struct. Unpack ('> I', data) [0])
Longlong_le = staticmethod (lambda data: struct. Unpack ('<Q', data) [0])
Ulonglong_le = staticmethod (lambda data: struct. Unpack ('<Q', data) [0])
Longlong_be = staticmethod (lambda data: struct. Unpack ('> Q', data) [0])
Ulonglong_be = staticmethod (lambda data: struct. Unpack ('> Q', data) [0])
To_short_le = staticmethod (lambda data: struct. Pack ('<H', data ))
To_ushort_le = staticmethod (lambda data: struct. Pack ('<H', data ))
To_short_be = staticmethod (lambda data: struct. Pack ('> H', data ))
To_ushort_be = staticmethod (lambda data: struct. Pack ('> H', data ))
To_int_le = staticmethod (lambda data: struct. Pack ('<I', data ))
To_uint_le = staticmethod (lambda data: struct. Pack ('<I', data ))
To_int_be = staticmethod (lambda data: struct. Pack ('> I', data ))
To_uint_be = staticmethod (lambda data: struct. Pack ('> I', data ))
To_longlong_le = staticmethod (lambda data: struct. Pack ('<Q', data ))
To_ulonglong_le = staticmethod (lambda data: struct. Pack ('<Q', data ))
To_longlong_be = staticmethod (lambda data: struct. Pack ('> Q', data ))
To_ulonglong_be = staticmethod (lambda data: struct. Pack ('> Q', data ))
Bitswap = ''. Join ([CHR (sum ([(Val> I) & 1) <(7-i) for I in range (8)])
For Val in range (1, 256)])
Del (I)
Del (VAL)
Test_bit = staticmethod (lambda value, N: bool (value> N) & 1 ))
Def lock (fileobj ):
"Lock a file object 'safety Y '.
That means a failure to lock because the platform doesn' t
Support fcntl or filesystem locks is not considered
Failure. This call does block.
Returns whether or not the lock was successful, or
Raises an exception in more extreme circumstances (full
Lock table, invalid file ).
"""
Try: Import fcntl
Failed t importerror:
Return false
Else:
Try: fcntl. lockf (fileobj, fcntl. lock_ex)
Handle t ioerror:
# Fixme: There's possibly a lot of complicated
# Logic that needs to go here in case the ioerror
# Is eacces or eagain.
Return false
Else:
Return true
Def unlock (fileobj ):
"Unlock a file object.
Don't call this on a file object unless a call to lock ()
Returned true.
"""
# If this fails there's a mismatched lock/unlock pair,
# So we definitely don't want to ignore errors.
Import fcntl
Fcntl. lockf (fileobj, fcntl. lock_un)
Def insert_bytes (fobj, size, offset, buffer_size = 2 ** 16 ):
"Insert size bytes of empty space starting at offset.
Fobj must be an open file object, open RB + or
Equivalent. mutagen tries to use MMAP to resize the file,
Falls back to a significantly slower method if MMAP fails.
"""
Assert 0 <size
Assert 0 <= offset
Locked = false
Fobj. Seek (0, 2)
Filesize = fobj. Tell ()
Movesize = filesize-offset
Fobj. Write ('\ x00' * size)
Fobj. Flush ()
Try:
Try:
Import MMAP
Map = MMAP. MMAP (fobj. fileno (), filesize + size)
Try: map. Move (Offset + size, offset, movesize)
Finally: map. Close ()
Except T (valueerror, environmenterror, importerror ):
# Handle broken MMAP scenarios
Locked = Lock (fobj)
Fobj. truncate (filesize)
Fobj. Seek (0, 2)
Padsize = size
# Don't generate an enormous string if we need to pad
# The file out several Megs.
While padsize:
Addsize = min (buffer_size, padsize)
Fobj. Write ("\ x00" * addsize)
Padsize-= addsize
Fobj. Seek (filesize, 0)
While movesize:
# At the start of this loop, fobj is pointing at the end
# Of the data we need to move, which is of movesize length.
Thismove = min (buffer_size, movesize)
# Seek back however much we're going to read this frame.
Fobj. Seek (-thismove, 1)
Nextpos = fobj. Tell ()
# Read it, so we're back at the end.
Data = fobj. Read (thismove)
# Seek back to where we need to write it.
Fobj. Seek (-thismove + size, 1)
# Write it.
Fobj. Write (data)
# And seek back to the end of the unmoved data.
Fobj. Seek (nextpos)
Movesize-= thismove
Fobj. Flush ()
Finally:
If locked:
Unlock (fobj)
Def delete_bytes (fobj, size, offset, buffer_size = 2 ** 16 ):
"Delete size bytes of empty space starting at offset.
Fobj must be an open file object, open RB + or
Equivalent. mutagen tries to use MMAP to resize the file,
Falls back to a significantly slower method if MMAP fails.
"""
Locked = false
Assert 0 <size
Assert 0 <= offset
Fobj. Seek (0, 2)
Filesize = fobj. Tell ()
Movesize = filesize-offset-size
Assert 0 <= movesize
Try:
If movesize> 0:
Fobj. Flush ()
Try:
Import MMAP
Map = MMAP. MMAP (fobj. fileno (), filesize)
Try: map. Move (offset, offset + size, movesize)
Finally: map. Close ()
Except T (valueerror, environmenterror, importerror ):
# Handle broken MMAP scenarios
Locked = Lock (fobj)
Fobj. Seek (Offset + size)
Buf = fobj. Read (buffer_size)
While Buf:
Fobj. Seek (offset)
Fobj. Write (BUF)
Offset + = Len (BUF)
Fobj. Seek (Offset + size)
Buf = fobj. Read (buffer_size)
Fobj. truncate (filesize-size)
Fobj. Flush ()
Finally:
If locked:
Unlock (fobj)
Def utf8 (data ):
"Convert a basestring to a valid UTF-8 Str ."""
If isinstance (data, STR ):
Return data. Decode ("UTF-8", "replace"). encode ("UTF-8 ")
Elif isinstance (data, Unicode ):
Return data. encode ("UTF-8 ")
Else: Raise typeerror ("only Unicode/STR types can be converted to UTF-8 ")
Def dict_match (D, key, default = none ):
Try:
Return d [Key]
Failed t keyerror:
For pattern, value in D. iteritems ():
If fnmatchcase (Key, pattern ):
Return Value
Return default