# Path: regenesis/core.py
# def get_catalog(catalog_name):
# 
# Path: regenesis/views/util.py
# def dimension_type_text(type_name):
#     if type_name.startswith('ZI-MM'):
#         return 'Zeitattribut'
#     if type_name.startswith('K-REG'):
#         return 'Geographisches Attribut'
#     if type_name.startswith('K-SACH'):
#         return 'Sachattribut'
#     if type_name.startswith('W-MM'):
#         return 'Messwert'
#     return type_name
# 
# Path: regenesis/database.py
# def get_fact_table(cube_name):
# def load_cube(cube, update=False):
# 
# Path: regenesis/queries.py
# def get_dimensions(cube_name):
#     rt = reference_table.table.alias('ref')
#     dt = dimension_table.table.alias('dim')
#     q = rt.join(dt, rt.c.dimension_name==dt.c.name)
#     q = q.select(use_labels=True)
#     q = q.where(rt.c.cube_name==cube_name)
#     res = engine.query(q)
#     return list(res)
# 
# Path: regenesis/views/util.py
# def parse_description(description):
#     description = '\n' + description
#     parts = SPLITTER.split(description)[1:]
#     data = {'method': 'Keine Angaben.', 'type': 'Keine Angaben.'}
#     for i in range(0, len(parts), 2):
#         section, text = parts[i], parts[i+1].strip()
#         if 'Art der Statistik' in section:
#             data['type'] = text
#         elif 'Rechtsgrundlage' in section:
#             data['legal'] = text
#         elif 'Bereitstellung' in section:
#             data['periodicity'] = text
#         elif 'Stichtag' in section:
#             data['stag'] = text
#         elif 'Regionalebene' in section:
#             data['geo'] = text
#         elif 'Methodische' in section:
#             data['method'] = text
#     return data


import re
from flask import Blueprint, render_template
from collections import defaultdict
from regenesis.core import app, engine, get_catalog
from regenesis.views.util import dimension_type_text
from regenesis.database import statistic_table, cube_table
from regenesis.queries import get_dimensions
from regenesis.views.util import parse_description
    keywords = set()
    for t in titles:
        t = re.sub('\(.*\)', '', t)
        for s in re.split('[\s\.\/]', t):
            s = s.strip()
            if not len(s) > 2 or s[0] != s[0].upper():
                continue
            keywords.add(s)
    return ', '.join(keywords)

def get_cubes(statistic_name=None):
    q = cube_table.table.select()
    q = q.where(cube_table.table.c.statistic_name==statistic_name)
    return list(engine.query(q))


@blueprint.route('/<catalog>/statistics/<slug>.<name>.html')
def view(catalog, slug, name):
    catalog = get_catalog(catalog)
    statistic = statistic_table.find_one(name=name)
    desc = parse_description(statistic['description_de'] or '')
    cubes = []
    dims = defaultdict(int)
    titles = set([statistic['title_de']])
    for cube in get_cubes(name):
      cube['dimensions'] = get_dimensions(cube['name'])
      for dim in cube['dimensions']:
        dim['show'] = True
        dims[dim['dim_name']] += 1
        titles.add(dim['dim_title_de'])

# Path: regenesis/core.py
# def get_catalog(catalog_name):
# 
# Path: regenesis/views/util.py
# def dimension_type_text(type_name):
#     if type_name.startswith('ZI-MM'):
#         return 'Zeitattribut'
#     if type_name.startswith('K-REG'):
#         return 'Geographisches Attribut'
#     if type_name.startswith('K-SACH'):
#         return 'Sachattribut'
#     if type_name.startswith('W-MM'):
#         return 'Messwert'
#     return type_name
# 
# Path: regenesis/database.py
# def get_fact_table(cube_name):
# def load_cube(cube, update=False):
# 
# Path: regenesis/queries.py
# def get_dimensions(cube_name):
#     rt = reference_table.table.alias('ref')
#     dt = dimension_table.table.alias('dim')
#     q = rt.join(dt, rt.c.dimension_name==dt.c.name)
#     q = q.select(use_labels=True)
#     q = q.where(rt.c.cube_name==cube_name)
#     res = engine.query(q)
#     return list(res)
# 
# Path: regenesis/views/util.py
# def parse_description(description):
#     description = '\n' + description
#     parts = SPLITTER.split(description)[1:]
#     data = {'method': 'Keine Angaben.', 'type': 'Keine Angaben.'}
#     for i in range(0, len(parts), 2):
#         section, text = parts[i], parts[i+1].strip()
#         if 'Art der Statistik' in section:
#             data['type'] = text
#         elif 'Rechtsgrundlage' in section:
#             data['legal'] = text
#         elif 'Bereitstellung' in section:
#             data['periodicity'] = text
#         elif 'Stichtag' in section:
#             data['stag'] = text
#         elif 'Regionalebene' in section:
#             data['geo'] = text
#         elif 'Methodische' in section:
#             data['method'] = text
#     return data


# Path: regenesis/views/statistic.py
import re
from flask import Blueprint, render_template
from collections import defaultdict
from regenesis.core import app, engine, get_catalog
from regenesis.views.util import dimension_type_text
from regenesis.database import statistic_table, cube_table
from regenesis.queries import get_dimensions
from regenesis.views.util import parse_description

    keywords = set()
    for t in titles:
        t = re.sub('\(.*\)', '', t)
        for s in re.split('[\s\.\/]', t):
            s = s.strip()
            if not len(s) > 2 or s[0] != s[0].upper():
                continue
            keywords.add(s)
    return ', '.join(keywords)

def get_cubes(statistic_name=None):
    q = cube_table.table.select()
    q = q.where(cube_table.table.c.statistic_name==statistic_name)
    return list(engine.query(q))


@blueprint.route('/<catalog>/statistics/<slug>.<name>.html')
def view(catalog, slug, name):
    catalog = get_catalog(catalog)
    statistic = statistic_table.find_one(name=name)
    desc = parse_description(statistic['description_de'] or '')
    cubes = []
    dims = defaultdict(int)
    titles = set([statistic['title_de']])
    for cube in get_cubes(name):
      cube['dimensions'] = get_dimensions(cube['name'])
      for dim in cube['dimensions']:
        dim['show'] = True
        dims[dim['dim_name']] += 1
        titles.add(dim['dim_title_de'])

        dim['type_text'] = dimension_type_text(dim['dim_measure_type'])