[CalendarServer-changes] [10920] PyCalendar/trunk/src/pycalendar
source_changes at macosforge.org
source_changes at macosforge.org
Thu Mar 14 07:03:20 PDT 2013
Revision: 10920
http://trac.calendarserver.org//changeset/10920
Author: cdaboo at apple.com
Date: 2013-03-14 07:03:20 -0700 (Thu, 14 Mar 2013)
Log Message:
-----------
Add validator tool.
Modified Paths:
--------------
PyCalendar/trunk/src/pycalendar/parser.py
Added Paths:
-----------
PyCalendar/trunk/src/pycalendar/validator.py
Modified: PyCalendar/trunk/src/pycalendar/parser.py
===================================================================
--- PyCalendar/trunk/src/pycalendar/parser.py 2013-03-14 02:53:41 UTC (rev 10919)
+++ PyCalendar/trunk/src/pycalendar/parser.py 2013-03-14 14:03:20 UTC (rev 10920)
@@ -43,6 +43,9 @@
# Use this to fix v2 BASE64 to v3 ENCODING=b - only PARSER_FIX or PARSER_ALLOW
VCARD_2_BASE64 = PARSER_FIX
+ # Allow DATE values when DATETIME specified (and vice versa)
+ INVALID_DATETIME_VALUE = PARSER_FIX
+
# Allow slightly invalid DURATION values
INVALID_DURATION_VALUE = PARSER_FIX
@@ -54,3 +57,18 @@
# Remove \-escaping in URI values when parsing - only PARSER_FIX or PARSER_ALLOW
BACKSLASH_IN_URI_VALUE = PARSER_FIX
+
+ @staticmethod
+ def allRaise():
+ """
+ Make all tests raise an error - never fix
+ """
+ ParserContext.INVALID_COLON_ESCAPE_SEQUENCE = ParserContext.PARSER_RAISE
+ ParserContext.INVALID_ESCAPE_SEQUENCES = ParserContext.PARSER_RAISE
+ ParserContext.BLANK_LINES_IN_DATA = ParserContext.PARSER_RAISE
+ ParserContext.VCARD_2_NO_PARAMETER_VALUES = ParserContext.PARSER_RAISE
+ ParserContext.VCARD_2_BASE64 = ParserContext.PARSER_RAISE
+ ParserContext.INVALID_DURATION_VALUE = ParserContext.PARSER_RAISE
+ ParserContext.INVALID_ADR_N_VALUES = ParserContext.PARSER_RAISE
+ ParserContext.INVALID_REQUEST_STATUS_VALUE = ParserContext.PARSER_RAISE
+ ParserContext.BACKSLASH_IN_URI_VALUE = ParserContext.PARSER_RAISE
Added: PyCalendar/trunk/src/pycalendar/validator.py
===================================================================
--- PyCalendar/trunk/src/pycalendar/validator.py (rev 0)
+++ PyCalendar/trunk/src/pycalendar/validator.py 2013-03-14 14:03:20 UTC (rev 10920)
@@ -0,0 +1,67 @@
+#!/usr/bin/env python
+##
+# Copyright (c) 2012 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.calendar import PyCalendar
+from pycalendar.exceptions import PyCalendarError
+from pycalendar.parser import ParserContext
+from pycalendar.vcard.card import Card
+import os
+import sys
+
+def validate(fname):
+ """
+ Check whether the contents of the specified file is valid iCalendar or vCard data.
+ """
+
+ data = open(fname).read()
+
+ ParserContext.allRaise()
+
+ if data.find("BEGIN:VCALENDAR") != -1:
+ try:
+ cal = PyCalendar.parseText(data)
+ except PyCalendarError, e:
+ print "Failed to parse iCalendar: %r" % (e,)
+ sys.exit(1)
+ elif data.find("BEGIN:VCARD") != -1:
+ try:
+ cal = Card.parseText(data)
+ except PyCalendarError, e:
+ print "Failed to parse vCard: %r" % (e,)
+ sys.exit(1)
+ else:
+ print "Failed to find valid iCalendar or vCard data"
+ sys.exit(1)
+
+ _ignore_fixed, unfixed = cal.validate(doFix=False, doRaise=False)
+ if unfixed:
+ print "List of problems: %s" % (unfixed,)
+ else:
+ print "No problems"
+
+ # Control character check - only HTAB, CR, LF allowed for characters in the range 0x00-0x1F
+ s = str(data)
+ if len(s.translate(None, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F")) != len(s):
+ for ctr, i in enumerate(data):
+ if i in "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F":
+ print "Control character %d at position %d" % (ord(i), ctr,)
+
+
+if __name__ == '__main__':
+
+ fname = os.path.expanduser(sys.argv[1])
+ validate(fname)
Property changes on: PyCalendar/trunk/src/pycalendar/validator.py
___________________________________________________________________
Added: svn:executable
+ *
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130314/d4aec850/attachment-0001.html>
More information about the calendarserver-changes
mailing list