Skip to content

Commit

Permalink
added function ldap.filter.time_span_filter()
Browse files Browse the repository at this point in the history
  • Loading branch information
stroeder committed Jul 24, 2016
1 parent 9f889bb commit ab1ba33
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Lib/
LDAPObject._l to completely invalidate C wrapper object
* LDAPObject.unbind_ext() now flushes trace file
* added functions ldap.strf_secs() and ldap.strp_secs()
* added function ldap.filter.time_span_filter()
* Refactored ldif.LDIFParser
* ldif.LDIFParser.version ís now Integer
* ignore multiple empty lines between records
Expand Down Expand Up @@ -1266,4 +1267,4 @@ Released 2.0.0pre02 2002-02-01
----------------------------------------------------------------
Released 1.10alpha3 2000-09-19

$Id: CHANGES,v 1.382 2016/07/17 19:46:49 stroeder Exp $
$Id: CHANGES,v 1.383 2016/07/24 15:14:56 stroeder Exp $
38 changes: 37 additions & 1 deletion Lib/ldap/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
See http://www.python-ldap.org/ for details.
\$Id: filter.py,v 1.10 2015/06/06 09:21:37 stroeder Exp $
\$Id: filter.py,v 1.11 2016/07/24 15:14:56 stroeder Exp $
Compability:
- Tested with Python 2.0+
"""

from ldap import __version__

from ldap.functions import strf_secs

import time


def escape_filter_chars(assertion_value,escape_mode=0):
"""
Expand Down Expand Up @@ -53,3 +57,35 @@ def filter_format(filter_template,assertion_values):
count of %s in filter_template.
"""
return filter_template % (tuple(map(escape_filter_chars,assertion_values)))


def time_span_filter(
filterstr='',
from_timestamp=0,
until_timestamp=None,
delta_attr='modifyTimestamp',
):
"""
If last_run_timestr is non-zero filterstr will be extended
"""
if until_timestamp is None:
until_timestamp = time.time()
if from_timestamp < 0:
from_timestamp = until_timestamp + from_timestamp
if from_timestamp > until_timestamp:
raise ValueError('from_timestamp %r must not be greater than until_timestamp %r' % (
from_timestamp, until_timestamp
))
return (
'(&'
'{filterstr}'
'({delta_attr}>={from_timestr})'
'(!({delta_attr}>={until_timestr}))'
')'
).format(
filterstr=filterstr,
delta_attr=delta_attr,
from_timestr=strf_secs(from_timestamp),
until_timestr=strf_secs(until_timestamp),
)
# end of time_span_filter()

0 comments on commit ab1ba33

Please sign in to comment.