Create a LDIF parser

This commit is contained in:
Josué Tille 2019-05-25 23:50:59 +02:00
parent 05fd706ad3
commit eeadb2ce17
No known key found for this signature in database
GPG key ID: D5E068C6DFA8681D
2 changed files with 39 additions and 0 deletions

View file

@ -372,3 +372,17 @@ Here how it looks like for domain and user:
(:file:`None` ?) so you need to check it returns code.
.. automethod:: moulinette.authenticators.ldap.Authenticator.remove
Reading LDIF file
=================
Reading parsing a ldif to be able to insert in the LDAP database is really easy. Here is how to get the content of a LDIF file
::
from moulinette.utils.filesystem import read_ldif
my_reslut = read_ldif("your_file.ldif")
Note that the main difference of what the auth object return with the search method is that this function return a 2-tuples with the "dn" and the LDAP entry.

View file

@ -77,6 +77,31 @@ def read_yaml(file_path):
return loaded_yaml
def read_ldif(file_path):
"""
Safely read a LDIF file and create struct in the same style than
what return the auth objet with the seach method
The main difference with the auth object is that this function return a 2-tuples
with the "dn" and the LDAP entry.
Keyword argument:
file_path -- Path to the ldif file
"""
from ldif import LDIFRecordList
# Open file and read content
try:
with open(file_path, "r") as f:
parser = LDIFRecordList(f)
parser.parse()
except IOError as e:
raise MoulinetteError('cannot_open_file', file=file_path, error=str(e))
except Exception as e:
raise MoulinetteError('error_reading_file', file=file_path, error=str(e))
return parser.all_records
def write_to_file(file_path, data, file_mode="w"):
"""
Write a single string or a list of string to a text file.