<!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>[13475] twext/trunk/twext/who/opendirectory/_service.py</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/13475">13475</a></dd>
<dt>Author</dt> <dd>sagen@apple.com</dd>
<dt>Date</dt> <dd>2014-05-14 19:40:33 -0700 (Wed, 14 May 2014)</dd>
</dl>
<h3>Log Message</h3>
<pre>Adds support for local node CompoundExpressions (which local node treats as case sensitive, so we break them down to their MatchExpressions to work around this)</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#twexttrunktwextwhoopendirectory_servicepy">twext/trunk/twext/who/opendirectory/_service.py</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="twexttrunktwextwhoopendirectory_servicepy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/opendirectory/_service.py (13474 => 13475)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/opendirectory/_service.py        2014-05-14 21:02:46 UTC (rev 13474)
+++ twext/trunk/twext/who/opendirectory/_service.py        2014-05-15 02:40:33 UTC (rev 13475)
</span><span class="lines">@@ -129,88 +129,97 @@
</span><span class="cx"> return u"OpenDirectory Node {self.nodeName!r}".format(self=self)
</span><span class="cx">
</span><span class="cx">
</span><ins>+
</ins><span class="cx"> @property
</span><del>- def session(self):
</del><ins>+ def node(self):
</ins><span class="cx"> """
</span><del>- Get the underlying directory session.
</del><ins>+ Get the underlying (network) directory node.
</ins><span class="cx"> """
</span><del>- self._connect()
- return self._session
</del><ins>+ if not hasattr(self, "_node"):
+ self._node = self._connect(self._nodeName)
+ return self._node
</ins><span class="cx">
</span><span class="cx">
</span><span class="cx"> @property
</span><del>- def node(self):
</del><ins>+ def localNode(self):
</ins><span class="cx"> """
</span><del>- Get the underlying (network) directory node.
</del><ins>+ Get the local node from the search path (if any), so that we can
+ handle it specially.
</ins><span class="cx"> """
</span><del>- self._connect()
- return self._node
</del><ins>+ if not hasattr(self, "_localNode"):
</ins><span class="cx">
</span><ins>+ if self.nodeName == ODSearchPath.search.value:
+ details, error = self.node.nodeDetailsForKeys_error_(
+ (ODAttribute.searchPath.value,), None
+ )
+ if error:
+ self.log.error(
+ "Error while examining Search path",
+ error=error
+ )
+ raise OpenDirectoryConnectionError(
+ "Unable to connect to OpenDirectory node",
+ error
+ )
</ins><span class="cx">
</span><del>- # @property
- # def localNode(self):
- # """
- # Get the local node from the search path (if any), so that we can
- # handle it specially.
- # """
- # if not hasattr(self, "_localNode"):
- # if self.nodeName == ODSearchPath.search.value:
- # result = getNodeAttributes(
- # self.node, ODSearchPath.search.value,
- # (ODAttribute.searchPath.value,)
- # )
- # if (
- # ODSearchPath.local.value in
- # result[ODAttribute.searchPath.value]
- # ):
- # try:
- # self._localNode = odInit(ODSearchPath.local.value)
- # except ODError, e:
- # self.log.error(
- # "Failed to open local node: {error}}",
- # error=e,
- # )
- # raise OpenDirectoryError("", e)
- # else:
- # self._localNode = None
</del><ins>+ if (
+ ODSearchPath.local.value in
+ details[ODAttribute.searchPath.value]
+ ):
+ self._localNode = self._connect(ODSearchPath.local.value)
+ else:
+ self._localNode = None
</ins><span class="cx">
</span><del>- # elif self.nodeName == ODSearchPath.local.value:
- # self._localNode = self.node
</del><ins>+ elif self.nodeName == ODSearchPath.local.value:
+ self._localNode = self.node
</ins><span class="cx">
</span><del>- # else:
- # self._localNode = None
</del><ins>+ else:
+ self._localNode = None
</ins><span class="cx">
</span><del>- # return self._localNode
</del><ins>+ return self._localNode
</ins><span class="cx">
</span><span class="cx">
</span><del>- def _connect(self):
</del><ins>+ @property
+ def session(self):
</ins><span class="cx"> """
</span><ins>+ Get the underlying directory session.
+ """
+ if not hasattr(self, "_session"):
+ session = ODSession.defaultSession()
+ self._session = session
+ return self._session
+
+
+ def _connect(self, nodeName):
+ """
</ins><span class="cx"> Connect to the directory server.
</span><span class="cx">
</span><ins>+ @param nodeName: The OD node name to connect to
+ @type nodeName: C{str}
+
+ @return: the OD node
+
</ins><span class="cx"> @raises: L{OpenDirectoryConnectionError} if unable to connect.
</span><span class="cx"> """
</span><del>- if not hasattr(self, "_session"):
- session = ODSession.defaultSession()
</del><span class="cx">
</span><del>- node, error = ODNode.nodeWithSession_name_error_(
- session, self.nodeName, None
</del><ins>+ node, error = ODNode.nodeWithSession_name_error_(
+ self.session, nodeName, None
+ )
+
+ if error:
+ self.log.error(
+ "Error while trying to connect to OpenDirectory node "
+ "{source.nodeName!r}: {error}",
+ error=error
</ins><span class="cx"> )
</span><ins>+ raise OpenDirectoryConnectionError(
+ "Unable to connect to OpenDirectory node",
+ error
+ )
</ins><span class="cx">
</span><del>- if error:
- self.log.error(
- "Error while trying to connect to OpenDirectory node "
- "{source.nodeName!r}: {error}",
- error=error
- )
- raise OpenDirectoryConnectionError(
- "Unable to connect to OpenDirectory node",
- error
- )
</del><ins>+ return node
</ins><span class="cx">
</span><del>- self._session = session
- self._node = node
</del><span class="cx">
</span><del>-
</del><span class="cx"> def _queryStringAndRecordTypeFromMatchExpression(self, expression):
</span><span class="cx"> """
</span><span class="cx"> Generates an OD query string from a match expression.
</span><span class="lines">@@ -287,7 +296,7 @@
</span><span class="cx"> )
</span><span class="cx"> if subExpRecordTypes:
</span><span class="cx"> if isinstance(subExpRecordTypes, unicode):
</span><del>- if bool(expression.operand is Operand.AND) != bool(queryToken): # AND or NOR
</del><ins>+ if bool(expression.operand is Operand.AND) != bool(queryToken): # AND or NOR
</ins><span class="cx"> if expression.operand is Operand.AND:
</span><span class="cx"> recordTypes = recordTypes & set([subExpRecordTypes])
</span><span class="cx"> else:
</span><span class="lines">@@ -348,13 +357,16 @@
</span><span class="cx"> )
</span><span class="cx">
</span><span class="cx">
</span><del>- def _queryFromCompoundExpression(self, expression):
</del><ins>+ def _queryFromCompoundExpression(self, expression, local=False):
</ins><span class="cx"> """
</span><span class="cx"> Form an OpenDirectory query from a compound expression.
</span><span class="cx">
</span><span class="cx"> @param expression: A compound expression.
</span><span class="cx"> @type expression: L{CompoundExpression}
</span><span class="cx">
</span><ins>+ @param local: Whether to restrict the query to the local node
+ @type local: C{Boolean}
+
</ins><span class="cx"> @return: A native OpenDirectory query or None if query will return no records
</span><span class="cx"> @rtype: L{ODQuery}
</span><span class="cx"> """
</span><span class="lines">@@ -369,7 +381,7 @@
</span><span class="cx"> maxResults = 0
</span><span class="cx">
</span><span class="cx"> query, error = ODQuery.queryWithNode_forRecordTypes_attribute_matchType_queryValues_returnAttributes_maximumResults_error_(
</span><del>- self.node,
</del><ins>+ self.node if not local else self.localNode,
</ins><span class="cx"> recordTypes,
</span><span class="cx"> None,
</span><span class="cx"> ODMatchType.compound.value if queryString else ODMatchType.any.value,
</span><span class="lines">@@ -391,7 +403,7 @@
</span><span class="cx"> return query
</span><span class="cx">
</span><span class="cx">
</span><del>- def _queryFromMatchExpression(self, expression, recordType=None):
</del><ins>+ def _queryFromMatchExpression(self, expression, recordType=None, local=False):
</ins><span class="cx"> """
</span><span class="cx"> Form an OpenDirectory query from a match expression.
</span><span class="cx">
</span><span class="lines">@@ -401,6 +413,9 @@
</span><span class="cx"> @param recordType: A record type to insert into the query.
</span><span class="cx"> @type recordType: L{NamedConstant}
</span><span class="cx">
</span><ins>+ @param local: Whether to restrict the query to the local node
+ @type local: C{Boolean}
+
</ins><span class="cx"> @return: A native OpenDirectory query.
</span><span class="cx"> @rtype: L{ODQuery}
</span><span class="cx"> """
</span><span class="lines">@@ -468,7 +483,7 @@
</span><span class="cx"> queryValue = unicode(expression.fieldValue)
</span><span class="cx">
</span><span class="cx"> query, error = ODQuery.queryWithNode_forRecordTypes_attribute_matchType_queryValues_returnAttributes_maximumResults_error_(
</span><del>- self.node,
</del><ins>+ self.node if not local else self.localNode,
</ins><span class="cx"> recordTypes,
</span><span class="cx"> queryAttribute,
</span><span class="cx"> matchType | caseInsensitive,
</span><span class="lines">@@ -550,17 +565,99 @@
</span><span class="cx"> )
</span><span class="cx">
</span><span class="cx">
</span><ins>+ @inlineCallbacks
</ins><span class="cx"> def recordsFromCompoundExpression(self, expression, records=None):
</span><ins>+ """
+ Returns records matching the CompoundExpression. Because the
+ local node doesn't perform Compound queries in a case insensitive
+ fashion (but will do case insensitive for a simple MatchExpression)
+ also call localRecordsFromCompoundExpression() which breaks the
+ CompoundExpression up into MatchExpressions for sending to the local
+ node.
+ """
+
</ins><span class="cx"> try:
</span><span class="cx"> query = self._queryFromCompoundExpression(expression)
</span><del>- return self._recordsFromQuery(query)
</del><span class="cx">
</span><span class="cx"> except QueryNotSupportedError:
</span><del>- return BaseDirectoryService.recordsFromCompoundExpression(
- self, expression
</del><ins>+ returnValue(
+ BaseDirectoryService.recordsFromCompoundExpression(
+ self, expression
+ )
</ins><span class="cx"> )
</span><span class="cx">
</span><ins>+ results = yield self._recordsFromQuery(query)
</ins><span class="cx">
</span><ins>+ if self.localNode is not None:
+
+ localRecords = yield self.localRecordsFromCompoundExpression(
+ expression
+ )
+ for localRecord in localRecords:
+ if localRecord not in results:
+ results.append(localRecord)
+
+ returnValue(results)
+
+
+
+ @inlineCallbacks
+ def localRecordsFromCompoundExpression(self, expression):
+ """
+ Takes a CompoundExpression, and recursively goes through each
+ MatchExpression, passing those specifically to the local node, and
+ ADDing or ORing the results as needed.
+ """
+
+ # We keep a set of resulting uids for each sub expression so it's
+ # easy to either union (OR) or intersection (AND) the sets
+ sets = []
+
+ # Mapping of uid to record
+ byUID = {}
+
+ for subExpression in expression.expressions:
+
+ if isinstance(subExpression, CompoundExpression):
+ subRecords = yield self.localRecordsFromCompoundExpression(
+ subExpression
+ )
+
+ elif isinstance(subExpression, MatchExpression):
+ subQuery = yield self._queryFromMatchExpression(
+ subExpression, local=True
+ )
+ subRecords = yield self._recordsFromQuery(subQuery)
+
+ else:
+ raise QueryNotSupportedError(
+ "Unsupported expression type: {}".format(
+ type(subExpression)
+ )
+ )
+
+ newSet = set()
+ for record in subRecords:
+ byUID[record.uid] = record
+ newSet.add(record.uid)
+ sets.append(newSet)
+
+ results = []
+ if byUID: # If there are any records
+ if expression.operand == Operand.AND:
+ uids = set.intersection(*sets)
+ elif expression.operand == Operand.OR:
+ uids = set.union(*sets)
+ else:
+ raise QueryNotSupportedError(
+ "Unsupported operand: {}".format(expression.operand)
+ )
+ for uid in uids:
+ results.append(byUID[uid])
+
+ returnValue(results)
+
+
</ins><span class="cx"> def _getUserRecord(self, username):
</span><span class="cx"> """
</span><span class="cx"> Fetch the OD record for a given user.
</span></span></pre>
</div>
</div>
</body>
</html>