/usr/lib/python2.7/dist-packages/openpyxl/descriptors/base.py is in python-openpyxl 2.3.0-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 242 243 244 245 246 247 | from __future__ import absolute_import
# Copyright (c) 2010-2015 openpyxl
"""
Based on Python Cookbook 3rd Edition, 8.13
http://chimera.labs.oreilly.com/books/1230000000393/ch08.html#_discussion_130
"""
import re
from openpyxl.compat import basestring, bytes, long
from openpyxl.xml.functions import Element
from .namespace import namespaced
class Descriptor(object):
def __init__(self, name=None, **kw):
self.name = name
for k, v in kw.items():
setattr(self, k, v)
def __set__(self, instance, value):
instance.__dict__[self.name] = value
class Typed(Descriptor):
"""Values must of a particular type"""
expected_type = type(None)
allow_none = False
nested = False
def __init__(self, *args, **kw):
super(Typed, self).__init__(*args, **kw)
self.__doc__ = "Values must be of type {0}".format(self.expected_type)
def __set__(self, instance, value):
if not isinstance(value, self.expected_type):
if (not self.allow_none
or (self.allow_none and value is not None)):
raise TypeError('expected ' + str(self.expected_type))
super(Typed, self).__set__(instance, value)
def __repr__(self):
return self.__doc__
def _convert(expected_type, value):
"""
Check value is of or can be converted to expected type.
"""
if not isinstance(value, expected_type):
try:
value = expected_type(value)
except:
raise TypeError('expected ' + str(expected_type))
return value
class Convertible(Typed):
"""Values must be convertible to a particular type"""
def __set__(self, instance, value):
if ((self.allow_none and value is not None)
or not self.allow_none):
value = _convert(self.expected_type, value)
super(Convertible, self).__set__(instance, value)
class Max(Convertible):
"""Values must be less than a `max` value"""
expected_type = float
allow_none = False
def __init__(self, **kw):
if 'max' not in kw and not hasattr(self, 'max'):
raise TypeError('missing max value')
super(Max, self).__init__(**kw)
def __set__(self, instance, value):
if ((self.allow_none and value is not None)
or not self.allow_none):
value = _convert(self.expected_type, value)
if value > self.max:
raise ValueError('Max value is {0}'.format(self.max))
super(Max, self).__set__(instance, value)
class Min(Convertible):
"""Values must be greater than a `min` value"""
expected_type = float
allow_none = False
def __init__(self, **kw):
if 'min' not in kw and not hasattr(self, 'min'):
raise TypeError('missing min value')
super(Min, self).__init__(**kw)
def __set__(self, instance, value):
if ((self.allow_none and value is not None)
or not self.allow_none):
value = _convert(self.expected_type, value)
if value < self.min:
raise ValueError('Min value is {0}'.format(self.min))
super(Min, self).__set__(instance, value)
class MinMax(Min, Max):
"""Values must be greater than `min` value and less than a `max` one"""
pass
class Set(Descriptor):
"""Value can only be from a set of know values"""
def __init__(self, name=None, **kw):
if not 'values' in kw:
raise TypeError("missing set of values")
kw['values'] = set(kw['values'])
super(Set, self).__init__(name, **kw)
self.__doc__ = "Value must be one of {0}".format(self.values)
def __set__(self, instance, value):
if value not in self.values:
raise ValueError(self.__doc__)
super(Set, self).__set__(instance, value)
class NoneSet(Set):
"""'none' will be treated as None"""
def __init__(self, name=None, **kw):
super(NoneSet, self).__init__(name, **kw)
self.values.add(None)
def __set__(self, instance, value):
if value == 'none':
value = None
super(NoneSet, self).__set__(instance, value)
class Integer(Convertible):
expected_type = long
class Float(Convertible):
expected_type = float
class Bool(Convertible):
expected_type = bool
def __set__(self, instance, value):
if isinstance(value, str):
if value in ('false', 'f', '0'):
value = False
super(Bool, self).__set__(instance, value)
class String(Typed):
expected_type = basestring
class ASCII(Typed):
expected_type = bytes
class Tuple(Typed):
expected_type = tuple
class Length(Descriptor):
def __init__(self, name=None, **kw):
if "length" not in kw:
raise TypeError("value length must be supplied")
super(Length, self).__init__(**kw)
def __set__(self, instance, value):
if len(value) != self.length:
raise ValueError("Value must be length {0}".format(self.length))
super(Length, self).__set__(instance, value)
class Default(Typed):
"""
When called returns an instance of the expected type.
Additional default values can be passed in to the descriptor
"""
def __init__(self, name=None, **kw):
if "defaults" not in kw:
kw['defaults'] = {}
super(Default, self).__init__(**kw)
def __call__(self):
return self.expected_type()
class Alias(Descriptor):
"""
Aliases can be used when either the desired attribute name is not allowed
or confusing in Python (eg. "type") or a more descriptve name is desired
(eg. "underline" for "u")
"""
def __init__(self, alias):
self.alias = alias
def __set__(self, instance, value):
setattr(instance, self.alias, value)
def __get__(self, instance, cls):
return getattr(instance, self.alias)
class MatchPattern(Descriptor):
"""Values must match a regex pattern """
allow_none = False
def __init__(self, name=None, **kw):
if 'pattern' not in kw and not hasattr(self, 'pattern'):
raise TypeError('missing pattern value')
super(MatchPattern, self).__init__(name, **kw)
self.test_pattern = re.compile(self.pattern)
def __set__(self, instance, value):
if ((self.allow_none and value is not None)
or not self.allow_none):
if not self.test_pattern.match(value):
raise ValueError('Value does not match pattern {0}'.format(self.pattern))
super(MatchPattern, self).__set__(instance, value)
|