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

source_changes at macosforge.org source_changes at macosforge.org
Wed Mar 12 11:25:08 PDT 2014


Revision: 12250
          http://trac.calendarserver.org//changeset/12250
Author:   wsanchez at apple.com
Date:     2014-01-06 18:15:58 -0800 (Mon, 06 Jan 2014)
Log Message:
-----------
Re-introduce _queryStringFromExpression().

Modified Paths:
--------------
    twext/trunk/twext/who/idirectory.py
    twext/trunk/twext/who/opendirectory/_constants.py
    twext/trunk/twext/who/opendirectory/_service.py
    twext/trunk/twext/who/opendirectory/test/test_service.py

Modified: twext/trunk/twext/who/idirectory.py
===================================================================
--- twext/trunk/twext/who/idirectory.py	2014-01-07 02:10:02 UTC (rev 12249)
+++ twext/trunk/twext/who/idirectory.py	2014-01-07 02:15:58 UTC (rev 12250)
@@ -73,6 +73,7 @@
     """
     Invalid directory record.
     """
+
     def __init__(self, message, fields):
         """
         @param message: An error message.

Modified: twext/trunk/twext/who/opendirectory/_constants.py
===================================================================
--- twext/trunk/twext/who/opendirectory/_constants.py	2014-01-07 02:10:02 UTC (rev 12249)
+++ twext/trunk/twext/who/opendirectory/_constants.py	2014-01-07 02:15:58 UTC (rev 12250)
@@ -389,30 +389,39 @@
 
 class ODMatchType(Values):
     all = ValueConstant(0x0001)
+    all.queryString = u"({attribute}=*)"
 
     equals = ValueConstant(0x2001)
     equals.matchType = MatchType.equals
+    equals.queryString = u"({attribute}={value})"
 
     startsWith = ValueConstant(0x2002)
     startsWith.matchType = MatchType.startsWith
+    startsWith.queryString = u"({attribute}={value}*)"
 
     endsWith = ValueConstant(0x2003)
     endsWith.matchType = MatchType.endsWith
+    endsWith.queryString = u"({attribute}=*{value})"
 
     contains = ValueConstant(0x2004)
     contains.matchType = MatchType.contains
+    contains.queryString = u"({attribute}=*{value}*)"
 
     lessThan = ValueConstant(0x2005)
     lessThan.matchType = MatchType.lessThan
+    lessThan.queryString = u"({attribute}<{value})"
 
     greaterThan = ValueConstant(0x2006)
     greaterThan.matchType = MatchType.greaterThan
+    greaterThan.queryString = u"({attribute}>{value})"
 
     lessThanOrEqualTo = ValueConstant(0x2007)
     lessThanOrEqualTo.matchType = MatchType.lessThanOrEqualTo
+    lessThanOrEqualTo.queryString = u"({attribute}<={value})"
 
     greaterThanOrEqualTo = ValueConstant(0x2008)
     greaterThanOrEqualTo.matchType = MatchType.greaterThanOrEqualTo
+    greaterThanOrEqualTo.queryString = u"({attribute}>={value})"
 
     compound = ValueConstant(0x210B)
 

Modified: twext/trunk/twext/who/opendirectory/_service.py
===================================================================
--- twext/trunk/twext/who/opendirectory/_service.py	2014-01-07 02:10:02 UTC (rev 12249)
+++ twext/trunk/twext/who/opendirectory/_service.py	2014-01-07 02:15:58 UTC (rev 12250)
@@ -40,7 +40,7 @@
     DirectoryRecord as BaseDirectoryRecord,
 )
 from ..expression import (
-    # CompoundExpression, Operand,
+    CompoundExpression, Operand,
     MatchExpression, MatchFlags,
 )
 from ..util import iterFlags, ConstantsContainer
@@ -228,70 +228,102 @@
             self._node = node
 
 
-    # def _queryStringFromExpression(self, expression):
-    #     """
-    #     Converts either a MatchExpression or a CompoundExpression into a
-    #     native OpenDirectory query string.
+    def _queryStringFromMatchExpression(self, expression):
+        """
+        Generates an LDAP query string from a match expression.
 
-    #     @param expression: The expression
-    #     @type expression: Either L{MatchExpression} or L{CompoundExpression}
+        @param expression: A match expression.
+        @type expression: L{MatchExpression}
 
-    #     @return: A native OpenDirectory query string
-    #     @rtype: C{unicode}
-    #     """
+        @return: An LDAP query string.
+        @rtype: C{unicode}
+        """
+        matchType = ODMatchType.fromMatchType(expression.matchType)
+        if matchType is None:
+            raise QueryNotSupportedError(
+                "Unknown match type: {0}".format(matchType)
+            )
 
-    #     if isinstance(expression, MatchExpression):
-    #         matchType = ODMatchType.fromMatchType(expression.matchType)
-    #         if matchType is None:
-    #             raise QueryNotSupportedError(
-    #                 "Unknown match type: {0}".format(matchType)
-    #             )
+        flags = tuple(iterFlags(expression.flags))
+        if MatchFlags.NOT in flags:
+            raise NotImplementedError("Need to handle NOT")
+        if MatchFlags.caseInsensitive in flags:
+            raise NotImplementedError("Need to handle caseInsensitive")
 
-    #         if expression.fieldName is self.fieldName.uid:
-    #             odAttr = ODAttribute.guid.value
-    #             value = expression.fieldValue
-    #         else:
-    #             odAttr = ODAttribute.fromFieldName(expression.fieldName)
-    #             if odAttr is None:
-    #                 raise OpenDirectoryQueryError(
-    #                     "Unknown field name: {0}"
-    #                     .format(expression.fieldName)
-    #                 )
-    #             odAttr = odAttr.value
-    #             value = expression.fieldValue
+        if expression.fieldName is self.fieldName.uid:
+            odAttr = ODAttribute.guid
+            value = expression.fieldValue
+        else:
+            odAttr = ODAttribute.fromFieldName(expression.fieldName)
+            if odAttr is None:
+                raise OpenDirectoryQueryError(
+                    "Unknown field name: {0}"
+                    .format(expression.fieldName)
+                )
+            value = expression.fieldValue
 
-    #         value = unicode(value)
+        value = unicode(value)
 
-    #         # FIXME: Shouldn't the value be quoted somehow?
-    #         queryString = {
-    #             ODMatchType.equals.value: u"({attr}={value})",
-    #             ODMatchType.startsWith.value: u"({attr}={value}*)",
-    #             ODMatchType.endsWith.value: u"({attr}=*{value})",
-    #             ODMatchType.contains.value: u"({attr}=*{value}*)",
-    #             ODMatchType.lessThan.value: u"({attr}<{value})",
-    #             ODMatchType.greaterThan.value: u"({attr}>{value})",
-    #         }.get(matchType.value, u"({attr}=*{value}*)").format(
-    #             attr=odAttr,
-    #             value=value
-    #         )
+        # FIXME: Shouldn't the value be quoted somehow?
+        return matchType.queryString.format(
+            attribute=odAttr.value, value=value
+        )
 
-    #     elif isinstance(expression, CompoundExpression):
-    #         queryString = u""
-    #         operand = u"&" if expression.operand is Operand.AND else u"|"
 
-    #         if len(expression.expressions) > 1:
-    #             queryString += u"("
-    #             queryString += operand
+    def _queryStringFromCompoundExpression(self, expression):
+        """
+        Generates an LDAP query string from a compound expression.
 
