""" Добавление метаданных из файлов json в общую базу метаданных. """

import json
import warnings

OVERWRITE = False

paths = ('../devel_program.pdf.json',
         '../enroll_rules.pdf.json',
         '../RegulationsOnEducationActivity.pdf.json')

dbase_path = 'sign.json'

with open(dbase_path, 'r', encoding='utf-8') as jsonfile:
    merged_dict = json.load(jsonfile)

for path in paths:
    with open(path, 'r', encoding='utf-8') as jsonfile:
        d = json.load(jsonfile)
    if not OVERWRITE:
        to_be_omitted = []
        for k in d.keys():
            if k in merged_dict:
                to_be_omitted.append(k)
        for k in to_be_omitted:
            msg = ('Значение "%s" имеется в файле "%s" - для перезаписи '
                   'укажите OVERWRITE = True')
            warnings.warn(msg % (k, dbase_path))
            d.pop(k)
    merged_dict.update(d)

with open(dbase_path, 'w', encoding='utf-8') as jsonfile:
    json.dump(merged_dict, jsonfile, ensure_ascii=False, indent=4)
        
