From ab1ba33d066e83228ac4071065e994b76c2b6b67 Mon Sep 17 00:00:00 2001 From: stroeder Date: Sun, 24 Jul 2016 15:14:56 +0000 Subject: [PATCH] added function ldap.filter.time_span_filter() --- CHANGES | 3 ++- Lib/ldap/filter.py | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 0febf32..74f02de 100644 --- a/CHANGES +++ b/CHANGES @@ -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 @@ -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 $ diff --git a/Lib/ldap/filter.py b/Lib/ldap/filter.py index 46f957e..727c1f8 100644 --- a/Lib/ldap/filter.py +++ b/Lib/ldap/filter.py @@ -3,7 +3,7 @@ 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+ @@ -11,6 +11,10 @@ from ldap import __version__ +from ldap.functions import strf_secs + +import time + def escape_filter_chars(assertion_value,escape_mode=0): """ @@ -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()