[CalendarServer-changes] [10770] CalendarServer/trunk/twext/who

source_changes at macosforge.org source_changes at macosforge.org
Thu Feb 21 09:45:57 PST 2013


Revision: 10770
          http://trac.calendarserver.org//changeset/10770
Author:   wsanchez at apple.com
Date:     2013-02-21 09:45:57 -0800 (Thu, 21 Feb 2013)
Log Message:
-----------
Move DirectoryQueryMatchExpression out of interface module and into a
new expression module.

Rename QueryFlags to MatchFlags, as these are specific to match
expressions, and difference expression classes should have their own
flags.

Modified Paths:
--------------
    CalendarServer/trunk/twext/who/directory.py
    CalendarServer/trunk/twext/who/idirectory.py
    CalendarServer/trunk/twext/who/index.py
    CalendarServer/trunk/twext/who/test/test_xml.py

Modified: CalendarServer/trunk/twext/who/directory.py
===================================================================
--- CalendarServer/trunk/twext/who/directory.py	2013-02-20 23:20:16 UTC (rev 10769)
+++ CalendarServer/trunk/twext/who/directory.py	2013-02-21 17:45:57 UTC (rev 10770)
@@ -34,8 +34,8 @@
 from twext.who.idirectory import QueryNotSupportedError, NotAllowedError
 from twext.who.idirectory import FieldName, RecordType
 from twext.who.idirectory import Operand
-from twext.who.idirectory import DirectoryQueryMatchExpression
 from twext.who.idirectory import IDirectoryService, IDirectoryRecord
+from twext.who.expression import MatchExpression
 from twext.who.util import uniqueResult, describe
 
 
@@ -115,7 +115,7 @@
 
 
     def recordsWithFieldValue(self, fieldName, value):
-        return self.recordsFromExpression(DirectoryQueryMatchExpression(fieldName, value))
+        return self.recordsFromExpression(MatchExpression(fieldName, value))
 
     @inlineCallbacks
     def recordWithUID(self, uid):
@@ -131,8 +131,8 @@
     @inlineCallbacks
     def recordWithShortName(self, recordType, shortName):
         returnValue(uniqueResult((yield self.recordsFromQuery((
-            DirectoryQueryMatchExpression(FieldName.recordType, recordType),
-            DirectoryQueryMatchExpression(FieldName.shortNames, shortName ),
+            MatchExpression(FieldName.recordType, recordType),
+            MatchExpression(FieldName.shortNames, shortName ),
         )))))
 
     def recordsWithEmailAddress(self, emailAddress):

Modified: CalendarServer/trunk/twext/who/idirectory.py
===================================================================
--- CalendarServer/trunk/twext/who/idirectory.py	2013-02-20 23:20:16 UTC (rev 10769)
+++ CalendarServer/trunk/twext/who/idirectory.py	2013-02-21 17:45:57 UTC (rev 10770)
@@ -28,10 +28,7 @@
 
     "RecordType",
     "FieldName",
-    "MatchType",
     "Operand",
-    "QueryFlags",
-    "DirectoryQueryMatchExpression",
 
     "IDirectoryService",
     "IDirectoryRecord",
@@ -40,7 +37,6 @@
 from zope.interface import Attribute, Interface
 
 from twisted.python.constants import Names, NamedConstant
-from twisted.python.constants import Flags, FlagConstant
 
 
 
@@ -126,20 +122,6 @@
 
 
 
-class MatchType(Names):
-    """
-    Query match types.
-    """
-    equals     = NamedConstant()
-    startsWith = NamedConstant()
-    contains   = NamedConstant()
-
-    equals.description     = "equals"
-    startsWith.description = "starts with"
-    contains.description   = "contains"
-
-
-
 class Operand(Names):
     OR  = NamedConstant()
     AND = NamedConstant()
@@ -149,53 +131,6 @@
 
 
 
