Package ldaptor :: Package protocols :: Package ldap :: Module ldifdelta
[hide private]
[frames] | no frames]

Source Code for Module ldaptor.protocols.ldap.ldifdelta

  1  from twisted.python.failure import Failure 
  2  from twisted.internet import error 
  3  from ldaptor.protocols.ldap import ldifprotocol 
  4  from ldaptor import delta, entry 
  5   
  6  WAIT_FOR_CHANGETYPE = 'WAIT_FOR_CHANGETYPE' 
  7  WAIT_FOR_MOD_SPEC = 'WAIT_FOR_MOD_SPEC' 
  8  IN_MOD_SPEC = 'IN_MOD_SPEC' 
  9  IN_ADD_ENTRY = 'IN_ADD_ENTRY' 
 10  IN_DELETE = 'IN_DELETE' 
 11   
12 -class LDIFDeltaMissingChangeTypeError(ldifprotocol.LDIFParseError):
13 """LDIF delta entry has no changetype.""" 14 pass
15
16 -class LDIFDeltaUnknownModificationError(ldifprotocol.LDIFParseError):
17 """LDIF delta modification has unknown mod-spec.""" 18 pass
19
20 -class LDIFDeltaModificationMissingEndDashError(ldifprotocol.LDIFParseError):
21 """LDIF delta modification has no ending dash.""" 22 pass
23
24 -class LDIFDeltaModificationDifferentAttributeTypeError(ldifprotocol.LDIFParseError):
25 """The attribute type for the change is not the as in the mod-spec header line.""" 26 pass
27
28 -class LDIFDeltaAddMissingAttributesError(ldifprotocol.LDIFParseError):
29 """Add operation needs to have atleast one attribute type and value.""" 30 pass
31
32 -class LDIFDeltaDeleteHasJunkAfterChangeTypeError(ldifprotocol.LDIFParseError):
33 """Delete operation takes no attribute types or values.""" 34 pass
35
36 -class LDIFDelta(ldifprotocol.LDIF):
37 - def state_WAIT_FOR_DN(self, line):
38 super(LDIFDelta, self).state_WAIT_FOR_DN(line) 39 if self.mode == ldifprotocol.IN_ENTRY: 40 self.mode = WAIT_FOR_CHANGETYPE
41
42 - def state_WAIT_FOR_CHANGETYPE(self, line):
43 assert self.dn is not None, 'self.dn must be set when in entry' 44 assert self.data is not None, 'self.data must be set when in entry' 45 46 if line == '': 47 raise LDIFDeltaMissingChangeTypeError, self.dn 48 49 key, val = self._parseLine(line) 50 51 if key != 'changetype': 52 raise LDIFDeltaMissingChangeTypeError, (self.dn, key, val) 53 54 if val == 'modify': 55 self.modifications = [] 56 self.mode = WAIT_FOR_MOD_SPEC 57 elif val == 'add': 58 self.mode = IN_ADD_ENTRY 59 elif val == 'delete': 60 self.mode = IN_DELETE 61 elif val == 'modrdn' or val == 'moddn': 62 raise NotImplementedError #TODO
63 64 MOD_SPEC_TO_DELTA = { 65 'add': delta.Add, 66 'delete': delta.Delete, 67 'replace': delta.Replace, 68 } 69
70 - def state_WAIT_FOR_MOD_SPEC(self, line):
71 if line == '': 72 # end of entry 73 self.mode = ldifprotocol.WAIT_FOR_DN 74 m = delta.ModifyOp(dn=self.dn, 75 modifications=self.modifications) 76 self.dn = None 77 self.data = None 78 self.modifications = None 79 self.gotEntry(m) 80 return 81 82 key, val = self._parseLine(line) 83 84 if key not in self.MOD_SPEC_TO_DELTA: 85 raise LDIFDeltaUnknownModificationError, \ 86 (self.dn, key) 87 88 self.mod_spec = key 89 self.mod_spec_attr = val 90 self.mod_spec_data = [] 91 self.mode = IN_MOD_SPEC
92
93 - def state_IN_MOD_SPEC(self, line):
94 if line == '': 95 raise LDIFDeltaModificationMissingEndDashError 96 97 if line == '-': 98 mod = self.MOD_SPEC_TO_DELTA[self.mod_spec] 99 de = mod(self.mod_spec_attr, self.mod_spec_data) 100 self.modifications.append(de) 101 del self.mod_spec 102 del self.mod_spec_attr 103 del self.mod_spec_data 104 self.mode = WAIT_FOR_MOD_SPEC 105 return 106 107 key, val = self._parseLine(line) 108 109 if key != self.mod_spec_attr: 110 raise LDIFDeltaModificationDifferentAttributeTypeError, \ 111 (key, self.mod_spec_attr) 112 113 self.mod_spec_data.append(val)
114
115 - def state_IN_ADD_ENTRY(self, line):
116 assert self.dn is not None, 'self.dn must be set when in entry' 117 assert self.data is not None, 'self.data must be set when in entry' 118 119 if line == '': 120 # end of entry 121 if not self.data: 122 raise LDIFDeltaAddMissingAttributesError, \ 123 self.dn 124 self.mode = ldifprotocol.WAIT_FOR_DN 125 o = delta.AddOp(entry.BaseLDAPEntry(dn=self.dn, 126 attributes=self.data)) 127 self.dn = None 128 self.data = None 129 self.gotEntry(o) 130 return 131 132 key, val = self._parseLine(line) 133 134 if not key in self.data: 135 self.data[key] = [] 136 137 self.data[key].append(val)
138
139 - def state_IN_DELETE(self, line):
140 assert self.dn is not None, 'self.dn must be set when in entry' 141 142 if line == '': 143 # end of entry 144 self.mode = ldifprotocol.WAIT_FOR_DN 145 o = delta.DeleteOp(dn=self.dn) 146 self.dn = None 147 self.data = None 148 self.gotEntry(o) 149 return 150 151 raise LDIFDeltaDeleteHasJunkAfterChangeTypeError, \ 152 (self.dn, line)
153
154 -def fromLDIFFile(f):
155 """Read LDIF data from a file.""" 156 157 p = LDIFDelta() 158 l = [] 159 p.gotEntry = l.append 160 while 1: 161 data = f.read() 162 if not data: 163 break 164 p.dataReceived(data) 165 p.connectionLost(Failure(error.ConnectionDone())) 166 167 return l
168