Mercurial > p > roundup > code
comparison roundup/hyperdb.py @ 676:bc46480e2a2b
Fixed serialisation problem...
...by moving the serialisation step out of the hyperdb.Class (get,
set) into the hyperdb.Database.
Also fixed htmltemplate after the showid changes I made yesterday.
Unit tests for all of the above written.
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Wed, 03 Apr 2002 05:54:31 +0000 |
| parents | e0a1cc7538e9 |
| children | 8d3cc8352b89 |
comparison
equal
deleted
inserted
replaced
| 675:e04d291a1194 | 676:bc46480e2a2b |
|---|---|
| 13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" | 14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" |
| 15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, | 15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, |
| 16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | 16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 17 # | 17 # |
| 18 # $Id: hyperdb.py,v 1.59 2002-03-12 22:52:26 richard Exp $ | 18 # $Id: hyperdb.py,v 1.60 2002-04-03 05:54:31 richard Exp $ |
| 19 | 19 |
| 20 __doc__ = """ | 20 __doc__ = """ |
| 21 Hyperdatabase implementation, especially field types. | 21 Hyperdatabase implementation, especially field types. |
| 22 """ | 22 """ |
| 23 | 23 |
| 169 def addnode(self, classname, nodeid, node): | 169 def addnode(self, classname, nodeid, node): |
| 170 '''Add the specified node to its class's db. | 170 '''Add the specified node to its class's db. |
| 171 ''' | 171 ''' |
| 172 raise NotImplementedError | 172 raise NotImplementedError |
| 173 | 173 |
| 174 def serialise(self, classname, node): | |
| 175 '''Copy the node contents, converting non-marshallable data into | |
| 176 marshallable data. | |
| 177 ''' | |
| 178 if DEBUG: print 'serialise', classname, node | |
| 179 properties = self.getclass(classname).getprops() | |
| 180 d = {} | |
| 181 for k, v in node.items(): | |
| 182 prop = properties[k] | |
| 183 | |
| 184 if isinstance(prop, Password): | |
| 185 d[k] = str(v) | |
| 186 elif isinstance(prop, Date) and v is not None: | |
| 187 d[k] = v.get_tuple() | |
| 188 elif isinstance(prop, Interval) and v is not None: | |
| 189 d[k] = v.get_tuple() | |
| 190 else: | |
| 191 d[k] = v | |
| 192 return d | |
| 193 | |
| 174 def setnode(self, classname, nodeid, node): | 194 def setnode(self, classname, nodeid, node): |
| 175 '''Change the specified node. | 195 '''Change the specified node. |
| 176 ''' | 196 ''' |
| 177 raise NotImplementedError | 197 raise NotImplementedError |
| 198 | |
| 199 def unserialise(self, classname, node): | |
| 200 '''Decode the marshalled node data | |
| 201 ''' | |
| 202 if DEBUG: print 'unserialise', classname, node | |
| 203 properties = self.getclass(classname).getprops() | |
| 204 d = {} | |
| 205 for k, v in node.items(): | |
| 206 prop = properties[k] | |
| 207 if isinstance(prop, Date) and v is not None: | |
| 208 d[k] = date.Date(v) | |
| 209 elif isinstance(prop, Interval) and v is not None: | |
| 210 d[k] = date.Interval(v) | |
| 211 elif isinstance(prop, Password): | |
| 212 p = password.Password() | |
| 213 p.unpack(v) | |
| 214 d[k] = p | |
| 215 else: | |
| 216 d[k] = v | |
| 217 return d | |
| 178 | 218 |
| 179 def getnode(self, classname, nodeid, db=None, cache=1): | 219 def getnode(self, classname, nodeid, db=None, cache=1): |
| 180 '''Get a node from the database. | 220 '''Get a node from the database. |
| 181 ''' | 221 ''' |
| 182 raise NotImplementedError | 222 raise NotImplementedError |
| 394 propvalues[key] = [] | 434 propvalues[key] = [] |
| 395 else: | 435 else: |
| 396 # TODO: None isn't right here, I think... | 436 # TODO: None isn't right here, I think... |
| 397 propvalues[key] = None | 437 propvalues[key] = None |
| 398 | 438 |
| 399 # convert all data to strings | |
| 400 for key, prop in self.properties.items(): | |
| 401 if isinstance(prop, Date): | |
| 402 if propvalues[key] is not None: | |
| 403 propvalues[key] = propvalues[key].get_tuple() | |
| 404 elif isinstance(prop, Interval): | |
| 405 if propvalues[key] is not None: | |
| 406 propvalues[key] = propvalues[key].get_tuple() | |
| 407 elif isinstance(prop, Password): | |
| 408 propvalues[key] = str(propvalues[key]) | |
| 409 | |
| 410 # done | 439 # done |
| 411 self.db.addnode(self.classname, newid, propvalues) | 440 self.db.addnode(self.classname, newid, propvalues) |
| 412 self.db.addjournal(self.classname, newid, 'create', propvalues) | 441 self.db.addjournal(self.classname, newid, 'create', propvalues) |
| 413 return newid | 442 return newid |
| 414 | 443 |
| 440 else: | 469 else: |
| 441 # TODO: None isn't right here, I think... | 470 # TODO: None isn't right here, I think... |
| 442 return None | 471 return None |
| 443 else: | 472 else: |
| 444 return default | 473 return default |
| 445 | |
| 446 # possibly convert the marshalled data to instances | |
| 447 if isinstance(prop, Date): | |
| 448 if d[propname] is None: | |
| 449 return None | |
| 450 return date.Date(d[propname]) | |
| 451 elif isinstance(prop, Interval): | |
| 452 if d[propname] is None: | |
| 453 return None | |
| 454 return date.Interval(d[propname]) | |
| 455 elif isinstance(prop, Password): | |
| 456 p = password.Password() | |
| 457 p.unpack(d[propname]) | |
| 458 return p | |
| 459 | 474 |
| 460 return d[propname] | 475 return d[propname] |
| 461 | 476 |
| 462 # XXX not in spec | 477 # XXX not in spec |
| 463 def getnode(self, nodeid, cache=1): | 478 def getnode(self, nodeid, cache=1): |
| 603 raise TypeError, 'new property "%s" not a string'%key | 618 raise TypeError, 'new property "%s" not a string'%key |
| 604 | 619 |
| 605 elif isinstance(prop, Password): | 620 elif isinstance(prop, Password): |
| 606 if not isinstance(value, password.Password): | 621 if not isinstance(value, password.Password): |
| 607 raise TypeError, 'new property "%s" not a Password'% key | 622 raise TypeError, 'new property "%s" not a Password'% key |
| 608 propvalues[key] = value = str(value) | 623 propvalues[key] = value |
| 609 | 624 |
| 610 elif value is not None and isinstance(prop, Date): | 625 elif value is not None and isinstance(prop, Date): |
| 611 if not isinstance(value, date.Date): | 626 if not isinstance(value, date.Date): |
| 612 raise TypeError, 'new property "%s" not a Date'% key | 627 raise TypeError, 'new property "%s" not a Date'% key |
| 613 propvalues[key] = value = value.get_tuple() | 628 propvalues[key] = value |
| 614 | 629 |
| 615 elif value is not None and isinstance(prop, Interval): | 630 elif value is not None and isinstance(prop, Interval): |
| 616 if not isinstance(value, date.Interval): | 631 if not isinstance(value, date.Interval): |
| 617 raise TypeError, 'new property "%s" not an Interval'% key | 632 raise TypeError, 'new property "%s" not an Interval'% key |
| 618 propvalues[key] = value = value.get_tuple() | 633 propvalues[key] = value |
| 619 | 634 |
| 620 node[key] = value | 635 node[key] = value |
| 621 | 636 |
| 622 # nothing to do? | 637 # nothing to do? |
| 623 if not propvalues: | 638 if not propvalues: |
| 1095 cl.create(name=options[i], order=i) | 1110 cl.create(name=options[i], order=i) |
| 1096 return hyperdb.Link(name) | 1111 return hyperdb.Link(name) |
| 1097 | 1112 |
| 1098 # | 1113 # |
| 1099 # $Log: not supported by cvs2svn $ | 1114 # $Log: not supported by cvs2svn $ |
| 1115 # Revision 1.59 2002/03/12 22:52:26 richard | |
| 1116 # more pychecker warnings removed | |
| 1117 # | |
| 1100 # Revision 1.58 2002/02/27 03:23:16 richard | 1118 # Revision 1.58 2002/02/27 03:23:16 richard |
| 1101 # Ran it through pychecker, made fixes | 1119 # Ran it through pychecker, made fixes |
| 1102 # | 1120 # |
| 1103 # Revision 1.57 2002/02/20 05:23:24 richard | 1121 # Revision 1.57 2002/02/20 05:23:24 richard |
| 1104 # Didn't accomodate new values for new properties | 1122 # Didn't accomodate new values for new properties |
