import MySQLdb, sys, configparser, time, codecs

from main_config import main_Config

def resource_of_morpheme(project):
#    config=configparser.ConfigParser()
#    config.read('main.ini')
#    mysql_id=config['settings']["id"]
#    mysql_pass=config['settings']["pass"]
#    using_os=config['settings']["os"]
    mysql_id, mysql_pass, projects, root_path, using_os = main_Config()

    time.sleep(0.3)

    try:
        conn=MySQLdb.connect(db=project, user=mysql_id, passwd=mysql_pass, charset="utf8mb4")
        cursor=conn.cursor()

        sql="select original_sentence from text_table"
        cursor.execute(sql)
        original_sentence = cursor.fetchall()

        conn.commit()
        cursor.close()
        conn.close()

        to_temp_file(original_sentence, using_os)

    except Exception as e:
        print(e)

def to_temp_file(sentence, using_os):
    g = codecs.open("morpheme_temp.txt","w","utf-8")
    g.write("")
    g.close()

    time.sleep(0.2)

    g = codecs.open("morpheme_temp.txt","a","utf-8")

    for n in sentence:
        n=n[0]
        if using_os == "nt":
            g.write(n)
            g.write("\r\n")
        elif  using_os == "posix":
            g.write(n)
            g.write("\n")

    g.close()


if __name__=="__main__":
    project = "ttt"
    resource_of_morpheme(project)
