Mercurial > p > roundup > code
comparison roundup/hyperdb.py @ 5378:35ea9b1efc14
Python 3 preparation: "raise" syntax.
Changing "raise Exception, value" to "raise Exception(value)".
Tool-assisted patch. Particular cases to check carefully are the one
place in frontends/ZRoundup/ZRoundup.py where a string exception
needed to be fixed, and the one in roundup/cgi/client.py involving
raising an exception with a traceback (requires three-argument form of
raise in Python 2, which as I understand it requires exec() to avoid a
Python 3 syntax error).
| author | Joseph Myers <jsm@polyomino.org.uk> |
|---|---|
| date | Tue, 24 Jul 2018 21:39:58 +0000 |
| parents | 8cd518058007 |
| children | 0942fe89e82e |
comparison
equal
deleted
inserted
replaced
| 5377:12fe83f90f0d | 5378:35ea9b1efc14 |
|---|---|
| 85 if not value: | 85 if not value: |
| 86 return None | 86 return None |
| 87 try: | 87 try: |
| 88 return password.Password(encrypted=value, scheme=self.scheme, strict=True) | 88 return password.Password(encrypted=value, scheme=self.scheme, strict=True) |
| 89 except password.PasswordValueError as message: | 89 except password.PasswordValueError as message: |
| 90 raise HyperdbValueError, \ | 90 raise HyperdbValueError(_('property %s: %s')%(kw['propname'], message)) |
| 91 _('property %s: %s')%(kw['propname'], message) | |
| 92 | 91 |
| 93 def sort_repr (self, cls, val, name): | 92 def sort_repr (self, cls, val, name): |
| 94 if not val: | 93 if not val: |
| 95 return val | 94 return val |
| 96 return str(val) | 95 return str(val) |
| 108 return db.getUserTimezone() | 107 return db.getUserTimezone() |
| 109 def from_raw(self, value, db, **kw): | 108 def from_raw(self, value, db, **kw): |
| 110 try: | 109 try: |
| 111 value = date.Date(value, self.offset(db)) | 110 value = date.Date(value, self.offset(db)) |
| 112 except ValueError as message: | 111 except ValueError as message: |
| 113 raise HyperdbValueError, _('property %s: %r is an invalid '\ | 112 raise HyperdbValueError(_('property %s: %r is an invalid '\ |
| 114 'date (%s)')%(kw['propname'], value, message) | 113 'date (%s)')%(kw['propname'], value, message)) |
| 115 return value | 114 return value |
| 116 def range_from_raw(self, value, db): | 115 def range_from_raw(self, value, db): |
| 117 """return Range value from given raw value with offset correction""" | 116 """return Range value from given raw value with offset correction""" |
| 118 return date.Range(value, date.Date, offset=self.offset(db)) | 117 return date.Range(value, date.Date, offset=self.offset(db)) |
| 119 def sort_repr (self, cls, val, name): | 118 def sort_repr (self, cls, val, name): |
| 125 """An object designating an Interval property.""" | 124 """An object designating an Interval property.""" |
| 126 def from_raw(self, value, **kw): | 125 def from_raw(self, value, **kw): |
| 127 try: | 126 try: |
| 128 value = date.Interval(value) | 127 value = date.Interval(value) |
| 129 except ValueError as message: | 128 except ValueError as message: |
| 130 raise HyperdbValueError, _('property %s: %r is an invalid '\ | 129 raise HyperdbValueError(_('property %s: %r is an invalid '\ |
| 131 'date interval (%s)')%(kw['propname'], value, message) | 130 'date interval (%s)')%(kw['propname'], value, message)) |
| 132 return value | 131 return value |
| 133 def sort_repr (self, cls, val, name): | 132 def sort_repr (self, cls, val, name): |
| 134 if not val: | 133 if not val: |
| 135 return val | 134 return val |
| 136 return val.as_seconds() | 135 return val.as_seconds() |
| 255 # perform the add/remove | 254 # perform the add/remove |
| 256 if remove: | 255 if remove: |
| 257 try: | 256 try: |
| 258 curvalue.remove(itemid) | 257 curvalue.remove(itemid) |
| 259 except ValueError: | 258 except ValueError: |
| 260 raise HyperdbValueError, _('property %s: %r is not ' \ | 259 raise HyperdbValueError(_('property %s: %r is not ' \ |
| 261 'currently an element')%(propname, item) | 260 'currently an element')%(propname, item)) |
| 262 else: | 261 else: |
| 263 newvalue.append(itemid) | 262 newvalue.append(itemid) |
| 264 if itemid not in curvalue: | 263 if itemid not in curvalue: |
| 265 curvalue.append(itemid) | 264 curvalue.append(itemid) |
| 266 | 265 |
| 309 def from_raw(self, value, **kw): | 308 def from_raw(self, value, **kw): |
| 310 value = value.strip() | 309 value = value.strip() |
| 311 try: | 310 try: |
| 312 value = float(value) | 311 value = float(value) |
| 313 except ValueError: | 312 except ValueError: |
| 314 raise HyperdbValueError, _('property %s: %r is not a number')%( | 313 raise HyperdbValueError(_('property %s: %r is not a number')%( |
| 315 kw['propname'], value) | 314 kw['propname'], value)) |
| 316 return value | 315 return value |
| 317 | 316 |
| 318 class Integer(_Type): | 317 class Integer(_Type): |
| 319 """An object designating an integer property""" | 318 """An object designating an integer property""" |
| 320 def from_raw(self, value, **kw): | 319 def from_raw(self, value, **kw): |
| 321 value = value.strip() | 320 value = value.strip() |
| 322 try: | 321 try: |
| 323 value = int(value) | 322 value = int(value) |
| 324 except ValueError: | 323 except ValueError: |
| 325 raise HyperdbValueError, _('property %s: %r is not an integer')%( | 324 raise HyperdbValueError(_('property %s: %r is not an integer')%( |
| 326 kw['propname'], value) | 325 kw['propname'], value)) |
| 327 return value | 326 return value |
| 328 # | 327 # |
| 329 # Support for splitting designators | 328 # Support for splitting designators |
| 330 # | 329 # |
| 331 class DesignatorError(ValueError): | 330 class DesignatorError(ValueError): |
| 333 def splitDesignator(designator, dre=re.compile(r'([^\d]+)(\d+)')): | 332 def splitDesignator(designator, dre=re.compile(r'([^\d]+)(\d+)')): |
| 334 """ Take a foo123 and return ('foo', 123) | 333 """ Take a foo123 and return ('foo', 123) |
| 335 """ | 334 """ |
| 336 m = dre.match(designator) | 335 m = dre.match(designator) |
| 337 if m is None: | 336 if m is None: |
| 338 raise DesignatorError, _('"%s" not a node designator')%designator | 337 raise DesignatorError(_('"%s" not a node designator')%designator) |
| 339 return m.group(1), m.group(2) | 338 return m.group(1), m.group(2) |
| 340 | 339 |
| 341 class Proptree(object): | 340 class Proptree(object): |
| 342 """ Simple tree data structure for property lookup. Each node in | 341 """ Simple tree data structure for property lookup. Each node in |
| 343 the tree is a roundup Class Property that has to be navigated to | 342 the tree is a roundup Class Property that has to be navigated to |
| 828 def iter_roles(roles): | 827 def iter_roles(roles): |
| 829 ''' handle the text processing of turning the roles list | 828 ''' handle the text processing of turning the roles list |
| 830 into something python can use more easily | 829 into something python can use more easily |
| 831 ''' | 830 ''' |
| 832 if not roles or not roles.strip(): | 831 if not roles or not roles.strip(): |
| 833 raise StopIteration, "Empty roles given" | 832 raise StopIteration("Empty roles given") |
| 834 for role in [x.lower().strip() for x in roles.split(',')]: | 833 for role in [x.lower().strip() for x in roles.split(',')]: |
| 835 yield role | 834 yield role |
| 836 | 835 |
| 837 | 836 |
| 838 # | 837 # |
| 852 or a ValueError is raised. The keyword arguments in 'properties' | 851 or a ValueError is raised. The keyword arguments in 'properties' |
| 853 must map names to property objects, or a TypeError is raised. | 852 must map names to property objects, or a TypeError is raised. |
| 854 """ | 853 """ |
| 855 for name in 'creation activity creator actor'.split(): | 854 for name in 'creation activity creator actor'.split(): |
| 856 if properties.has_key(name): | 855 if properties.has_key(name): |
| 857 raise ValueError, '"creation", "activity", "creator" and '\ | 856 raise ValueError('"creation", "activity", "creator" and '\ |
| 858 '"actor" are reserved' | 857 '"actor" are reserved') |
| 859 | 858 |
| 860 self.classname = classname | 859 self.classname = classname |
| 861 self.properties = properties | 860 self.properties = properties |
| 862 self.db = weakref.proxy(db) # use a weak ref to avoid circularity | 861 self.db = weakref.proxy(db) # use a weak ref to avoid circularity |
| 863 self.key = '' | 862 self.key = '' |
| 1158 def setlabelprop(self, labelprop): | 1157 def setlabelprop(self, labelprop): |
| 1159 """Set the label property. Used for override of labelprop | 1158 """Set the label property. Used for override of labelprop |
| 1160 resolution order. | 1159 resolution order. |
| 1161 """ | 1160 """ |
| 1162 if labelprop not in self.getprops(): | 1161 if labelprop not in self.getprops(): |
| 1163 raise ValueError, _("Not a property name: %s") % labelprop | 1162 raise ValueError(_("Not a property name: %s") % labelprop) |
| 1164 self._labelprop = labelprop | 1163 self._labelprop = labelprop |
| 1165 | 1164 |
| 1166 def setorderprop(self, orderprop): | 1165 def setorderprop(self, orderprop): |
| 1167 """Set the order property. Used for override of orderprop | 1166 """Set the order property. Used for override of orderprop |
| 1168 resolution order | 1167 resolution order |
| 1169 """ | 1168 """ |
| 1170 if orderprop not in self.getprops(): | 1169 if orderprop not in self.getprops(): |
| 1171 raise ValueError, _("Not a property name: %s") % orderprop | 1170 raise ValueError(_("Not a property name: %s") % orderprop) |
| 1172 self._orderprop = orderprop | 1171 self._orderprop = orderprop |
| 1173 | 1172 |
| 1174 def getkey(self): | 1173 def getkey(self): |
| 1175 """Return the name of the key property for this class or None.""" | 1174 """Return the name of the key property for this class or None.""" |
| 1176 raise NotImplementedError | 1175 raise NotImplementedError |
| 1565 if not idre or not idre.match(value): | 1564 if not idre or not idre.match(value): |
| 1566 if linkcl.getkey(): | 1565 if linkcl.getkey(): |
| 1567 try: | 1566 try: |
| 1568 value = linkcl.lookup(value) | 1567 value = linkcl.lookup(value) |
| 1569 except KeyError as message: | 1568 except KeyError as message: |
| 1570 raise HyperdbValueError, _('property %s: %r is not a %s.')%( | 1569 raise HyperdbValueError(_('property %s: %r is not a %s.')%( |
| 1571 propname, value, prop.classname) | 1570 propname, value, prop.classname)) |
| 1572 else: | 1571 else: |
| 1573 raise HyperdbValueError, _('you may only enter ID values '\ | 1572 raise HyperdbValueError(_('you may only enter ID values '\ |
| 1574 'for property %s')%propname | 1573 'for property %s')%propname) |
| 1575 return value | 1574 return value |
| 1576 | 1575 |
| 1577 def fixNewlines(text): | 1576 def fixNewlines(text): |
| 1578 """ Homogenise line endings. | 1577 """ Homogenise line endings. |
| 1579 | 1578 |
| 1600 # ensure it's a valid property name | 1599 # ensure it's a valid property name |
| 1601 propname = propname.strip() | 1600 propname = propname.strip() |
| 1602 try: | 1601 try: |
| 1603 proptype = properties[propname] | 1602 proptype = properties[propname] |
| 1604 except KeyError: | 1603 except KeyError: |
| 1605 raise HyperdbValueError, _('%r is not a property of %s')%(propname, | 1604 raise HyperdbValueError(_('%r is not a property of %s')%(propname, |
| 1606 klass.classname) | 1605 klass.classname)) |
| 1607 | 1606 |
| 1608 # if we got a string, strip it now | 1607 # if we got a string, strip it now |
| 1609 if isinstance(value, type('')): | 1608 if isinstance(value, type('')): |
| 1610 value = value.strip() | 1609 value = value.strip() |
| 1611 | 1610 |
| 1706 except KeyError as value: | 1705 except KeyError as value: |
| 1707 # we trap this but re-raise it as AttributeError - all other | 1706 # we trap this but re-raise it as AttributeError - all other |
| 1708 # exceptions should pass through untrapped | 1707 # exceptions should pass through untrapped |
| 1709 pass | 1708 pass |
| 1710 # nope, no such attribute | 1709 # nope, no such attribute |
| 1711 raise AttributeError, str(value) | 1710 raise AttributeError(str(value)) |
| 1712 def __getitem__(self, name): | 1711 def __getitem__(self, name): |
| 1713 return self.cl.get(self.nodeid, name) | 1712 return self.cl.get(self.nodeid, name) |
| 1714 def __setattr__(self, name, value): | 1713 def __setattr__(self, name, value): |
| 1715 try: | 1714 try: |
| 1716 return self.cl.set(self.nodeid, **{name: value}) | 1715 return self.cl.set(self.nodeid, **{name: value}) |
| 1717 except KeyError as value: | 1716 except KeyError as value: |
| 1718 raise AttributeError, str(value) | 1717 raise AttributeError(str(value)) |
| 1719 def __setitem__(self, name, value): | 1718 def __setitem__(self, name, value): |
| 1720 self.cl.set(self.nodeid, **{name: value}) | 1719 self.cl.set(self.nodeid, **{name: value}) |
| 1721 def history(self, enforceperm=True, skipquiet=True): | 1720 def history(self, enforceperm=True, skipquiet=True): |
| 1722 return self.cl.history(self.nodeid, | 1721 return self.cl.history(self.nodeid, |
| 1723 enforceperm=enforceperm, | 1722 enforceperm=enforceperm, |
