def __parseSchema(self, expand_oid_macros=False): """Parser for .schema files, like the one from OpenLDAP""" obj_str = 'objectclass' obj_len = len(obj_str) att_str = 'attributetype' att_len = len(att_str) oid_macros = {} f = open(self.filename, 'r') blocks = f.read().split('\n\n') for block in blocks: block = block.strip() if 'objectIdentifier' in block: oid_macros = self.__parseOIDMacros(block) self.macros = self.__getMacroDefinitions(block) elif re.match('^objectclass', block): block = block.replace('\n', ' ').replace('\t', ' ').strip() obj = ObjectClass(block[obj_len:]) # Extra parsing to get the X-ORIGIN values as the python-ldap # parser isn't parsing the value for objectClasses if 'X-ORIGIN' in block: waste, originstr = block.split('X-ORIGIN') parts = originstr.strip().split('\'') obj.x_origin = parts[1] self.objClasses.append(obj) elif re.match('^attributetype', block): block = block.replace('\n', ' ').replace('\t', ' ').strip() att = AttributeType(block[att_len:]) self.attrTypes.append(att) if expand_oid_macros: error_msg = "You requested for the expansion of OID macros." \ + " The definition for macro `{}` was not found." \ + " Storing it without expansion." for obj in self.objClasses: if ':' in obj.oid: macro, index = obj.oid.split(':') try: obj.oid = oid_macros[macro] + '.' + index except KeyError as e: logging.warning(error_msg, macro) logging.debug(e, exc_info=True) for att in self.attrTypes: if ':' in att.oid: macro, index = att.oid.split(':') try: att.oid = oid_macros[macro] + '.' + index except KeyError as e: logging.warning(error_msg, macro) logging.debug(e, exc_info=True)
def get_attributetypes(self): """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') ] return attributetypes
def get_attributetypes(self, json=False): """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] if json: attributetypes = [vars(AttributeType(at)) for at in results.getValues('attributeTypes')] for attr in attributetypes: # Add normalized name for sorting attr['name'] = attr['names'][0].lower() attributetypes = sorted(attributetypes, key=itemgetter('name')) result = {'type': 'list', 'items': attributetypes} return dump_json(result) else: attributetypes = [AttributeType(at) for at in results.getValues('attributeTypes')] return attributetypes
def test_empty_attributetype_attrs(self): """Check types and values of attributes of a minimal AttributeType""" # (OID 2.999 is actually "/Example", for use in documentation) attr = AttributeType('( 2.999 )') self.assertEqual(attr.oid, '2.999') self.assertEqual(attr.names, ()) self.assertEqual(attr.desc, None) self.assertEqual(attr.obsolete, False) self.assertEqual(attr.single_value, False) self.assertEqual(attr.syntax, None) self.assertEqual(attr.no_user_mod, False) self.assertEqual(attr.equality, None) self.assertEqual(attr.substr, None) self.assertEqual(attr.ordering, None) self.assertEqual(attr.usage, 0) self.assertEqual(attr.sup, ()) self.assertEqual(attr.x_origin, ())
class myLdifParser(LDIFParser): def __init__(self, ldif_file): self.ldif_file = ldif_file self.entries = [] def parse(self): with open(self.ldif_file, 'rb') as f: parser = LDIFParser(f) for dn, entry in parser.parse(): for e in entry: for i, v in enumerate(entry[e][:]): if isinstance(v, bytes): entry[e][i] = v.decode('utf-8') self.entries.append((dn, entry)) schema_names = {} for schema_fn in glob.glob('*.ldif'): parser = myLdifParser(schema_fn) parser.parse() if 'attributeTypes' in parser.entries[0][1]: for a in parser.entries[0][1]['attributeTypes']: att = AttributeType(a) for n in att.names: schema_names[n] = att.syntax with open('opendj_attributes_schema.json', 'w') as w: json.dump(schema_names, w, indent=2)