# Path: regenesis/mappings.py
# KEYS_TRANSLATE = {
#     'wert': 'value',
#     'wert_verfaelscht': 'value_error',
#     'qualitaet': 'quality',
#     'gesperrt': 'locked',
#     'fach_schl': 'key',
#     'zi_wert': 'ts_value',
#     'ktx': 'title_de',
#     'ltx': 'long_title_de',
#     'erl': 'description_de',
#     'ktx_2': 'title_en',
#     'ltx_2': 'long_title_en',
#     'erl_2': 'description_en',
#     'def': 'definition_de',
#     'def_2': 'definition_en',
#     'me_name': 'measure_name_de',
#     'me_name_2': 'measure_name_en',
#     'notizen': 'notes',
#     'siehe_fach_schl': 'see_key',
#     'pzt': 'periodicity',
#     'guelt_ab': 'valid_from',
#     'guelt_bis': 'valid_until',
#     '"mit werten"': 'includes_values',
#     'typ': 'type',
#     'nkm_stellen': 'float_precision',
#     'me_name': 'unit_name',
#     'mm_typ': 'measure_type',
#     'ghm_werte_jn': 'secret_values',
#     'ober_bgr_jn': 'meta_variable',
#     'summierbar': 'summable',
#     'bestand': 'atemporal',
#     'max_sbr': 'max_width',
#     'dst': 'data_type',
#     'rhf_achse': 'axis_order',
#     'rhf_bsr': 'label_order',
#     'zi_rhf_achse': 'axis_order',
#     'zi_rhf_bsr': 'label_order'
#     }
# 
# KEYS_IGNORE = ['spr_tmp', 'trans_flag_2', 'genesis_vbd', 'regiostat', 'eu_vbd', 'max_width']
# 
# KEYS_LOCALIZED = ['title_en', 'long_title_en', 'definition_en', 'description_en', 'me_name_2']
# 
# Path: regenesis/formats.py
# def parse_date(date_text):
#     m = YEAR.match(date_text)
#     if m is not None:
#         year = int(m.group(1))
#         return (datetime(year, 1, 1), datetime(year+1, 1, 1)-second)
#     m = DATE.match(date_text)
#     if m is not None:
#         year, month, day = int(m.group(3)), int(m.group(2)), int(m.group(1))
#         return (datetime(year, month, day), datetime(year, month, day)+timedelta(days=1)-second)
#     m = DECADE.match(date_text)
#     if m is not None:
#         begin, end = int(m.group(1)), int(m.group(2))
#         return (datetime(begin, 1, 1), datetime(end+1, 1, 1)-second)
#     m = YEARS.match(date_text)
#     if m is not None:
#         begin, end = int(m.group(1)), int(m.group(1)[:2] + m.group(2))
#         return (datetime(begin, 1, 1), datetime(end+1, 1, 1)-second)
#     m = SEMESTER_WS.match(date_text)
#     if m is not None:
#         year = int(m.group(1))
#         return (datetime(year, 10, 1), datetime(year+1, 4, 1)-second)
#     m = SEMESTER_SS.match(date_text)
#     if m is not None:
#         year = int(m.group(1))
#         return (datetime(year, 4, 1), datetime(year, 10, 1)-second)
#     m = WEEK.match(date_text)
#     if m is not None:
#         week, year = int(m.group(1)), int(m.group(2))
#         some_day = datetime(year, 1, 1) + timedelta(weeks=week-1)
#         return some_day - timedelta(days=some_day.weekday()), \
#                some_day + timedelta(days=7-some_day.weekday()) - second
#     m = Q1.match(date_text)
#     if m is not None:
#         year = int(m.group(1))
#         return (datetime(year, 1, 1), datetime(year, 4, 1)-second)
#     m = Q2.match(date_text)
#     if m is not None:
#         year = int(m.group(1))
#         return (datetime(year, 4, 1), datetime(year, 7, 1)-second)
#     m = Q3.match(date_text)
#     if m is not None:
#         year = int(m.group(1))
#         return (datetime(year, 7, 1), datetime(year, 10, 1)-second)
#     m = Q4.match(date_text)
#     if m is not None:
#         year = int(m.group(1))
#         return (datetime(year, 10, 1), datetime(year+1, 1, 1)-second)
#     return (None, None)
# 
# def parse_bool(bool_text):
#     return bool_text == 'J'
# 
# Path: regenesis/util.py
# def make_key(*a):
#     parts = []
#     for part in a:
#         if isinstance(part, datetime):
#             part = part.isoformat()
#         elif part is None:
#             part = '**'
#         else:
#             part = unicode(part)
#         parts.append(part)
#     return sha1('||'.join(parts)).hexdigest()
# 
# def flatten(d, sep='_'):
#     out = {}
#     for k, v in d.items():
#         if isinstance(v, dict):
#             for ik, iv in flatten(v, sep=sep).items():
#                 out[k + sep + ik] = iv
#         else:
#             out[k] = v
#     return out


from collections import defaultdict
from csv import reader
from StringIO import StringIO
from regenesis.mappings import KEYS_TRANSLATE, KEYS_IGNORE, KEYS_LOCALIZED
from regenesis.formats import parse_date, parse_bool
from regenesis.util import make_key, flatten