-    #         for subExpression in expression.expressions:
-    #             queryString += self._queryStringFromExpression(subExpression)
+        @param expression: A match expression.
+        @type expression: L{MatchExpression}
 
-    #         if len(expression.expressions) > 1:
-    #             queryString += u")"
+        @return: An LDAP query string.
+        @rtype: C{unicode}
+        """
+        queryTokens = []
 
-    #     return queryString
+        if len(expression.expressions) > 1:
+            queryTokens.append(u"(")
 
+            if expression.operand is Operand.AND:
+                queryTokens.append(u"&")
+            else:
+                queryTokens.append(u"|")
 
+        for subExpression in expression.expressions:
+            queryTokens.append(
+                self._queryStringFromExpression(subExpression)
+            )
+
+        if len(expression.expressions) > 1:
+            queryTokens.append(u")")
+
+        return u"".join(queryTokens)
+
+
+    def _queryStringFromExpression(self, expression):
+        """
+        Converts either a MatchExpression or a CompoundExpression into an LDAP
+        query string.
+
+        @param expression: An expression.
+        @type expression: L{MatchExpression} or L{CompoundExpression}
+
+        @return: A native OpenDirectory query string
+        @rtype: C{unicode}
+        """
+
+        if isinstance(expression, MatchExpression):
+            return self._queryStringFromMatchExpression(expression)
+
+        if isinstance(expression, CompoundExpression):
+            return self._queryStringFromCompoundExpression(expression)
+
+        raise QueryNotSupportedError(
+            "Unknown expression type: {0!r}".format(expression)
+        )
+
+
     # def _queryFromCompoundExpression(self, expression):
     #     """
     #     Form an OpenDirectory query from a compound expression.

