From a07920506ccb3affe5e9d45e9511b21292615aac Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Fri, 6 Jul 2012 17:20:57 -0400 Subject: [PATCH] __repr__ and __str__ for all classes (22) --- earwigbot/bot.py | 8 ++++++++ earwigbot/commands/__init__.py | 9 +++++++++ earwigbot/config.py | 9 +++++++++ earwigbot/irc/connection.py | 11 +++++++++++ earwigbot/irc/data.py | 9 +++++++++ earwigbot/irc/frontend.py | 11 +++++++++++ earwigbot/irc/rc.py | 8 ++++++++ earwigbot/irc/watcher.py | 11 +++++++++++ earwigbot/managers.py | 10 ++++++++++ earwigbot/tasks/__init__.py | 10 ++++++++++ earwigbot/wiki/sitesdb.py | 9 +++++++++ 11 files changed, 105 insertions(+) diff --git a/earwigbot/bot.py b/earwigbot/bot.py index 8e134d5..8607dc1 100644 --- a/earwigbot/bot.py +++ b/earwigbot/bot.py @@ -75,6 +75,14 @@ class Bot(object): self.commands.load() self.tasks.load() + def __repr__(self): + """Return the canonical string representation of the Bot.""" + return "Bot(config={0!r})".format(self.config) + + def __str__(self): + """Return a nice string representation of the Bot.""" + return "".format(self.config.root_dir) + def _dispatch_irc_component(self, name, klass): """Create a new IRC component, record it internally, and start it.""" component = klass(self) diff --git a/earwigbot/commands/__init__.py b/earwigbot/commands/__init__.py index 3743b25..e213cfe 100644 --- a/earwigbot/commands/__init__.py +++ b/earwigbot/commands/__init__.py @@ -75,6 +75,15 @@ class Command(object): self.setup() + def __repr__(self): + """Return the canonical string representation of the Command.""" + res = "Command(name={0!r}, commands={1!r}, hooks={2!r}, bot={3!r})" + return res.format(self.name, self.commands, self.hooks, self.bot) + + def __str__(self): + """Return a nice string representation of the Command.""" + return "".format(self.name, self.bot) + def setup(self): """Hook called immediately after the command is loaded. diff --git a/earwigbot/config.py b/earwigbot/config.py index 16978f5..48057a1 100644 --- a/earwigbot/config.py +++ b/earwigbot/config.py @@ -87,6 +87,15 @@ class BotConfig(object): (self._irc, ("watcher", "nickservPassword")), ] + def __repr__(self): + """Return the canonical string representation of the BotConfig.""" + res = "BotConfig(root_dir={0!r}, level={1!r})" + return res.format(self.root_dir, self.logging_level) + + def __str__(self): + """Return a nice string representation of the BotConfig.""" + return "".format(self.root_dir) + def _load(self): """Load data from our JSON config file (config.yml) into self._data.""" filename = self._config_path diff --git a/earwigbot/irc/connection.py b/earwigbot/irc/connection.py index 56c9d81..36f2728 100644 --- a/earwigbot/irc/connection.py +++ b/earwigbot/irc/connection.py @@ -44,6 +44,17 @@ class IRCConnection(object): self._last_recv = time() self._last_ping = 0 + def __repr__(self): + """Return the canonical string representation of the IRCConnection.""" + res = "IRCConnection(host={0!r}, port={1!r}, nick={2!r}, ident={3!r}, realname={4!r})" + return res.format(self.host, self.port, self.nick, self.ident, + self.realname) + + def __str__(self): + """Return a nice string representation of the IRCConnection.""" + res = "" + return res.format(self.nick, self.ident, self.host, self.port) + def _connect(self): """Connect to our IRC server.""" self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) diff --git a/earwigbot/irc/data.py b/earwigbot/irc/data.py index de7a1ac..cd2b9fc 100644 --- a/earwigbot/irc/data.py +++ b/earwigbot/irc/data.py @@ -39,6 +39,15 @@ class Data(object): self._parse(msgtype) + def __repr__(self): + """Return the canonical string representation of the Data.""" + res = "Data(bot={0!r}, my_nick={1!r}, line={2!r})" + return res.format(self.bot, self.my_nick, self.line) + + def __str__(self): + """Return a nice string representation of the Data.""" + return "".format(" ".join(self.line)) + def _parse(self, msgtype): """Parse a line from IRC into its components as instance attributes.""" sender = re.findall(":(.*?)!(.*?)@(.*?)\Z", self.line[0])[0] diff --git a/earwigbot/irc/frontend.py b/earwigbot/irc/frontend.py index 5235edc..20fb64a 100644 --- a/earwigbot/irc/frontend.py +++ b/earwigbot/irc/frontend.py @@ -47,6 +47,17 @@ class Frontend(IRCConnection): cf["realname"]) self._connect() + def __repr__(self): + """Return the canonical string representation of the Frontend.""" + res = "Frontend(host={0!r}, port={1!r}, nick={2!r}, ident={3!r}, realname={4!r}, bot={5!r})" + return res.format(self.host, self.port, self.nick, self.ident, + self.realname, self.bot) + + def __str__(self): + """Return a nice string representation of the Frontend.""" + res = "" + return res.format(self.nick, self.ident, self.host, self.port) + def _process_message(self, line): """Process a single message from IRC.""" if line[1] == "JOIN": diff --git a/earwigbot/irc/rc.py b/earwigbot/irc/rc.py index 841da0e..49f2e29 100644 --- a/earwigbot/irc/rc.py +++ b/earwigbot/irc/rc.py @@ -37,6 +37,14 @@ class RC(object): self.chan = chan self.msg = msg + def __repr__(self): + """Return the canonical string representation of the RC.""" + return "RC(chan={0!r}, msg={1!r})".format(self.chan, self.msg) + + def __str__(self): + """Return a nice string representation of the RC.""" + return "".format(self.msg, self.chan) + def parse(self): """Parse a recent change event into some variables.""" # Strip IRC color codes; we don't want or need 'em: diff --git a/earwigbot/irc/watcher.py b/earwigbot/irc/watcher.py index d333e52..67c3787 100644 --- a/earwigbot/irc/watcher.py +++ b/earwigbot/irc/watcher.py @@ -48,6 +48,17 @@ class Watcher(IRCConnection): self._prepare_process_hook() self._connect() + def __repr__(self): + """Return the canonical string representation of the Watcher.""" + res = "Watcher(host={0!r}, port={1!r}, nick={2!r}, ident={3!r}, realname={4!r}, bot={5!r})" + return res.format(self.host, self.port, self.nick, self.ident, + self.realname, self.bot) + + def __str__(self): + """Return a nice string representation of the Watcher.""" + res = "" + return res.format(self.nick, self.ident, self.host, self.port) + def _process_message(self, line): """Process a single message from IRC.""" if line[1] == "PRIVMSG": diff --git a/earwigbot/managers.py b/earwigbot/managers.py index c3b0c58..cc1e2da 100644 --- a/earwigbot/managers.py +++ b/earwigbot/managers.py @@ -57,6 +57,16 @@ class _ResourceManager(object): self._resource_base = base # e.g. Command or Task self._resource_access_lock = RLock() + def __repr__(self): + """Return the canonical string representation of the manager.""" + res = "{0}(bot={1!r}, name={2!r}, base={3!r})" + return res.format(self.__class__.__name__, self.bot, + self._resource_name, self._resource_base) + + def __str__(self): + """Return a nice string representation of the manager.""" + return "<{0} of {1}>".format(self.__class__.__name__, self.bot) + def __iter__(self): with self.lock: for resource in self._resources.itervalues(): diff --git a/earwigbot/tasks/__init__.py b/earwigbot/tasks/__init__.py index 35c0046..f49f724 100644 --- a/earwigbot/tasks/__init__.py +++ b/earwigbot/tasks/__init__.py @@ -56,6 +56,16 @@ class Task(object): self.logger = bot.tasks.logger.getChild(self.name) self.setup() + def __repr__(self): + """Return the canonical string representation of the Task.""" + res = "Task(name={0!r}, number={1!r}, bot={2!r})" + return res.format(self.name, self.number, self.bot) + + def __str__(self): + """Return a nice string representation of the Task.""" + res = "" + return res.format(self.name, self.number, self.bot) + def setup(self): """Hook called immediately after the task is loaded. diff --git a/earwigbot/wiki/sitesdb.py b/earwigbot/wiki/sitesdb.py index 2a75dc0..1f3265b 100644 --- a/earwigbot/wiki/sitesdb.py +++ b/earwigbot/wiki/sitesdb.py @@ -63,6 +63,15 @@ class SitesDB(object): self._cookie_file = path.join(bot.config.root_dir, ".cookies") self._cookiejar = None + def __repr__(self): + """Return the canonical string representation of the SitesDB.""" + res = "SitesDB(config={0!r}, sitesdb={1!r}, cookie_file={2!r})" + return res.format(self.config, self._sitesdb, self._cookie_file) + + def __str__(self): + """Return a nice string representation of the SitesDB.""" + return "".format(self._sitesdb) + def _get_cookiejar(self): """Return a LWPCookieJar object loaded from our .cookies file.