# Path: mcm/datastructures.py
# class CmdPathRow(dict):
# 
# 
#     def __str__(self):
#         bool_map = {True:'yes', False:'no', None:'""', '':'""'}
#         return ' '.join('{}={}'.format(key, bool_map.get(value, value)) for key, value in self.items())
# 
# 
#     def __hash__(self):
#         return hash(tuple(self.items()))
# 
# 
#     def __sub__(self, other):
#         '''Return new instance with key,valie pairs from self not listed in other.'''
# 
#         diff = dict(set(self.items()) - set(other.items()))
#         return CmdPathRow( diff )
# 
# 
#     def isunique(self, other, keys):
#         '''Test whether every key,value pair (from keys) is in other.'''
# 
#         pairs = set( (key,self[key]) for key in keys )
#         return pairs <= set(other.items())


from itertools import zip_longest
from mcm.datastructures import CmdPathRow

    def compare(self, wanted, present):

        for wanted_rule in wanted:
            present_rule = self.findPair(searched=wanted_rule, present=present)
            diff = wanted_rule - present_rule
            self.decide(wanted_rule, diff, present_rule)

        DEL = set(present) - set(self.NO_DELETE)
        return tuple(self.ADD), tuple(self.SET), tuple(DEL)


    def decide(self, wanted, difference, present):

        if wanted and difference and not present:
            self.ADD.append( wanted )
        elif wanted and difference and present:
            self.NO_DELETE.append(present)
            difference['.id'] = present['.id']
            self.SET.append( difference )
        elif wanted and not difference and present:
            self.NO_DELETE.append(present)


    def findPair(self, searched, present):

        for row in present:
            if row.isunique(other=searched, keys=self.keys):
                return row
        else:

# Path: mcm/datastructures.py
# class CmdPathRow(dict):
# 
# 
#     def __str__(self):
#         bool_map = {True:'yes', False:'no', None:'""', '':'""'}
#         return ' '.join('{}={}'.format(key, bool_map.get(value, value)) for key, value in self.items())
# 
# 
#     def __hash__(self):
#         return hash(tuple(self.items()))
# 
# 
#     def __sub__(self, other):
#         '''Return new instance with key,valie pairs from self not listed in other.'''
# 
#         diff = dict(set(self.items()) - set(other.items()))
#         return CmdPathRow( diff )
# 
# 
#     def isunique(self, other, keys):
#         '''Test whether every key,value pair (from keys) is in other.'''
# 
#         pairs = set( (key,self[key]) for key in keys )
#         return pairs <= set(other.items())


# Path: mcm/comparators.py
from itertools import zip_longest
from mcm.datastructures import CmdPathRow


    def compare(self, wanted, present):

        for wanted_rule in wanted:
            present_rule = self.findPair(searched=wanted_rule, present=present)
            diff = wanted_rule - present_rule
            self.decide(wanted_rule, diff, present_rule)

        DEL = set(present) - set(self.NO_DELETE)
        return tuple(self.ADD), tuple(self.SET), tuple(DEL)


    def decide(self, wanted, difference, present):

        if wanted and difference and not present:
            self.ADD.append( wanted )
        elif wanted and difference and present:
            self.NO_DELETE.append(present)
            difference['.id'] = present['.id']
            self.SET.append( difference )
        elif wanted and not difference and present:
            self.NO_DELETE.append(present)


    def findPair(self, searched, present):

        for row in present:
            if row.isunique(other=searched, keys=self.keys):
                return row
        else:

            return CmdPathRow(dict())