# Path: logviewer/exc.py
# class AuthenticationError(RuntimeError):
#     pass
# 
# Path: logviewer/parser.py
# def parse_log(fp, start=None):
#     def extract_time(match):
#         t = match.group('timestamp')
#         try:
#             # ISO 8601
#             timestamp = t.split('.')[0]
#             return datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S')
#         except ValueError:
#             # RFC 2822
#             t = email.utils.parsedate(t)
#             if t is not None:
#             	t = time.mktime(t)
#             else:
#             	t = 0 # fail
#             return datetime.fromtimestamp(t)
# 
#     no = 0
#     for line in fp:
#         m = LINE_PATTERN.match(line)
#         if not m:
#             continue
# 
#         if 'PRIVMSG' in m.group('data'):
#             no += 1
# 
#             if start and no < start:
#                 continue
#             pm = PRIVMSG_PATTERN.match(m.group('data'))
#             if not pm:
#                 continue
# 
#             is_bot = not pm.group('nick')
#             pmm = PROXY_MSG_PATTERN.match(pm.group('text'))
#             if is_bot and pmm:
#                 nick = pmm.group('nick')
#                 text = pmm.group('text')
#                 is_bot = False
#             else:
#                 nick = pm.group('nick')
#                 text = pm.group('text')
# 
#             yield dict(type='privmsg', no=no,
#                 time=extract_time(m),
#                 nick=nick, text=text, is_bot=is_bot)
#         elif (m.group('dir') == '>>>' and start is None and
#               m.group('data').startswith('JOIN ') and
#               JOIN_PATTERN.match(m.group('data'))):
#             yield dict(type='join', time=extract_time(m), is_bot=True, no=-1)
# 
# Path: logviewer/routing.py
# class DateConverter(BaseConverter):
#     def to_python(self, value):
#     def to_url(self, value):
# 
# Path: logviewer/util.py
# def irc_channels(config):


import os
import io
import re
import time
import json
import random
import datetime
import itertools
import functools
import sqlite3
import sphinxapi
import flask
from contextlib import closing
from flask import (Flask, request, redirect, session, url_for, render_template,
                   current_app, jsonify)
from logviewer.exc import AuthenticationError
from logviewer.parser import parse_log
from logviewer import routing, util
class Log(object):
    def __init__(self, channel, date):
        if not channel.startswith('#'):
            raise ValueError()
        self.name = channel[1:]
        self.date = date

    @property
    def is_today(self):
        return self.date == datetime.date.today()

    @property
    def path(self):
        path = os.path.join(app.config['LOG_DIR'], self.name + '.log')
        if not self.is_today:
            return path + '.' + self.date.strftime('%Y%m%d')
        else:
            return path

    @property
    def exists(self):
        return os.path.isfile(self.path)

    def url(self, recent=None, **kwargs):
        return url_for('log', channel=self.name, date=self.date, recent=recent, **kwargs)

    def get_messages(self, start=None):
        if not self.exists:
            return
        with io.open(self.path, encoding='utf-8', errors='replace') as fp:

# Path: logviewer/exc.py
# class AuthenticationError(RuntimeError):
#     pass
# 
# Path: logviewer/parser.py
# def parse_log(fp, start=None):
#     def extract_time(match):
#         t = match.group('timestamp')
#         try:
#             # ISO 8601
#             timestamp = t.split('.')[0]
#             return datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S')
#         except ValueError:
#             # RFC 2822
#             t = email.utils.parsedate(t)
#             if t is not None:
#             	t = time.mktime(t)
#             else:
#             	t = 0 # fail
#             return datetime.fromtimestamp(t)
# 
#     no = 0
#     for line in fp:
#         m = LINE_PATTERN.match(line)
#         if not m:
#             continue
# 
#         if 'PRIVMSG' in m.group('data'):
#             no += 1
# 
#             if start and no < start:
#                 continue
#             pm = PRIVMSG_PATTERN.match(m.group('data'))
#             if not pm:
#                 continue
# 
#             is_bot = not pm.group('nick')
#             pmm = PROXY_MSG_PATTERN.match(pm.group('text'))
#             if is_bot and pmm:
#                 nick = pmm.group('nick')
#                 text = pmm.group('text')
#                 is_bot = False
#             else:
#                 nick = pm.group('nick')
#                 text = pm.group('text')
# 
#             yield dict(type='privmsg', no=no,
#                 time=extract_time(m),
#                 nick=nick, text=text, is_bot=is_bot)
#         elif (m.group('dir') == '>>>' and start is None and
#               m.group('data').startswith('JOIN ') and
#               JOIN_PATTERN.match(m.group('data'))):
#             yield dict(type='join', time=extract_time(m), is_bot=True, no=-1)
# 
# Path: logviewer/routing.py
# class DateConverter(BaseConverter):
#     def to_python(self, value):
#     def to_url(self, value):
# 
# Path: logviewer/util.py
# def irc_channels(config):


# Path: logviewer/app.py
import os
import io
import re
import time
import json
import random
import datetime
import itertools
import functools
import sqlite3
import sphinxapi
import flask
from contextlib import closing
from flask import (Flask, request, redirect, session, url_for, render_template,
                   current_app, jsonify)
from logviewer.exc import AuthenticationError
from logviewer.parser import parse_log
from logviewer import routing, util

class Log(object):
    def __init__(self, channel, date):
        if not channel.startswith('#'):
            raise ValueError()
        self.name = channel[1:]
        self.date = date

    @property
    def is_today(self):
        return self.date == datetime.date.today()

    @property
    def path(self):
        path = os.path.join(app.config['LOG_DIR'], self.name + '.log')
        if not self.is_today:
            return path + '.' + self.date.strftime('%Y%m%d')
        else:
            return path

    @property
    def exists(self):
        return os.path.isfile(self.path)

    def url(self, recent=None, **kwargs):
        return url_for('log', channel=self.name, date=self.date, recent=recent, **kwargs)

    def get_messages(self, start=None):
        if not self.exists:
            return
        with io.open(self.path, encoding='utf-8', errors='replace') as fp:

            for msg in parse_log(fp, start):