# Path: regenesis/queries.py
# def get_all_statistics():
#     return list(statistic_table)
# 
# def get_all_dimensions():
#     return list(dimension_table)
# 
# Path: regenesis/queries.py
# def get_cubes(cube_name=None):
#     ct = cube_table.table.alias('cube')
#     st = statistic_table.table.alias('statistic')
#     q = ct.join(st, st.c.name==ct.c.statistic_name)
#     q = q.select(use_labels=True)
#     if cube_name is not None:
#         q = q.where(ct.c.name==cube_name)
#     return list(engine.query(q))
# 
# def query_cube(cube_name, readable=True):
#     cube = get_cube(cube_name)
#     dimensions = get_dimensions(cube_name)
# 
#     fact_table = get_fact_table(cube_name).table.alias('fact')
#     q = fact_table.select()
#     selects, wheres, tables = [], [], [fact_table]
# 
#     if not readable:
#         selects.append(fact_table.columns['fact_id'].label('REGENESIS_ID'))
# 
#     params = {}
#     for dim in dimensions:
#         name = dim.get('dim_name')
#         field = name.upper()
#         title = dim.get('dim_title_de')
# 
#         if readable:
#             unit = dim.get('ref_unit_name')
#             if unit is not None:
#                 field = '%s; %s' % (field, unit)
#             field = '%s (%s)' % (title, field)
# 
#         type_ = dim.get('ref_type')
#         if type_ == 'measure':
#             selects.append(fact_table.columns[name].label(field))
#             if not readable:
#                 selects.append(fact_table.columns[name + "_quality"].label(field + '_QUALITY'))
#                 selects.append(fact_table.columns[name + "_error"].label(field + '_ERROR'))
#         if type_ == 'time':
#             selects.append(fact_table.columns[name].label(field))
#             if not readable:
#                 selects.append(fact_table.columns[name + '_from'].label(field + '_FROM'))
#                 selects.append(fact_table.columns[name + '_until'].label(field + '_UNTIL'))
#         elif type_ == 'axis':
#             vt = value_table.table.alias('value_%s' % name)
#             id_col = field + ' - ID' if readable else field + '_CODE'
#             selects.append(vt.c.name.label(id_col))
#             selects.append(vt.c.title_de.label(field))
#             tables.append(vt)
#             params[name] = name
#             wheres.append(vt.c.dimension_name==bindparam(name, value=name))
#             wheres.append(vt.c.value_id==fact_table.c[name])
# 
#     q = select(selects, and_(*wheres), tables)
#     return q, params
#     #return engine.query(q)
#     #pprint(list(engine.query(q)))
# 
# Path: regenesis/database.py
# def get_fact_table(cube_name):
# def load_cube(cube, update=False):
# 
# Path: regenesis/util.py
# def make_key(*a):
# def flatten(d, sep='_'):
# 
# Path: regenesis/web.py
# def text_filter(s):
# def text_filter_wrapped(s):
# def slugify(text):
# def set_template_globals():
# def dimension_type_text(type_name):
# def nop():
# def page_api():
# def page_faq():
# def page_contact():
# def index():
# 
# Path: regenesis/core.py
# def get_catalog(catalog_name):


import os
import shutil
from dataset import freeze
from regenesis.queries import get_all_statistics, get_all_dimensions
from regenesis.queries import get_cubes, query_cube
from regenesis.database import value_table
from regenesis.util import slugify
from regenesis.web import app
from regenesis.core import engine
    return os.path.join( app.root_path, '..', 'build')
    #return '/Users/fl/tmp/regenesis'

def freeze_request(req_path):
    print "Freezing %s..." % req_path
    path = os.path.join(get_output_dir(), req_path.lstrip('/'))
    dirname = os.path.dirname(path)
    if not os.path.exists(dirname):
        os.makedirs(dirname)
    fh = open(path, 'w')
    res = client.get(req_path)
    fh.write(res.data)
    fh.close()


def freeze_html():
    print "Copying /static..."
    outdir = os.path.join(get_output_dir(), 'static')
    if os.path.isdir(outdir):
        shutil.rmtree(outdir)
    shutil.copytree(app.static_folder, outdir)
    freeze_request('/index.html')
    freeze_request('/faq.html')
    freeze_request('/api.html')
    freeze_request('/contact.html')
    for catalog in ['regional']:
        freeze_request('/%s/index.html' % catalog)
        for dimension in get_all_dimensions():
            freeze_request('/%s/dimensions/%s.html' % (catalog, dimension['name']))
        for statistic in get_all_statistics():

