# -*- coding: UTF-8 -*-

"""Convert files of SQL dumps into CSV
"""

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#

import os
import re
from shutil import copyfile

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#

INPUT_DIR = '../split_sql/'

OUTPUT_DIR = '../split_sql_renamed/'

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#

if __name__ == '__main__':

  os.makedirs( OUTPUT_DIR, exist_ok = True )

  files = sorted(os.listdir(INPUT_DIR))

  for file in files[:-1]:
    with open(os.path.join(INPUT_DIR, file), 'r') as f:
      try:
        print(file)
        text = f.read()
        name = re.findall(r'Table structure for table `(.*)`\n', text)[0]
        copyfile(
          src = os.path.join(INPUT_DIR, file),
          dst = os.path.join(OUTPUT_DIR, name) )
      except UnicodeDecodeError:
        pass

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#