import json
import urllib.request
import os
import shutil


projects = {
    'queues': 'tasks.proto'
}


try:
    shutil.rmtree('schemas')
except:
    pass

try:
    os.mkdir('schemas')
except:
    pass

for project in projects:
    response = urllib.request.urlopen(f'https://platform.sprinthub.ru/schemas/get?project={project}').read()
    data = json.loads(response)
    os.mkdir(f'schemas/{project}')
for key, value in data.items():
    with open(f'schemas/{project}/{key}', 'w+') as fp:
        fp.write(value)


for key, value in projects.items():
    os.system(f'python -m grpc_tools.protoc --proto_path schemas --python_out=. --pyi_out=. --grpc_python_out=. ./schemas/{key}/{value}')

try:
    shutil.rmtree('schemas')
except:
    pass