Modified: twext/trunk/twext/who/opendirectory/test/test_service.py
===================================================================
--- twext/trunk/twext/who/opendirectory/test/test_service.py	2014-01-07 02:10:02 UTC (rev 12249)
+++ twext/trunk/twext/who/opendirectory/test/test_service.py	2014-01-07 02:15:58 UTC (rev 12250)
@@ -20,10 +20,11 @@
 
 from twisted.trial import unittest
 
-# from ...expression import (
-#     CompoundExpression, MatchExpression, MatchType, Operand
-# )
-# from .._service import DirectoryService
+from ...expression import (
+    CompoundExpression, Operand, MatchExpression, MatchType, MatchFlags
+)
+from .._constants import ODAttribute
+from .._service import DirectoryService
 
 
 
@@ -32,89 +33,149 @@
     Tests for L{DirectoryService}.
     """
 
-    # def test_queryStringFromExpression(self):
-    #     service = DirectoryService()
+    def test_queryStringFromMatchExpression_matchTypes(self):
+        """
+        Match expressions with each match type produces the correct
+        operator=value string.
+        """
 
-    #     # MatchExpressions
+        service = DirectoryService()
 
-    #     for matchType, expected in (
-    #         (MatchType.equals, u"=xyzzy"),
-    #         (MatchType.startsWith, u"=xyzzy*"),
-    #         (MatchType.endsWith, u"=*xyzzy"),
-    #         (MatchType.contains, u"=*xyzzy*"),
-    #     ):
-    #         expression = MatchExpression(
-    #             service.fieldName.shortNames, u"xyzzy",
-    #             matchType=matchType
-    #         )
-    #         queryString = service._queryStringFromExpression(expression)
-    #         self.assertEquals(
-    #             queryString,
-    #             u"(dsAttrTypeStandard:RecordName{exp})".format(exp=expected)
-    #         )
+        for matchType, expected in (
+            (MatchType.equals, u"=xyzzy"),
+            (MatchType.startsWith, u"=xyzzy*"),
+            (MatchType.endsWith, u"=*xyzzy"),
+            (MatchType.contains, u"=*xyzzy*"),
+            (MatchType.lessThan, u"<xyzzy"),
+            (MatchType.greaterThan, u">xyzzy"),
+            (MatchType.lessThanOrEqualTo, u"<=xyzzy"),
+            (MatchType.greaterThanOrEqualTo, u">=xyzzy"),
+        ):
+            expression = MatchExpression(
+                service.fieldName.shortNames, u"xyzzy",
+                matchType=matchType
+            )
+            queryString = service._queryStringFromExpression(expression)
+            self.assertEquals(
+                queryString,
+                u"({attribute}{expected})".format(
+                    attribute=ODAttribute.shortName.value, expected=expected
+                )
+            )
 
-    #     # CompoundExpressions
 
-    #     expression = CompoundExpression(
-    #         [
-    #             MatchExpression(
-    #                 service.fieldName.uid, u"a",
-    #                 matchType=MatchType.contains
-    #             ),
-    #             MatchExpression(
-    #                 service.fieldName.guid, u"b",
-    #                 matchType=MatchType.contains
-    #             ),
-    #             MatchExpression(
-    #                 service.fieldName.shortNames, u"c",
-    #                 matchType=MatchType.contains
-    #             ),
-    #             MatchExpression(
-    #                 service.fieldName.emailAddresses, u"d",
-    #                 matchType=MatchType.startsWith
-    #             ),
-    #             MatchExpression(
-    #                 service.fieldName.fullNames, u"e",
-    #                 matchType=MatchType.equals
-    #             ),
-    #         ],
-    #         Operand.AND
-    #     )
-    #     queryString = service._queryStringFromExpression(expression)
-    #     self.assertEquals(
-    #         queryString,
-    #         (
-    #             u"(&(dsAttrTypeStandard:GeneratedUID=*a*)"
-    #             u"(dsAttrTypeStandard:GeneratedUID=*b*)"
-    #             u"(dsAttrTypeStandard:RecordName=*c*)"
-    #             u"(dsAttrTypeStandard:EMailAddress=d*)"
-    #             u"(dsAttrTypeStandard:RealName=e))"
-    #         )
-    #     )
+    def test_queryStringFromMatchExpression_match_not(self):
+        """
+        Match expression with the C{NOT} flag adds the C{!} operator.
+        """
 
-    #     expression = CompoundExpression(
-    #         [
-    #             MatchExpression(
-    #                 service.fieldName.shortNames, u"a",
-    #                 matchType=MatchType.contains
-    #             ),
-    #             MatchExpression(
-    #                 service.fieldName.emailAddresses, u"b",
-    #                 matchType=MatchType.startsWith
-    #             ),
-    #             MatchExpression(
-    #                 service.fieldName.fullNames, u"c",
-    #                 matchType=MatchType.equals
-    #             ),
-    #         ],
-    #         Operand.OR
-    #     )
-    #     queryString = service._queryStringFromExpression(expression)
-    #     self.assertEquals(
-    #         queryString,
-    #         (
-    #             u"(|(dsAttrTypeStandard:RecordName=*a*)"
-    #             u"(dsAttrTypeStandard:EMailAddress=b*)"
-    #             u"(dsAttrTypeStandard:RealName=c))"
-    #         )
-    #     )
+        service = DirectoryService()
+
+        expression = MatchExpression(
+            service.fieldName.shortNames, u"xyzzy",
+            flags=MatchFlags.NOT
+        )
+        queryString = service._queryStringFromExpression(expression)
+        self.assertEquals(
+            queryString,
+            u"(!{attribute}=xyzzy)".format(
+                attribute=ODAttribute.shortName.value,
+            )
+        )
+
+    test_queryStringFromMatchExpression_match_not.todo = "unimplemented"
+
+
+    def test_queryStringFromMatchExpression_match_caseInsensitive(self):
+        """
+        Match expression with the C{caseInsensitive} flag adds the C{??????}
+        operator.
+        """
+
+        service = DirectoryService()
+
+        expression = MatchExpression(
+            service.fieldName.shortNames, u"xyzzy",
+            flags=MatchFlags.caseInsensitive
+        )
+        queryString = service._queryStringFromExpression(expression)
+        self.assertEquals(
+            queryString,
+            u"???????({attribute}=xyzzy)".format(
+                attribute=ODAttribute.shortName.value,
+            )
+        )
+
+    test_queryStringFromMatchExpression_match_caseInsensitive.todo = (
+        "unimplemented"
+    )
+
+
+    def test_queryStringFromExpression(self):
+        service = DirectoryService()
+
+        # CompoundExpressions
+
+        expression = CompoundExpression(
+            [
+                MatchExpression(
+                    service.fieldName.uid, u"a",
+                    matchType=MatchType.contains
+                ),
+                MatchExpression(
+                    service.fieldName.guid, u"b",
+                    matchType=MatchType.contains
+                ),
+                MatchExpression(
+                    service.fieldName.shortNames, u"c",
+                    matchType=MatchType.contains
+                ),
+                MatchExpression(
+                    service.fieldName.emailAddresses, u"d",
+                    matchType=MatchType.startsWith
+                ),
+                MatchExpression(
+                    service.fieldName.fullNames, u"e",
+                    matchType=MatchType.equals
+                ),
+            ],
+            Operand.AND
+        )
+        queryString = service._queryStringFromExpression(expression)
+        self.assertEquals(
+            queryString,
+            (
+                u"(&(dsAttrTypeStandard:GeneratedUID=*a*)"
+                u"(dsAttrTypeStandard:GeneratedUID=*b*)"
+                u"(dsAttrTypeStandard:RecordName=*c*)"
+                u"(dsAttrTypeStandard:EMailAddress=d*)"
+                u"(dsAttrTypeStandard:RealName=e))"
+            )
+        )
+
+        expression = CompoundExpression(
+            [
+                MatchExpression(
+                    service.fieldName.shortNames, u"a",
+                    matchType=MatchType.contains
+                ),
+                MatchExpression(
+                    service.fieldName.emailAddresses, u"b",
+                    matchType=MatchType.startsWith
+                ),
+                MatchExpression(
+                    service.fieldName.fullNames, u"c",
+                    matchType=MatchType.equals
+                ),
+            ],
+            Operand.OR
+        )
+        queryString = service._queryStringFromExpression(expression)
+        self.assertEquals(
+            queryString,
+            (
+                u"(|(dsAttrTypeStandard:RecordName=*a*)"
+                u"(dsAttrTypeStandard:EMailAddress=b*)"
+                u"(dsAttrTypeStandard:RealName=c))"
+            )
+        )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140312/10be8fe5/attachment.html>


More information about the calendarserver-changes mailing list