LDAP Filter

LDAP filter expressions.

class freeiam.ldap.filter.AND(expressions: list[Expression | Token], *, sep: Token | None = None)[source]

Bases: Operator

A group of AND conjunction expressions (&(...)(...)).

expression = '&'
operator(a, b, /)

Same as a & b.

class freeiam.ldap.filter.ApproximateMatch(attr: str, value: str, is_escaped: bool = False)[source]

Bases: Comparison

Compare approximately (attr~=value).

attr
expression = '~='
is_escaped
raw_value
class freeiam.ldap.filter.Comparison(attr: str, value: str, is_escaped: bool = False)[source]

Bases: Expression

Base class for comparison operators.

attr
copy() Self[source]

Copy the object (without preserving optional whitespace).

property escaped: str

Get the value in a encoded/escaped form.

expression = ''
is_escaped
operator: Callable[[...], bool] | None = None
raw_value
property value: str

Get the value in a decoded form.

class freeiam.ldap.filter.EqualityMatch(attr: str, value: str, is_escaped: bool = False)[source]

Bases: Comparison

Compare for equality (attr=value).

attr
expression = '='
is_escaped
operator(a, b, /)

Same as a == b.

raw_value
class freeiam.ldap.filter.ExtensibleMatch(attr: str, value: str, dn: str | None, matchingrule: str | None, is_escaped: bool = True)[source]

Bases: Comparison

Compare with extensible match (attr:dn:rule:=value, optional: dn / Matching Rule OID).

attr
copy() Self[source]

Copy the object (without preserving optional whitespace).

dn
expression = ':='
is_escaped
matchingrule
raw_value
class freeiam.ldap.filter.Filter(filter_expr: str | None, *, strict: bool = False, _debug: bool = False)[source]

Bases: object

A LDAP Filter according to RFC 4515.

RE_HEXESCAPE = re.compile('\\\\([0-9A-Fa-f]{2})')
ast
classmethod attr(attr: str, dn: str | None = None, matchingrule: str | None = None) Attribute[source]

Get attribute for comparison.

error() FilterError[source]

Get FilterError.

classmethod escape(value: str, escape_mode: EscapeMode = EscapeMode.RESTRICTED) str[source]

Escape LDAP filter characters.

classmethod escape_formatted(format_string: str, values: Sequence[str]) str[source]

Escape LDAP filter characters in format string.

filter_expr
classmethod from_format(format_string: str, values: list[str]) Self[source]

Get a Filter from a filter format string.

classmethod get_and(*expressions: Expression) AND[source]

Get conjunction.

classmethod get_approx(attr: str, value: str) ApproximateMatch[source]

Get ApproximateMatch.

classmethod get_eq(attr: str, value: str) EqualityMatch[source]

Get EqualityMatch.

classmethod get_extensible(attr: str, dn: str | None, matchingrule: str | None, value: str) ExtensibleMatch[source]

Get ExtensibleMatch.

classmethod get_gt(attr: str, value: str | int) NOT | GreaterOrEqual[source]

Get greather then equivialent.

classmethod get_gt_eq(attr: str, value: str | int) GreaterOrEqual[source]

Get GreaterOrEqual.

classmethod get_lt(attr: str, value: str | int) NOT | LessOrEqual[source]

Get Lower than equivialent.

classmethod get_lt_eq(attr: str, value: str | int) LessOrEqual[source]

Get LessOrEqual.

classmethod get_not(expression: Expression) NOT[source]

Get negation.

classmethod get_or(*expressions: Expression) OR[source]

Get disjunction.

classmethod get_pres(attr: str) PresenceMatch[source]

Get PresenceMatch.

classmethod get_substring(attr: str, *values: str) SubstringMatch[source]

Get SubstringMatch.

parse(strict: bool = False) None[source]

Parse.

parser = Lark(open('<string>'), parser='earley', lexer='dynamic', ...)
pretty(indent: int = 0) str[source]

Transform into a pretty presentation.

property root: Expression | None

The first object in the filter.

classmethod time_span_filter(from_timestamp: int = 0, until_timestamp: int | None = None, delta_attr: str = 'modifyTimestamp') Self[source]

Get timespan filter e.g. ‘(&(modifyTimestamp>=19700101000000Z)(!(modifyTimestamp>=19700101000001Z)))’.

classmethod unescape(value: str) str[source]

Reverse the escaping of filter characters.

walk(comparison_callback: Callable[[Self, Container, Comparison], None] | None, operator_callback: Callable[[Self, Container, AND | OR | NOT | Container], None] | None, strategy: WalkStrategy = WalkStrategy.POST) None[source]

Walk the filter expressions and operator conjunctions iteratively.

class freeiam.ldap.filter.GreaterOrEqual(attr: str, value: str, is_escaped: bool = False)[source]

Bases: Comparison

Compare for greater or equals (attr>=value).

attr
expression = '>='
is_escaped
operator(a, b, /)

Same as a >= b.

raw_value
class freeiam.ldap.filter.LessOrEqual(attr: str, value: str, is_escaped: bool = False)[source]

Bases: Comparison

Compare for less than or equals (attr<=value).

attr
expression = '<='
is_escaped
operator(a, b, /)

Same as a <= b.

raw_value
class freeiam.ldap.filter.NOT(expressions: list[Expression | Token], *, sep: Token | None = None)[source]

Bases: Operator

A group of NOT negation expressions (!( ... )).

expression = '!'
operator(a, /)

Same as not a.

class freeiam.ldap.filter.OR(expressions: list[Expression | Token], *, sep: Token | None = None)[source]

Bases: Operator

A group of OR disjunction expressions (|(...)(...)).

expression = '|'
operator(a, b, /)

Same as a | b.

class freeiam.ldap.filter.Operator(expressions: list[Expression | Token], *, sep: Token | None = None)[source]

Bases: Group

A logical operator.

class freeiam.ldap.filter.PresenceMatch(attr: str, value: str, is_escaped: bool = False)[source]

Bases: Comparison

Compare for presence (attr=*).

attr
expression = '=*'
is_escaped
raw_value
class freeiam.ldap.filter.SubstringMatch(attr: str, value: str, is_escaped: bool = False)[source]

Bases: Comparison

Compare substring match (attr=val*).

attr
expression = '='
is_escaped
operator(a, b, /)

Same as b in a (note reversed operands).

raw_value
property values: tuple[str, ...]

Get substring match values.