/usr/lib/python3/dist-packages/mido/frozen.py is in python3-mido 1.2.7-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | from .messages import Message
from .midifiles import MetaMessage, UnknownMetaMessage
class Frozen(object):
def __repr__(self):
text = super(Frozen, self).__repr__()
return '<frozen {}'.format(text[1:])
def __setattr__(self, *_):
raise ValueError('frozen message is immutable')
def __hash__(self):
return hash(tuple(sorted(vars(self).items())))
class FrozenMessage(Frozen, Message):
pass
class FrozenMetaMessage(Frozen, MetaMessage):
pass
class FrozenUnknownMetaMessage(Frozen, UnknownMetaMessage):
pass
def is_frozen(msg):
"""Return True if message is frozen, otherwise False."""
return isinstance(msg, Frozen)
# Todo: these two functions are almost the same except inverted. There
# should be a way to refactor them to lessen code duplication.
def freeze_message(msg):
"""Freeze message.
Returns a frozen version of the message. Frozen messages are
immutable, hashable and can be used as dictionary keys.
Will return None if called with None. This allows you to do things
like::
msg = freeze_message(port.poll())
"""
if isinstance(msg, Frozen):
# Already frozen.
return msg
elif isinstance(msg, Message):
class_ = FrozenMessage
elif isinstance(msg, UnknownMetaMessage):
class_ = FrozenUnknownMetaMessage
elif isinstance(msg, MetaMessage):
class_ = FrozenMetaMessage
elif msg is None:
return None
else:
raise ValueError('first argument must be a message or None')
frozen = class_.__new__(class_)
vars(frozen).update(vars(msg))
return frozen
def thaw_message(msg):
"""Thaw message.
Returns a mutable version of a frozen message.
Will return None if called with None.
"""
if not isinstance(msg, Frozen):
# Already thawed, just return a copy.
return msg.copy()
elif isinstance(msg, FrozenMessage):
class_ = Message
elif isinstance(msg, FrozenUnknownMetaMessage):
class_ = UnknownMetaMessage
elif isinstance(msg, FrozenMetaMessage):
class_ = MetaMessage
elif msg is None:
return None
else:
raise ValueError('first argument must be a message or None')
thawed = class_.__new__(class_)
vars(thawed).update(vars(msg))
return thawed
|