/usr/share/pyshared/bitfield/models.py is in python-django-bitfield 1.6.4-1.
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | from django.db.models import signals
from django.db.models.sql.expressions import SQLEvaluator
from django.db.models.fields import Field, BigIntegerField
from django.db.models.fields.subclassing import Creator
try:
from django.db.models.fields.subclassing import SubfieldBase
except ImportError:
# django 1.2
from django.db.models.fields.subclassing import LegacyConnection as SubfieldBase # NOQA
from bitfield.forms import BitFormField
from bitfield.query import BitQueryLookupWrapper
from bitfield.types import BitHandler, Bit
# Count binary capacity. Truncate "0b" prefix from binary form.
# Twice faster than bin(i)[2:] or math.floor(math.log(i))
MAX_FLAG_COUNT = int(len(bin(BigIntegerField.MAX_BIGINT)) - 2)
class BitFieldFlags(object):
def __init__(self, flags):
if len(flags) > MAX_FLAG_COUNT:
raise ValueError('Too many flags')
self._flags = flags
def __repr__(self):
return repr(self._flags)
def __iter__(self):
for flag in self._flags:
yield flag
def __getattr__(self, key):
if key not in self._flags:
raise AttributeError
return Bit(self._flags.index(key))
def iteritems(self):
for flag in self._flags:
yield flag, Bit(self._flags.index(flag))
def iterkeys(self):
for flag in self._flags:
yield flag
def itervalues(self):
for flag in self._flags:
yield Bit(self._flags.index(flag))
def items(self):
return list(self.iteritems())
def keys(self):
return list(self.iterkeys())
def values(self):
return list(self.itervalues())
class BitFieldCreator(Creator):
"""
Descriptor for BitFields. Checks to make sure that all flags of the
instance match the class. This is to handle the case when caching
an older version of the instance and a newer version of the class is
available (usually during deploys).
"""
def __get__(self, obj, type=None):
if obj is None:
return BitFieldFlags(self.field.flags)
retval = obj.__dict__[self.field.name]
if self.field.__class__ is BitField:
# Update flags from class in case they've changed.
retval._keys = self.field.flags
return retval
class BitFieldMeta(SubfieldBase):
"""
Modified SubFieldBase to use our contribute_to_class method (instead of
monkey-patching make_contrib). This uses our BitFieldCreator descriptor
in place of the default.
NOTE: If we find ourselves needing custom descriptors for fields, we could
make this generic.
"""
def __new__(cls, name, bases, attrs):
def contribute_to_class(self, cls, name):
BigIntegerField.contribute_to_class(self, cls, name)
setattr(cls, self.name, BitFieldCreator(self))
new_class = super(BitFieldMeta, cls).__new__(cls, name, bases, attrs)
new_class.contribute_to_class = contribute_to_class
return new_class
class BitField(BigIntegerField):
__metaclass__ = BitFieldMeta
def __init__(self, flags, default=None, *args, **kwargs):
if isinstance(flags, dict):
# Get only integer keys in correct range
valid_keys = (k for k in flags.keys() if isinstance(k, int) and (0 <= k < MAX_FLAG_COUNT))
if not valid_keys:
raise ValueError('Wrong keys or empty dictionary')
# Fill list with values from dict or with empty values
flags = [flags.get(i, '') for i in range(max(valid_keys) + 1)]
if len(flags) > MAX_FLAG_COUNT:
raise ValueError('Too many flags')
if isinstance(default, (list, tuple, set, frozenset)):
new_value = 0
for flag in default:
new_value |= Bit(flags.index(flag))
default = new_value
BigIntegerField.__init__(self, default=default, *args, **kwargs)
self.flags = flags
def south_field_triple(self):
"Returns a suitable description of this field for South."
from south.modelsinspector import introspector
field_class = "django.db.models.fields.BigIntegerField"
args, kwargs = introspector(self)
return (field_class, args, kwargs)
def formfield(self, form_class=BitFormField, **kwargs):
return Field.formfield(self, form_class, choices=[(k, k) for k in self.flags], **kwargs)
def pre_save(self, instance, add):
value = getattr(instance, self.attname)
return value
def get_prep_value(self, value):
if isinstance(value, (BitHandler, Bit)):
value = value.mask
return int(value)
# def get_db_prep_save(self, value, connection):
# if isinstance(value, Bit):
# return BitQuerySaveWrapper(self.model._meta.db_table, self.name, value)
# return super(BitField, self).get_db_prep_save(value, connection=connection)
def get_db_prep_lookup(self, lookup_type, value, connection, prepared=False):
if isinstance(value, SQLEvaluator) and isinstance(value.expression, Bit):
value = value.expression
if isinstance(value, (BitHandler, Bit)):
return BitQueryLookupWrapper(self.model._meta.db_table, self.db_column or self.name, value)
return BigIntegerField.get_db_prep_lookup(self, lookup_type=lookup_type, value=value,
connection=connection, prepared=prepared)
def get_prep_lookup(self, lookup_type, value):
if isinstance(value, SQLEvaluator) and isinstance(value.expression, Bit):
value = value.expression
if isinstance(value, Bit):
if lookup_type in ('exact',):
return value
raise TypeError('Lookup type %r not supported with `Bit` type.' % lookup_type)
return BigIntegerField.get_prep_lookup(self, lookup_type, value)
def to_python(self, value):
if isinstance(value, Bit):
value = value.mask
if not isinstance(value, BitHandler):
# Regression for #1425: fix bad data that was created resulting
# in negative values for flags. Compute the value that would
# have been visible ot the application to preserve compatibility.
if isinstance(value, (int, long)) and value < 0:
new_value = 0
for bit_number, _ in enumerate(self.flags):
new_value |= (value & (2 ** bit_number))
value = new_value
value = BitHandler(value, self.flags)
else:
# Ensure flags are consistent for unpickling
value._keys = self.flags
return value
class CompositeBitFieldWrapper(object):
def __init__(self, fields):
self.fields = fields
def __getattr__(self, attr):
if attr == 'fields':
return super(CompositeBitFieldWrapper, self).__getattr__(attr)
for field in self.fields:
if hasattr(field, attr):
return getattr(field, attr)
raise AttributeError('%s is not a valid flag' % attr)
def __hasattr__(self, attr):
if attr == 'fields':
return super(CompositeBitFieldWrapper, self).__hasattr__(attr)
for field in self.fields:
if hasattr(field, attr):
return True
return False
def __setattr__(self, attr, value):
if attr == 'fields':
super(CompositeBitFieldWrapper, self).__setattr__(attr, value)
return
for field in self.fields:
if hasattr(field, attr):
setattr(field, attr, value)
return
raise AttributeError('%s is not a valid flag' % attr)
class CompositeBitField(object):
def __init__(self, fields):
self.fields = fields
def contribute_to_class(self, cls, name):
self.name = name
self.model = cls
cls._meta.add_virtual_field(self)
signals.class_prepared.connect(self.validate_fields, sender=cls)
setattr(cls, name, self)
def validate_fields(self, sender, **kwargs):
cls = sender
model_fields = dict([
(f.name, f) for f in cls._meta.fields if f.name in self.fields])
all_flags = sum([model_fields[f].flags for f in self.fields], ())
if len(all_flags) != len(set(all_flags)):
raise ValueError('BitField flags must be unique.')
def __get__(self, instance, instance_type=None):
fields = [getattr(instance, f) for f in self.fields]
return CompositeBitFieldWrapper(fields)
def __set__(self, *args, **kwargs):
raise NotImplementedError('CompositeBitField cannot be set.')
|