Explorar el Código

__repr__ and __str__ for all classes (22)

tags/v0.1^2
Ben Kurtovic hace 12 años
padre
commit
a07920506c
Se han modificado 11 ficheros con 105 adiciones y 0 borrados
  1. +8
    -0
      earwigbot/bot.py
  2. +9
    -0
      earwigbot/commands/__init__.py
  3. +9
    -0
      earwigbot/config.py
  4. +11
    -0
      earwigbot/irc/connection.py
  5. +9
    -0
      earwigbot/irc/data.py
  6. +11
    -0
      earwigbot/irc/frontend.py
  7. +8
    -0
      earwigbot/irc/rc.py
  8. +11
    -0
      earwigbot/irc/watcher.py
  9. +10
    -0
      earwigbot/managers.py
  10. +10
    -0
      earwigbot/tasks/__init__.py
  11. +9
    -0
      earwigbot/wiki/sitesdb.py

+ 8
- 0
earwigbot/bot.py Ver fichero

@@ -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 "<Bot at {0}>".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)


+ 9
- 0
earwigbot/commands/__init__.py Ver fichero

@@ -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 "<Command {0} of {1}>".format(self.name, self.bot)

def setup(self):
"""Hook called immediately after the command is loaded.



+ 9
- 0
earwigbot/config.py Ver fichero

@@ -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 "<BotConfig at {0}>".format(self.root_dir)

def _load(self):
"""Load data from our JSON config file (config.yml) into self._data."""
filename = self._config_path


+ 11
- 0
earwigbot/irc/connection.py Ver fichero

@@ -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 = "<IRCConnection {0}!{1} at {2}:{3}>"
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)


+ 9
- 0
earwigbot/irc/data.py Ver fichero

@@ -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 "<Data of {0!r}>".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]


+ 11
- 0
earwigbot/irc/frontend.py Ver fichero

@@ -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 = "<Frontend {0}!{1} at {2}:{3}>"
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":


+ 8
- 0
earwigbot/irc/rc.py Ver fichero

@@ -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 "<RC of {0!r} on {1}>".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:


+ 11
- 0
earwigbot/irc/watcher.py Ver fichero

@@ -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 = "<Watcher {0}!{1} at {2}:{3}>"
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":


+ 10
- 0
earwigbot/managers.py Ver fichero

@@ -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():


+ 10
- 0
earwigbot/tasks/__init__.py Ver fichero

@@ -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 = "<Task {0} ({1}) of {2}>"
return res.format(self.name, self.number, self.bot)

def setup(self):
"""Hook called immediately after the task is loaded.



+ 9
- 0
earwigbot/wiki/sitesdb.py Ver fichero

@@ -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 "<SitesDB at {0}>".format(self._sitesdb)

def _get_cookiejar(self):
"""Return a LWPCookieJar object loaded from our .cookies file.



Cargando…
Cancelar
Guardar