[CalendarServer-changes] [15000] PyCalendar/trunk/src/pycalendar/icalendar

source_changes at macosforge.org source_changes at macosforge.org
Mon Jul 27 08:12:16 PDT 2015


Revision: 15000
          http://trac.calendarserver.org//changeset/15000
Author:   cdaboo at apple.com
Date:     2015-07-27 08:12:16 -0700 (Mon, 27 Jul 2015)
Log Message:
-----------
Allow optional limit on recurrence expansion size.

Modified Paths:
--------------
    PyCalendar/trunk/src/pycalendar/icalendar/recurrence.py
    PyCalendar/trunk/src/pycalendar/icalendar/recurrenceset.py

Added Paths:
-----------
    PyCalendar/trunk/src/pycalendar/icalendar/exceptions.py

Added: PyCalendar/trunk/src/pycalendar/icalendar/exceptions.py
===================================================================
--- PyCalendar/trunk/src/pycalendar/icalendar/exceptions.py	                        (rev 0)
+++ PyCalendar/trunk/src/pycalendar/icalendar/exceptions.py	2015-07-27 15:12:16 UTC (rev 15000)
@@ -0,0 +1,20 @@
+##
+#    Copyright (c) 2015 Cyrus Daboo. 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.
+##
+
+from pycalendar.exceptions import ErrorBase
+
+class TooManyInstancesError(ErrorBase):
+    pass

Modified: PyCalendar/trunk/src/pycalendar/icalendar/recurrence.py
===================================================================
--- PyCalendar/trunk/src/pycalendar/icalendar/recurrence.py	2015-07-26 15:18:57 UTC (rev 14999)
+++ PyCalendar/trunk/src/pycalendar/icalendar/recurrence.py	2015-07-27 15:12:16 UTC (rev 15000)
@@ -17,6 +17,7 @@
 from pycalendar import xmlutils
 from pycalendar.datetime import DateTime
 from pycalendar.icalendar import definitions, xmldefinitions
+from pycalendar.icalendar.exceptions import TooManyInstancesError
 from pycalendar.period import Period
 from pycalendar.valueutils import ValueMixin
 import cStringIO as StringIO
@@ -880,7 +881,7 @@
         return result
 
 
-    def expand(self, start, range, items, float_offset=0):
+    def expand(self, start, range, items, float_offset=0, maxInstances=None):
 
         # Have to normalize this to be very sure we are starting with a valid date, as otherwise
         # we could end up looping forever when doing recurrence.
@@ -906,9 +907,9 @@
 
             # Simple expansion is one where there is no BYXXX rule part
             if not self.hasBy():
-                self.mFullyCached = self.simpleExpand(start, range, self.mRecurrences, float_offset)
+                self.mFullyCached = self.simpleExpand(start, range, self.mRecurrences, float_offset, maxInstances=maxInstances)
             else:
-                self.mFullyCached = self.complexExpand(start, range, self.mRecurrences, float_offset)
+                self.mFullyCached = self.complexExpand(start, range, self.mRecurrences, float_offset, maxInstances=maxInstances)
 
             # Set cache values
             self.mCached = True
@@ -925,7 +926,7 @@
         return limited
 
 
-    def simpleExpand(self, start, range, items, float_offset):
+    def simpleExpand(self, start, range, items, float_offset, maxInstances=None):
         start_iter = start.duplicate()
         ctr = 0
 
@@ -942,6 +943,8 @@
 
             # Add current one to list
             items.append(start_iter.duplicate())
+            if maxInstances and len(items) > maxInstances:
+                raise TooManyInstancesError("Too many instances")
 
             # Get next item
             start_iter.recur(self.mFreq, self.mInterval, allow_invalid=True)
@@ -961,7 +964,7 @@
                     return True
 
 
-    def complexExpand(self, start, range, items, float_offset):
+    def complexExpand(self, start, range, items, float_offset, maxInstances=None):
         start_iter = start.duplicate()
         ctr = 0
 
@@ -1039,6 +1042,8 @@
 
                 # Add current one to list
                 items.append(iter)
+                if maxInstances and len(items) > maxInstances:
+                    raise TooManyInstancesError("Too many instances")
 
                 # Check limits
                 if self.mUseCount:

Modified: PyCalendar/trunk/src/pycalendar/icalendar/recurrenceset.py
===================================================================
--- PyCalendar/trunk/src/pycalendar/icalendar/recurrenceset.py	2015-07-26 15:18:57 UTC (rev 14999)
+++ PyCalendar/trunk/src/pycalendar/icalendar/recurrenceset.py	2015-07-27 15:12:16 UTC (rev 15000)
@@ -14,6 +14,7 @@
 #    limitations under the License.
 ##
 
+from pycalendar.icalendar.exceptions import TooManyInstancesError
 from pycalendar.utils import set_difference
 
 class RecurrenceSet(object):
@@ -182,7 +183,7 @@
         return self.mExperiods
 
 
-    def expand(self, start, range, items, float_offset=0):
+    def expand(self, start, range, items, float_offset=0, maxInstances=None):
         # Need to return whether the limit was applied or not
         limited = False
 
@@ -197,18 +198,22 @@
 
         # RRULES
         for iter in self.mRrules:
-            if iter.expand(start, range, include, float_offset=float_offset):
+            if iter.expand(start, range, include, float_offset=float_offset, maxInstances=maxInstances):
                 limited = True
 
         # RDATES
         for iter in self.mRdates:
             if range.isDateWithinPeriod(iter):
                 include.append(iter)
+                if maxInstances and len(include) > maxInstances:
+                    raise TooManyInstancesError("Too many instances")
             else:
                 limited = True
         for iter in self.mRperiods:
             if range.isPeriodOverlap(iter):
                 include.append(iter.getStart())
+                if maxInstances and len(include) > maxInstances:
+                    raise TooManyInstancesError("Too many instances")
             else:
                 limited = True
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20150727/bfb9a5be/attachment.html>


More information about the calendarserver-changes mailing list