[CalendarServer-changes] [9035] CalendarServer/trunk/calendarserver/tools/shell

source_changes at macosforge.org source_changes at macosforge.org
Wed Apr 11 16:36:08 PDT 2012


Revision: 9035
          http://trac.macosforge.org/projects/calendarserver/changeset/9035
Author:   wsanchez at apple.com
Date:     2012-04-11 16:36:08 -0700 (Wed, 11 Apr 2012)
Log Message:
-----------
Refactor record info generation into its own module.

Modified Paths:
--------------
    CalendarServer/trunk/calendarserver/tools/shell/vfs.py

Added Paths:
-----------
    CalendarServer/trunk/calendarserver/tools/shell/directory.py

Added: CalendarServer/trunk/calendarserver/tools/shell/directory.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/shell/directory.py	                        (rev 0)
+++ CalendarServer/trunk/calendarserver/tools/shell/directory.py	2012-04-11 23:36:08 UTC (rev 9035)
@@ -0,0 +1,156 @@
+##
+# Copyright (c) 2012 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# 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 "AS IS" 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.
+##
+
+"""
+Directory tools
+"""
+
+__all__ = [
+    "recordInfo",
+    "recordBasicInfo",
+    "recordGroupMembershipInfo",
+    "recordProxyAccessInfo",
+]
+
+
+from twisted.internet.defer import succeed
+from twisted.internet.defer import inlineCallbacks, returnValue
+
+from calendarserver.tools.tables import Table
+
+
+ at inlineCallbacks
+def recordInfo(directory, record):
+    """
+    Complete record information.
+    """
+    info = []
+
+    def add(name, subInfo):
+        if subInfo:
+            info.append("%s:" % (name,))
+            info.append(subInfo)
+
+    add("Directory record" , (yield recordBasicInfo(directory, record)))
+    add("Group memberships", (yield recordGroupMembershipInfo(directory, record)))
+    add("Proxy access"     , (yield recordProxyAccessInfo(directory, record)))
+
+    returnValue("\n".join(info))
+        
+
+def recordBasicInfo(directory, record):
+    """
+    Basic information for a record.
+    """
+    table = Table()
+
+    def add(name, value):
+        if value:
+            table.addRow((name, value))
+
+    add("Service"    , record.service   )
+    add("Record Type", record.recordType)
+
+    for shortName in record.shortNames:
+        add("Short Name", shortName)
+
+    add("GUID"      , record.guid     )
+    add("Full Name" , record.fullName )
+    add("First Name", record.firstName)
+    add("Last Name" , record.lastName )
+
+    for email in record.emailAddresses:
+        add("Email Address", email)
+
+    for cua in record.calendarUserAddresses:
+        add("Calendar User Address", cua)
+
+    add("Server ID"           , record.serverID              )
+    add("Partition ID"        , record.partitionID           )
+    add("Enabled"             , record.enabled               )
+    add("Enabled for Calendar", record.enabledForCalendaring )
+    add("Enabled for Contacts", record.enabledForAddressBooks)
+
+    return succeed(table.toString())
+
+
+def recordGroupMembershipInfo(directory, record):
+    """
+    Group membership info for a record.
+    """
+    rows = []
+
+    for group in record.groups():
+        rows.append((group.uid, group.shortNames[0], group.fullName))
+
+    if not rows:
+        return succeed(None)
+
+    rows = sorted(rows,
+        key = lambda row: (row[1], row[2])
+    )
+
+    table = Table()
+    table.addHeader(("UID", "Short Name", "Full Name"))
+    for row in rows:
+        table.addRow(row)
+
+    return succeed(table.toString())
+
+
+ at inlineCallbacks
+def recordProxyAccessInfo(directory, record):
+    """
+    Group membership info for a record.
+    """
+    # FIXME: This proxy finding logic should be in DirectoryRecord.
+
+    def meAndMyGroups(record=record, groups=set((record,))):
+        for group in record.groups():
+            groups.add(group)
+            meAndMyGroups(group, groups)
+        return groups
+
+    # FIXME: This module global is really gross.
+    from twistedcaldav.directory.calendaruserproxy import ProxyDBService
+
+    rows = []
+    proxyInfoSeen = set()
+    for record in meAndMyGroups():
+        proxyUIDs = (yield ProxyDBService.getMemberships(record.uid))
+
+        for proxyUID in proxyUIDs:
+            # These are of the form: F153A05B-FF27-4B6C-BD6D-D1239D0082B0#calendar-proxy-read
+            # I don't know how to get DirectoryRecord objects for the proxyUID here, so, let's cheat for now.
+            proxyUID, proxyType = proxyUID.split("#")
+            if (proxyUID, proxyType) not in proxyInfoSeen:
+                proxyRecord = directory.recordWithUID(proxyUID)
+                rows.append((proxyUID, proxyRecord.recordType, proxyRecord.shortNames[0], proxyRecord.fullName, proxyType))
+                proxyInfoSeen.add((proxyUID, proxyType))
+
+    if not rows:
+        returnValue(None)
+
+    rows = sorted(rows,
+        key = lambda row: (row[1], row[2], row[4])
+    )
+
+    table = Table()
+    table.addHeader(("UID", "Record Type", "Short Name", "Full Name", "Access"))
+    for row in rows:
+        table.addRow(row)
+
+    returnValue(table.toString())

