# Path: pg_export/filters.py
# def untype_default(default, column_type):
#     return default.replace("'::"+column_type, "'") \
#                   .replace("'::public."+column_type[-1], "'") \
#                   .replace("'::"+column_type.split('.')[-1], "'")
# 
# def ljust(string, width, fillchar):
#     return string.ljust(width, fillchar)
# 
# def rjust(string, width, fillchar):
#     return string.rjust(width, fillchar)
# 
# def join_attr(ittr, attribute, delimiter):
#     return delimiter.join(i.get(attribute) for i in ittr)
# 
# def concat_items(l1, s, l2):
#     return ['%s%s%s' % (i1, s, i2) for i1, i2 in zip(l1, l2)]


import sys
import os
from jinja2 import Environment, FileSystemLoader
from pg_export.filters import (untype_default, ljust,
                               rjust, join_attr, concat_items)
# -*- coding:utf-8 -*-



class Renderer:
    def __init__(self, fork, version):
        base_path = os.path.join(os.path.dirname(__file__), 'templates')

        path = [fork + '.'.join(version)]
        for i in reversed(range(1, len(version))):
            path.append(fork + '.'.join(version[:i] + ('x',)))

        path = [os.path.join(base_path, p) for p in path]

        if not any(os.path.isdir(p) for p in path):
            raise Exception('Version not suported: template not found:\n' +
                            '\n'.join(path))

        path.append(os.path.join(base_path, 'base'))

        self.env = Environment(
            loader=FileSystemLoader([
                self.join_path(os.path.dirname(__file__), 'templates', p)
                for p in path]))

        self.env.filters['untype_default'] = untype_default
        self.env.filters['ljust'] = ljust
        self.env.filters['rjust'] = rjust

# Path: pg_export/filters.py
# def untype_default(default, column_type):
#     return default.replace("'::"+column_type, "'") \
#                   .replace("'::public."+column_type[-1], "'") \
#                   .replace("'::"+column_type.split('.')[-1], "'")
# 
# def ljust(string, width, fillchar):
#     return string.ljust(width, fillchar)
# 
# def rjust(string, width, fillchar):
#     return string.rjust(width, fillchar)
# 
# def join_attr(ittr, attribute, delimiter):
#     return delimiter.join(i.get(attribute) for i in ittr)
# 
# def concat_items(l1, s, l2):
#     return ['%s%s%s' % (i1, s, i2) for i1, i2 in zip(l1, l2)]


# Path: pg_export/renderer.py
import sys
import os
from jinja2 import Environment, FileSystemLoader
from pg_export.filters import (untype_default, ljust,
                               rjust, join_attr, concat_items)

# -*- coding:utf-8 -*-



class Renderer:
    def __init__(self, fork, version):
        base_path = os.path.join(os.path.dirname(__file__), 'templates')

        path = [fork + '.'.join(version)]
        for i in reversed(range(1, len(version))):
            path.append(fork + '.'.join(version[:i] + ('x',)))

        path = [os.path.join(base_path, p) for p in path]

        if not any(os.path.isdir(p) for p in path):
            raise Exception('Version not suported: template not found:\n' +
                            '\n'.join(path))

        path.append(os.path.join(base_path, 'base'))

        self.env = Environment(
            loader=FileSystemLoader([
                self.join_path(os.path.dirname(__file__), 'templates', p)
                for p in path]))

        self.env.filters['untype_default'] = untype_default
        self.env.filters['ljust'] = ljust
        self.env.filters['rjust'] = rjust

        self.env.filters['join_attr'] = join_attr