LDAP DN

LDAP Distinguished Name (DN) utilities.

class freeiam.ldap.dn.DN(dn: str, format: DNFormat | None = None)[source]

Bases: object

A LDAP Distinguished Name.

property attribute: str | None

Get attribute name of the first RDN component.

>>> DN('cn=foo,cn=bar').attribute
'cn'
property attributes: tuple[str, ...]

Get attribute name of the first RDN component.

>>> DN('uid=1+cn=2,dc=3').attributes
('uid', 'cn')
classmethod compose(*parts: DN | str | tuple[str, str] | tuple[str, str, int]) Self[source]

Compose a DN from different segments.

>>> base = DN('dc=freeiam,dc=org')
>>> str(DN.compose(('cn', 'admin'), 'ou=foo,ou=bar', base))
"cn=admin,ou=foo,ou=bar,dc=freeiam,dc=org"
dn
endswith(other: Self | str) bool[source]

Check if DN is descendant of another base DN.

>>> DN('cn=foo,cn=bar').endswith('cn=bar')
True
>>> DN('cn=foo,cn=bar').endswith('cn=foo')
False
>>> DN('cn=foo').endswith('cn=foo,cn=bar')
False
>>> DN('cn=foo,cn=bar').endswith('')
True
classmethod escape(value: str) str[source]

Escape LDAP DN value.

classmethod get(dn: Self | str) Self[source]

Get a DN from string or existing DN.

get_parent(end: Self | str) Self | None[source]

Get the parent DN until a certain base.

>>> base = DN('dc=freeiam,dc=org')
>>> DN('cn=foo,dc=freeiam,dc=org').get_parent(base) == base
True
>>> DN('dc=freeiam,dc=org').get_parent(base)
None
classmethod get_unique(dns: list[str]) set[Self][source]

Return a unique set of DNs.

>>> len(DN.unique(['CN=users,dc=freeiam,dc=org', 'cn=users,dc=freeiam,dc=org', 'cn = users,dc=freeiam,dc=org', 'CN=Users,dc=freeiam,dc=org']))
1
classmethod get_unique_str(dns: list[Self]) set[str][source]

Return a unique set of DN strings from DNs.

>>> DN.get_unique_str(DN.unique(['cn=foo', 'cn=bar']) - DN.unique(['cn = foo'])) == {'cn=bar'}
True
property multi_rdn: tuple[tuple[str, str], ...]

Get all attrs and values of the RDN.

>>> DN('uid=1+cn=2,dc=3').rdn
(('uid', '1'), ('cn', '2'))
classmethod normalize(dn: Self | str) str[source]

Normalize DN.

property parent: Self | None

Get the parent DN.

>>> DN('cn=item,cn=parent').parent == DN('cn=parent')
True
property rdn: tuple[str, str] | tuple[()]

Get attr and value of the first RDN component.

>>> DN('cn=foo,cn=bar').rdn
('cn', 'foo')
property rdns: list[list[tuple[str, str, int]]]

Get the single RDN items.

startswith(other: Self | str) bool[source]

Check if DN starts with another DN.

>>> DN('cn=foo,cn=bar').startswith('cn=foo')
True
>>> DN('cn=foo,cn=bar').startswith('cn=bar')
False
>>> DN('cn=foo,cn=bar').startswith('')
True
property value: str | None

Get value of the first RDN component.

>>> DN('cn=foo,cn=bar').value
'foo'
property values: tuple[str, ...]

Get value of the first RDN component.

>>> DN('uid=1+cn=2,dc=3').values
('1', '2')
walk(base: Self | str | None = None) Generator[Self, None, None][source]

Walk the reversed DN components from the given base.

>>> [str(x) for x in DN('cn=foo,cn=bar,cn=baz,cn=blub').walk('cn=baz,cn=blub')]
['cn=baz,cn=blub', 'cn=bar,cn=baz,cn=blub', 'cn=foo,cn=bar,cn=baz,cn=blub']
>>> [str(x) for x in DN('cn=foo,cn=bar,cn=baz,cn=blub').walk()]
['cn=blub', 'cn=baz,cn=blub', 'cn=bar,cn=baz,cn=blub', 'cn=foo,cn=bar,cn=baz,cn=blub']