Skip to content

Commit

Permalink
[chanlogs] Don't encode to utf-8 on Python 3
Browse files Browse the repository at this point in the history
encoding a string to utf-8 on Python 3 won't work and will cause this module to
fail.
  • Loading branch information
Elad Alfassa committed Apr 29, 2014
1 parent c3b8bb3 commit a693b17
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions chanlogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import os
import os.path
import threading
import sys
from datetime import datetime
import willie.module
import willie.tools
Expand Down Expand Up @@ -63,12 +64,16 @@ def _format_template(tpl, bot, **kwargs):
if not bot.config.chanlogs.microseconds:
dt = dt.replace(microsecond=0)

return tpl.format(
formatted = tpl.format(
origin=bot.origin, datetime=dt.isoformat(),
date=dt.date().isoformat(), time=dt.time().isoformat(),
**kwargs
) + "\n"

if sys.version_info.major < 3 and isinstance(formatted, unicode):
formatted = formatted.encode('utf-8')
return formatted


def setup(bot):
if not getattr(bot.config, "chanlogs", None):
Expand Down Expand Up @@ -106,7 +111,7 @@ def log_message(bot, message):
fpath = get_fpath(bot)
with bot.memory['chanlog_locks'][fpath]:
with open(fpath, "a") as f:
f.write(logline.encode('utf-8'))
f.write(logline)


@willie.module.rule('.*')
Expand All @@ -118,7 +123,7 @@ def log_join(bot, trigger):
fpath = get_fpath(bot, channel=trigger)
with bot.memory['chanlog_locks'][fpath]:
with open(fpath, "a") as f:
f.write(logline.encode('utf-8'))
f.write(logline)


@willie.module.rule('.*')
Expand All @@ -130,7 +135,7 @@ def log_part(bot, trigger):
fpath = get_fpath(bot, channel=trigger)
with bot.memory['chanlog_locks'][fpath]:
with open(fpath, "a") as f:
f.write(logline.encode('utf-8'))
f.write(logline)


@willie.module.rule('.*')
Expand All @@ -149,7 +154,7 @@ def log_quit(bot, trigger):
fpath = get_fpath(bot, channel)
with bot.memory['chanlog_locks'][fpath]:
with open(fpath, "a") as f:
f.write(logline.encode('utf-8'))
f.write(logline)


@willie.module.rule('.*')
Expand All @@ -168,4 +173,4 @@ def log_nick_change(bot, trigger):
fpath = get_fpath(bot, channel)
with bot.memory['chanlog_locks'][fpath]:
with open(fpath, "a") as f:
f.write(logline.encode('utf-8'))
f.write(logline)

0 comments on commit a693b17

Please sign in to comment.