import salt
import salt.client
import salt.gdc.groups

from openstack_dashboard.settings import SALT_MASTER_CONFIG
from openstack_dashboard.settings import SALT_SLS_DIR
from openstack_dashboard.settings import SALT_SLS_REPO_DIR


import yaml
import operator
from os import listdir
from os.path import isfile,isdir

from openstack_dashboard.dashboards.groups.groupmember import Member,Group

class SlsGoFru_HighLevelKey():

    # Example:
    #   Input:
    #   hash= {'a': {'B': {1: 2}}, 1: 2, 'R': {'b': {'1': 2, 'T': {'a': 1}}}, 'b': {1: 2, 3: {'1': {'3': {'a': 1, '3': 2}}}}, '4': {'3': {1: 2, 3: 4}}}
    #   high_level_key = 'b'
    #   phrase='a'
    #   key_of_incoming_hash = None (always use None)
    #   Output:
    #   {'3': 1, 'T': 1}



    def __init__(self, high_level_key = None  , key_of_incoming_hash = None  ,  hash=None , phrase=None):

        repos_inside_high_level_key=getattr(self,'repos_inside_high_level_key',{})

        if high_level_key == None:

            for key in hash.keys():

                if phrase == key:

                    if key_of_incoming_hash!= None:

                        repos_inside_high_level_key[key_of_incoming_hash] = hash[key]

                elif (isinstance(hash[key], dict)):

                    instance = SlsGoFru_HighLevelKey(high_level_key = None, key_of_incoming_hash = key , hash=hash[key],phrase=phrase)

                    for key in instance.repos_inside_high_level_key.keys():

                        repos_inside_high_level_key[key] = instance.repos_inside_high_level_key[key]

            setattr(self, 'repos_inside_high_level_key', repos_inside_high_level_key)


        else:

            if (isinstance(hash, dict)):

                for key in hash.keys():