# Path: automlk/dataset.py
# def get_dataset_list(include_results=False):
#     """
#     get the list of all datasets
# 
#     :param include_results: flag to determine if the status are also retrieved (default = False)
#     :return: list of datasets objects or empty list if error (eg. redis or environment not set)
#     """
#     # try:
#     return [get_dataset(dataset_id, include_results) for dataset_id in get_dataset_ids()]
#     # except:
#     #    return []
# 
# def update_dataset(dataset_id, name, folder_id, description, source, url):
#     """
#     update specific fields of the dataset
# 
#     :param dataset_id: id of the dataset
#     :param name: new name of the dataset
#     :param folder_id: folder
#     :param description: new description of the dataset
#     :param source: source of the dataset
#     :param url: url of the dataset
#     :return:
#     """
#     dt = get_dataset(dataset_id)
#     dt.name = name
#     if folder_id not in [f['id'] for f in get_folder_list()]:
#         raise ValueError('folder id %s is not a folder' % str(folder_id))
#     dt.folder_id = folder_id
#     dt.description = description
#     dt.source = source
#     dt.url = url
#     dt.save(dataset_id)
# 
# Path: automlk/store.py
# def get_key_store(key):
#     """
#     retieves value from key in store
# 
#     :param key: key of the data
#     :return: value of the data
#     """
#     if get_use_redis():
#         return json.loads(rds.get(str(key)))
#     else:
#         try:
#             if exists_key_store(key):
#                 return json.load(open(store_folder + '/' + __clean_key(key) + '.json', 'r'))
#             else:
#                 return None
#         except:
#             return None
# 
# Path: automlk/solutions_pp.py
# PP_CATEGORIES = ['missing', 'categorical', 'float', 'date', 'text', 'scaling', 'feature']
# class PpSolution(object):
#     def __init__(self, ref, name, process, default_params, space_params, pp_type, selectable=True, limit_size=1e32,
#                  problem_type='*'):
# 
# Path: automlk/folders.py
# def create_folder(id_parent, name):
#     """
#     creates the folder under parent id
# 
#     :param id_parent: id
#     :return:
#     """
#     id_folder = incr_key_store('folders:counter')
#     rpush_key_store('folders:list', {'id': id_folder, 'parent': id_parent, 'name': name})
#     return id_folder
# 
# def get_folder_list():
#     """
#     returns the list of folders
# 
#     :return:
#     """
#     if exists_key_store('folders:list'):
#         return get_key_store('folders:list')
#     else:
#         # initialize folder list with root (All)
#         set_key_store('folders:counter', 0)
#         rpush_key_store('folders:list', {'id': 0, 'parent': -1, 'name': 'All'})
#         return get_key_store('folders:list')


from automlk.dataset import get_dataset_list, update_dataset
from automlk.store import get_key_store
from automlk.solutions_pp import pp_solutions_map
from automlk.folders import create_folder, get_folder_list

"""
upgrade folders

"""


folder_list = get_folder_list()
folders = [f['name'] for f in folder_list]

for dt in get_dataset_list():
    print('-'*60)
    print(dt.name)

    res = get_key_store('dataset:%s' % dt.dataset_id)
    name = res['init_data']['domain']
    if name not in folders:
        folders.append(name)
        # create this folder

# Path: automlk/dataset.py
# def get_dataset_list(include_results=False):
#     """
#     get the list of all datasets
# 
#     :param include_results: flag to determine if the status are also retrieved (default = False)
#     :return: list of datasets objects or empty list if error (eg. redis or environment not set)
#     """
#     # try:
#     return [get_dataset(dataset_id, include_results) for dataset_id in get_dataset_ids()]
#     # except:
#     #    return []
# 
# def update_dataset(dataset_id, name, folder_id, description, source, url):
#     """
#     update specific fields of the dataset
# 
#     :param dataset_id: id of the dataset
#     :param name: new name of the dataset
#     :param folder_id: folder
#     :param description: new description of the dataset
#     :param source: source of the dataset
#     :param url: url of the dataset
#     :return:
#     """
#     dt = get_dataset(dataset_id)
#     dt.name = name
#     if folder_id not in [f['id'] for f in get_folder_list()]:
#         raise ValueError('folder id %s is not a folder' % str(folder_id))
#     dt.folder_id = folder_id
#     dt.description = description
#     dt.source = source
#     dt.url = url
#     dt.save(dataset_id)
# 
# Path: automlk/store.py
# def get_key_store(key):
#     """
#     retieves value from key in store
# 
#     :param key: key of the data
#     :return: value of the data
#     """
#     if get_use_redis():
#         return json.loads(rds.get(str(key)))
#     else:
#         try:
#             if exists_key_store(key):
#                 return json.load(open(store_folder + '/' + __clean_key(key) + '.json', 'r'))
#             else:
#                 return None
#         except:
#             return None
# 
# Path: automlk/solutions_pp.py
# PP_CATEGORIES = ['missing', 'categorical', 'float', 'date', 'text', 'scaling', 'feature']
# class PpSolution(object):
#     def __init__(self, ref, name, process, default_params, space_params, pp_type, selectable=True, limit_size=1e32,
#                  problem_type='*'):
# 
# Path: automlk/folders.py
# def create_folder(id_parent, name):
#     """
#     creates the folder under parent id
# 
#     :param id_parent: id
#     :return:
#     """
#     id_folder = incr_key_store('folders:counter')
#     rpush_key_store('folders:list', {'id': id_folder, 'parent': id_parent, 'name': name})
#     return id_folder
# 
# def get_folder_list():
#     """
#     returns the list of folders
# 
#     :return:
#     """
#     if exists_key_store('folders:list'):
#         return get_key_store('folders:list')
#     else:
#         # initialize folder list with root (All)
#         set_key_store('folders:counter', 0)
#         rpush_key_store('folders:list', {'id': 0, 'parent': -1, 'name': 'All'})
#         return get_key_store('folders:list')


# Path: scripts/upgrade_folders.py
from automlk.dataset import get_dataset_list, update_dataset
from automlk.store import get_key_store
from automlk.solutions_pp import pp_solutions_map
from automlk.folders import create_folder, get_folder_list


"""
upgrade folders

"""


folder_list = get_folder_list()
folders = [f['name'] for f in folder_list]

for dt in get_dataset_list():
    print('-'*60)
    print(dt.name)

    res = get_key_store('dataset:%s' % dt.dataset_id)
    name = res['init_data']['domain']
    if name not in folders:
        folders.append(name)
        # create this folder

        folder_id = create_folder(0, name)