Modified: CalendarServer/trunk/calendarserver/tools/shell/vfs.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/shell/vfs.py	2012-04-11 22:41:20 UTC (rev 9034)
+++ CalendarServer/trunk/calendarserver/tools/shell/vfs.py	2012-04-11 23:36:08 UTC (rev 9035)
@@ -48,7 +48,9 @@
 from twistedcaldav.ical import InvalidICalendarDataError
 
 from calendarserver.tools.tables import Table
+from calendarserver.tools.shell.directory import recordInfo
 
+
 class File(object):
     """
     Object in virtual data hierarchy.
@@ -357,107 +359,10 @@
     def list(self):
         return Folder.list(self)
 
-    @inlineCallbacks
     def describe(self):
-        description = []
-        description.append("Principal home for UID: %s\n" % (self.uid,))
+        return recordInfo(self.service.directory, self.record)
 
-        if self.record is not None:
-            #
-            # Basic record info
-            #
 
-            rows = []
-
-            def add(name, value):
-                if value:
-                    rows.append((name, value))
-
-            add("Service"    , self.record.service   )
-            add("Record Type", self.record.recordType)
-
-            for shortName in self.record.shortNames:
-                add("Short Name", shortName)
-
-            add("GUID"      , self.record.guid     )
-            add("Full Name" , self.record.fullName )
-            add("First Name", self.record.firstName)
-            add("Last Name" , self.record.lastName )
-
-            for email in self.record.emailAddresses:
-                add("Email Address", email)
-
-            for cua in self.record.calendarUserAddresses:
-                add("Calendar User Address", cua)
-
-            add("Server ID"           , self.record.serverID              )
-            add("Partition ID"        , self.record.partitionID           )
-            add("Enabled"             , self.record.enabled               )
-            add("Enabled for Calendar", self.record.enabledForCalendaring )
-            add("Enabled for Contacts", self.record.enabledForAddressBooks)
-
-            if rows:
-                description.append("Directory Record:")
-                description.append(tableString(rows))
-
-            #
-            # Group memberships
-            #
-            rows = []
-
-            for group in self.record.groups():
-                rows.append((group.uid, group.shortNames[0], group.fullName))
-
-            if rows:
-                def sortKey(row):
-                    return (row[1], row[2])
-                description.append("Group Memberships:")
-                description.append(tableString(
-                    sorted(rows, key=sortKey),
-                    header=("UID", "Short Name", "Full Name")
-                ))
-
-            #
-            # Proxy for...
-            #
-
-            # FIXME: This logic should be in the DirectoryRecord.
-
-            def meAndMyGroups(record=self.record, groups=set((self.record,))):
-                for group in record.groups():
-                    groups.add(group)
-                    meAndMyGroups(group, groups)
-                return groups
-                
-            # FIXME: This module global is really gross.
-            from twistedcaldav.directory.calendaruserproxy import ProxyDBService
-
-            rows = []
-            proxyInfoSeen = set()
-            for record in meAndMyGroups():
-                proxyUIDs = (yield ProxyDBService.getMemberships(record.uid))
-
-                for proxyUID in proxyUIDs:
-                    # These are of the form: F153A05B-FF27-4B6C-BD6D-D1239D0082B0#calendar-proxy-read
-                    # I don't know how to get DirectoryRecord objects for the proxyUID here, so, let's cheat for now.
-                    proxyUID, proxyType = proxyUID.split("#")
-                    if (proxyUID, proxyType) not in proxyInfoSeen:
-                        proxyRecord = self.service.directory.recordWithUID(proxyUID)
-                        rows.append((proxyUID, proxyRecord.recordType, proxyRecord.shortNames[0], proxyRecord.fullName, proxyType))
-                        proxyInfoSeen.add((proxyUID, proxyType))
-
-            if rows:
-                def sortKey(row):
-                    return (row[1], row[2], row[4])
-                description.append("Proxy Access:")
-                description.append(tableString(
-                    sorted(rows, key=sortKey),
-                    header=("UID", "Record Type", "Short Name", "Full Name", "Access")
-                ))
-
-        returnValue("\n".join(description))
-
-
 class CalendarHomeFolder(Folder):
     """
     Calendar home folder.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20120411/779238d2/attachment-0001.html>


More information about the calendarserver-changes mailing list