Package caldavclientlibrary :: Package admin :: Package xmlaccounts :: Module record
[hide private]
[frames] | no frames]

Source Code for Module caldavclientlibrary.admin.xmlaccounts.record

  1  ## 
  2  # Copyright (c) 2007-2016 Apple Inc. All rights reserved. 
  3  # 
  4  # Licensed under the Apache License, Version 2.0 (the "License"); 
  5  # you may not use this file except in compliance with the License. 
  6  # You may obtain a copy of the License at 
  7  # 
  8  # http://www.apache.org/licenses/LICENSE-2.0 
  9  # 
 10  # Unless required by applicable law or agreed to in writing, software 
 11  # distributed under the License is distributed on an "AS IS" BASIS, 
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 13  # See the License for the specific language governing permissions and 
 14  # limitations under the License. 
 15  ## 
 16   
 17  from caldavclientlibrary.admin.xmlaccounts import recordtypes 
 18  from caldavclientlibrary.admin.xmlaccounts import tags 
 19   
 20  from caldavclientlibrary.protocol.utils.xmlhelpers import SubElementWithData 
 21   
 22  from xml.etree.ElementTree import Element 
 23   
24 -class XMLRecord(object):
25 """ 26 Represents a single principal record. This class can parse and generate the appropriate XML. 27 """ 28
29 - def __init__(self):
30 self.recordType = None 31 self.repeat = 0 32 self.uid = None 33 self.guid = None 34 self.password = None 35 self.name = None 36 self.members = set() 37 self.calendarUserAddresses = set() 38 self.autoSchedule = False 39 self.enabledForCalendaring = True 40 self.proxies = set() 41 self.proxyFor = set()
42
43 - def parseXML(self, node):
44 """ 45 Parse a single principal record from XML. 46 47 @param node: the XML element for the principal being parsed. 48 @type node: C{xml.etree.ElementTree.Element} 49 """ 50 self.recordType = recordtypes.TAGS_TO_RECORD_TYPES[node.tag] 51 self.repeat = int(node.get(tags.ATTRIBUTE_REPEAT, "0")) 52 for child in node.getchildren(): 53 if child.tag == tags.ELEMENT_UID: 54 self.uid = child.text 55 elif child.tag == tags.ELEMENT_GUID: 56 self.guid = child.text 57 elif child.tag == tags.ELEMENT_PASSWORD: 58 self.password = child.text 59 elif child.tag == tags.ELEMENT_NAME: 60 self.name = child.text 61 elif child.tag == tags.ELEMENT_MEMBERS: 62 self._parseMembers(child, self.members) 63 elif child.tag == tags.ELEMENT_CUADDR: 64 self.calendarUserAddresses.add(child.text) 65 elif child.tag == tags.ELEMENT_AUTOSCHEDULE: 66 # Only Resources & Locations 67 if self.recordType not in (recordtypes.recordType_resources, recordtypes.recordType_locations,): 68 raise ValueError("<auto-schedule> element only allowed for Resources and Locations: %s" % (child.tag,)) 69 self.autoSchedule = True 70 elif child.tag == tags.ELEMENT_DISABLECALENDAR: 71 # Only Groups 72 if self.recordType not in (recordtypes.recordType_users, recordtypes.recordType_groups,): 73 raise ValueError("<disable-calendar> element only allowed for Groups: %s" % (child.tag,)) 74 self.enabledForCalendaring = False 75 elif child.tag == tags.ELEMENT_PROXIES: 76 # Only Resources & Locations 77 if self.recordType not in (recordtypes.recordType_resources, recordtypes.recordType_locations,): 78 raise ValueError("<proxies> element only allowed for Resources and Locations: %s" % (child.tag,)) 79 self._parseMembers(child, self.proxies) 80 else: 81 raise RuntimeError("Unknown account attribute: %s" % (child.tag,))
82
83 - def _parseMembers(self, node, addto):
84 """ 85 Parse an XML <members> or <proxies> element list. 86 87 @param node: the <members> or <proxies> element to parse. 88 @type node: C{xml.etree.ElementTree.Element} 89 @param addto: the list to add the parsed information into. The items in the list are tuples 90 of the record type and record uid. 91 @type addto: C{list} 92 """ 93 for child in node.getchildren(): 94 if child.tag == tags.ELEMENT_MEMBER: 95 recordType = child.get(tags.ATTRIBUTE_RECORDTYPE, recordtypes.recordType_users) 96 addto.add((recordType, child.text))
97
98 - def writeXML(self):
99 """ 100 Generate a single XML principal record element. 101 102 @return: the root element for the principal record. 103 @rtype: C{xml.etree.ElementTree.Element} 104 """ 105 106 root = Element(recordtypes.RECORD_TYPES_TO_TAGS[self.recordType]) 107 if self.repeat: 108 root.set(tags.ATTRIBUTE_REPEAT, str(self.repeat)) 109 110 SubElementWithData(root, tags.ELEMENT_UID, self.uid) 111 SubElementWithData(root, tags.ELEMENT_GUID, self.guid) 112 SubElementWithData(root, tags.ELEMENT_PASSWORD, self.password) 113 SubElementWithData(root, tags.ELEMENT_NAME, self.name) 114 if self.recordType == recordtypes.recordType_groups: 115 members = SubElementWithData(root, tags.ELEMENT_MEMBERS) 116 for member in self.members: 117 SubElementWithData(members, tags.ELEMENT_MEMBER, member[1], {tags.ATTRIBUTE_RECORDTYPE:member[0]}) 118 if self.calendarUserAddresses: 119 for cuaddr in self.calendarUserAddresses: 120 SubElementWithData(root, tags.ELEMENT_CUADDR, cuaddr) 121 if self.recordType in (recordtypes.recordType_resources, recordtypes.recordType_locations,) and self.autoSchedule: 122 SubElementWithData(root, tags.ELEMENT_AUTOSCHEDULE) 123 if self.recordType in (recordtypes.recordType_users, recordtypes.recordType_groups,) and not self.enabledForCalendaring: 124 SubElementWithData(root, tags.ELEMENT_DISABLECALENDAR) 125 if self.recordType in (recordtypes.recordType_resources, recordtypes.recordType_locations,): 126 proxies = SubElementWithData(root, tags.ELEMENT_PROXIES) 127 for proxy in self.proxies: 128 SubElementWithData(proxies, tags.ELEMENT_MEMBER, proxy[1], {tags.ATTRIBUTE_RECORDTYPE:proxy[0]}) 129 130 return root
131