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

Source Code for Module caldavclientlibrary.admin.xmlaccounts.directory

  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  from caldavclientlibrary.admin.xmlaccounts.record import XMLRecord 
 20   
 21  from xml.etree.ElementTree import Element 
 22   
23 -class XMLDirectory(object):
24 """ 25 Model object for the XML-based directory. This can parse and generate the full XML file. 26 """ 27
28 - def __init__(self):
29 self.realm = "" 30 self.records = {} 31 for type in recordtypes.RECORD_TYPES: 32 self.records[type] = []
33
34 - def addRecord(self, record):
35 """ 36 Add a new principal record to the directory. 37 38 @param record: the record to add. 39 @type record: L{admin.xmlaccounts.record.XMLRecord} 40 """ 41 self.records[record.recordType].append(record)
42
43 - def containsRecord(self, recordType, uid):
44 """ 45 Test whether the directory contains a record of the specified type and user id. 46 47 @param recordType: a principal record type. 48 @type recordType: one of L{admin.xmlaccounts.recordtypes.RECORD_TYPES} 49 @param uid: the user id to check. 50 @type uid: C{str} 51 52 @return: C{True} if present in the directory, C{False} otherwise. 53 @rtype: C{boolean} 54 """ 55 for record in self.records[recordType]: 56 if record.uid == uid: 57 return True 58 else: 59 return False
60
61 - def containsGUID(self, guid):
62 """ 63 Test whether the directory contains a record with the specified GUID. 64 65 @param guid: the GUID to check. 66 @type guid: C{str} 67 68 @return: C{True} if present in the directory, C{False} otherwise. 69 @rtype: C{boolean} 70 """ 71 for type in recordtypes.RECORD_TYPES: 72 for record in self.records[type]: 73 if record.guid == guid: 74 return True 75 return False
76
77 - def getRecord(self, recordType, uid):
78 """ 79 Return the record in the directory with the matching record type and user id. 80 81 @param recordType: a principal record type. 82 @type recordType: one of L{admin.xmlaccounts.recordtypes.RECORD_TYPES} 83 @param uid: the user id to check. 84 @type uid: C{str} 85 86 @return: the matching record, or C{None} if not found. 87 @rtype: L{admin.xmlaccounts.record.XMLRecord} 88 """ 89 for record in self.records[recordType]: 90 if record.uid == uid: 91 return record 92 else: 93 return None
94
95 - def removeRecord(self, recordType, uid):
96 """ 97 Remove the record with the matching type and user id from the directory. 98 99 @param recordType: a principal record type. 100 @type recordType: one of L{admin.xmlaccounts.recordtypes.RECORD_TYPES} 101 @param uid: the user id to remove. 102 @type uid: C{str} 103 104 @return: C{True} if found and removed, C{False} otherwise. 105 @rtype: C{boolean} 106 """ 107 for record in self.records[recordType]: 108 if record.uid == uid: 109 self.records[recordType].remove(record) 110 return True 111 else: 112 return False
113
114 - def parseXML(self, node):
115 """ 116 Parse an entire XML directory. 117 118 @param node: the XML element for the root of the directory file. 119 @type node: C{xml.etree.ElementTree.Element} 120 """ 121 if node.tag == tags.ELEMENT_ACCOUNTS: 122 self.realm = node.get(tags.ATTRIBUTE_REALM, "") 123 124 for child in node.getchildren(): 125 record = XMLRecord() 126 record.parseXML(child) 127 self.records[record.recordType].append(record)
128 129 # TODO: Now resolve group and proxy references 130
131 - def writeXML(self):
132 """ 133 Generate an entire XML directory. 134 135 @return: the root element for the principal record. 136 @rtype: C{xml.etree.ElementTree.Element} 137 """ 138 139 root = Element(tags.ELEMENT_ACCOUNTS) 140 if self.realm: 141 root.set(tags.ATTRIBUTE_REALM, self.realm) 142 for type in recordtypes.RECORD_TYPES: 143 self.writeXMLRecords(root, self.records[type]) 144 return root
145
146 - def writeXMLRecords(self, root, records):
147 """ 148 Generate the XML for all records in the specified list. 149 150 @param root: the XML root element for the directory. 151 @type root: C{xml.etree.ElementTree.Element} 152 @param records: a list of L{admin.xmlaccounts.record.XMLRecord} to process. 153 @type records: C{list} 154 """ 155 for record in records: 156 root.append(record.writeXML())
157