<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[12112] twext/trunk/twext/who</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/12112">12112</a></dd>
<dt>Author</dt> <dd>wsanchez@apple.com</dd>
<dt>Date</dt> <dd>2013-12-16 14:37:53 -0800 (Mon, 16 Dec 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>Add (incomplete) OpenDirectory implementation.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#twexttrunktwextwhoexpressionpy">twext/trunk/twext/who/expression.py</a></li>
<li><a href="#twexttrunktwextwhoidirectorypy">twext/trunk/twext/who/idirectory.py</a></li>
<li><a href="#twexttrunktwextwhoopendirectory__init__py">twext/trunk/twext/who/opendirectory/__init__.py</a></li>
<li><a href="#twexttrunktwextwhoopendirectorydsquerypy">twext/trunk/twext/who/opendirectory/dsquery.py</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li>twext/trunk/twext/who/opendirectory/</li>
<li><a href="#twexttrunktwextwhoopendirectoryservicepy">twext/trunk/twext/who/opendirectory/service.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="twexttrunktwextwhoexpressionpy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/expression.py (12111 => 12112)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/expression.py        2013-12-16 17:12:05 UTC (rev 12111)
+++ twext/trunk/twext/who/expression.py        2013-12-16 22:37:53 UTC (rev 12112)
</span><span class="lines">@@ -78,6 +78,9 @@
</span><span class="cx">     startsWith = NamedConstant()
</span><span class="cx">     startsWith.description = u&quot;starts with&quot;
</span><span class="cx"> 
</span><ins>+    endsWith = NamedConstant()
+    startsWith.description = u&quot;ends with&quot;
+
</ins><span class="cx">     contains = NamedConstant()
</span><span class="cx">     contains.description = u&quot;contains&quot;
</span><span class="cx"> 
</span><span class="lines">@@ -184,6 +187,10 @@
</span><span class="cx">         fieldName, fieldValue,
</span><span class="cx">         matchType=MatchType.equals, flags=None
</span><span class="cx">     ):
</span><ins>+        if flags is None:
+            # Gross way to get a FlagsConstant with no flags in it:
+            flags = MatchFlags.NOT &amp; MatchFlags.caseInsensitive
+
</ins><span class="cx">         self.fieldName = fieldName
</span><span class="cx">         self.fieldValue = fieldValue
</span><span class="cx">         self.matchType = matchType
</span></span></pre></div>
<a id="twexttrunktwextwhoidirectorypy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/idirectory.py (12111 => 12112)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/idirectory.py        2013-12-16 17:12:05 UTC (rev 12111)
+++ twext/trunk/twext/who/idirectory.py        2013-12-16 22:37:53 UTC (rev 12112)
</span><span class="lines">@@ -107,10 +107,10 @@
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     Constants for common directory record types.
</span><span class="cx">     &quot;&quot;&quot;
</span><del>-    user  = NamedConstant()
</del><ins>+    user = NamedConstant()
+    user.description  = u&quot;user&quot;
+
</ins><span class="cx">     group = NamedConstant()
</span><del>-
-    user.description  = u&quot;user&quot;
</del><span class="cx">     group.description = u&quot;group&quot;
</span><span class="cx"> 
</span><span class="cx"> 
</span></span></pre></div>
<a id="twexttrunktwextwhoopendirectory__init__py"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/opendirectory/__init__.py (12111 => 12112)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/platform/darwin/od/__init__.py        2013-12-16 17:12:05 UTC (rev 12111)
+++ twext/trunk/twext/who/opendirectory/__init__.py        2013-12-16 22:37:53 UTC (rev 12112)
</span><span class="lines">@@ -13,3 +13,11 @@
</span><span class="cx"> # See the License for the specific language governing permissions and
</span><span class="cx"> # limitations under the License.
</span><span class="cx"> ##
</span><ins>+
+__all__ = [
+    &quot;DirectoryService&quot;,
+    &quot;DirectoryRecord&quot;,
+]
+
+
+from .service import DirectoryService, DirectoryRecord
</ins></span></pre></div>
<a id="twexttrunktwextwhoopendirectorydsquerypy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/opendirectory/dsquery.py (12111 => 12112)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/platform/darwin/od/dsquery.py        2013-12-16 17:12:05 UTC (rev 12111)
+++ twext/trunk/twext/who/opendirectory/dsquery.py        2013-12-16 22:37:53 UTC (rev 12112)
</span><span class="lines">@@ -25,6 +25,8 @@
</span><span class="cx"> 
</span><span class="cx"> import dsattributes
</span><span class="cx"> 
</span><ins>+
+
</ins><span class="cx"> class match(object):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     Represents and attribute/value match operation.
</span></span></pre></div>
<a id="twexttrunktwextwhoopendirectoryservicepy"></a>
<div class="addfile"><h4>Added: twext/trunk/twext/who/opendirectory/service.py (0 => 12112)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/opendirectory/service.py                                (rev 0)
+++ twext/trunk/twext/who/opendirectory/service.py        2013-12-16 22:37:53 UTC (rev 12112)
</span><span class="lines">@@ -0,0 +1,418 @@
</span><ins>+# -*- test-case-name: twext.who.test.test_util -*-
+##
+# Copyright (c) 2013 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+from __future__ import print_function
+
+&quot;&quot;&quot;
+OpenDirectory directory service implementation.
+&quot;&quot;&quot;
+
+__all__ = [
+    &quot;OpenDirectoryError&quot;,
+    &quot;DirectoryService&quot;,
+    &quot;DirectoryRecord&quot;,
+]
+
+from itertools import chain
+
+from twext.python.log import Logger
+from twisted.python.constants import Names, NamedConstant
+from twisted.python.constants import Values, ValueConstant
+
+from twext.who.idirectory import (
+    DirectoryServiceError, QueryNotSupportedError,
+    FieldName as BaseFieldName,
+    RecordType as BaseRecordType,
+)
+from twext.who.directory import (
+    DirectoryService as BaseDirectoryService,
+    DirectoryRecord as BaseDirectoryRecord,
+)
+from twext.who.expression import CompoundExpression, Operand
+from twext.who.expression import MatchExpression, MatchType, MatchFlags
+from twext.who.util import iterFlags, ConstantsContainer
+
+import dsattributes
+from opendirectory import (
+    ODError, odInit,
+    getNodeAttributes,
+    queryRecordsWithAttribute_list,
+)
+
+
+
+#
+# Exceptions
+#
+
+class OpenDirectoryError(DirectoryServiceError):
+    &quot;&quot;&quot;
+    OpenDirectory error.
+    &quot;&quot;&quot;
+
+
+
+#
+# Constants
+#
+
+class FieldName(Names):
+    metaNodeLocation = NamedConstant()
+    metaNodeLocation.description = &quot;source OD node&quot;
+    metaNodeLocation.multiValue = False
+
+
+#
+# Constants
+#
+
+class ODRecordType(Values):
+    user = ValueConstant(dsattributes.kDSStdRecordTypeUsers)
+    user.recordType = BaseRecordType.user
+
+    group = ValueConstant(dsattributes.kDSStdRecordTypeGroups)
+    group.recordType = BaseRecordType.group
+
+
+    @classmethod
+    def recordTypeForRecordType(cls, recordType):
+        if not hasattr(cls, &quot;_recordTypeByRecordType&quot;):
+            cls._recordTypeByRecordType = dict((
+                (recordType.recordType, recordType)
+                for recordType in cls.iterconstants()
+            ))
+
+        return cls._recordTypeByRecordType.get(recordType, None)
+
+
+
+class ODAttribute(Values):
+    recordType = ValueConstant(dsattributes.kDSNAttrRecordType)
+    recordType.fieldName = BaseFieldName.recordType
+
+    uid = ValueConstant(dsattributes.kDS1AttrGeneratedUID)
+    uid.fieldName = BaseFieldName.uid
+
+    guid = ValueConstant(dsattributes.kDS1AttrGeneratedUID)
+    guid.fieldName = BaseFieldName.guid
+
+    shortName = ValueConstant(dsattributes.kDSNAttrRecordName)
+    shortName.fieldName = BaseFieldName.shortNames
+
+    fullName = ValueConstant(dsattributes.kDS1AttrDistinguishedName)
+    fullName.fieldName = BaseFieldName.fullNames
+
+    emailAddress = ValueConstant(dsattributes.kDSNAttrEMailAddress)
+    emailAddress.fieldName = BaseFieldName.emailAddresses
+
+    metaNodeLocation = ValueConstant(dsattributes.kDSNAttrMetaNodeLocation)
+    metaNodeLocation.fieldName = FieldName.metaNodeLocation
+
+
+    @classmethod
+    def attributeForFieldName(cls, fieldName):
+        if not hasattr(cls, &quot;_attributesByFieldName&quot;):
+            cls._attributesByFieldName = dict((
+                (attribute.fieldName, attribute)
+                for attribute in cls.iterconstants()
+                if hasattr(attribute, &quot;fieldName&quot;)
+            ))
+
+        return cls._attributesByFieldName.get(fieldName, None)
+
+
+
+class ODMatchType(Values):
+    equals = ValueConstant(dsattributes.eDSExact)
+    equals.matchType = MatchType.equals
+
+    startsWith = ValueConstant(dsattributes.eDSStartsWith)
+    startsWith.matchType = MatchType.startsWith
+
+    endsWith = ValueConstant(dsattributes.eDSEndsWith)
+    endsWith.matchType = MatchType.endsWith
+
+    contains = ValueConstant(dsattributes.eDSContains)
+    contains.matchType = MatchType.contains
+
+
+    @classmethod
+    def matchTypeForMatchType(cls, matchType):
+        if not hasattr(cls, &quot;_matchTypeByMatchType&quot;):
+            cls._matchTypeByMatchType = dict((
+                (matchType.matchType, matchType)
+                for matchType in cls.iterconstants()
+            ))
+
+        return cls._matchTypeByMatchType.get(matchType, None)
+
+
+
+#
+# Directory Service
+#
+
+class DirectoryService(BaseDirectoryService):
+    &quot;&quot;&quot;
+    OpenDirectory directory service.
+    &quot;&quot;&quot;
+    log = Logger()
+
+    fieldName = ConstantsContainer(chain(
+        BaseDirectoryService.fieldName.iterconstants(),
+        FieldName.iterconstants()
+    ))
+
+
+    def __init__(self, nodeName=&quot;/Search&quot;):
+        &quot;&quot;&quot;
+        @param nodeName: the OpenDirectory node to query against.
+        @type nodeName: bytes
+        &quot;&quot;&quot;
+        self._nodeName = nodeName
+
+
+    @property
+    def nodeName(self):
+        return self._nodeName
+
+
+    @property
+    def realmName(self):
+        return &quot;OpenDirectory Node {self.nodeName!r}&quot;.format(self=self)
+
+
+    @property
+    def node(self):
+        &quot;&quot;&quot;
+        Get the underlying (network) directory node.
+        &quot;&quot;&quot;
+        if not hasattr(self, &quot;_node&quot;):
+            try:
+                self._node = odInit(self.nodeName)
+            except ODError, e:
+                self.log.error(
+                    &quot;OpenDirectory initialization error&quot;
+                    &quot;(node={source.nodeName}): {error}&quot;,
+                    error=e
+                )
+                raise OpenDirectoryError(e)
+
+        return self._node
+
+
+    @property
+    def localNode(self):
+        &quot;&quot;&quot;
+        Get the local node from the search path (if any), so that we can handle
+        it specially.
+        &quot;&quot;&quot;
+        if not hasattr(self, &quot;_localNode&quot;):
+            if self.nodeName == &quot;/Search&quot;:
+                result = getNodeAttributes(
+                    self.node, &quot;/Search&quot;,
+                    (dsattributes.kDS1AttrSearchPath,)
+                )
+                if (
+                    &quot;/Local/Default&quot; in
+                    result[dsattributes.kDS1AttrSearchPath]
+                ):
+                    try:
+                        self._localNode = odInit(&quot;/Local/Default&quot;)
+                    except ODError, e:
+                        self.log.error(
+                            &quot;Failed to open local node: {error}}&quot;,
+                            error=e,
+                        )
+                        raise OpenDirectoryError(e)
+                else:
+                    self._localNode = None
+
+            elif self.nodeName == &quot;/Local/Default&quot;:
+                self._localNode = self.node
+
+            else:
+                self._localNode = None
+
+        return self._localNode
+
+
+    def recordsFromMatchExpression(self, expression):
+        if not isinstance(expression, MatchExpression):
+            raise TypeError(expression)
+
+        matchType = ODMatchType.matchTypeForMatchType(expression.matchType)
+        if matchType is None:
+            raise QueryNotSupportedError(
+                &quot;Unknown match type: {0}&quot;.format(matchType)
+            )
+
+        caseInsensitive = (
+            MatchFlags.caseInsensitive in iterFlags(expression.flags)
+        )
+
+        if expression.fieldName is self.fieldName.recordType:
+            raise NotImplementedError()
+        else:
+            results = queryRecordsWithAttribute_list(
+                self.node,
+                ODAttribute.attributeForFieldName(expression.fieldName).value,
+                expression.fieldValue.encode(&quot;utf-8&quot;),
+                matchType.value,
+                caseInsensitive,
+                [
+                    recordType.value
+                    for recordType in ODRecordType.iterconstants()
+                ],
+                [attr.value for attr in ODAttribute.iterconstants()],
+            )
+
+        # def uniqueTupleFromAttribute(self, attribute):
+        #     if attribute:
+        #         if isinstance(attribute, bytes):
+        #             return (attribute,)
+        #         else:
+        #             s = set()
+        #             return tuple((
+        #                 (s.add(x), x)[1] for x in attribute if x not in s
+        #             ))
+        #     else:
+        #         return ()
+
+        for (shortName, attributes) in results:
+            fields = {}
+
+            for (key, value) in attributes.iteritems():
+                if key == &quot;dsAttrTypeStandard:AppleMetaRecordName&quot;:
+                    # We get this even though we did not ask for it...
+                    continue
+
+                try:
+                    attribute = ODAttribute.lookupByValue(key)
+                except ValueError:
+                    self.log.debug(
+                        &quot;Got back unexpected attribute {attribute} &quot;
+                        &quot;for record with short name {shortName}&quot;,
+                        attribute=key, shortName=shortName
+                    )
+                    continue
+                fieldName = attribute.fieldName
+
+                try:
+                    if BaseFieldName.isMultiValue(fieldName):
+                        if type(value) is bytes:
+                            value = (value,)
+                        elif type(value) is not list:
+                            raise TypeError()
+
+                        fields[fieldName] = tuple(
+                            x.decode(&quot;utf-8&quot;) for x in value
+                        )
+
+                    else:
+                        if type(value) is list:
+                            assert len(value) == 1
+                            value = value[0]
+                        elif type(value) is not bytes:
+                            raise TypeError()
+
+                        if fieldName is self.fieldName.recordType:
+                            fields[fieldName] = ODRecordType.lookupByValue(
+                                value
+                            ).recordType
+                        else:
+                            fields[fieldName] = value.decode(&quot;utf-8&quot;)
+
+                except TypeError:
+                    raise AssertionError(
+                        &quot;Unexpected type {0} for attribute {1}&quot;
+                        .format(type(value), fieldName)
+                    )
+
+
+            yield DirectoryRecord(self, fields)
+
+
+    def recordsFromExpression(self, expression):
+        &quot;&quot;&quot;
+        This implementation can handle L{MatchExpression} expressions; other
+        expressions are passed up to the superclass.
+        &quot;&quot;&quot;
+        if isinstance(expression, CompoundExpression):
+            raise NotImplementedError(Operand)
+
+        elif isinstance(expression, MatchExpression):
+            try:
+                return self.recordsFromMatchExpression(expression)
+            except QueryNotSupportedError:
+                return BaseDirectoryService.recordsFromExpression(
+                    self, expression
+                )
+
+        else:
+            return BaseDirectoryService.recordsFromExpression(
+                self, expression
+            )
+
+
+
+
+class DirectoryRecord(BaseDirectoryRecord):
+    &quot;&quot;&quot;
+    OpenDirectory directory record.
+    &quot;&quot;&quot;
+
+    def __init__(self, service, fields):
+         # Make sure that uid and guid are both set and equal
+        uid = fields.get(service.fieldName.uid, None)
+        guid = fields.get(service.fieldName.guid, None)
+
+        if uid is not None and guid is not None:
+            if uid != guid:
+                raise ValueError(
+                    &quot;uid and guid must be equal ({uid} != {guid})&quot;
+                    .format(uid=uid, guid=guid)
+                )
+        elif uid is None:
+            fields[service.fieldName.uid] = guid
+        elif guid is None:
+            fields[service.fieldName.guid] = uid
+
+        super(DirectoryRecord, self).__init__(service, fields)
+
+
+    requiredFields = BaseDirectoryRecord.requiredFields + (BaseFieldName.guid,)
+
+
+
+
+if __name__ == &quot;__main__&quot;:
+    service = DirectoryService()
+    print(
+        &quot;Service = {service}\n&quot;
+        &quot;Node = {service.node}\n&quot;
+        &quot;Local node = {service.localNode}\n&quot;
+        .format(service=service)
+    )
+
+    matchMorgen = MatchExpression(
+        service.fieldName.shortNames, u&quot;sagen&quot;,
+        matchType=MatchType.equals,
+    )
+    for record in service.recordsFromExpression(matchMorgen):
+        print(&quot;*&quot; * 80)
+        print(record.description())
</ins></span></pre>
</div>
</div>

</body>
</html>