Mercurial > p > roundup > code
comparison roundup/hyperdb.py @ 89:b6b30ba53986
Fixed some of the exceptions so they're the right type.
Removed the str()-ification of node ids so we don't mask oopsy errors
any more.
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Fri, 27 Jul 2001 06:25:35 +0000 |
| parents | b89712288636 |
| children | a55c1301ba1f |
comparison
equal
deleted
inserted
replaced
| 88:168def1ce74d | 89:b6b30ba53986 |
|---|---|
| 1 # $Id: hyperdb.py,v 1.3 2001-07-27 05:17:14 richard Exp $ | 1 # $Id: hyperdb.py,v 1.4 2001-07-27 06:25:35 richard Exp $ |
| 2 | 2 |
| 3 # standard python modules | 3 # standard python modules |
| 4 import cPickle, re, string | 4 import cPickle, re, string |
| 5 | 5 |
| 6 # roundup modules | 6 # roundup modules |
| 114 raise ValueError, 'node with key "%s" exists'%value | 114 raise ValueError, 'node with key "%s" exists'%value |
| 115 | 115 |
| 116 prop = self.properties[key] | 116 prop = self.properties[key] |
| 117 | 117 |
| 118 if prop.isLinkType: | 118 if prop.isLinkType: |
| 119 value = str(value) | 119 if type(value) != type(''): |
| 120 raise ValueError, 'link value must be String' | |
| 121 # value = str(value) | |
| 120 link_class = self.properties[key].classname | 122 link_class = self.properties[key].classname |
| 123 # if it isn't a number, it's a key | |
| 121 if not num_re.match(value): | 124 if not num_re.match(value): |
| 122 try: | 125 try: |
| 123 value = self.db.classes[link_class].lookup(value) | 126 value = self.db.classes[link_class].lookup(value) |
| 124 except: | 127 except: |
| 125 raise ValueError, 'new property "%s": %s not a %s'%( | 128 raise IndexError, 'new property "%s": %s not a %s'%( |
| 126 key, value, self.properties[key].classname) | 129 key, value, self.properties[key].classname) |
| 127 propvalues[key] = value | 130 propvalues[key] = value |
| 128 if not self.db.hasnode(link_class, value): | 131 if not self.db.hasnode(link_class, value): |
| 129 raise ValueError, '%s has no node %s'%(link_class, value) | 132 raise IndexError, '%s has no node %s'%(link_class, value) |
| 130 | 133 |
| 131 # register the link with the newly linked node | 134 # register the link with the newly linked node |
| 132 self.db.addjournal(link_class, value, 'link', | 135 self.db.addjournal(link_class, value, 'link', |
| 133 (self.classname, newid, key)) | 136 (self.classname, newid, key)) |
| 134 | 137 |
| 135 elif prop.isMultilinkType: | 138 elif prop.isMultilinkType: |
| 136 if type(value) != type([]): | 139 if type(value) != type([]): |
| 137 raise TypeError, 'new property "%s" not a list of ids'%key | 140 raise TypeError, 'new property "%s" not a list of ids'%key |
| 138 link_class = self.properties[key].classname | 141 link_class = self.properties[key].classname |
| 139 l = [] | 142 l = [] |
| 140 for entry in map(str, value): | 143 for entry in value: |
| 144 if type(entry) != type(''): | |
| 145 raise ValueError, 'link value must be String' | |
| 146 # if it isn't a number, it's a key | |
| 141 if not num_re.match(entry): | 147 if not num_re.match(entry): |
| 142 try: | 148 try: |
| 143 entry = self.db.classes[link_class].lookup(entry) | 149 entry = self.db.classes[link_class].lookup(entry) |
| 144 except: | 150 except: |
| 145 raise ValueError, 'new property "%s": %s not a %s'%( | 151 raise IndexError, 'new property "%s": %s not a %s'%( |
| 146 key, entry, self.properties[key].classname) | 152 key, entry, self.properties[key].classname) |
| 147 l.append(entry) | 153 l.append(entry) |
| 148 value = l | 154 value = l |
| 149 propvalues[key] = value | 155 propvalues[key] = value |
| 150 | 156 |
| 151 # handle additions | 157 # handle additions |
| 152 for id in value: | 158 for id in value: |
| 153 if not self.db.hasnode(link_class, id): | 159 if not self.db.hasnode(link_class, id): |
| 154 raise ValueError, '%s has no node %s'%(link_class, id) | 160 raise IndexError, '%s has no node %s'%(link_class, id) |
| 155 # register the link with the newly linked node | 161 # register the link with the newly linked node |
| 156 self.db.addjournal(link_class, id, 'link', | 162 self.db.addjournal(link_class, id, 'link', |
| 157 (self.classname, newid, key)) | 163 (self.classname, newid, key)) |
| 158 | 164 |
| 159 elif prop.isStringType: | 165 elif prop.isStringType: |
| 166 | 172 |
| 167 elif prop.isIntervalType: | 173 elif prop.isIntervalType: |
| 168 if not hasattr(value, 'isInterval'): | 174 if not hasattr(value, 'isInterval'): |
| 169 raise TypeError, 'new property "%s" not an Interval'% key | 175 raise TypeError, 'new property "%s" not an Interval'% key |
| 170 | 176 |
| 171 for key,prop in self.properties.items(): | 177 for key, prop in self.properties.items(): |
| 172 if propvalues.has_key(str(key)): | 178 if propvalues.has_key(key): |
| 173 continue | 179 continue |
| 174 if prop.isMultilinkType: | 180 if prop.isMultilinkType: |
| 175 propvalues[key] = [] | 181 propvalues[key] = [] |
| 176 else: | 182 else: |
| 177 propvalues[key] = None | 183 propvalues[key] = None |
| 186 | 192 |
| 187 'nodeid' must be the id of an existing node of this class or an | 193 'nodeid' must be the id of an existing node of this class or an |
| 188 IndexError is raised. 'propname' must be the name of a property | 194 IndexError is raised. 'propname' must be the name of a property |
| 189 of this class or a KeyError is raised. | 195 of this class or a KeyError is raised. |
| 190 """ | 196 """ |
| 191 d = self.db.getnode(self.classname, str(nodeid)) | 197 # nodeid = str(nodeid) |
| 198 d = self.db.getnode(self.classname, nodeid) | |
| 192 return d[propname] | 199 return d[propname] |
| 193 | 200 |
| 194 # XXX not in spec | 201 # XXX not in spec |
| 195 def getnode(self, nodeid): | 202 def getnode(self, nodeid): |
| 196 ''' Return a convenience wrapper for the node | 203 ''' Return a convenience wrapper for the node |
| 217 """ | 224 """ |
| 218 if not propvalues: | 225 if not propvalues: |
| 219 return | 226 return |
| 220 if self.db.journaltag is None: | 227 if self.db.journaltag is None: |
| 221 raise DatabaseError, 'Database open read-only' | 228 raise DatabaseError, 'Database open read-only' |
| 222 nodeid = str(nodeid) | 229 # nodeid = str(nodeid) |
| 223 node = self.db.getnode(self.classname, nodeid) | 230 node = self.db.getnode(self.classname, nodeid) |
| 224 if node.has_key(self.db.RETIRED_FLAG): | 231 if node.has_key(self.db.RETIRED_FLAG): |
| 225 raise IndexError | 232 raise IndexError |
| 226 num_re = re.compile('^\d+$') | 233 num_re = re.compile('^\d+$') |
| 227 for key, value in propvalues.items(): | 234 for key, value in propvalues.items(): |
| 237 raise ValueError, 'node with key "%s" exists'%value | 244 raise ValueError, 'node with key "%s" exists'%value |
| 238 | 245 |
| 239 prop = self.properties[key] | 246 prop = self.properties[key] |
| 240 | 247 |
| 241 if prop.isLinkType: | 248 if prop.isLinkType: |
| 242 value = str(value) | 249 # value = str(value) |
| 243 link_class = self.properties[key].classname | 250 link_class = self.properties[key].classname |
| 251 # if it isn't a number, it's a key | |
| 252 if type(value) != type(''): | |
| 253 raise ValueError, 'link value must be String' | |
| 244 if not num_re.match(value): | 254 if not num_re.match(value): |
| 245 try: | 255 try: |
| 246 value = self.db.classes[link_class].lookup(value) | 256 value = self.db.classes[link_class].lookup(value) |
| 247 except: | 257 except: |
| 248 raise ValueError, 'new property "%s": %s not a %s'%( | 258 raise IndexError, 'new property "%s": %s not a %s'%( |
| 249 key, value, self.properties[key].classname) | 259 key, value, self.properties[key].classname) |
| 250 | 260 |
| 251 if not self.db.hasnode(link_class, value): | 261 if not self.db.hasnode(link_class, value): |
| 252 raise ValueError, '%s has no node %s'%(link_class, value) | 262 raise IndexError, '%s has no node %s'%(link_class, value) |
| 253 | 263 |
| 254 # register the unlink with the old linked node | 264 # register the unlink with the old linked node |
| 255 if node[key] is not None: | 265 if node[key] is not None: |
| 256 self.db.addjournal(link_class, node[key], 'unlink', | 266 self.db.addjournal(link_class, node[key], 'unlink', |
| 257 (self.classname, nodeid, key)) | 267 (self.classname, nodeid, key)) |
| 264 elif prop.isMultilinkType: | 274 elif prop.isMultilinkType: |
| 265 if type(value) != type([]): | 275 if type(value) != type([]): |
| 266 raise TypeError, 'new property "%s" not a list of ids'%key | 276 raise TypeError, 'new property "%s" not a list of ids'%key |
| 267 link_class = self.properties[key].classname | 277 link_class = self.properties[key].classname |
| 268 l = [] | 278 l = [] |
| 269 for entry in map(str, value): | 279 for entry in value: |
| 280 # if it isn't a number, it's a key | |
| 281 if type(entry) != type(''): | |
| 282 raise ValueError, 'link value must be String' | |
| 270 if not num_re.match(entry): | 283 if not num_re.match(entry): |
| 271 try: | 284 try: |
| 272 entry = self.db.classes[link_class].lookup(entry) | 285 entry = self.db.classes[link_class].lookup(entry) |
| 273 except: | 286 except: |
| 274 raise ValueError, 'new property "%s": %s not a %s'%( | 287 raise IndexError, 'new property "%s": %s not a %s'%( |
| 275 key, entry, self.properties[key].classname) | 288 key, entry, self.properties[key].classname) |
| 276 l.append(entry) | 289 l.append(entry) |
| 277 value = l | 290 value = l |
| 278 propvalues[key] = value | 291 propvalues[key] = value |
| 279 | 292 |
| 288 l.remove(id) | 301 l.remove(id) |
| 289 | 302 |
| 290 # handle additions | 303 # handle additions |
| 291 for id in value: | 304 for id in value: |
| 292 if not self.db.hasnode(link_class, id): | 305 if not self.db.hasnode(link_class, id): |
| 293 raise ValueError, '%s has no node %s'%(link_class, id) | 306 raise IndexError, '%s has no node %s'%(link_class, id) |
| 294 if id in l: | 307 if id in l: |
| 295 continue | 308 continue |
| 296 # register the link with the newly linked node | 309 # register the link with the newly linked node |
| 297 self.db.addjournal(link_class, id, 'link', | 310 self.db.addjournal(link_class, id, 'link', |
| 298 (self.classname, nodeid, key)) | 311 (self.classname, nodeid, key)) |
| 322 and the node's id is never reused. | 335 and the node's id is never reused. |
| 323 | 336 |
| 324 Retired nodes are not returned by the find(), list(), or lookup() | 337 Retired nodes are not returned by the find(), list(), or lookup() |
| 325 methods, and other nodes may reuse the values of their key properties. | 338 methods, and other nodes may reuse the values of their key properties. |
| 326 """ | 339 """ |
| 327 nodeid = str(nodeid) | 340 # nodeid = str(nodeid) |
| 328 if self.db.journaltag is None: | 341 if self.db.journaltag is None: |
| 329 raise DatabaseError, 'Database open read-only' | 342 raise DatabaseError, 'Database open read-only' |
| 330 node = self.db.getnode(self.classname, nodeid) | 343 node = self.db.getnode(self.classname, nodeid) |
| 331 node[self.db.RETIRED_FLAG] = 1 | 344 node[self.db.RETIRED_FLAG] = 1 |
| 332 self.db.setnode(self.classname, nodeid, node) | 345 self.db.setnode(self.classname, nodeid, node) |
| 393 'nodeid' must be the id of an existing node in the class linked | 406 'nodeid' must be the id of an existing node in the class linked |
| 394 to by the given property, or an IndexError is raised. | 407 to by the given property, or an IndexError is raised. |
| 395 """ | 408 """ |
| 396 propspec = propspec.items() | 409 propspec = propspec.items() |
| 397 for propname, nodeid in propspec: | 410 for propname, nodeid in propspec: |
| 398 nodeid = str(nodeid) | 411 # nodeid = str(nodeid) |
| 399 # check the prop is OK | 412 # check the prop is OK |
| 400 prop = self.properties[propname] | 413 prop = self.properties[propname] |
| 401 if not prop.isLinkType and not prop.isMultilinkType: | 414 if not prop.isLinkType and not prop.isMultilinkType: |
| 402 raise TypeError, "'%s' not a Link/Multilink property"%propname | 415 raise TypeError, "'%s' not a Link/Multilink property"%propname |
| 403 if not self.db.hasnode(prop.classname, nodeid): | 416 if not self.db.hasnode(prop.classname, nodeid): |
| 409 for id in self.db.getnodeids(self.classname, cldb): | 422 for id in self.db.getnodeids(self.classname, cldb): |
| 410 node = self.db.getnode(self.classname, id, cldb) | 423 node = self.db.getnode(self.classname, id, cldb) |
| 411 if node.has_key(self.db.RETIRED_FLAG): | 424 if node.has_key(self.db.RETIRED_FLAG): |
| 412 continue | 425 continue |
| 413 for propname, nodeid in propspec: | 426 for propname, nodeid in propspec: |
| 414 nodeid = str(nodeid) | 427 # nodeid = str(nodeid) |
| 415 property = node[propname] | 428 property = node[propname] |
| 416 if prop.isLinkType and nodeid == property: | 429 if prop.isLinkType and nodeid == property: |
| 417 l.append(id) | 430 l.append(id) |
| 418 elif prop.isMultilinkType and nodeid in property: | 431 elif prop.isMultilinkType and nodeid in property: |
| 419 l.append(id) | 432 l.append(id) |
| 738 cl.create(name=option[i], order=i) | 751 cl.create(name=option[i], order=i) |
| 739 return hyperdb.Link(name) | 752 return hyperdb.Link(name) |
| 740 | 753 |
| 741 # | 754 # |
| 742 # $Log: not supported by cvs2svn $ | 755 # $Log: not supported by cvs2svn $ |
| 756 # Revision 1.3 2001/07/27 05:17:14 richard | |
| 757 # just some comments | |
| 758 # | |
| 743 # Revision 1.2 2001/07/22 12:09:32 richard | 759 # Revision 1.2 2001/07/22 12:09:32 richard |
| 744 # Final commit of Grande Splite | 760 # Final commit of Grande Splite |
| 745 # | 761 # |
| 746 # Revision 1.1 2001/07/22 11:58:35 richard | 762 # Revision 1.1 2001/07/22 11:58:35 richard |
| 747 # More Grande Splite | 763 # More Grande Splite |
