#!/usr/bin/python

import ruamel.yaml
import sys
import json

def list_all_dict(dstDict, srcDict):
    if isinstance(dstDict,dict):
        for x in range(len(dstDict)):
            temp_key = list(dstDict.keys())[x]
            temp_value = dstDict[temp_key]
            if isinstance(temp_value, dict):
                if temp_key in srcDict:
                    list_all_dict(temp_value, srcDict[temp_key])
                else:
                    res = {}
                    res.setdefault("key", temp_key)
                    res.setdefault("error", "not recognize")
                    print(output_str(res))
                    sys.exit(0)
            else:
                if temp_value == srcDict[temp_key]:
                    continue
                else:
                    res = {}
                    res.setdefault("key", temp_key)
                    res.setdefault("error", "not equal")
                    print(output_str(res))
                    sys.exit(0)

def output_str(res):
    return json.dumps(res)

def loadConf(remoteConfig, lacalConfig):
    try:
        with open(remoteConfig, 'r') as f:
            remoteDict = json.loads(f.read())
    except FileNotFoundError:
        res = {}
        res.setdefault("file", remoteConfig)
        res.setdefault("check", "file_not_found")
        print(output_str(res))
        sys.exit(0)
    except:
        try:
            with open(remoteConfig, 'rb') as f:
                yaml=ruamel.yaml.YAML(typ='safe')
                remoteDict = yaml.load(f)
        except:
            res = {}
            res.setdefault("file", remoteConfig)
            res.setdefault("check", "file_format_error")
            print(output_str(res))
            sys.exit(0)
    
    if type(remoteDict) != dict:
        res = {}
        res.setdefault("file", remoteConfig)
        res.setdefault("check", "file_format_error")
        print(output_str(res))
        sys.exit(0) 

    try:
        with open(lacalConfig, 'r') as f:
            localDict = json.loads(f.read())
    except FileNotFoundError:
        res = {}
        res.setdefault("file", localConfig)
        res.setdefault("check", "file_not_found")
        print(output_str(res))
        sys.exit(0)
    except:
        try:
            with open(localConfig, 'rb') as f:
                yaml=ruamel.yaml.YAML(typ='safe')
                localDict = yaml.load(f)
        except:
            res = {}
            res.setdefault("file", localConfig)
            res.setdefault("check", "file_format_error")
            print(output_str(res))
            sys.exit(0)
    
    if type(localDict) != dict:
        res = {}
        res.setdefault("file", localConfig)
        res.setdefault("check", "file_format_error")
        print(output_str(res))
        sys.exit(0)
    
    list_all_dict(remoteDict, localDict)
    sys.exit(0)
    
    
    

if __name__ == "__main__":
    if len(sys.argv) == 3:
        remoteConfig=sys.argv[1]
        localConfig=sys.argv[2]
    else:
        res = {}
        res.setdefault("check", "Usage: %s remoteConfig localConfig" % (sys.argv[0]))
        print(output_str(res))
        sys.exit(0)
    
    loadConf(remoteConfig, localConfig)