import os
from fnmatch import fnmatch
splittedList = []
listCSV = []
os.system("title pgn splitter v1.3")
os.system("explorer .\\")
def start():
    global splittedList
    global listCSV
    splittedList = []
    listCSV = []
    print("Выберите режим импорта исходного текста:")
    choose = input("1. По пути файла .pgn\n 2. По тексту из .pgn\n  0. Выход\n>>")
    if choose == "1": fileOpen()
    elif choose == "2": textTake()
    elif choose == "0": exit()
    else: 
        print("Некорректный ввод")
        start()
def fileOpen():
    global splittedList
    global listCSV
    splittedList = []
    listCSV = []
    fileToImport = input("Введите путь импортируемого файла (.pgn):\n>>")
    x = 0
    for i in list(fileToImport):
        if i == ".": x += 1
    if x != 1: 
        fileToImport += ".pgn"
        print("К имеющемуся пути был добален .pgn")
    with open(fileToImport, "r") as impFile:
        unsplitted = impFile.read()
    splitText(unsplitted)
def textTake(): 
    unsplitted = input("Введите строку из .pgn:\n>>")
    splitText(unsplitted)
def splitText(unsplitted):
    x = 0
    tooSplittedList = unsplitted.split(" ")
    for f in tooSplittedList:
        try:
            if fnmatch(f, "*."): splittedList.append(tooSplittedList[x]+";"+tooSplittedList[x+1]+";"+tooSplittedList[x+2])
            x += 1
        except IndexError:
            splittedList.append(tooSplittedList[x]+";"+tooSplittedList[x+1])
    x = 0
    for i in splittedList:
        listCSV.append(splittedList[x]+";")
        x += 1
    fileToExport = input("Введите путь экспортируемого файла (.csv):\n>>")
    x = 0
    for i in list(fileToExport):
        if i == ".": 
            x += 1
    if x != 1: 
        fileToExport += ".csv"
        print("К имеющемуся пути был добален .csv")
    with open(fileToExport, "w") as expFile:
        for item in listCSV:
            expFile.write(str(item) + "\n")
    print("Успешный вывод в файл")
    start()
start()