示例#1
0
    def ldapobjectMatchesQuery(self, rawobject, regex_query):
        '''Will return True if the rawobject matches all of the parameters of the query'''

        #Feeling optimistic today...
        found = True

        #Should we return this object?
        for key, search_item in regex_query.iteritems():
            try:
                #If none of the attribute does not match the query
                #FIXME : metadata is not multivalued, so it introduces a special case
                #FIXME : the fix would be to make metadata multivalued. 
                value_to_test = utils.get_nested_attribute(rawobject, key)

                if isinstance(value_to_test, list):
                    if not any(filter(search_item.search, value_to_test)):
                        #We will not return that object
                        found = False
                        break
                else:
                    if not search_item.search(value_to_test):
                        #We will not return that object
                        found = False
                        break
                
            except KeyError:
                #The attribute can't be found, so
                #we will not return the object 
                found = False
                break

        return found
示例#2
0
    def testGetNestedAttribute(self):
        ldapobject = {
            'regular':1,
            'a':{'nested':{'key':3}},
        }

        #Defaults to regular dict 
        self.assertEquals(ldapobject['regular'], utils.get_nested_attribute(ldapobject, 'regular'))

        #Key error on missing regular key
        with self.assertRaises(KeyError):
            utils.get_nested_attribute(ldapobject, 'asdf')

        #Nested key
        self.assertEquals(ldapobject['a']['nested']['key'], utils.get_nested_attribute(ldapobject, 'a.nested.key'))

        #Key error on missing nested key
        with self.assertRaises(KeyError):
            utils.get_nested_attribute(ldapobject, 'a.s.d.f')