# Path: orm.py
# def now():
# def set_sqlite_pragma(dbapi_connection, connection_record):
#     def is_blank(self):
#     def save(self):
#     def save(self):
#     def save(self):
# def counter():
# def exithandler():
# class Value(Base):
# class DataSet(Base):
# class Indicator(Base):


import xypath
import messytables
import orm
import re
import time
import dl
from hamcrest import equal_to, is_in
from orm import session, Value, DataSet, Indicator
        try:
            messy = messytables.excel.XLSTableSet(fh)
            break  # success!
        except messytables.error.ReadError, e:
            print e
            return
            
    table = xypath.Table.from_messy(list(messy.tables)[0])
    indicators = table.filter(is_in(indicator_list))
    indname = indicators.shift(x=-1)
    if not len(indname) == len(indicator_list):
        print "missing indicators", [x.value for x in indname]

    code = table.filter(equal_to('Indicator Code'))

    years = code.fill(xypath.RIGHT)
    junction = indname.junction(years)
    for ind_cell, year_cell, value_cell in junction:
        vdict = dict(value)
        vdict['indID'] = ind_cell.value
        vdict['period'] = year_cell.value
        vdict['value'] = value_cell.value

        indicator = {'indID': vdict['indID']}
        nameunits = re.search('(.*)\((.*)\)', vdict['indID'])
        if nameunits:
            (indicator['name'], indicator['units']) = nameunits.groups()
        else:
            indicator['name'] = vdict['indID']
            indicator['units'] = 'uno'

# Path: orm.py
# def now():
# def set_sqlite_pragma(dbapi_connection, connection_record):
#     def is_blank(self):
#     def save(self):
#     def save(self):
#     def save(self):
# def counter():
# def exithandler():
# class Value(Base):
# class DataSet(Base):
# class Indicator(Base):


# Path: worldbank.py
import xypath
import messytables
import orm
import re
import time
import dl
from hamcrest import equal_to, is_in
from orm import session, Value, DataSet, Indicator

        try:
            messy = messytables.excel.XLSTableSet(fh)
            break  # success!
        except messytables.error.ReadError, e:
            print e
            return
            
    table = xypath.Table.from_messy(list(messy.tables)[0])
    indicators = table.filter(is_in(indicator_list))
    indname = indicators.shift(x=-1)
    if not len(indname) == len(indicator_list):
        print "missing indicators", [x.value for x in indname]

    code = table.filter(equal_to('Indicator Code'))

    years = code.fill(xypath.RIGHT)
    junction = indname.junction(years)
    for ind_cell, year_cell, value_cell in junction:
        vdict = dict(value)
        vdict['indID'] = ind_cell.value
        vdict['period'] = year_cell.value
        vdict['value'] = value_cell.value

        indicator = {'indID': vdict['indID']}
        nameunits = re.search('(.*)\((.*)\)', vdict['indID'])
        if nameunits:
            (indicator['name'], indicator['units']) = nameunits.groups()
        else:
            indicator['name'] = vdict['indID']
            indicator['units'] = 'uno'

        Indicator(**indicator).save()