-class QueryFlags(Flags):
-    """
-    Query flags.
-    """
-    NOT = FlagConstant()
-    NOT.description = "not"
-
-    caseInsensitive = FlagConstant()
-    caseInsensitive.description = "case insensitive"
-
-
-
-class DirectoryQueryMatchExpression(object):
-    """
-    Query for a matching value in a given field.
-
-    @ivar fieldName: a L{NamedConstant} specifying the field
-    @ivar fieldValue: a text value to match
-    @ivar matchType: a L{NamedConstant} specifying the match algorythm
-    @ivar flags: L{NamedConstant} specifying additional options
-    """
-
-    def __init__(self, fieldName, fieldValue, matchType=MatchType.equals, flags=None):
-        self.fieldName  = fieldName
-        self.fieldValue = fieldValue
-        self.matchType  = matchType
-        self.flags      = flags
-
-    def __repr__(self):
-        def describe(constant):
-            return getattr(constant, "description", str(constant))
-
-        if self.flags is None:
-            flags = ""
-        else:
-            flags = " (%s)" % (self.flags,)
-
-        return "<%s: %r %s %r%s>" % (
-            self.__class__.__name__,
-            describe(self.fieldName),
-            describe(self.matchType),
-            describe(self.fieldValue),
-            flags
-        )
-
-
-
 ##
 # Interfaces
 ##

Modified: CalendarServer/trunk/twext/who/index.py
===================================================================
--- CalendarServer/trunk/twext/who/index.py	2013-02-20 23:20:16 UTC (rev 10769)
+++ CalendarServer/trunk/twext/who/index.py	2013-02-21 17:45:57 UTC (rev 10770)
@@ -29,8 +29,7 @@
 
 from twext.who.util import MergedConstants, describe, uniqueResult, iterFlags
 from twext.who.idirectory import FieldName as BaseFieldName
-from twext.who.idirectory import MatchType, QueryFlags
-from twext.who.idirectory import DirectoryQueryMatchExpression
+from twext.who.expression import MatchExpression, MatchType, MatchFlags
 from twext.who.directory import DirectoryService as BaseDirectoryService
 from twext.who.directory import DirectoryRecord as BaseDirectoryRecord
 
@@ -106,9 +105,9 @@
 
         if flags is not None:
             for flag in iterFlags(flags):
-                if flag == QueryFlags.NOT:
+                if flag == MatchFlags.NOT:
                     predicate = lambda x: not x
-                elif flag == QueryFlags.caseInsensitive:
+                elif flag == MatchFlags.caseInsensitive:
                     normalize = lambda x: x.lower()
                 else:
                     raise NotImplementedError("Unknown query flag: %s" % (describe(flag),))
@@ -191,7 +190,7 @@
 
 
     def recordsFromExpression(self, expression, records=None):
-        if isinstance(expression, DirectoryQueryMatchExpression):
+        if isinstance(expression, MatchExpression):
             if expression.fieldName in self.indexedFields:
                 return self.indexedRecordsFromMatchExpression(expression, records=records)
             else:

Modified: CalendarServer/trunk/twext/who/test/test_xml.py
===================================================================
--- CalendarServer/trunk/twext/who/test/test_xml.py	2013-02-20 23:20:16 UTC (rev 10769)
+++ CalendarServer/trunk/twext/who/test/test_xml.py	2013-02-21 17:45:57 UTC (rev 10770)
@@ -24,8 +24,8 @@
 from twisted.internet.defer import inlineCallbacks
 
 from twext.who.idirectory import NoSuchRecordError
-from twext.who.idirectory import DirectoryQueryMatchExpression
-from twext.who.idirectory import Operand, MatchType, QueryFlags
+from twext.who.idirectory import Operand
+from twext.who.expression import MatchExpression, MatchType, MatchFlags
 from twext.who.xml import ParseError
 from twext.who.xml import DirectoryService, DirectoryRecord
 
@@ -166,7 +166,7 @@
             def query(self, field, value, matchType=MatchType.equals, flags=None):
                 name = getattr(self.fieldName, field)
                 assert name is not None
