[CalendarServer-changes] [3449] CalendarServer/trunk
source_changes at macosforge.org
source_changes at macosforge.org
Wed Dec 3 16:31:51 PST 2008
Revision: 3449
http://trac.macosforge.org/projects/calendarserver/changeset/3449
Author: wsanchez at apple.com
Date: 2008-12-03 16:31:50 -0800 (Wed, 03 Dec 2008)
Log Message:
-----------
Add/use twext.python.plistlib.
Modified Paths:
--------------
CalendarServer/trunk/bin/proxyclean
CalendarServer/trunk/calendarserver/tap/test/test_caldav.py
CalendarServer/trunk/contrib/SBS/bin/calendarServer_restore
CalendarServer/trunk/twistedcaldav/admin/formatters.py
CalendarServer/trunk/twistedcaldav/admin/logs.py
CalendarServer/trunk/twistedcaldav/backup.py
CalendarServer/trunk/twistedcaldav/config.py
CalendarServer/trunk/twistedcaldav/directory/appleopendirectory.py
CalendarServer/trunk/twistedcaldav/directory/sudo.py
CalendarServer/trunk/twistedcaldav/test/test_config.py
Added Paths:
-----------
CalendarServer/trunk/twext/python/
CalendarServer/trunk/twext/python/__init__.py
CalendarServer/trunk/twext/python/_plistlib.py
CalendarServer/trunk/twext/python/plistlib.py
Removed Paths:
-------------
CalendarServer/trunk/twistedcaldav/py/plistlib.py
Modified: CalendarServer/trunk/bin/proxyclean
===================================================================
--- CalendarServer/trunk/bin/proxyclean 2008-12-03 22:10:52 UTC (rev 3448)
+++ CalendarServer/trunk/bin/proxyclean 2008-12-04 00:31:50 UTC (rev 3449)
@@ -21,10 +21,7 @@
import os
import sys
-try:
- from plistlib import readPlist
-except ImportError:
- from twistedcaldav.py.plistlib import readPlist
+from twext.python.plistlib import readPlist
try:
import opendirectory
Modified: CalendarServer/trunk/calendarserver/tap/test/test_caldav.py
===================================================================
--- CalendarServer/trunk/calendarserver/tap/test/test_caldav.py 2008-12-03 22:10:52 UTC (rev 3448)
+++ CalendarServer/trunk/calendarserver/tap/test/test_caldav.py 2008-12-04 00:31:50 UTC (rev 3449)
@@ -17,11 +17,6 @@
import os
from os.path import dirname, abspath
-try:
- from plistlib import writePlist
-except ImportError:
- from twistedcaldav.py.plistlib import writePlist
-
from twisted.python.usage import Options, UsageError
from twisted.python.reflect import namedAny
from twisted.application.service import IService
@@ -30,6 +25,8 @@
from twisted.web2.dav import auth
from twisted.web2.log import LogWrapperResource
+from twext.python.plistlib import writePlist
+
from twistedcaldav.config import config, ConfigDict, defaultConfig, _mergeData
from twistedcaldav.directory.aggregate import AggregateDirectoryService
Modified: CalendarServer/trunk/contrib/SBS/bin/calendarServer_restore
===================================================================
--- CalendarServer/trunk/contrib/SBS/bin/calendarServer_restore 2008-12-03 22:10:52 UTC (rev 3448)
+++ CalendarServer/trunk/contrib/SBS/bin/calendarServer_restore 2008-12-04 00:31:50 UTC (rev 3449)
@@ -19,10 +19,7 @@
import sys
import fnmatch
-try:
- from plistlib import readPlist
-except ImportError:
- from twistedcaldav.py.plistlib import readPlist
+from twext.python.plistlib import readPlist
ServiceConf = "85-calendarServer.plist"
Copied: CalendarServer/trunk/twext/python/_plistlib.py (from rev 3441, CalendarServer/trunk/twistedcaldav/py/plistlib.py)
===================================================================
--- CalendarServer/trunk/twext/python/_plistlib.py (rev 0)
+++ CalendarServer/trunk/twext/python/_plistlib.py 2008-12-04 00:31:50 UTC (rev 3449)
@@ -0,0 +1,471 @@
+#
+# Added to standard library in Python 2.6 (Mac only in prior versions)
+#
+"""plistlib.py -- a tool to generate and parse MacOSX .plist files.
+
+The PropertList (.plist) file format is a simple XML pickle supporting
+basic object types, like dictionaries, lists, numbers and strings.
+Usually the top level object is a dictionary.
+
+To write out a plist file, use the writePlist(rootObject, pathOrFile)
+function. 'rootObject' is the top level object, 'pathOrFile' is a
+filename or a (writable) file object.
+
+To parse a plist from a file, use the readPlist(pathOrFile) function,
+with a file name or a (readable) file object as the only argument. It
+returns the top level object (again, usually a dictionary).
+
+To work with plist data in strings, you can use readPlistFromString()
+and writePlistToString().
+
+Values can be strings, integers, floats, booleans, tuples, lists,
+dictionaries, Data or datetime.datetime objects. String values (including
+dictionary keys) may be unicode strings -- they will be written out as
+UTF-8.
+
+The <data> plist type is supported through the Data class. This is a
+thin wrapper around a Python string.
+
+Generate Plist example:
+
+ pl = dict(
+ aString="Doodah",
+ aList=["A", "B", 12, 32.1, [1, 2, 3]],
+ aFloat = 0.1,
+ anInt = 728,
+ aDict=dict(
+ anotherString="<hello & hi there!>",
+ aUnicodeValue=u'M\xe4ssig, Ma\xdf',
+ aTrueValue=True,
+ aFalseValue=False,
+ ),
+ someData = Data("<binary gunk>"),
+ someMoreData = Data("<lots of binary gunk>" * 10),
+ aDate = datetime.datetime.fromtimestamp(time.mktime(time.gmtime())),
+ )
+ # unicode keys are possible, but a little awkward to use:
+ pl[u'\xc5benraa'] = "That was a unicode key."
+ writePlist(pl, fileName)
+
+Parse Plist example:
+
+ pl = readPlist(pathOrFile)
+ print pl["aKey"]
+"""
+
+
+__all__ = [
+ "readPlist", "writePlist", "readPlistFromString", "writePlistToString",
+ "readPlistFromResource", "writePlistToResource",
+ "Plist", "Data", "Dict"
+]
+# Note: the Plist and Dict classes have been deprecated.
+
+import binascii
+import datetime
+from cStringIO import StringIO
+import re
+
+
+def readPlist(pathOrFile):
+ """Read a .plist file. 'pathOrFile' may either be a file name or a
+ (readable) file object. Return the unpacked root object (which
+ usually is a dictionary).
+ """
+ didOpen = 0
+ if isinstance(pathOrFile, (str, unicode)):
+ pathOrFile = open(pathOrFile)
+ didOpen = 1
+ p = PlistParser()
+ rootObject = p.parse(pathOrFile)
+ if didOpen:
+ pathOrFile.close()
+ return rootObject
+
+
+def writePlist(rootObject, pathOrFile):
+ """Write 'rootObject' to a .plist file. 'pathOrFile' may either be a
+ file name or a (writable) file object.
+ """
+ didOpen = 0
+ if isinstance(pathOrFile, (str, unicode)):
+ pathOrFile = open(pathOrFile, "w")
+ didOpen = 1
+ writer = PlistWriter(pathOrFile)
+ writer.writeln("<plist version=\"1.0\">")
+ writer.writeValue(rootObject)
+ writer.writeln("</plist>")
+ if didOpen:
+ pathOrFile.close()
+
+
+def readPlistFromString(data):
+ """Read a plist data from a string. Return the root object.
+ """
+ return readPlist(StringIO(data))
+
+
+def writePlistToString(rootObject):
+ """Return 'rootObject' as a plist-formatted string.
+ """
+ f = StringIO()
+ writePlist(rootObject, f)
+ return f.getvalue()
+
+
+def readPlistFromResource(path, restype='plst', resid=0):
+ """Read plst resource from the resource fork of path.
+ """
+ from Carbon.File import FSRef, FSGetResourceForkName
+ from Carbon.Files import fsRdPerm
+ from Carbon import Res
+ fsRef = FSRef(path)
+ resNum = Res.FSOpenResourceFile(fsRef, FSGetResourceForkName(), fsRdPerm)
+ Res.UseResFile(resNum)
+ plistData = Res.Get1Resource(restype, resid).data
+ Res.CloseResFile(resNum)
+ return readPlistFromString(plistData)
+
+
+def writePlistToResource(rootObject, path, restype='plst', resid=0):
+ """Write 'rootObject' as a plst resource to the resource fork of path.
+ """
+ from Carbon.File import FSRef, FSGetResourceForkName
+ from Carbon.Files import fsRdWrPerm
+ from Carbon import Res
+ plistData = writePlistToString(rootObject)
+ fsRef = FSRef(path)
+ resNum = Res.FSOpenResourceFile(fsRef, FSGetResourceForkName(), fsRdWrPerm)
+ Res.UseResFile(resNum)
+ try:
+ Res.Get1Resource(restype, resid).RemoveResource()
+ except Res.Error:
+ pass
+ res = Res.Resource(plistData)
+ res.AddResource(restype, resid, '')
+ res.WriteResource()
+ Res.CloseResFile(resNum)
+
+
+class DumbXMLWriter:
+
+ def __init__(self, file, indentLevel=0, indent="\t"):
+ self.file = file
+ self.stack = []
+ self.indentLevel = indentLevel
+ self.indent = indent
+
+ def beginElement(self, element):
+ self.stack.append(element)
+ self.writeln("<%s>" % element)
+ self.indentLevel += 1
+
+ def endElement(self, element):
+ assert self.indentLevel > 0
+ assert self.stack.pop() == element
+ self.indentLevel -= 1
+ self.writeln("</%s>" % element)
+
+ def simpleElement(self, element, value=None):
+ if value is not None:
+ value = _escapeAndEncode(value)
+ self.writeln("<%s>%s</%s>" % (element, value, element))
+ else:
+ self.writeln("<%s/>" % element)
+
+ def writeln(self, line):
+ if line:
+ self.file.write(self.indentLevel * self.indent + line + "\n")
+ else:
+ self.file.write("\n")
+
+
+# Contents should conform to a subset of ISO 8601
+# (in particular, YYYY '-' MM '-' DD 'T' HH ':' MM ':' SS 'Z'. Smaller units may be omitted with
+# a loss of precision)
+_dateParser = re.compile(r"(?P<year>\d\d\d\d)(?:-(?P<month>\d\d)(?:-(?P<day>\d\d)(?:T(?P<hour>\d\d)(?::(?P<minute>\d\d)(?::(?P<second>\d\d))?)?)?)?)?Z")
+
+def _dateFromString(s):
+ order = ('year', 'month', 'day', 'hour', 'minute', 'second')
+ gd = _dateParser.match(s).groupdict()
+ lst = []
+ for key in order:
+ val = gd[key]
+ if val is None:
+ break
+ lst.append(int(val))
+ return datetime.datetime(*lst)
+
+def _dateToString(d):
+ return '%04d-%02d-%02dT%02d:%02d:%02dZ' % (
+ d.year, d.month, d.day,
+ d.hour, d.minute, d.second
+ )
+
+
+# Regex to find any control chars, except for \t \n and \r
+_controlCharPat = re.compile(
+ r"[\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0b\x0c\x0e\x0f"
+ r"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f]")
+
+def _escapeAndEncode(text):
+ m = _controlCharPat.search(text)
+ if m is not None:
+ raise ValueError("strings can't contains control characters; "
+ "use plistlib.Data instead")
+ text = text.replace("\r\n", "\n") # convert DOS line endings
+ text = text.replace("\r", "\n") # convert Mac line endings
+ text = text.replace("&", "&") # escape '&'
+ text = text.replace("<", "<") # escape '<'
+ text = text.replace(">", ">") # escape '>'
+ return text.encode("utf-8") # encode as UTF-8
+
+
+PLISTHEADER = """\
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+"""
+
+class PlistWriter(DumbXMLWriter):
+
+ def __init__(self, file, indentLevel=0, indent="\t", writeHeader=1):
+ if writeHeader:
+ file.write(PLISTHEADER)
+ DumbXMLWriter.__init__(self, file, indentLevel, indent)
+
+ def writeValue(self, value):
+ if isinstance(value, (str, unicode)):
+ self.simpleElement("string", value)
+ elif isinstance(value, bool):
+ # must switch for bool before int, as bool is a
+ # subclass of int...
+ if value:
+ self.simpleElement("true")
+ else:
+ self.simpleElement("false")
+ elif isinstance(value, int):
+ self.simpleElement("integer", str(value))
+ elif isinstance(value, float):
+ self.simpleElement("real", repr(value))
+ elif isinstance(value, dict):
+ self.writeDict(value)
+ elif isinstance(value, Data):
+ self.writeData(value)
+ elif isinstance(value, datetime.datetime):
+ self.simpleElement("date", _dateToString(value))
+ elif isinstance(value, (tuple, list)):
+ self.writeArray(value)
+ else:
+ raise TypeError("unsuported type: %s" % type(value))
+
+ def writeData(self, data):
+ self.beginElement("data")
+ self.indentLevel -= 1
+ maxlinelength = 76 - len(self.indent.replace("\t", " " * 8) *
+ self.indentLevel)
+ for line in data.asBase64(maxlinelength).split("\n"):
+ if line:
+ self.writeln(line)
+ self.indentLevel += 1
+ self.endElement("data")
+
+ def writeDict(self, d):
+ self.beginElement("dict")
+ for key, value in sorted(d.items()):
+ if not isinstance(key, (str, unicode)):
+ raise TypeError("keys must be strings")
+ self.simpleElement("key", key)
+ self.writeValue(value)
+ self.endElement("dict")
+
+ def writeArray(self, array):
+ self.beginElement("array")
+ for value in array:
+ self.writeValue(value)
+ self.endElement("array")
+
+
+class _InternalDict(dict):
+
+ # This class is needed while Dict is scheduled for deprecation:
+ # we only need to warn when a *user* instantiates Dict or when
+ # the "attribute notation for dict keys" is used.
+
+ def __getattr__(self, attr):
+ try:
+ value = self[attr]
+ except KeyError:
+ raise AttributeError, attr
+ from warnings import warn
+ warn("Attribute access from plist dicts is deprecated, use d[key] "
+ "notation instead", PendingDeprecationWarning)
+ return value
+
+ def __setattr__(self, attr, value):
+ from warnings import warn
+ warn("Attribute access from plist dicts is deprecated, use d[key] "
+ "notation instead", PendingDeprecationWarning)
+ self[attr] = value
+
+ def __delattr__(self, attr):
+ try:
+ del self[attr]
+ except KeyError:
+ raise AttributeError, attr
+ from warnings import warn
+ warn("Attribute access from plist dicts is deprecated, use d[key] "
+ "notation instead", PendingDeprecationWarning)
+
+class Dict(_InternalDict):
+
+ def __init__(self, **kwargs):
+ from warnings import warn
+ warn("The plistlib.Dict class is deprecated, use builtin dict instead",
+ PendingDeprecationWarning)
+ super(Dict, self).__init__(**kwargs)
+
+
+class Plist(_InternalDict):
+
+ """This class has been deprecated. Use readPlist() and writePlist()
+ functions instead, together with regular dict objects.
+ """
+
+ def __init__(self, **kwargs):
+ from warnings import warn
+ warn("The Plist class is deprecated, use the readPlist() and "
+ "writePlist() functions instead", PendingDeprecationWarning)
+ super(Plist, self).__init__(**kwargs)
+
+ def fromFile(cls, pathOrFile):
+ """Deprecated. Use the readPlist() function instead."""
+ rootObject = readPlist(pathOrFile)
+ plist = cls()
+ plist.update(rootObject)
+ return plist
+ fromFile = classmethod(fromFile)
+
+ def write(self, pathOrFile):
+ """Deprecated. Use the writePlist() function instead."""
+ writePlist(self, pathOrFile)
+
+
+def _encodeBase64(s, maxlinelength=76):
+ # copied from base64.encodestring(), with added maxlinelength argument
+ maxbinsize = (maxlinelength//4)*3
+ pieces = []
+ for i in xrange(0, len(s), maxbinsize):
+ chunk = s[i : i + maxbinsize]
+ pieces.append(binascii.b2a_base64(chunk))
+ return "".join(pieces)
+
+class Data:
+
+ """Wrapper for binary data."""
+
+ def __init__(self, data):
+ self.data = data
+
+ def fromBase64(cls, data):
+ # base64.decodestring just calls binascii.a2b_base64;
+ # it seems overkill to use both base64 and binascii.
+ return cls(binascii.a2b_base64(data))
+ fromBase64 = classmethod(fromBase64)
+
+ def asBase64(self, maxlinelength=76):
+ return _encodeBase64(self.data, maxlinelength)
+
+ def __cmp__(self, other):
+ if isinstance(other, self.__class__):
+ return cmp(self.data, other.data)
+ elif isinstance(other, str):
+ return cmp(self.data, other)
+ else:
+ return cmp(id(self), id(other))
+
+ def __repr__(self):
+ return "%s(%s)" % (self.__class__.__name__, repr(self.data))
+
+
+class PlistParser:
+
+ def __init__(self):
+ self.stack = []
+ self.currentKey = None
+ self.root = None
+
+ def parse(self, fileobj):
+ from xml.parsers.expat import ParserCreate
+ parser = ParserCreate()
+ parser.StartElementHandler = self.handleBeginElement
+ parser.EndElementHandler = self.handleEndElement
+ parser.CharacterDataHandler = self.handleData
+ parser.ParseFile(fileobj)
+ return self.root
+
+ def handleBeginElement(self, element, attrs):
+ self.data = []
+ handler = getattr(self, "begin_" + element, None)
+ if handler is not None:
+ handler(attrs)
+
+ def handleEndElement(self, element):
+ handler = getattr(self, "end_" + element, None)
+ if handler is not None:
+ handler()
+
+ def handleData(self, data):
+ self.data.append(data)
+
+ def addObject(self, value):
+ if self.currentKey is not None:
+ self.stack[-1][self.currentKey] = value
+ self.currentKey = None
+ elif not self.stack:
+ # this is the root object
+ self.root = value
+ else:
+ self.stack[-1].append(value)
+
+ def getData(self):
+ data = "".join(self.data)
+ try:
+ data = data.encode("ascii")
+ except UnicodeError:
+ pass
+ self.data = []
+ return data
+
+ # element handlers
+
+ def begin_dict(self, attrs):
+ d = _InternalDict()
+ self.addObject(d)
+ self.stack.append(d)
+ def end_dict(self):
+ self.stack.pop()
+
+ def end_key(self):
+ self.currentKey = self.getData()
+
+ def begin_array(self, attrs):
+ a = []
+ self.addObject(a)
+ self.stack.append(a)
+ def end_array(self):
+ self.stack.pop()
+
+ def end_true(self):
+ self.addObject(True)
+ def end_false(self):
+ self.addObject(False)
+ def end_integer(self):
+ self.addObject(int(self.getData()))
+ def end_real(self):
+ self.addObject(float(self.getData()))
+ def end_string(self):
+ self.addObject(self.getData())
+ def end_data(self):
+ self.addObject(Data.fromBase64(self.getData()))
+ def end_date(self):
+ self.addObject(_dateFromString(self.getData()))
Added: CalendarServer/trunk/twext/python/plistlib.py
===================================================================
--- CalendarServer/trunk/twext/python/plistlib.py (rev 0)
+++ CalendarServer/trunk/twext/python/plistlib.py 2008-12-04 00:31:50 UTC (rev 3449)
@@ -0,0 +1,30 @@
+##
+# Copyright (c) 2008 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.
+##
+
+try:
+ _plistlib = __import__("plistlib")
+
+except ImportError:
+ from twext.python._plistlib import *
+
+ import twext.python._plistlib
+ __all__ = twext.python._plistlib.__all__
+ del twext
+
+else:
+ for symbol in _plistlib.__all__:
+ globals()[symbol] = getattr(_plistlib, symbol)
+ del _plistlib
Modified: CalendarServer/trunk/twistedcaldav/admin/formatters.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/admin/formatters.py 2008-12-03 22:10:52 UTC (rev 3448)
+++ CalendarServer/trunk/twistedcaldav/admin/formatters.py 2008-12-04 00:31:50 UTC (rev 3449)
@@ -16,10 +16,7 @@
import sys
-try:
- import plistlib
-except ImportError:
- import twistedcaldav.py.plistlib
+from twext.python.plistlib import Dict and PlistDict, writePlist
FORMATTERS = {}
@@ -336,19 +333,19 @@
name = "plist"
def report_principals(self, report):
- plist = plistlib.Dict()
+ plist = PlistDict()
plist[report['type']] = list(report['records'])
- plistlib.writePlist(plist, self.dest)
+ writePlist(plist, self.dest)
report_users = report_groups = report_resources = report_locations = report_principals
def report_stats(self, report):
- plist = plistlib.Dict()
+ plist = PlistDict()
plist[report['type']] = report['data']
- plistlib.writePlist(plist, self.dest)
+ writePlist(plist, self.dest)
report_logs = report_stats
Modified: CalendarServer/trunk/twistedcaldav/admin/logs.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/admin/logs.py 2008-12-03 22:10:52 UTC (rev 3448)
+++ CalendarServer/trunk/twistedcaldav/admin/logs.py 2008-12-04 00:31:50 UTC (rev 3449)
@@ -26,10 +26,7 @@
import sys
import time
-try:
- from plistlib import readPlist, writePlist
-except ImportError:
- from twistedcaldav.py.plistlib import readPlist, writePlist
+from twext.python.plistlib import readPlist, writePlist
from twistedcaldav.admin import util
Modified: CalendarServer/trunk/twistedcaldav/backup.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/backup.py 2008-12-03 22:10:52 UTC (rev 3448)
+++ CalendarServer/trunk/twistedcaldav/backup.py 2008-12-04 00:31:50 UTC (rev 3449)
@@ -23,10 +23,7 @@
import fnmatch
import commands
-try:
- from plistlib import readPlist
-except ImportError:
- from twistedcaldav.py.plistlib import readPlist
+from twext.python.plistlib import readPlist
VERBOSE = os.environ.get('VERBOSE', False)
FUNCLOG = os.environ.get('FUNCLOG', False)
Modified: CalendarServer/trunk/twistedcaldav/config.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/config.py 2008-12-03 22:10:52 UTC (rev 3448)
+++ CalendarServer/trunk/twistedcaldav/config.py 2008-12-04 00:31:50 UTC (rev 3449)
@@ -27,14 +27,11 @@
import copy
import re
-try:
- from plistlib import readPlist
-except ImportError:
- from twistedcaldav.py.plistlib import readPlist
-
from twisted.web2.dav import davxml
from twisted.web2.dav.resource import TwistedACLInheritable
+from twext.python.plistlib import readPlist
+
from twistedcaldav.log import Logger
from twistedcaldav.log import clearLogLevels, setLogLevelForNamespace, InvalidLogLevelError
Modified: CalendarServer/trunk/twistedcaldav/directory/appleopendirectory.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/directory/appleopendirectory.py 2008-12-03 22:10:52 UTC (rev 3448)
+++ CalendarServer/trunk/twistedcaldav/directory/appleopendirectory.py 2008-12-04 00:31:50 UTC (rev 3449)
@@ -27,10 +27,7 @@
from random import random
from uuid import UUID
-try:
- from plistlib import readPlistFromString
-except ImportError:
- from twistedcaldav.py.plistlib import readPlistFromString
+from twext.python.plistlib import readPlistFromString
import opendirectory
import dsattributes
Modified: CalendarServer/trunk/twistedcaldav/directory/sudo.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/directory/sudo.py 2008-12-03 22:10:52 UTC (rev 3448)
+++ CalendarServer/trunk/twistedcaldav/directory/sudo.py 2008-12-04 00:31:50 UTC (rev 3449)
@@ -23,15 +23,12 @@
"SudoDirectoryService",
]
-try:
- from plistlib import readPlist
-except ImportError:
- from twistedcaldav.py.plistlib import readPlist
-
from twisted.python.filepath import FilePath
from twisted.cred.credentials import IUsernamePassword, IUsernameHashedPassword
from twisted.cred.error import UnauthorizedLogin
+from twext.python.plistlib import readPlist
+
from twistedcaldav.directory.directory import DirectoryService, DirectoryRecord
from twistedcaldav.directory.directory import UnknownRecordTypeError
Deleted: CalendarServer/trunk/twistedcaldav/py/plistlib.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/py/plistlib.py 2008-12-03 22:10:52 UTC (rev 3448)
+++ CalendarServer/trunk/twistedcaldav/py/plistlib.py 2008-12-04 00:31:50 UTC (rev 3449)
@@ -1,471 +0,0 @@
-#
-# Added to standard library in Python 2.6 (Mac only in prior versions)
-#
-"""plistlib.py -- a tool to generate and parse MacOSX .plist files.
-
-The PropertList (.plist) file format is a simple XML pickle supporting
-basic object types, like dictionaries, lists, numbers and strings.
-Usually the top level object is a dictionary.
-
-To write out a plist file, use the writePlist(rootObject, pathOrFile)
-function. 'rootObject' is the top level object, 'pathOrFile' is a
-filename or a (writable) file object.
-
-To parse a plist from a file, use the readPlist(pathOrFile) function,
-with a file name or a (readable) file object as the only argument. It
-returns the top level object (again, usually a dictionary).
-
-To work with plist data in strings, you can use readPlistFromString()
-and writePlistToString().
-
-Values can be strings, integers, floats, booleans, tuples, lists,
-dictionaries, Data or datetime.datetime objects. String values (including
-dictionary keys) may be unicode strings -- they will be written out as
-UTF-8.
-
-The <data> plist type is supported through the Data class. This is a
-thin wrapper around a Python string.
-
-Generate Plist example:
-
- pl = dict(
- aString="Doodah",
- aList=["A", "B", 12, 32.1, [1, 2, 3]],
- aFloat = 0.1,
- anInt = 728,
- aDict=dict(
- anotherString="<hello & hi there!>",
- aUnicodeValue=u'M\xe4ssig, Ma\xdf',
- aTrueValue=True,
- aFalseValue=False,
- ),
- someData = Data("<binary gunk>"),
- someMoreData = Data("<lots of binary gunk>" * 10),
- aDate = datetime.datetime.fromtimestamp(time.mktime(time.gmtime())),
- )
- # unicode keys are possible, but a little awkward to use:
- pl[u'\xc5benraa'] = "That was a unicode key."
- writePlist(pl, fileName)
-
-Parse Plist example:
-
- pl = readPlist(pathOrFile)
- print pl["aKey"]
-"""
-
-
-__all__ = [
- "readPlist", "writePlist", "readPlistFromString", "writePlistToString",
- "readPlistFromResource", "writePlistToResource",
- "Plist", "Data", "Dict"
-]
-# Note: the Plist and Dict classes have been deprecated.
-
-import binascii
-import datetime
-from cStringIO import StringIO
-import re
-
-
-def readPlist(pathOrFile):
- """Read a .plist file. 'pathOrFile' may either be a file name or a
- (readable) file object. Return the unpacked root object (which
- usually is a dictionary).
- """
- didOpen = 0
- if isinstance(pathOrFile, (str, unicode)):
- pathOrFile = open(pathOrFile)
- didOpen = 1
- p = PlistParser()
- rootObject = p.parse(pathOrFile)
- if didOpen:
- pathOrFile.close()
- return rootObject
-
-
-def writePlist(rootObject, pathOrFile):
- """Write 'rootObject' to a .plist file. 'pathOrFile' may either be a
- file name or a (writable) file object.
- """
- didOpen = 0
- if isinstance(pathOrFile, (str, unicode)):
- pathOrFile = open(pathOrFile, "w")
- didOpen = 1
- writer = PlistWriter(pathOrFile)
- writer.writeln("<plist version=\"1.0\">")
- writer.writeValue(rootObject)
- writer.writeln("</plist>")
- if didOpen:
- pathOrFile.close()
-
-
-def readPlistFromString(data):
- """Read a plist data from a string. Return the root object.
- """
- return readPlist(StringIO(data))
-
-
-def writePlistToString(rootObject):
- """Return 'rootObject' as a plist-formatted string.
- """
- f = StringIO()
- writePlist(rootObject, f)
- return f.getvalue()
-
-
-def readPlistFromResource(path, restype='plst', resid=0):
- """Read plst resource from the resource fork of path.
- """
- from Carbon.File import FSRef, FSGetResourceForkName
- from Carbon.Files import fsRdPerm
- from Carbon import Res
- fsRef = FSRef(path)
- resNum = Res.FSOpenResourceFile(fsRef, FSGetResourceForkName(), fsRdPerm)
- Res.UseResFile(resNum)
- plistData = Res.Get1Resource(restype, resid).data
- Res.CloseResFile(resNum)
- return readPlistFromString(plistData)
-
-
-def writePlistToResource(rootObject, path, restype='plst', resid=0):
- """Write 'rootObject' as a plst resource to the resource fork of path.
- """
- from Carbon.File import FSRef, FSGetResourceForkName
- from Carbon.Files import fsRdWrPerm
- from Carbon import Res
- plistData = writePlistToString(rootObject)
- fsRef = FSRef(path)
- resNum = Res.FSOpenResourceFile(fsRef, FSGetResourceForkName(), fsRdWrPerm)
- Res.UseResFile(resNum)
- try:
- Res.Get1Resource(restype, resid).RemoveResource()
- except Res.Error:
- pass
- res = Res.Resource(plistData)
- res.AddResource(restype, resid, '')
- res.WriteResource()
- Res.CloseResFile(resNum)
-
-
-class DumbXMLWriter:
-
- def __init__(self, file, indentLevel=0, indent="\t"):
- self.file = file
- self.stack = []
- self.indentLevel = indentLevel
- self.indent = indent
-
- def beginElement(self, element):
- self.stack.append(element)
- self.writeln("<%s>" % element)
- self.indentLevel += 1
-
- def endElement(self, element):
- assert self.indentLevel > 0
- assert self.stack.pop() == element
- self.indentLevel -= 1
- self.writeln("</%s>" % element)
-
- def simpleElement(self, element, value=None):
- if value is not None:
- value = _escapeAndEncode(value)
- self.writeln("<%s>%s</%s>" % (element, value, element))
- else:
- self.writeln("<%s/>" % element)
-
- def writeln(self, line):
- if line:
- self.file.write(self.indentLevel * self.indent + line + "\n")
- else:
- self.file.write("\n")
-
-
-# Contents should conform to a subset of ISO 8601
-# (in particular, YYYY '-' MM '-' DD 'T' HH ':' MM ':' SS 'Z'. Smaller units may be omitted with
-# a loss of precision)
-_dateParser = re.compile(r"(?P<year>\d\d\d\d)(?:-(?P<month>\d\d)(?:-(?P<day>\d\d)(?:T(?P<hour>\d\d)(?::(?P<minute>\d\d)(?::(?P<second>\d\d))?)?)?)?)?Z")
-
-def _dateFromString(s):
- order = ('year', 'month', 'day', 'hour', 'minute', 'second')
- gd = _dateParser.match(s).groupdict()
- lst = []
- for key in order:
- val = gd[key]
- if val is None:
- break
- lst.append(int(val))
- return datetime.datetime(*lst)
-
-def _dateToString(d):
- return '%04d-%02d-%02dT%02d:%02d:%02dZ' % (
- d.year, d.month, d.day,
- d.hour, d.minute, d.second
- )
-
-
-# Regex to find any control chars, except for \t \n and \r
-_controlCharPat = re.compile(
- r"[\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0b\x0c\x0e\x0f"
- r"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f]")
-
-def _escapeAndEncode(text):
- m = _controlCharPat.search(text)
- if m is not None:
- raise ValueError("strings can't contains control characters; "
- "use plistlib.Data instead")
- text = text.replace("\r\n", "\n") # convert DOS line endings
- text = text.replace("\r", "\n") # convert Mac line endings
- text = text.replace("&", "&") # escape '&'
- text = text.replace("<", "<") # escape '<'
- text = text.replace(">", ">") # escape '>'
- return text.encode("utf-8") # encode as UTF-8
-
-
-PLISTHEADER = """\
-<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
-"""
-
-class PlistWriter(DumbXMLWriter):
-
- def __init__(self, file, indentLevel=0, indent="\t", writeHeader=1):
- if writeHeader:
- file.write(PLISTHEADER)
- DumbXMLWriter.__init__(self, file, indentLevel, indent)
-
- def writeValue(self, value):
- if isinstance(value, (str, unicode)):
- self.simpleElement("string", value)
- elif isinstance(value, bool):
- # must switch for bool before int, as bool is a
- # subclass of int...
- if value:
- self.simpleElement("true")
- else:
- self.simpleElement("false")
- elif isinstance(value, int):
- self.simpleElement("integer", str(value))
- elif isinstance(value, float):
- self.simpleElement("real", repr(value))
- elif isinstance(value, dict):
- self.writeDict(value)
- elif isinstance(value, Data):
- self.writeData(value)
- elif isinstance(value, datetime.datetime):
- self.simpleElement("date", _dateToString(value))
- elif isinstance(value, (tuple, list)):
- self.writeArray(value)
- else:
- raise TypeError("unsuported type: %s" % type(value))
-
- def writeData(self, data):
- self.beginElement("data")
- self.indentLevel -= 1
- maxlinelength = 76 - len(self.indent.replace("\t", " " * 8) *
- self.indentLevel)
- for line in data.asBase64(maxlinelength).split("\n"):
- if line:
- self.writeln(line)
- self.indentLevel += 1
- self.endElement("data")
-
- def writeDict(self, d):
- self.beginElement("dict")
- for key, value in sorted(d.items()):
- if not isinstance(key, (str, unicode)):
- raise TypeError("keys must be strings")
- self.simpleElement("key", key)
- self.writeValue(value)
- self.endElement("dict")
-
- def writeArray(self, array):
- self.beginElement("array")
- for value in array:
- self.writeValue(value)
- self.endElement("array")
-
-
-class _InternalDict(dict):
-
- # This class is needed while Dict is scheduled for deprecation:
- # we only need to warn when a *user* instantiates Dict or when
- # the "attribute notation for dict keys" is used.
-
- def __getattr__(self, attr):
- try:
- value = self[attr]
- except KeyError:
- raise AttributeError, attr
- from warnings import warn
- warn("Attribute access from plist dicts is deprecated, use d[key] "
- "notation instead", PendingDeprecationWarning)
- return value
-
- def __setattr__(self, attr, value):
- from warnings import warn
- warn("Attribute access from plist dicts is deprecated, use d[key] "
- "notation instead", PendingDeprecationWarning)
- self[attr] = value
-
- def __delattr__(self, attr):
- try:
- del self[attr]
- except KeyError:
- raise AttributeError, attr
- from warnings import warn
- warn("Attribute access from plist dicts is deprecated, use d[key] "
- "notation instead", PendingDeprecationWarning)
-
-class Dict(_InternalDict):
-
- def __init__(self, **kwargs):
- from warnings import warn
- warn("The plistlib.Dict class is deprecated, use builtin dict instead",
- PendingDeprecationWarning)
- super(Dict, self).__init__(**kwargs)
-
-
-class Plist(_InternalDict):
-
- """This class has been deprecated. Use readPlist() and writePlist()
- functions instead, together with regular dict objects.
- """
-
- def __init__(self, **kwargs):
- from warnings import warn
- warn("The Plist class is deprecated, use the readPlist() and "
- "writePlist() functions instead", PendingDeprecationWarning)
- super(Plist, self).__init__(**kwargs)
-
- def fromFile(cls, pathOrFile):
- """Deprecated. Use the readPlist() function instead."""
- rootObject = readPlist(pathOrFile)
- plist = cls()
- plist.update(rootObject)
- return plist
- fromFile = classmethod(fromFile)
-
- def write(self, pathOrFile):
- """Deprecated. Use the writePlist() function instead."""
- writePlist(self, pathOrFile)
-
-
-def _encodeBase64(s, maxlinelength=76):
- # copied from base64.encodestring(), with added maxlinelength argument
- maxbinsize = (maxlinelength//4)*3
- pieces = []
- for i in xrange(0, len(s), maxbinsize):
- chunk = s[i : i + maxbinsize]
- pieces.append(binascii.b2a_base64(chunk))
- return "".join(pieces)
-
-class Data:
-
- """Wrapper for binary data."""
-
- def __init__(self, data):
- self.data = data
-
- def fromBase64(cls, data):
- # base64.decodestring just calls binascii.a2b_base64;
- # it seems overkill to use both base64 and binascii.
- return cls(binascii.a2b_base64(data))
- fromBase64 = classmethod(fromBase64)
-
- def asBase64(self, maxlinelength=76):
- return _encodeBase64(self.data, maxlinelength)
-
- def __cmp__(self, other):
- if isinstance(other, self.__class__):
- return cmp(self.data, other.data)
- elif isinstance(other, str):
- return cmp(self.data, other)
- else:
- return cmp(id(self), id(other))
-
- def __repr__(self):
- return "%s(%s)" % (self.__class__.__name__, repr(self.data))
-
-
-class PlistParser:
-
- def __init__(self):
- self.stack = []
- self.currentKey = None
- self.root = None
-
- def parse(self, fileobj):
- from xml.parsers.expat import ParserCreate
- parser = ParserCreate()
- parser.StartElementHandler = self.handleBeginElement
- parser.EndElementHandler = self.handleEndElement
- parser.CharacterDataHandler = self.handleData
- parser.ParseFile(fileobj)
- return self.root
-
- def handleBeginElement(self, element, attrs):
- self.data = []
- handler = getattr(self, "begin_" + element, None)
- if handler is not None:
- handler(attrs)
-
- def handleEndElement(self, element):
- handler = getattr(self, "end_" + element, None)
- if handler is not None:
- handler()
-
- def handleData(self, data):
- self.data.append(data)
-
- def addObject(self, value):
- if self.currentKey is not None:
- self.stack[-1][self.currentKey] = value
- self.currentKey = None
- elif not self.stack:
- # this is the root object
- self.root = value
- else:
- self.stack[-1].append(value)
-
- def getData(self):
- data = "".join(self.data)
- try:
- data = data.encode("ascii")
- except UnicodeError:
- pass
- self.data = []
- return data
-
- # element handlers
-
- def begin_dict(self, attrs):
- d = _InternalDict()
- self.addObject(d)
- self.stack.append(d)
- def end_dict(self):
- self.stack.pop()
-
- def end_key(self):
- self.currentKey = self.getData()
-
- def begin_array(self, attrs):
- a = []
- self.addObject(a)
- self.stack.append(a)
- def end_array(self):
- self.stack.pop()
-
- def end_true(self):
- self.addObject(True)
- def end_false(self):
- self.addObject(False)
- def end_integer(self):
- self.addObject(int(self.getData()))
- def end_real(self):
- self.addObject(float(self.getData()))
- def end_string(self):
- self.addObject(self.getData())
- def end_data(self):
- self.addObject(Data.fromBase64(self.getData()))
- def end_date(self):
- self.addObject(_dateFromString(self.getData()))
Modified: CalendarServer/trunk/twistedcaldav/test/test_config.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/test/test_config.py 2008-12-03 22:10:52 UTC (rev 3448)
+++ CalendarServer/trunk/twistedcaldav/test/test_config.py 2008-12-04 00:31:50 UTC (rev 3449)
@@ -14,10 +14,7 @@
# limitations under the License.
##
-try:
- from plistlib import writePlist
-except ImportError:
- from twistedcaldav.py.plistlib import writePlist
+from twext.python.plistlib import writePlist
from twistedcaldav.log import logLevelForNamespace
from twistedcaldav.config import config, defaultConfig, ConfigurationError
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20081203/1e6768d8/attachment-0001.html>
More information about the calendarserver-changes
mailing list