# Path: PW_explorer/helper.py
# def mkdir_p(path):
#     """Make a directory if it doesn't already exist"""
#     try:
#         os.makedirs(path)
#     except OSError as exc:  # Python >2.5
#         if exc.errno == errno.EEXIST and os.path.isdir(path):
#             pass
#         else:
#             raise
# 
# def turn_list_into_str(l):
# 
#     if isinstance(l, (list,set,frozenset)):
#         if len(l) > 1:
#             l_ = [turn_list_into_str(l1) for l1 in l[1:]]
#             return "{}({})".format(l[0], ",".join(l_))
#         elif len(l) == 1:
#             return "{}".format(l[0])
#         else:
#             return ""
#     else:
#         return l
# 
# Path: PW_explorer/Input_Parsers/Meta_Data_Parser/meta_data_parser.py
# ASP_COMMENT_SYMBOL = '%'
# 
# ASP_SYNTAX_ATTRIBUTE_DEF_KEYWORD = 'schema'


import pandas as pd
import os
import sqlite3
from .helper import (
    mkdir_p,
    turn_list_into_str,
)
from .Input_Parsers.Meta_Data_Parser.meta_data_parser import (
    ASP_COMMENT_SYMBOL,
    ASP_SYNTAX_ATTRIBUTE_DEF_KEYWORD,
)
#!/usr/bin/env python3



class PWEExport:

    @staticmethod
    def get_sqlite_schema(dfs):

        # this approach will take constant time since there is just one row in the exported database.
        TEST_DB_LOCATION = os.path.abspath("test.db")
        conn_t = sqlite3.connect(TEST_DB_LOCATION)
        for rl_name, df in dfs.items():
            t = df.ix[0:0]
            t.to_sql(str(rl_name), conn_t, if_exists='replace')
        schema_q = conn_t.execute("SELECT * FROM sqlite_master WHERE type='table' ORDER BY name;")
        schemas = []
        for row in schema_q.fetchall():
            schemas.append(row[4])
        for rl_name, df in dfs.items():
            conn_t.execute('DROP TABLE ' + str(rl_name))
        conn_t.commit()
        conn_t.close()
        os.remove(TEST_DB_LOCATION)
        return schemas

    @staticmethod
    def export_to_sqlite_db(export_loc, dfs, db_name):


# Path: PW_explorer/helper.py
# def mkdir_p(path):
#     """Make a directory if it doesn't already exist"""
#     try:
#         os.makedirs(path)
#     except OSError as exc:  # Python >2.5
#         if exc.errno == errno.EEXIST and os.path.isdir(path):
#             pass
#         else:
#             raise
# 
# def turn_list_into_str(l):
# 
#     if isinstance(l, (list,set,frozenset)):
#         if len(l) > 1:
#             l_ = [turn_list_into_str(l1) for l1 in l[1:]]
#             return "{}({})".format(l[0], ",".join(l_))
#         elif len(l) == 1:
#             return "{}".format(l[0])
#         else:
#             return ""
#     else:
#         return l
# 
# Path: PW_explorer/Input_Parsers/Meta_Data_Parser/meta_data_parser.py
# ASP_COMMENT_SYMBOL = '%'
# 
# ASP_SYNTAX_ATTRIBUTE_DEF_KEYWORD = 'schema'


# Path: PW_explorer/export.py
import pandas as pd
import os
import sqlite3
from .helper import (
    mkdir_p,
    turn_list_into_str,
)
from .Input_Parsers.Meta_Data_Parser.meta_data_parser import (
    ASP_COMMENT_SYMBOL,
    ASP_SYNTAX_ATTRIBUTE_DEF_KEYWORD,
)

#!/usr/bin/env python3



class PWEExport:

    @staticmethod
    def get_sqlite_schema(dfs):

        # this approach will take constant time since there is just one row in the exported database.
        TEST_DB_LOCATION = os.path.abspath("test.db")
        conn_t = sqlite3.connect(TEST_DB_LOCATION)
        for rl_name, df in dfs.items():
            t = df.ix[0:0]
            t.to_sql(str(rl_name), conn_t, if_exists='replace')
        schema_q = conn_t.execute("SELECT * FROM sqlite_master WHERE type='table' ORDER BY name;")
        schemas = []
        for row in schema_q.fetchall():
            schemas.append(row[4])
        for rl_name, df in dfs.items():
            conn_t.execute('DROP TABLE ' + str(rl_name))
        conn_t.commit()
        conn_t.close()
        os.remove(TEST_DB_LOCATION)
        return schemas

    @staticmethod
    def export_to_sqlite_db(export_loc, dfs, db_name):


        mkdir_p(export_loc)