#-*- coding: utf-8 -*-
#from __future__ import unicode_literals
#from __future__ import print_function

from jinja2 import Template
import re
from config import PREDEFINED_VARIABLE_TABLE
from links import Link

class ParserException(Exception):
    pass


def to_unicode(val):
    if type(val) == unicode:
        return val

    elif type(val) == str:
        try:
            return val.decode('utf-8')
        except UnicodeDecodeError:
            return val.decode('euc-kr')
    else:
        raise AssertionError('not valid type')

class VariableConverter(object):
    def __init__(self, var_table):
        self.var_table = var_table

    def run(self, text):
        template = Template(
            text,
        )
        output = template.render(**self.var_table)
        return output

class LineParser(object):
    PROG = re.compile(r'(?P<src>.+)->(?P<dst>.+)')

    def parse(self, line):
        line = line.strip()
        if not line:
            return False, None, None

        if line[0] == '#':
            return False, None, None

        m = self.PROG.match(line)
        if not m:
            raise ParserException('Not valid line : {}'.format(line))

        src = m.group('src').strip()
        dst = m.group('dst').strip()

        if not src:
            raise ParserException('Empty src : {}'.format(line))
        if not dst:
            raise ParserException('Empty dst : {}'.format(line))

        assert type(src) == unicode
        assert type(dst) == unicode
        return True, src, dst

def run(filename):
    with open(filename, 'rb') as f:
        content = f.read()
        content = to_unicode(content)
        assert type(content) == unicode

    var_converter = VariableConverter(PREDEFINED_VARIABLE_TABLE)
    content = var_converter.run(content)

    line_list = content.splitlines()

    line_parser = LineParser()
    for line in line_list:
        success, src, dst = line_parser.parse(line)
        if not success:
            continue
        link = Link(src, dst)
        link.create()

#-*- coding: utf-8 -*-
#from __future__ import unicode_literals
#from __future__ import print_function

from jinja2 import Template
import re
from config import PREDEFINED_VARIABLE_TABLE
from links import Link, LinkException

class ParserException(Exception):
    pass


def to_unicode(val):
    if type(val) == unicode:
        return val

    elif type(val) == str:
        try:
            return val.decode('utf-8')
        except UnicodeDecodeError:
            return val.decode('euc-kr')
    else:
        raise AssertionError('not valid type')

class VariableConverter(object):
    def __init__(self, var_table):
        self.var_table = var_table

    def run(self, text):
        template = Template(
            text,
        )
        output = template.render(**self.var_table)
        return output

class LineParser(object):
    PROG = re.compile(r'(?P<src>.+)->(?P<dst>.+)')

    def parse(self, line):
        line = line.strip()
        if not line:
            return False, None, None

        if line[0] == '#':
            return False, None, None

        m = self.PROG.match(line)
        if not m:
            raise ParserException('Not valid line : {}'.format(line))

        src = m.group('src').strip()
        dst = m.group('dst').strip()

        if not src:
            raise ParserException('Empty src : {}'.format(line))
        if not dst:
            raise ParserException('Empty dst : {}'.format(line))

        assert type(src) == unicode
        assert type(dst) == unicode
        return True, src, dst

def run(filename):
    with open(filename, 'rb') as f:
        content = f.read()
        content = to_unicode(content)
        assert type(content) == unicode

    var_converter = VariableConverter(PREDEFINED_VARIABLE_TABLE)
    content = var_converter.run(content)

    line_list = content.splitlines()

    line_parser = LineParser()
    for line in line_list:
        success, src, dst = line_parser.parse(line)
        if not success:
            continue
        link = Link(src, dst)
        try:
            link.create()
        except LinkException as e:
            print(e.message)

support failover