mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
Create a LDIF parser
This commit is contained in:
parent
05fd706ad3
commit
eeadb2ce17
2 changed files with 39 additions and 0 deletions
14
doc/ldap.rst
14
doc/ldap.rst
|
@ -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.
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Add table
Reference in a new issue