#!/usr/bin/env python

import sys

try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO  # NOQA

from socket import gaierror, gethostbyname, gethostname

from circuits.web import Controller, Logger

from .helpers import urlopen


class DummyLogger(object):

    def __init__(self):
        super(DummyLogger, self).__init__()

        self.message = None

    def info(self, message):
        self.message = message


class Root(Controller):

    def index(self):
        return "Hello World!"


def test_file(webapp):
    logfile = StringIO()
    logger = Logger(file=logfile)
    logger.register(webapp)

    f = urlopen(webapp.server.http.base)
    s = f.read()
    assert s == b"Hello World!"

    logfile.seek(0)
    s = logfile.read().strip()

    try:
        address = gethostbyname(gethostname())
    except gaierror:
        address = "127.0.0.1"

    d = {}
    d["h"] = address
    d["l"] = "-"
    d["u"] = "-"
    d["r"] = "GET / HTTP/1.1"
    d["s"] = "200"
    d["b"] = "12"
    d["f"] = ""
    d["a"] = "Python-urllib/%s" % sys.version[:3]

    keys = list(d.keys())

    for k in keys:
        assert d[k] in s

    logfile.close()
    logger.unregister()


def test_logger(webapp):
    logobj = DummyLogger()
    logger = Logger(logger=logobj)
    logger.register(webapp)

    f = urlopen(webapp.server.http.base)
    s = f.read()
    assert s == b"Hello World!"

    s = logobj.message

    try:
        address = gethostbyname(gethostname())
    except gaierror:
        address = "127.0.0.1"

    d = {}
    d["h"] = address
    d["l"] = "-"
    d["u"] = "-"
    d["r"] = "GET / HTTP/1.1"
    d["s"] = "200"
    d["b"] = "12"
    d["f"] = ""
    d["a"] = "Python-urllib/%s" % sys.version[:3]

    keys = list(d.keys())

    for k in keys:
        assert d[k] in s

    logger.unregister()


def test_filename(webapp, tmpdir):
    logfile = str(tmpdir.ensure("logfile"))
    logger = Logger(file=logfile)
    logger.register(webapp)

    logfile = open(logfile, "r")

    f = urlopen(webapp.server.http.base)
    s = f.read()
    assert s == b"Hello World!"

    logfile.seek(0)
    s = logfile.read().strip()

    try:
        address = gethostbyname(gethostname())
    except gaierror:
        address = "127.0.0.1"

    d = {}
    d["h"] = address
    d["l"] = "-"
    d["u"] = "-"
    d["r"] = "GET / HTTP/1.1"
    d["s"] = "200"
    d["b"] = "12"
    d["f"] = ""
    d["a"] = "Python-urllib/%s" % sys.version[:3]

    keys = list(d.keys())

    for k in keys:
        assert d[k] in s

    logfile.close()
    logger.unregister()

@@ -26,16 +26,26 @@
 port sys
+%0Aimport re
 %0A%0Atry:%0A 
@@ -1133,32 +1133,195 @@
  for k in keys:%0A
+        if d%5Bk%5D and d%5Bk%5D.startswith(%22127.%22):%0A            # loopback network: 127.0.0.0/8%0A            assert re.search(%22127(%5C.%5B0-9%5D%7B1,3%7D)%7B3%7D%22, s)%0A        else:%0A    
         assert d
@@ -1321,32 +1321,32 @@
 ssert d%5Bk%5D in s%0A
-
 %0A    logfile.clo
@@ -1953,32 +1953,195 @@
  for k in keys:%0A
+        if d%5Bk%5D and d%5Bk%5D.startswith(%22127.%22):%0A            # loopback network: 127.0.0.0/8%0A            assert re.search(%22127(%5C.%5B0-9%5D%7B1,3%7D)%7B3%7D%22, s)%0A        else:%0A    
         assert d
@@ -2141,32 +2141,32 @@
 ssert d%5Bk%5D in s%0A
-
 %0A    logger.unre
@@ -2841,32 +2841,195 @@
  for k in keys:%0A
+        if d%5Bk%5D and d%5Bk%5D.startswith(%22127.%22):%0A            # loopback network: 127.0.0.0/8%0A            assert re.search(%22127(%5C.%5B0-9%5D%7B1,3%7D)%7B3%7D%22, s)%0A        else:%0A    
         assert d

fix test: tests/web/test_logger.py