def ldif2mutt(ldif_file_spec): """Parse file specified by LDIF_FILE_SPEC, generating corresponding mutt alias entries. LDIF_FILE_SPEC is a string specifying a single file.""" # DEBUGP should be either True or False (any other value will interpreted as False) debugp = False ldif_file = ldif_file_spec[0] parser = ldif3.LDIFParser(open(ldif_file, 'rb')) for dn, entry in parser.parse(): mail = entry['mail'] mail = mail[0] disp_name = entry['displayName'] disp_name = disp_name[0] # attempt to distinguish between several possibilities: # 1. displayName is the same as mail # 2. displayName corresponds to actual name of individual mail_only_p = False if "@" in disp_name: mail_only_p = True if mail_only_p: nick = disp_name.replace(" ", "") nick = downcase_char(nick, 0) print('alias {} {}'.format(nick, mail)) else: nick = disp_name.replace(" ", "") nick = downcase_char(nick, 0) # use quoted string and angle address (RFC 5322) for email print('alias {} "{}" <{}>'.format(nick, disp_name, mail))
def load(cls, fh): if isinstance(ldif3, Exception): raise ldif3 parser = ldif3.LDIFParser(fh, strict=False) for dn, entry in parser.parse(): yield map_keys(MultiDict(entry), cls.fields)
def test_parse_binary_raw(self): self.stream = BytesIO(b'dn: cn=Bjorn J Jensen\n' b'jpegPhoto:: 8PLz\nfoo: bar') self.p = ldif3.LDIFParser(self.stream, encoding=None) items = list(self.p.parse()) self.assertEqual(items, [('cn=Bjorn J Jensen', { u'jpegPhoto': [b'\xf0\xf2\xf3'], u'foo': [b'bar'], })])
def set_(ldif_file): """Load DNs and attributes from a LDIF files and set them in LDAP. """ # We need to strip all operational attributes before putting # the entry back into LDAP. We do this by retrieving and empty # DN and listing which attributes it has; they will be all # operational. empty_item = context.GLOBAL.admin.conn.get( entry_dn=None, attributes=['+'] ) operational_attributes = empty_item.keys() for entry_dn, entry_data in ldif3.LDIFParser(ldif_file).parse(): cli.out('Loading %r', entry_dn) cleaned_data = { k: v for k, v in entry_data.items() if k not in operational_attributes } context.GLOBAL.admin.conn.set( entry_dn, cleaned_data )
def setUp(self): self.stream = BytesIO(BYTES_EMPTY_ATTR_VALUE) self.p = ldif3.LDIFParser(self.stream)
def test_iter_blocks_with_additional_spaces(self): self.stream = BytesIO(BYTES_SPACE) self.p = ldif3.LDIFParser(self.stream) self.assertEqual(list(self.p._iter_blocks()), BLOCKS)
def setUp(self): self.stream = BytesIO(BYTES) self.p = ldif3.LDIFParser(self.stream)
# to.ldif shall contain the final state in the ldap server import logging import sys import ldif3 import os def dn_parts_count(par_dn): return len(par_dn.split(',')) logging.basicConfig(level=logging.INFO) # populate from fromparser = ldif3.LDIFParser(open(sys.argv[1], mode="rb")) fromparser.parse() from_dict = {k: v for k, v in fromparser.parse()} from_set = set(from_dict) logging.info("from: %d entries" % len(from_dict)) # populate to toparser = ldif3.LDIFParser(open(sys.argv[2], mode="rb")) toparser.parse() to_dict = {k: v for k, v in toparser.parse()} to_set = set(to_dict) logging.info("to: %d entries" % len(to_dict)) writer = ldif3.LDIFWriter(os.fdopen(sys.stdout.fileno(), mode="wb")) # delete dn's that are only on the to -