[CalendarServer-changes] [11932] CalendarServer/trunk/twext/who
source_changes at macosforge.org
source_changes at macosforge.org
Wed Mar 12 11:21:59 PDT 2014
Revision: 11932
http://trac.calendarserver.org//changeset/11932
Author: wsanchez at apple.com
Date: 2013-11-09 19:40:56 -0800 (Sat, 09 Nov 2013)
Log Message:
-----------
Move logic for predicate and normalize functions to MatchType class.
Modified Paths:
--------------
CalendarServer/trunk/twext/who/expression.py
CalendarServer/trunk/twext/who/index.py
Modified: CalendarServer/trunk/twext/who/expression.py
===================================================================
--- CalendarServer/trunk/twext/who/expression.py 2013-11-09 01:48:39 UTC (rev 11931)
+++ CalendarServer/trunk/twext/who/expression.py 2013-11-10 03:40:56 UTC (rev 11932)
@@ -31,6 +31,7 @@
from twisted.python.constants import Names, NamedConstant
from twisted.python.constants import Flags, FlagConstant
+from twext.who.util import iterFlags, describe
#
@@ -93,7 +94,78 @@
caseInsensitive.description = u"case insensitive"
+ @staticmethod
+ def _setMatchFunctions(flags):
+ """
+ Compute a predicate and normalize functions for the given match
+ expression flags.
+ @param flags: Match expression flags.
+ @type flags: L{MatchFlags}
+
+ @return: Predicate and normalize functions.
+ @rtype: L{tuple} of callables.
+ """
+ predicate = lambda x: x
+ normalize = lambda x: x
+
+ if flags is None:
+ flags = FlagConstant()
+ else:
+ for flag in iterFlags(flags):
+ if flag == MatchFlags.NOT:
+ predicate = lambda x: not x
+ elif flag == MatchFlags.caseInsensitive:
+ normalize = lambda x: x.lower()
+ else:
+ raise NotImplementedError(
+ "Unknown query flag: {0}".format(describe(flag))
+ )
+
+ flags._predicate = predicate
+ flags._normalize = normalize
+
+ return flags
+
+
+ @staticmethod
+ def predicator(flags):
+ """
+ Determine a predicate function for the given flags.
+
+ @param flags: Match expression flags.
+ @type flags: L{MatchFlags}
+
+ @return: a L{callable} that accepts an L{object} argument and returns a
+ L{object} that has the opposite or same truth value as the argument,
+ depending on whether L{MatchFlags.NOT} is or is not in C{flags}.
+ @rtype: callable
+ """
+ if not hasattr(flags, "_predicate"):
+ flags = MatchFlags._setMatchFunctions(flags)
+ return flags._predicate
+
+
+ @staticmethod
+ def normalizer(flags):
+ """
+ Determine a predicate function for the given flags.
+
+ @param flags: Match expression flags.
+ @type flags: L{MatchFlags}
+
+ @return: a L{callable} that accepts a L{unicode} and returns the same
+ L{unicode} or a normalized L{unicode} that can be compared with other
+ normalized L{unicode}s in a case-insensitive fashion, depending on
+ whether L{MatchFlags.caseInsensitive} is not or is in C{flags}.
+ @rtype: callable
+ """
+ if not hasattr(flags, "_normalize"):
+ flags = MatchFlags._setMatchFunctions(flags)
+ return flags._normalize
+
+
+
class MatchExpression(object):
"""
Query for a matching value in a given field.
Modified: CalendarServer/trunk/twext/who/index.py
===================================================================
--- CalendarServer/trunk/twext/who/index.py 2013-11-09 01:48:39 UTC (rev 11931)
+++ CalendarServer/trunk/twext/who/index.py 2013-11-10 03:40:56 UTC (rev 11932)
@@ -30,7 +30,7 @@
from twisted.internet.defer import succeed, inlineCallbacks, returnValue
from twext.who.util import ConstantsContainer
-from twext.who.util import describe, uniqueResult, iterFlags
+from twext.who.util import describe, uniqueResult
from twext.who.idirectory import FieldName as BaseFieldName
from twext.who.expression import MatchExpression, MatchType, MatchFlags
from twext.who.directory import DirectoryService as BaseDirectoryService
@@ -113,35 +113,6 @@
self._index = None
- @staticmethod
- def _matchFlags(flags):
- """
- Compute a predicate and normalize functions for the given match
- expression flags.
-
- @param flags: Match expression flags.
- @type flags: L{MatchFlags}
-
- @return: Predicate and normalize functions.
- @rtype: L{tuple} of callables.
- """
- predicate = lambda x: x
- normalize = lambda x: x
-
- if flags is not None:
- for flag in iterFlags(flags):
- if flag == MatchFlags.NOT:
- predicate = lambda x: not x
- elif flag == MatchFlags.caseInsensitive:
- normalize = lambda x: x.lower()
- else:
- raise NotImplementedError(
- "Unknown query flag: {0}".format(describe(flag))
- )
-
- return predicate, normalize
-
-
def indexedRecordsFromMatchExpression(self, expression, records=None):
"""
Finds records in the internal indexes matching a single expression.
@@ -156,7 +127,8 @@
@return: The matching records.
@rtype: deferred iterable of L{DirectoryRecord}s
"""
- predicate, normalize = self._matchFlags(expression.flags)
+ predicate = MatchFlags.predicator(expression.flags)
+ normalize = MatchFlags.normalizer(expression.flags)
fieldIndex = self.index[expression.fieldName]
matchValue = normalize(expression.fieldValue)
@@ -210,7 +182,8 @@
@return: The matching records.
@rtype: deferred iterable of L{DirectoryRecord}s
"""
- predicate, normalize = self._matchFlags(expression.flags)
+ predicate = MatchFlags.predicator(expression.flags)
+ normalize = MatchFlags.normalizer(expression.flags)
matchValue = normalize(expression.fieldValue)
matchType = expression.matchType
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140312/36bde201/attachment.html>
More information about the calendarserver-changes
mailing list