From 7f070448b7b11881208caacb834bc73e6d45f15f Mon Sep 17 00:00:00 2001 From: James Lu Date: Fri, 24 Feb 2017 22:28:26 -0800 Subject: [PATCH] utils, Irc: add abstraction to warn on deprecated attribute usage (#273) --- classes.py | 4 +++- utils.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/classes.py b/classes.py index 0e2e611..ef1cae2 100644 --- a/classes.py +++ b/classes.py @@ -33,7 +33,7 @@ class ProtocolError(Exception): ### Internal classes (users, servers, channels) -class Irc(): +class Irc(utils.DeprecatedAttributesObject): """Base IRC object for PyLink.""" def __init__(self, netname, proto, conf): @@ -42,6 +42,8 @@ class Irc(): (a string), the name of the protocol module to use for this connection, and a configuration object. """ + self.deprecated_attributes = {'conf': 'Deprecated since 1.2; consider switching to conf.conf'} + self.loghandlers = [] self.name = netname self.conf = conf diff --git a/utils.py b/utils.py index 64616bf..13d93b1 100644 --- a/utils.py +++ b/utils.py @@ -560,3 +560,17 @@ class IRCParser(argparse.ArgumentParser): def error(self, message): raise InvalidArgumentsError(message) + +class DeprecatedAttributesObject(): + """ + Object implementing deprecated attributes and warnings on access. + """ + + def __getattribute__(self, attr): + # Note: "self.deprecated_attributes" calls this too, so the != check is + # needed to prevent a recursive loop! + if attr != 'deprecated_attributes' and attr in self.deprecated_attributes: + log.warning('Attribute %s.%s is deprecated: %s' % (self.__class__.__name__, attr, + self.deprecated_attributes.get(attr))) + + return object.__getattribute__(self, attr)