"""
module that defines pytables cache
"""
import os
import tempfile

import tables

from pyhis import usgs_core

# default hdf5 file path
HDF5_FILE_PATH = os.path.join(tempfile.gettempdir(), "pyhis_usgs.h5")


class USGSSite(tables.IsDescription):
    agency = tables.StringCol(20)
    code = tables.StringCol(20)
    county = tables.StringCol(30)
    huc = tables.StringCol(20)
    name = tables.StringCol(250)
    network = tables.StringCol(20)
    site_type = tables.StringCol(20)
    state_code = tables.StringCol(2)

    class location(tables.IsDescription):
        srs = tables.StringCol(20)
        latitude = tables.Float32Col()
        longitude = tables.Float32Col()

    class timezone_info(tables.IsDescription):
        uses_dst = tables.BoolCol()

        class default_tz(tables.IsDescription):
            abbreviation = tables.StringCol(5)
            offset = tables.StringCol(7)

        class dst_tz(tables.IsDescription):
            abbreviation = tables.StringCol(5)
            offset = tables.StringCol(7)


def init_h5(path=HDF5_FILE_PATH):
    h5file = tables.openFile(path, mode="w", title="pyHIS USGS cache")
    h5file.createTable('/', 'sites', USGSSite, "USGS Sites")
    h5file.close()


def update_site_list(state_code, path=HDF5_FILE_PATH):
    """update list of sites for a given state_code"""
    sites = usgs_core.get_sites(state_code)

    # XXX: use some sort of mutex or file lock to guard against concurrent
    # processes writing to the file
    h5file = tables.openFile(path, mode="r+")
    site_table = h5file.root.sites
    site_row = site_table.row
    for site in sites.itervalues():
        flattened = _flatten_dict(site)
        for k, v in flattened.iteritems():
            site_row[k] = v
        site_row.append()
    site_table.flush()


def _flatten_dict(d, prepend=''):
    """flattens a nested dict structure into structure suitable for inserting
    into a pytables table; assumes that no keys in the nested dict structure
    contain the character '/'
    """
    return_dict = {}

    for k, v in d.iteritems():
        if isinstance(v, dict):
            flattened = _flatten_dict(v, prepend=prepend + k + '/')
            return_dict.update(flattened)
        else:
            return_dict[prepend + k] = v

    return return_dict


if __name__ == '__main__':
    init_h5()
    update_site_list('RI')

@@ -1036,37 +1036,410 @@
 def 
-init_h5(path=HDF5_FILE_PATH):
+get_sites(path=HDF5_FILE_PATH):%0A    %22%22%22gets a list of sites from an hdf5 file%22%22%22%0A    h5file = tables.openFile(path, mode='r')%0A    site_table = h5file.root.sites%0A    names = site_table.description._v_nestedNames%0A    return %5B_row_to_dict(row, names) for row in site_table.iterrows()%5D%0A%0A%0Adef init_h5(path=HDF5_FILE_PATH, mode='w'):%0A    %22%22%22creates an hdf5 file an initialized it with relevant tables, etc%22%22%22
 %0A   
@@ -1479,11 +1479,12 @@
 ode=
-%22w%22
+mode
 , ti
@@ -2161,16 +2161,35 @@
 flush()%0A
+    h5file.close()%0A
 %0A%0Adef _f
@@ -2685,16 +2685,412 @@
 _dict%0A%0A%0A
+def _row_to_dict(row, names):%0A    %22%22%22converts a row to a dict representation, given the row and nested names%0A    (i.e. table.description._v_nestedNames)%0A    %22%22%22%0A    return_dict = %7B%7D%0A    for name, val in zip(names, row%5B:%5D):%0A        if not type(name) == tuple:%0A            return_dict%5Bname%5D = val%0A        else:%0A            return_dict%5Bname%5B0%5D%5D = _row_to_dict(val, name%5B1%5D)%0A    return return_dict%0A%0A%0A
 if __nam
@@ -3112,16 +3112,17 @@
 _':%0A    
+#
 init_h5(
@@ -3127,16 +3127,17 @@
 5()%0A    
+#
 update_s
@@ -3151,8 +3151,73 @@
 t('RI')%0A
+    sites = get_sites()%0A    import pdb; pdb.set_trace()%0A    pass%0A

add get_sites to pull data out of pytables store