Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the option to use the bot's preferred timezone for logs. #737

Merged
merged 2 commits into from
Feb 15, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions willie/modules/chanlogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
import threading
import sys
from datetime import datetime
try:
from pytz import timezone
from pytz import all_timezones
import pytz
except ImportError:
pytz = None
import willie.module
import willie.tools
from willie.config import ConfigurationError
Expand All @@ -36,10 +42,25 @@ def configure(config):
config.add_option("chanlogs", "by_day", "Split log files by day", default=True)
config.add_option("chanlogs", "privmsg", "Record private messages", default=False)
config.add_option("chanlogs", "microseconds", "Microsecond precision", default=False)
config.add_option("chanlogs", "localtime", "Attempt to use preferred timezone", default=False)
# could ask if user wants to customize message templates,
# but that seems unnecessary


def get_datetime(bot):
"""
Returns a datetime object of the current time.
"""
dt = datetime.utcnow()
if pytz:
dt = dt.replace(tzinfo=timezone('UTC'))
if bot.config.chanlogs.localtime:
dt = dt.astimezone(timezone(bot.config.clock.tz))
if not bot.config.chanlogs.microseconds:
dt = dt.replace(microsecond=0)
return dt


def get_fpath(bot, trigger, channel=None):
"""
Returns a string corresponding to the path to the file where the message
Expand All @@ -49,9 +70,7 @@ def get_fpath(bot, trigger, channel=None):
channel = channel or trigger.sender
channel = channel.lstrip("#")

dt = datetime.utcnow()
if not bot.config.chanlogs.microseconds:
dt = dt.replace(microsecond=0)
dt = get_datetime(bot)
if bot.config.chanlogs.by_day:
fname = "{channel}-{date}.log".format(channel=channel, date=dt.date().isoformat())
else:
Expand All @@ -60,9 +79,7 @@ def get_fpath(bot, trigger, channel=None):


def _format_template(tpl, bot, trigger, **kwargs):
dt = datetime.utcnow()
if not bot.config.chanlogs.microseconds:
dt = dt.replace(microsecond=0)
dt = get_datetime(bot)

formatted = tpl.format(
trigger=trigger, datetime=dt.isoformat(),
Expand Down