From 97c0e6127f81f3a52ad6281988f3fc4a53c9b7b2 Mon Sep 17 00:00:00 2001 From: Mark Reynolds Date: Sun, 1 Nov 2015 15:28:48 -0500 Subject: [PATCH] Ticket 48329 - add matching rule functions to schema module Description: Needed a way to access the matching rules in lib389 Also did much needed pep8 cleanup https://fedorahosted.org/389/ticket/48329 Reviewed by: ? --- lib389/schema.py | 101 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 76 insertions(+), 25 deletions(-) diff --git a/lib389/schema.py b/lib389/schema.py index 7932398..845e61a 100644 --- a/lib389/schema.py +++ b/lib389/schema.py @@ -25,7 +25,8 @@ class Schema(object): def get_entry(self): """get the schema as an LDAP entry""" attrs = ['attributeTypes', 'objectClasses'] - return self.conn.search_s(DN_SCHEMA, ldap.SCOPE_BASE, 'objectclass=*', attrs)[0] + return self.conn.search_s(DN_SCHEMA, ldap.SCOPE_BASE, + 'objectclass=*', attrs)[0] def get_subschema(self): """get the schema as a python-ldap SubSchema object""" @@ -38,7 +39,8 @@ class Schema(object): def file_to_ldap(self, filename): """convert the given schema file name to its python-ldap format suitable for passing to ldap.schema.SubSchema() - @param filename - the full path and filename of a schema file in ldif format""" + @param filename - the full path and filename of a schema file in ldif + format""" import six.moves.urllib.request import six.moves.urllib.parse import ldif @@ -55,7 +57,8 @@ class Schema(object): def file_to_subschema(self, filename): """convert the given schema file name to its python-ldap format ldap.schema.SubSchema object - @param filename - the full path and filename of a schema file in ldif format""" + @param filename - the full path and filename of a schema file in ldif + format""" ent = self.file_to_ldap(filename) if not ent: return None @@ -63,50 +66,90 @@ class Schema(object): def add_schema(self, attr, val): """Add a schema element to the schema. - @param attr the attribute type to use e.g. attributeTypes or objectClasses + @param attr - the attribute type to use e.g. attributeTypes or + objectClasses @param val the schema element definition to add""" self.conn.modify_s(DN_SCHEMA, [(ldap.MOD_ADD, attr, val)]) def del_schema(self, attr, val): """Delete a schema element from the schema. - @param attr the attribute type to use e.g. attributeTypes or objectClasses - @param val the schema element definition to delete""" + @param attr - the attribute type to use e.g. attributeTypes or + objectClasses + @param val - the schema element definition to delete""" self.conn.modify_s(DN_SCHEMA, [(ldap.MOD_DELETE, attr, val)]) def add_attribute(self, *attributes): """Add an attribute type definition to the schema. - @param attributes a single or list of attribute type defintions to add""" + @param attributes a single or list of attribute type defintions to add + """ return self.add_schema('attributeTypes', attributes) def add_objectclass(self, *objectclasses): """Add an object class definition to the schema. - @param objectclasses a single or list of object class defintions to add""" + @param objectclasses a single or list of object class defintions to add + """ return self.add_schema('objectClasses', objectclasses) def get_schema_csn(self): """return the schema nsSchemaCSN attribute""" - ents = self.conn.search_s(DN_SCHEMA, ldap.SCOPE_BASE, "objectclass=*", ['nsSchemaCSN']) + ents = self.conn.search_s(DN_SCHEMA, ldap.SCOPE_BASE, + "objectclass=*", ['nsSchemaCSN']) ent = ents[0] return ent.getValue('nsSchemaCSN') def get_objectclasses(self): - """Returns a list of ldap.schema.models.ObjectClass objectsfor all objectClasses supported by this instance. + """Returns a list of ldap.schema.models.ObjectClass objectsfor all + objectClasses supported by this instance. """ attrs = ['objectClasses'] - results = self.conn.search_s(DN_SCHEMA, ldap.SCOPE_BASE, 'objectclass=*', attrs)[0] - objectclasses = [ObjectClass(oc) for oc in results.getValues('objectClasses')] + results = self.conn.search_s(DN_SCHEMA, ldap.SCOPE_BASE, + 'objectclass=*', attrs)[0] + objectclasses = [ObjectClass(oc) for oc in + results.getValues('objectClasses')] return objectclasses def get_attributetypes(self): - """Returns a list of ldap.schema.models.AttributeType objects for all attributeTypes supported by this instance. + """Returns a list of ldap.schema.models.AttributeType objects for all + attributeTypes supported by this instance. """ attrs = ['attributeTypes'] - results = self.conn.search_s(DN_SCHEMA, ldap.SCOPE_BASE, 'objectclass=*', attrs)[0] - attributetypes = [AttributeType(at) for at in results.getValues('attributeTypes')] + results = self.conn.search_s(DN_SCHEMA, ldap.SCOPE_BASE, + 'objectclass=*', attrs)[0] + attributetypes = [AttributeType(at) for at in + results.getValues('attributeTypes')] return attributetypes + def get_matchingrules(self): + """Return a list of the server defined matching rules""" + attrs = ['matchingrules'] + results = self.conn.search_s(DN_SCHEMA, ldap.SCOPE_BASE, + 'objectclass=*', attrs)[0] + matchingRules = [MatchingRule(mr) for mr in + results.getValues('matchingRules')] + return matchingRules + + def query_matchingrule(self, mr_name): + """Returns a single matching rule instance that matches the mr_name. + Returns None if the matching rule doesn't exist. + + @param mr_name - The name of the matching rule you want to query. + + return MatchingRule or None + + + """ + matchingRules = self.get_matchingrules() + matchingRule = [mr for mr in matchingRules + if matchingRules in mr.names] + if len(matchingRule) != 1: + # This is an error. + return None + matchingRule = matchingRule[0] + return matchingRule + def query_objectclass(self, objectclassname): - """Returns a single ObjectClass instance that matches objectclassname. Returns None if the objectClass doesn't exist. + """Returns a single ObjectClass instance that matches objectclassname. + Returns None if the objectClass doesn't exist. @param objectclassname - The name of the objectClass you want to query. @@ -116,32 +159,40 @@ class Schema(object): """ objectclasses = self.get_objectclasses() - objectclass = [oc for oc in objectclasses if objectclassname in oc.names] + objectclass = [oc for oc in objectclasses + if objectclassname in oc.names] if len(objectclass) != 1: - #This is an error. + # This is an error. return None objectclass = objectclass[0] return objectclass def query_attributetype(self, attributetypename): - """Returns a tuple of the AttributeType, and what objectclasses may or must take this attributeType. Returns None if attributetype doesn't exist. + """Returns a tuple of the AttributeType, and what objectclasses may or + must take this attributeType. Returns None if attributetype doesn't + exist. - @param attributetypename - The name of the attributeType you want to query + @param attributetypename - The name of the attributeType you want to + query return (AttributeType, Must, May) or None ex. query_attributetype('uid') - ( , [, ...], [, ...] ) + ( , + [, ...], + [, ...] ) """ - # First, get the attribute that matches name. We need to consider alternate names. - # There is no way to search this, so we have to filter our set of all attribute types. + # First, get the attribute that matches name. We need to consider + # alternate names. There is no way to search this, so we have to + # filter our set of all attribute types. objectclasses = self.get_objectclasses() attributetypes = self.get_attributetypes() attributetypename = attributetypename.lower() - attributetype = [at for at in attributetypes if attributetypename in at.names] + attributetype = [at for at in attributetypes + if attributetypename in at.names] if len(attributetype) != 1: - #This is an error. + # This is an error. return None attributetype = attributetype[0] # Get the primary name of this attribute -- 2.4.3