-                return DirectoryQueryMatchExpression(
+                return MatchExpression(
                     name, value,
                     matchType = matchType,
                     flags = flags,
@@ -373,7 +373,7 @@
         records = yield service.recordsFromQuery(
             (
                 service.query("emailAddresses", "shared at example.com"),
-                service.query("shortNames", "sagen", flags=QueryFlags.NOT),
+                service.query("shortNames", "sagen", flags=MatchFlags.NOT),
             ),
             operand=Operand.AND
         )
@@ -386,7 +386,7 @@
         records = yield service.recordsFromQuery(
             (
                 service.query("emailAddresses", "shared at example.com"),
-                service.query("fullNames", "Andre LaBranche", flags=QueryFlags.NOT),
+                service.query("fullNames", "Andre LaBranche", flags=MatchFlags.NOT),
             ),
             operand=Operand.AND
         )
@@ -397,7 +397,7 @@
     def test_queryCaseInsensitive(self):
         service = self._testService()
         records = yield service.recordsFromQuery((
-            service.query("shortNames", "SagEn", flags=QueryFlags.caseInsensitive),
+            service.query("shortNames", "SagEn", flags=MatchFlags.caseInsensitive),
         ))
         self.assertRecords(records, ("__sagen__",))
 
@@ -406,7 +406,7 @@
     def test_queryCaseInsensitiveNoIndex(self):
         service = self._testService()
         records = yield service.recordsFromQuery((
-            service.query("fullNames", "moRGen SAGen", flags=QueryFlags.caseInsensitive),
+            service.query("fullNames", "moRGen SAGen", flags=MatchFlags.caseInsensitive),
         ))
         self.assertRecords(records, ("__sagen__",))
 
@@ -436,7 +436,7 @@
             service.query(
                 "shortNames", "w",
                 matchType = MatchType.startsWith,
-                flags = QueryFlags.NOT,
+                flags = MatchFlags.NOT,
             ),
         ))
         self.assertRecords(
@@ -470,7 +470,7 @@
             service.query(
                 "shortNames", "wil",
                 matchType = MatchType.startsWith,
-                flags = QueryFlags.NOT,
+                flags = MatchFlags.NOT,
             ),
         ))
         self.assertRecords(
@@ -499,7 +499,7 @@
             service.query(
                 "fullNames", "Wilfredo",
                 matchType = MatchType.startsWith,
-                flags = QueryFlags.NOT,
+                flags = MatchFlags.NOT,
             ),
         ))
         self.assertRecords(
@@ -527,7 +527,7 @@
             service.query(
                 "shortNames", "WIL",
                 matchType = MatchType.startsWith,
-                flags = QueryFlags.caseInsensitive,
+                flags = MatchFlags.caseInsensitive,
             ),
         ))
         self.assertRecords(records, ("__wsanchez__",))
@@ -540,7 +540,7 @@
             service.query(
                 "fullNames", "wilfrEdo",
                 matchType = MatchType.startsWith,
-                flags = QueryFlags.caseInsensitive,
+                flags = MatchFlags.caseInsensitive,
             ),
         ))
         self.assertRecords(records, ("__wsanchez__",))
@@ -571,7 +571,7 @@
             service.query(
                 "shortNames", "sanchez",
                 matchType = MatchType.contains,
-                flags = QueryFlags.NOT,
+                flags = MatchFlags.NOT,
             ),
         ))
         self.assertRecords(
@@ -599,7 +599,7 @@
             service.query(
                 "fullNames", "fred",
                 matchType = MatchType.contains,
-                flags = QueryFlags.NOT,
+                flags = MatchFlags.NOT,
             ),
         ))
         self.assertRecords(
@@ -627,7 +627,7 @@
             service.query(
                 "shortNames", "Sanchez",
                 matchType=MatchType.contains,
-                flags=QueryFlags.caseInsensitive,
+                flags=MatchFlags.caseInsensitive,
             ),
         ))
         self.assertRecords(records, ("__wsanchez__",))
@@ -640,7 +640,7 @@
             service.query(
                 "fullNames", "frEdo",
                 matchType=MatchType.contains,
-                flags=QueryFlags.caseInsensitive,
+                flags=MatchFlags.caseInsensitive,
             ),
         ))
         self.assertRecords(records, ("__wsanchez__",))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130221/b849d2ad/attachment-0001.html>


More information about the calendarserver-changes mailing list