# -*- coding: utf-8 -*-
"""io functions for ddf files"""

import os
# import pandas as pd


def to_csv(df, out_dir, ftype, concept, by=None, **kwargs):
    """save a ddf dataframe to csv file.
    the file path of csv will be out_dir/ddf--$ftype--$concept--$by.csv
    """

    if not by:
        path = os.path.join(out_dir, 'ddf--'+ftype+'--'+concept+'.csv')
    else:
        if isinstance(by, list):
            filename = 'dff--' + '--'.join([ftype, concept]) + '--'.join(by) + '.csv'
        else:
            filename = 'dff--' + '--'.join([ftype, concept, by]) + '.csv'

        path = os.path.join(out_dir, filename)

    df.to_csv(path, **kwargs)


def load_google_xls(filehash):
    # TODO: return the xls file with given filehash
    raise NotImplementedError


def cleanup(path):
    """remove all ddf files in the given path"""
    for f in os.listdir(path):
        if f.startswith("ddf--"):
            os.remove(os.path.join(path, f))

@@ -61,16 +61,30 @@
 port os%0A
+import shutil%0A
 # import
@@ -793,27 +793,38 @@
 cleanup(path
+, how='ddf'
 ):%0A
-
     %22%22%22remov
@@ -860,16 +860,104 @@
 path%22%22%22%0A
+    # TODO: support names don't have standard ddf name format.%0A    if how == 'ddf':%0A    
     for 
@@ -987,16 +987,20 @@
         
+    
 if f.sta
@@ -1029,16 +1029,20 @@
         
+   
+ 
 os.remov
@@ -1066,8 +1066,343 @@
 th, f))%0A
+        os.remove(os.path.join(path, 'datapackage.json'))%0A    if how == 'lang':%0A        if os.path.exists(os.path.join(path, 'lang')):%0A            shutil.rmtree(os.path.join(path, 'lang'))%0A    if how == 'langsplit':%0A        if os.path.exists(os.path.join(path, 'langsplit')):%0A            shutil.rmtree(os.path.join(path, 'langsplit'))%0A

support cleaning up lang/langsplit