/usr/lib/python2.7/dist-packages/sqlobject/include/hashcol.py is in python-sqlobject 3.1.0+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | import sqlobject.col
from sqlobject.compat import PY2, string_type
__all__ = ['HashCol']
class DbHash:
""" Presents a comparison object for hashes, allowing plain text to be
automagically compared with the base content. """
def __init__(self, hash, hashMethod):
self.hash = hash
self.hashMethod = hashMethod
def _get_key(self, other):
"""Create the hash of the other class"""
if not isinstance(other, string_type):
raise TypeError(
"A hash may only be compared with a string, or None.")
return self.hashMethod(other)
def __eq__(self, other):
if other is None:
if self.hash is None:
return True
return False
other_key = self._get_key(other)
return other_key == self.hash
def __lt__(self, other):
if other is None:
return False
other_key = self._get_key(other)
return other_key < self.hash
def __gt__(self, other):
if other is None:
if self.hash is None:
return False
return True
other_key = self._get_key(other)
return other_key > self.hash
def __le__(self, other):
if other is None:
if self.hash is None:
return True
return False
other_key = self._get_key(other)
return other_key <= self.hash
def __ge__(self, other):
if other is None:
return True
other_key = self._get_key(other)
return other_key >= self.hash
def __repr__(self):
return "<DbHash>"
class HashValidator(sqlobject.col.StringValidator):
""" Provides formal SQLObject validation services for the HashCol. """
def to_python(self, value, state):
""" Passes out a hash object. """
if value is None:
return None
return DbHash(hash=value, hashMethod=self.hashMethod)
def from_python(self, value, state):
""" Store the given value as a MD5 hash, or None if specified. """
if value is None:
return None
return self.hashMethod(value)
class SOHashCol(sqlobject.col.SOStringCol):
""" The internal HashCol definition. By default, enforces a md5 digest. """
def __init__(self, **kw):
if 'hashMethod' not in kw:
from hashlib import md5
if PY2:
self.hashMethod = lambda v: md5(v).hexdigest()
else:
self.hashMethod = lambda v: md5(v.encode('utf8')).hexdigest()
if 'length' not in kw:
kw['length'] = 32
else:
self.hashMethod = kw['hashMethod']
del kw['hashMethod']
super(SOHashCol, self).__init__(**kw)
def createValidators(self):
return [HashValidator(name=self.name, hashMethod=self.hashMethod)] + \
super(SOHashCol, self).createValidators()
class HashCol(sqlobject.col.StringCol):
""" End-user HashCol class. May be instantiated with 'hashMethod', a function
which returns the string hash of any other string (i.e. basestring). """
baseClass = SOHashCol
|