Skip to content
Snippets Groups Projects

MAINT: refactor _Messages class

Merged Henrik tom Wörden requested to merge f-messages into dev
All threads resolved!
Files
2
+ 61
64
@@ -2340,7 +2340,7 @@ class _ParentList(list):
raise KeyError(str(parent) + " not found.")
class _Messages(dict):
class Messages(list):
"""This 'kind of dictionary' stores error, warning, info, and other
messages. The mentioned three messages types are messages of special use.
@@ -2405,7 +2405,7 @@ class _Messages(dict):
"""
def clear_server_messages(self):
"""Removes all error, warning and info messages."""
"""Removes all messages of type error, warning and info."""
rem = []
for m in self:
@@ -2415,9 +2415,15 @@ class _Messages(dict):
for m in rem:
self.remove(m)
return self
def __setitem__(self, key, value): # @ReservedAssignment
if not isinstance(value, Message):
warn("__setitem__ will in future only accept Message objects as second argument. "
"You will no longe be"
" able to pass bodys such that Message object is created on the fly",
DeprecationWarning)
if not isinstance(key, int):
warn("__setitem__ will in future only accept int as first argument",
DeprecationWarning)
if isinstance(key, tuple):
if len(key) == 2:
type = key[0] # @ReservedAssignment
@@ -2449,13 +2455,19 @@ class _Messages(dict):
if isinstance(value, Message):
body = value.body
description = value.description
m = Message
else:
body = value
description = None
m = Message(type=type, code=code, description=description, body=body)
dict.__setitem__(self, _Messages._msg_key(type, code), m)
m = Message(type=type, code=code, description=description, body=body)
if isinstance(key, int):
super().__setitem__(key, m)
else:
self.append(m)
def __getitem__(self, key):
if not isinstance(key, int):
warn("__getitem__ only supports integer keys in future.", DeprecationWarning)
if isinstance(key, tuple):
if len(key) == 2:
type = key[0] # @ReservedAssignment
@@ -2467,82 +2479,68 @@ class _Messages(dict):
raise TypeError(
"('type', 'code'), ('type'), or 'type' expected.")
elif isinstance(key, int) and int(key) >= 0:
for m in self.values():
if key == 0:
return m
else:
key -= 1
type = key # @ReservedAssignment
code = None
return super().__getitem__(key)
else:
type = key # @ReservedAssignment
code = None
m = dict.__getitem__(self, _Messages._msg_key(type, code))
m = self.get(type, code)
if m is None:
return
if m.description:
return (m.description, m.body)
else:
return m.body
def __init__(self):
dict.__init__(self)
def __delitem__(self, key):
if isinstance(key, tuple):
if len(key) == 2:
type = key[0] # @ReservedAssignment
code = key[1]
elif len(key) == 1:
type = key[0] # @ReservedAssignment
code = None
else:
raise TypeError(
"('type', 'code'), ('type'), or 'type' expected.")
warn("__delitem__ only supports integer keys in future.", DeprecationWarning)
if self.get(key[0], key[1]) is not None:
self.remove(self.get(key[0], key[1]))
else:
type = key # @ReservedAssignment
code = None
return dict.__delitem__(self, _Messages._msg_key(type, code))
super().__delitem__(key)
def remove(self, obj, obj2=None):
if isinstance(obj, Message):
return dict.__delitem__(self, _Messages._msg_key.get(obj))
return self.__delitem__((obj, obj2))
if obj2 is not None:
warn("Supplying a second argument to remove is deprecated.",
DeprecationWarning)
super().remove(self.get(obj, obj2))
else:
super().remove(obj)
def get(self, type, code=None, default=None): # @ReservedAssignment
try:
return dict.__getitem__(self, _Messages._msg_key(type, code))
except KeyError:
return default
for msg in self:
if msg.type == type and msg.code == code:
return msg
return default
def extend(self, messages):
self.append(messages)
return self
super().extend(messages)
def append(self, msg):
if hasattr(msg, "__iter__"):
if isinstance(msg, _Messages) or isinstance(msg, list):
warn("Supplying a list-like object to append is deprecated. Please use extend"
" instead.", DeprecationWarning)
for m in msg:
self.append(m)
return
return self
if isinstance(msg, Message):
dict.__setitem__(self, _Messages._msg_key.get(msg), msg)
super().append(msg)
return self
else:
raise TypeError("Argument was not a Message")
def to_xml(self, add_to_element):
for m in self:
melem = m.to_xml()
add_to_element.append(melem)
return self
def __repr__(self):
xml = etree.Element("Messages")
self.to_xml(xml)
def __iter__(self):
return dict.values(self).__iter__()
return xml2str(xml)
class _msg_key:
def __init__(self, type, code): # @ReservedAssignment
warn("This class is deprecated.", DeprecationWarning)
self._type = type
self._code = code
@@ -2561,18 +2559,13 @@ class _Messages(dict):
return str(self._type) + (str(",") + str(self._code)
if self._code is not None else '')
def to_xml(self, add_to_element):
for m in self:
melem = m.to_xml()
add_to_element.append(melem)
return self
def __repr__(self):
xml = etree.Element("Messages")
self.to_xml(xml)
return xml2str(xml)
class _Messages(Messages):
def __init__(self, *args, **kwargs):
warn("_Messages is deprecated. "
"Use class Messages instead and beware of the slightly different API of the new"
" Messages class", DeprecationWarning)
super().__init__(*args, **kwargs)
def _basic_sync(e_local, e_remote):
@@ -4397,6 +4390,10 @@ def execute_query(q, unique=False, raise_exception_on_error=True, cache=True, fl
class DropOffBox(list):
def __init__(self, *args, **kwargs):
warn(DeprecationWarning(
"The DropOffBox is deprecated and will be removed in future."))
super().__init__(*args, **kwargs)
path = None
Loading