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

source_changes at macosforge.org source_changes at macosforge.org
Thu Apr 12 16:49:06 PDT 2012


Revision: 9046
          http://trac.macosforge.org/projects/calendarserver/changeset/9046
Author:   wsanchez at apple.com
Date:     2012-04-12 16:49:04 -0700 (Thu, 12 Apr 2012)
Log Message:
-----------
Add ListEntry class

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

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

Added: CalendarServer/trunk/calendarserver/tools/shell/test/test_vfs.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/shell/test/test_vfs.py	                        (rev 0)
+++ CalendarServer/trunk/calendarserver/tools/shell/test/test_vfs.py	2012-04-12 23:49:04 UTC (rev 9046)
@@ -0,0 +1,102 @@
+#!/usr/bin/env python
+##
+# 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.
+##
+
+import twisted.trial.unittest 
+from twisted.internet.defer import succeed
+
+from calendarserver.tools.shell.vfs import ListEntry
+from calendarserver.tools.shell.vfs import File, Folder
+
+
+class TestListEntry(twisted.trial.unittest.TestCase):
+    def test_toString(self):
+        self.assertEquals(ListEntry(File  , "thingo"           ).toString(), "thingo" )
+        self.assertEquals(ListEntry(File  , "thingo", Foo="foo").toString(), "thingo" )
+        self.assertEquals(ListEntry(Folder, "thingo"           ).toString(), "thingo/")
+        self.assertEquals(ListEntry(Folder, "thingo", Foo="foo").toString(), "thingo/")
+
+    def test_fieldNamesImplicit(self):
+        # This test assumes File doesn't set list.fieldNames.
+        assert not hasattr(File.list, "fieldNames")
+
+        self.assertEquals(set(ListEntry(File, "thingo").fieldNames), set(("Name",)))
+
+    def test_fieldNamesExplicit(self):
+        def fieldNames(fileClass):
+            return ListEntry(fileClass, "thingo", Flavor="Coconut", Style="Hard")
+
+        # Full list
+        class MyFile(File):
+            def list(self): return succeed(())
+            list.fieldNames = ("Name", "Flavor")
+        self.assertEquals(fieldNames(MyFile).fieldNames, ("Name", "Flavor"))
+
+        # Full list, different order
+        class MyFile(File):
+            def list(self): return succeed(())
+            list.fieldNames = ("Flavor", "Name")
+        self.assertEquals(fieldNames(MyFile).fieldNames, ("Flavor", "Name"))
+
+        # Omits Name, which is implicitly added
+        class MyFile(File):
+            def list(self): return succeed(())
+            list.fieldNames = ("Flavor",)
+        self.assertEquals(fieldNames(MyFile).fieldNames, ("Name", "Flavor"))
+
+        # Emtpy
+        class MyFile(File):
+            def list(self): return succeed(())
+            list.fieldNames = ()
+        self.assertEquals(fieldNames(MyFile).fieldNames, ("Name",))
+
+    def test_toFieldsImplicit(self):
+        # This test assumes File doesn't set list.fieldNames.
+        assert not hasattr(File.list, "fieldNames")
+
+        # Name first, rest sorted by field name
+        self.assertEquals(
+            tuple(ListEntry(File, "thingo", Flavor="Coconut", Style="Hard").toFields()),
+            ("thingo", "Coconut", "Hard")
+        )
+
+    def test_toFieldsExplicit(self):
+        def fields(fileClass):
+            return tuple(ListEntry(fileClass, "thingo", Flavor="Coconut", Style="Hard").toFields())
+
+        # Full list
+        class MyFile(File):
+            def list(self): return succeed(())
+            list.fieldNames = ("Name", "Flavor")
+        self.assertEquals(fields(MyFile), ("thingo", "Coconut"))
+
+        # Full list, different order
+        class MyFile(File):
+            def list(self): return succeed(())
+            list.fieldNames = ("Flavor", "Name")
+        self.assertEquals(fields(MyFile), ("Coconut", "thingo"))
+
+        # Omits Name, which is implicitly added
+        class MyFile(File):
+            def list(self): return succeed(())
+            list.fieldNames = ("Flavor",)
+        self.assertEquals(fields(MyFile), ("thingo", "Coconut"))
+
+        # Emtpy
+        class MyFile(File):
+            def list(self): return succeed(())
+            list.fieldNames = ()
+        self.assertEquals(fields(MyFile), ("thingo",))

Modified: CalendarServer/trunk/calendarserver/tools/shell/vfs.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/shell/vfs.py	2012-04-12 22:21:46 UTC (rev 9045)
+++ CalendarServer/trunk/calendarserver/tools/shell/vfs.py	2012-04-12 23:49:04 UTC (rev 9046)
@@ -51,6 +51,43 @@
 from calendarserver.tools.shell.directory import recordInfo
 
 
+class ListEntry(object):
+    """
+    Information about a C{File} as returned by C{File.list()}.
+    """
+    def __init__(self, Class, Name, **fields):
+        self.fileClass = Class
+        self.fileName  = Name
+        self.fields    = fields
+
+        fields["Name"] = Name
+
+    def __str__(self):
+        return self.toString()
+
+    def toString(self):
+        if issubclass(self.fileClass, Folder):
+            return "%s/" % (self.fileName,)
+        else:
+            return self.fileName
+
+    @property
+    def fieldNames(self):
+        if not hasattr(self, "_fieldNames"):
+            if hasattr(self.fileClass.list, "fieldNames"):
+                if "Name" in self.fileClass.list.fieldNames:
+                    self._fieldNames = tuple(self.fileClass.list.fieldNames)
+                else:
+                    self._fieldNames = ("Name",) + tuple(self.fileClass.list.fieldNames)
+            else:
+                self._fieldNames = ["Name"] + sorted(n for n in self.fields if n != "Name")
+
+        return self._fieldNames
+
+    def toFields(self):
+        return (self.fields[fieldName] for fieldName in self.fieldNames)
+
+
 class File(object):
     """
     Object in virtual data hierarchy.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20120412/27ddbe90/attachment-0001.html>


More information about the calendarserver-changes mailing list