# Path: regenesis/queries.py
# def get_all_statistics():
#     return list(statistic_table)
# 
# def get_all_dimensions():
#     return list(dimension_table)
# 
# Path: regenesis/queries.py
# def get_cubes(cube_name=None):
#     ct = cube_table.table.alias('cube')
#     st = statistic_table.table.alias('statistic')
#     q = ct.join(st, st.c.name==ct.c.statistic_name)
#     q = q.select(use_labels=True)
#     if cube_name is not None:
#         q = q.where(ct.c.name==cube_name)
#     return list(engine.query(q))
# 
# def query_cube(cube_name, readable=True):
#     cube = get_cube(cube_name)
#     dimensions = get_dimensions(cube_name)
# 
#     fact_table = get_fact_table(cube_name).table.alias('fact')
#     q = fact_table.select()
#     selects, wheres, tables = [], [], [fact_table]
# 
#     if not readable:
#         selects.append(fact_table.columns['fact_id'].label('REGENESIS_ID'))
# 
#     params = {}
#     for dim in dimensions:
#         name = dim.get('dim_name')
#         field = name.upper()
#         title = dim.get('dim_title_de')
# 
#         if readable:
#             unit = dim.get('ref_unit_name')
#             if unit is not None:
#                 field = '%s; %s' % (field, unit)
#             field = '%s (%s)' % (title, field)
# 
#         type_ = dim.get('ref_type')
#         if type_ == 'measure':
#             selects.append(fact_table.columns[name].label(field))
#             if not readable:
#                 selects.append(fact_table.columns[name + "_quality"].label(field + '_QUALITY'))
#                 selects.append(fact_table.columns[name + "_error"].label(field + '_ERROR'))
#         if type_ == 'time':
#             selects.append(fact_table.columns[name].label(field))
#             if not readable:
#                 selects.append(fact_table.columns[name + '_from'].label(field + '_FROM'))
#                 selects.append(fact_table.columns[name + '_until'].label(field + '_UNTIL'))
#         elif type_ == 'axis':
#             vt = value_table.table.alias('value_%s' % name)
#             id_col = field + ' - ID' if readable else field + '_CODE'
#             selects.append(vt.c.name.label(id_col))
#             selects.append(vt.c.title_de.label(field))
#             tables.append(vt)
#             params[name] = name
#             wheres.append(vt.c.dimension_name==bindparam(name, value=name))
#             wheres.append(vt.c.value_id==fact_table.c[name])
# 
#     q = select(selects, and_(*wheres), tables)
#     return q, params
#     #return engine.query(q)
#     #pprint(list(engine.query(q)))
# 
# Path: regenesis/database.py
# def get_fact_table(cube_name):
# def load_cube(cube, update=False):
# 
# Path: regenesis/util.py
# def make_key(*a):
# def flatten(d, sep='_'):
# 
# Path: regenesis/web.py
# def text_filter(s):
# def text_filter_wrapped(s):
# def slugify(text):
# def set_template_globals():
# def dimension_type_text(type_name):
# def nop():
# def page_api():
# def page_faq():
# def page_contact():
# def index():
# 
# Path: regenesis/core.py
# def get_catalog(catalog_name):


# Path: regenesis/freeze.py
import os
import shutil
from dataset import freeze
from regenesis.queries import get_all_statistics, get_all_dimensions
from regenesis.queries import get_cubes, query_cube
from regenesis.database import value_table
from regenesis.util import slugify
from regenesis.web import app
from regenesis.core import engine

    return os.path.join( app.root_path, '..', 'build')
    #return '/Users/fl/tmp/regenesis'

def freeze_request(req_path):
    print "Freezing %s..." % req_path
    path = os.path.join(get_output_dir(), req_path.lstrip('/'))
    dirname = os.path.dirname(path)
    if not os.path.exists(dirname):
        os.makedirs(dirname)
    fh = open(path, 'w')
    res = client.get(req_path)
    fh.write(res.data)
    fh.close()


def freeze_html():
    print "Copying /static..."
    outdir = os.path.join(get_output_dir(), 'static')
    if os.path.isdir(outdir):
        shutil.rmtree(outdir)
    shutil.copytree(app.static_folder, outdir)
    freeze_request('/index.html')
    freeze_request('/faq.html')
    freeze_request('/api.html')
    freeze_request('/contact.html')
    for catalog in ['regional']:
        freeze_request('/%s/index.html' % catalog)
        for dimension in get_all_dimensions():
            freeze_request('/%s/dimensions/%s.html' % (catalog, dimension['name']))
        for statistic in get_all_statistics():

            slug = slugify(statistic['title_de'])