FIELD_TYPES = {
  'eu_vbd': parse_bool,
  'genesis_vbd': parse_bool,
  'regiostat': parse_bool,
  'secret_values': parse_bool,
  'spr_tmp': parse_bool,
  'regiostat': parse_bool,
  'trans_flag_2': parse_bool,
  'meta_variable': parse_bool,
  'summable': parse_bool,
  'atemporal': parse_bool,

# Path: regenesis/mappings.py
# KEYS_TRANSLATE = {
#     'wert': 'value',
#     'wert_verfaelscht': 'value_error',
#     'qualitaet': 'quality',
#     'gesperrt': 'locked',
#     'fach_schl': 'key',
#     'zi_wert': 'ts_value',
#     'ktx': 'title_de',
#     'ltx': 'long_title_de',
#     'erl': 'description_de',
#     'ktx_2': 'title_en',
#     'ltx_2': 'long_title_en',
#     'erl_2': 'description_en',
#     'def': 'definition_de',
#     'def_2': 'definition_en',
#     'me_name': 'measure_name_de',
#     'me_name_2': 'measure_name_en',
#     'notizen': 'notes',
#     'siehe_fach_schl': 'see_key',
#     'pzt': 'periodicity',
#     'guelt_ab': 'valid_from',
#     'guelt_bis': 'valid_until',
#     '"mit werten"': 'includes_values',
#     'typ': 'type',
#     'nkm_stellen': 'float_precision',
#     'me_name': 'unit_name',
#     'mm_typ': 'measure_type',
#     'ghm_werte_jn': 'secret_values',
#     'ober_bgr_jn': 'meta_variable',
#     'summierbar': 'summable',
#     'bestand': 'atemporal',
#     'max_sbr': 'max_width',
#     'dst': 'data_type',
#     'rhf_achse': 'axis_order',
#     'rhf_bsr': 'label_order',
#     'zi_rhf_achse': 'axis_order',
#     'zi_rhf_bsr': 'label_order'
#     }
# 
# KEYS_IGNORE = ['spr_tmp', 'trans_flag_2', 'genesis_vbd', 'regiostat', 'eu_vbd', 'max_width']
# 
# KEYS_LOCALIZED = ['title_en', 'long_title_en', 'definition_en', 'description_en', 'me_name_2']
# 
# Path: regenesis/formats.py
# def parse_date(date_text):
#     m = YEAR.match(date_text)
#     if m is not None:
#         year = int(m.group(1))
#         return (datetime(year, 1, 1), datetime(year+1, 1, 1)-second)
#     m = DATE.match(date_text)
#     if m is not None:
#         year, month, day = int(m.group(3)), int(m.group(2)), int(m.group(1))
#         return (datetime(year, month, day), datetime(year, month, day)+timedelta(days=1)-second)
#     m = DECADE.match(date_text)
#     if m is not None:
#         begin, end = int(m.group(1)), int(m.group(2))
#         return (datetime(begin, 1, 1), datetime(end+1, 1, 1)-second)
#     m = YEARS.match(date_text)
#     if m is not None:
#         begin, end = int(m.group(1)), int(m.group(1)[:2] + m.group(2))
#         return (datetime(begin, 1, 1), datetime(end+1, 1, 1)-second)
#     m = SEMESTER_WS.match(date_text)
#     if m is not None:
#         year = int(m.group(1))
#         return (datetime(year, 10, 1), datetime(year+1, 4, 1)-second)
#     m = SEMESTER_SS.match(date_text)
#     if m is not None:
#         year = int(m.group(1))
#         return (datetime(year, 4, 1), datetime(year, 10, 1)-second)
#     m = WEEK.match(date_text)
#     if m is not None:
#         week, year = int(m.group(1)), int(m.group(2))
#         some_day = datetime(year, 1, 1) + timedelta(weeks=week-1)
#         return some_day - timedelta(days=some_day.weekday()), \
#                some_day + timedelta(days=7-some_day.weekday()) - second
#     m = Q1.match(date_text)
#     if m is not None:
#         year = int(m.group(1))
#         return (datetime(year, 1, 1), datetime(year, 4, 1)-second)
#     m = Q2.match(date_text)
#     if m is not None:
#         year = int(m.group(1))
#         return (datetime(year, 4, 1), datetime(year, 7, 1)-second)
#     m = Q3.match(date_text)
#     if m is not None:
#         year = int(m.group(1))
#         return (datetime(year, 7, 1), datetime(year, 10, 1)-second)
#     m = Q4.match(date_text)
#     if m is not None:
#         year = int(m.group(1))
#         return (datetime(year, 10, 1), datetime(year+1, 1, 1)-second)
#     return (None, None)
# 
# def parse_bool(bool_text):
#     return bool_text == 'J'
# 
# Path: regenesis/util.py
# def make_key(*a):
#     parts = []
#     for part in a:
#         if isinstance(part, datetime):
#             part = part.isoformat()
#         elif part is None:
#             part = '**'
#         else:
#             part = unicode(part)
#         parts.append(part)
#     return sha1('||'.join(parts)).hexdigest()
# 
# def flatten(d, sep='_'):
#     out = {}
#     for k, v in d.items():
#         if isinstance(v, dict):
#             for ik, iv in flatten(v, sep=sep).items():
#                 out[k + sep + ik] = iv
#         else:
#             out[k] = v
#     return out


# Path: regenesis/cube.py
from collections import defaultdict
from csv import reader
from StringIO import StringIO
from regenesis.mappings import KEYS_TRANSLATE, KEYS_IGNORE, KEYS_LOCALIZED
from regenesis.formats import parse_date, parse_bool
from regenesis.util import make_key, flatten



FIELD_TYPES = {
  'eu_vbd': parse_bool,
  'genesis_vbd': parse_bool,
  'regiostat': parse_bool,
  'secret_values': parse_bool,
  'spr_tmp': parse_bool,
  'regiostat': parse_bool,
  'trans_flag_2': parse_bool,
  'meta_variable': parse_bool,
  'summable': parse_bool,
  'atemporal': parse_bool,

  'valid_from': lambda d: parse_date(d)[0],