/usr/lib/python2.7/dist-packages/oslo_versionedobjects/fields.py is in python-oslo.versionedobjects 1.17.0-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 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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 | # Copyright 2013 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import abc
import datetime
from distutils import versionpredicate
import re
import uuid
import warnings
import copy
import iso8601
import netaddr
from oslo_utils import strutils
from oslo_utils import timeutils
import six
from oslo_versionedobjects._i18n import _, _LE
from oslo_versionedobjects import _utils
from oslo_versionedobjects import exception
class KeyTypeError(TypeError):
def __init__(self, expected, value):
super(KeyTypeError, self).__init__(
_('Key %(key)s must be of type %(expected)s not %(actual)s'
) % {'key': repr(value),
'expected': expected.__name__,
'actual': value.__class__.__name__,
})
class ElementTypeError(TypeError):
def __init__(self, expected, key, value):
super(ElementTypeError, self).__init__(
_('Element %(key)s:%(val)s must be of type %(expected)s'
' not %(actual)s'
) % {'key': key,
'val': repr(value),
'expected': expected,
'actual': value.__class__.__name__,
})
@six.add_metaclass(abc.ABCMeta)
class AbstractFieldType(object):
@abc.abstractmethod
def coerce(self, obj, attr, value):
"""This is called to coerce (if possible) a value on assignment.
This method should convert the value given into the designated type,
or throw an exception if this is not possible.
:param:obj: The VersionedObject on which an attribute is being set
:param:attr: The name of the attribute being set
:param:value: The value being set
:returns: A properly-typed value
"""
pass
@abc.abstractmethod
def from_primitive(self, obj, attr, value):
"""This is called to deserialize a value.
This method should deserialize a value from the form given by
to_primitive() to the designated type.
:param:obj: The VersionedObject on which the value is to be set
:param:attr: The name of the attribute which will hold the value
:param:value: The serialized form of the value
:returns: The natural form of the value
"""
pass
@abc.abstractmethod
def to_primitive(self, obj, attr, value):
"""This is called to serialize a value.
This method should serialize a value to the form expected by
from_primitive().
:param:obj: The VersionedObject on which the value is set
:param:attr: The name of the attribute holding the value
:param:value: The natural form of the value
:returns: The serialized form of the value
"""
pass
@abc.abstractmethod
def describe(self):
"""Returns a string describing the type of the field."""
pass
@abc.abstractmethod
def stringify(self, value):
"""Returns a short stringified version of a value."""
pass
class FieldType(AbstractFieldType):
@staticmethod
def coerce(obj, attr, value):
return value
@staticmethod
def from_primitive(obj, attr, value):
return value
@staticmethod
def to_primitive(obj, attr, value):
return value
def describe(self):
return self.__class__.__name__
def stringify(self, value):
return str(value)
def get_schema(self):
raise NotImplementedError()
class UnspecifiedDefault(object):
pass
class Field(object):
def __init__(self, field_type, nullable=False,
default=UnspecifiedDefault, read_only=False):
self._type = field_type
self._nullable = nullable
self._default = default
self._read_only = read_only
def __repr__(self):
return '%s(default=%s,nullable=%s)' % (self._type.__class__.__name__,
self._default, self._nullable)
@property
def nullable(self):
return self._nullable
@property
def default(self):
return self._default
@property
def read_only(self):
return self._read_only
def _null(self, obj, attr):
if self.nullable:
return None
elif self._default != UnspecifiedDefault:
# NOTE(danms): We coerce the default value each time the field
# is set to None as our contract states that we'll let the type
# examine the object and attribute name at that time.
return self._type.coerce(obj, attr, copy.deepcopy(self._default))
else:
raise ValueError(_("Field `%s' cannot be None") % attr)
def coerce(self, obj, attr, value):
"""Coerce a value to a suitable type.
This is called any time you set a value on an object, like:
foo.myint = 1
and is responsible for making sure that the value (1 here) is of
the proper type, or can be sanely converted.
This also handles the potentially nullable or defaultable
nature of the field and calls the coerce() method on a
FieldType to actually do the coercion.
:param:obj: The object being acted upon
:param:attr: The name of the attribute/field being set
:param:value: The value being set
:returns: The properly-typed value
"""
if value is None:
return self._null(obj, attr)
else:
return self._type.coerce(obj, attr, value)
def from_primitive(self, obj, attr, value):
"""Deserialize a value from primitive form.
This is responsible for deserializing a value from primitive
into regular form. It calls the from_primitive() method on a
FieldType to do the actual deserialization.
:param:obj: The object being acted upon
:param:attr: The name of the attribute/field being deserialized
:param:value: The value to be deserialized
:returns: The deserialized value
"""
if value is None:
return None
else:
return self._type.from_primitive(obj, attr, value)
def to_primitive(self, obj, attr, value):
"""Serialize a value to primitive form.
This is responsible for serializing a value to primitive
form. It calls to_primitive() on a FieldType to do the actual
serialization.
:param:obj: The object being acted upon
:param:attr: The name of the attribute/field being serialized
:param:value: The value to be serialized
:returns: The serialized value
"""
if value is None:
return None
else:
return self._type.to_primitive(obj, attr, value)
def describe(self):
"""Return a short string describing the type of this field."""
name = self._type.describe()
prefix = self.nullable and 'Nullable' or ''
return prefix + name
def stringify(self, value):
if value is None:
return 'None'
else:
return self._type.stringify(value)
def get_schema(self):
schema = self._type.get_schema()
schema.update({'readonly': self.read_only})
if self.nullable:
schema['type'].append('null')
default = self.default
if default != UnspecifiedDefault:
schema.update({'default': default})
return schema
class String(FieldType):
@staticmethod
def coerce(obj, attr, value):
# FIXME(danms): We should really try to avoid the need to do this
accepted_types = six.integer_types + (float, six.string_types,
datetime.datetime)
if isinstance(value, accepted_types):
return six.text_type(value)
else:
raise ValueError(_('A string is required in field %(attr)s, '
'not a %(type)s') %
{'attr': attr, 'type': type(value).__name__})
@staticmethod
def stringify(value):
return '\'%s\'' % value
def get_schema(self):
return {'type': ['string']}
class SensitiveString(String):
"""A string field type that may contain sensitive (password) information.
Passwords in the string value are masked when stringified.
"""
def stringify(self, value):
return super(SensitiveString, self).stringify(
strutils.mask_password(value))
class VersionPredicate(String):
@staticmethod
def coerce(obj, attr, value):
try:
versionpredicate.VersionPredicate('check (%s)' % value)
except ValueError:
raise ValueError(_('Version %(val)s is not a valid predicate in '
'field %(attr)s') %
{'val': value, 'attr': attr})
return value
class Enum(String):
def __init__(self, valid_values, **kwargs):
if not valid_values:
raise exception.EnumRequiresValidValuesError()
try:
# Test validity of the values
for value in valid_values:
super(Enum, self).coerce(None, 'init', value)
except (TypeError, ValueError):
raise exception.EnumValidValuesInvalidError()
self._valid_values = valid_values
super(Enum, self).__init__(**kwargs)
@property
def valid_values(self):
return copy.copy(self._valid_values)
def coerce(self, obj, attr, value):
if value not in self._valid_values:
msg = _("Field value %s is invalid") % value
raise ValueError(msg)
return super(Enum, self).coerce(obj, attr, value)
def stringify(self, value):
if value not in self._valid_values:
msg = _("Field value %s is invalid") % value
raise ValueError(msg)
return super(Enum, self).stringify(value)
def get_schema(self):
return {'enum': self._valid_values}
class UUID(FieldType):
_PATTERN = (r'^[a-fA-F0-9]{8}-?[a-fA-F0-9]{4}-?[a-fA-F0-9]{4}-?[a-fA-F0-9]'
r'{4}-?[a-fA-F0-9]{12}$')
@staticmethod
def coerce(obj, attr, value):
# FIXME(danms): We should actually verify the UUIDness here
with warnings.catch_warnings():
warnings.simplefilter("once")
try:
uuid.UUID(str(value))
except Exception:
# This is to ensure no breaking behaviour for current
# users
warnings.warn("%s is an invalid UUID. Using UUIDFields "
"with invalid UUIDs is no longer "
"supported, and will be removed in a future "
"release. Please update your "
"code to input valid UUIDs or accept "
"ValueErrors for invalid UUIDs. See "
"http://docs.openstack.org/developer/oslo.versionedobjects/api/fields.html#oslo_versionedobjects.fields.UUIDField " # noqa
"for further details" % value, FutureWarning)
return str(value)
def get_schema(self):
return {'type': ['string'], 'pattern': self._PATTERN}
class MACAddress(FieldType):
_PATTERN = r'^[0-9a-f]{2}(:[0-9a-f]{2}){5}$'
_REGEX = re.compile(_PATTERN)
@staticmethod
def coerce(obj, attr, value):
if isinstance(value, six.string_types):
lowered = value.lower().replace('-', ':')
if MACAddress._REGEX.match(lowered):
return lowered
raise ValueError(_LE("Malformed MAC %s"), value)
def get_schema(self):
return {'type': ['string'], 'pattern': self._PATTERN}
class PCIAddress(FieldType):
_PATTERN = r'^[0-9a-f]{4}:[0-9a-f]{2}:[0-1][0-9a-f].[0-7]$'
_REGEX = re.compile(_PATTERN)
@staticmethod
def coerce(obj, attr, value):
if isinstance(value, six.string_types):
newvalue = value.lower()
if PCIAddress._REGEX.match(newvalue):
return newvalue
raise ValueError(_LE("Malformed PCI address %s"), value)
def get_schema(self):
return {'type': ['string'], 'pattern': self._PATTERN}
class Integer(FieldType):
@staticmethod
def coerce(obj, attr, value):
return int(value)
def get_schema(self):
return {'type': ['integer']}
class NonNegativeInteger(FieldType):
@staticmethod
def coerce(obj, attr, value):
v = int(value)
if v < 0:
raise ValueError(_('Value must be >= 0 for field %s') % attr)
return v
class Float(FieldType):
def coerce(self, obj, attr, value):
return float(value)
def get_schema(self):
return {'type': ['number']}
class NonNegativeFloat(FieldType):
@staticmethod
def coerce(obj, attr, value):
v = float(value)
if v < 0:
raise ValueError(_('Value must be >= 0 for field %s') % attr)
return v
class Boolean(FieldType):
@staticmethod
def coerce(obj, attr, value):
return bool(value)
def get_schema(self):
return {'type': ['boolean']}
class FlexibleBoolean(Boolean):
@staticmethod
def coerce(obj, attr, value):
return strutils.bool_from_string(value)
class DateTime(FieldType):
def __init__(self, tzinfo_aware=True, *args, **kwargs):
self.tzinfo_aware = tzinfo_aware
super(DateTime, self).__init__(*args, **kwargs)
def coerce(self, obj, attr, value):
if isinstance(value, six.string_types):
# NOTE(danms): Being tolerant of isotime strings here will help us
# during our objects transition
value = timeutils.parse_isotime(value)
elif not isinstance(value, datetime.datetime):
raise ValueError(_('A datetime.datetime is required '
'in field %(attr)s, not a %(type)s') %
{'attr': attr, 'type': type(value).__name__})
if value.utcoffset() is None and self.tzinfo_aware:
# NOTE(danms): Legacy objects from sqlalchemy are stored in UTC,
# but are returned without a timezone attached.
# As a transitional aid, assume a tz-naive object is in UTC.
value = value.replace(tzinfo=iso8601.iso8601.Utc())
elif not self.tzinfo_aware:
value = value.replace(tzinfo=None)
return value
def from_primitive(self, obj, attr, value):
return self.coerce(obj, attr, timeutils.parse_isotime(value))
def get_schema(self):
return {'type': ['string'], 'format': 'date-time'}
@staticmethod
def to_primitive(obj, attr, value):
return _utils.isotime(value)
@staticmethod
def stringify(value):
return _utils.isotime(value)
class IPAddress(FieldType):
@staticmethod
def coerce(obj, attr, value):
try:
return netaddr.IPAddress(value)
except netaddr.AddrFormatError as e:
raise ValueError(six.text_type(e))
def from_primitive(self, obj, attr, value):
return self.coerce(obj, attr, value)
@staticmethod
def to_primitive(obj, attr, value):
return str(value)
class IPV4Address(IPAddress):
@staticmethod
def coerce(obj, attr, value):
result = IPAddress.coerce(obj, attr, value)
if result.version != 4:
raise ValueError(_('Network "%(val)s" is not valid '
'in field %(attr)s') %
{'val': value, 'attr': attr})
return result
def get_schema(self):
return {'type': ['string'], 'format': 'ipv4'}
class IPV6Address(IPAddress):
@staticmethod
def coerce(obj, attr, value):
result = IPAddress.coerce(obj, attr, value)
if result.version != 6:
raise ValueError(_('Network "%(val)s" is not valid '
'in field %(attr)s') %
{'val': value, 'attr': attr})
return result
def get_schema(self):
return {'type': ['string'], 'format': 'ipv6'}
class IPV4AndV6Address(IPAddress):
@staticmethod
def coerce(obj, attr, value):
result = IPAddress.coerce(obj, attr, value)
if result.version != 4 and result.version != 6:
raise ValueError(_('Network "%(val)s" is not valid '
'in field %(attr)s') %
{'val': value, 'attr': attr})
return result
def get_schema(self):
return {'type': ['string'], 'format': 'ipv6'}
class IPNetwork(IPAddress):
@staticmethod
def coerce(obj, attr, value):
try:
return netaddr.IPNetwork(value)
except netaddr.AddrFormatError as e:
raise ValueError(six.text_type(e))
class IPV4Network(IPNetwork):
_PATTERN = (r'^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-'
r'9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2]['
r'0-9]|3[0-2]))$')
@staticmethod
def coerce(obj, attr, value):
try:
return netaddr.IPNetwork(value, version=4)
except netaddr.AddrFormatError as e:
raise ValueError(six.text_type(e))
def get_schema(self):
return {'type': ['string'], 'pattern': self._PATTERN}
class IPV6Network(IPNetwork):
def __init__(self, *args, **kwargs):
super(IPV6Network, self).__init__(*args, **kwargs)
self._PATTERN = self._create_pattern()
@staticmethod
def coerce(obj, attr, value):
try:
return netaddr.IPNetwork(value, version=6)
except netaddr.AddrFormatError as e:
raise ValueError(six.text_type(e))
def _create_pattern(self):
ipv6seg = '[0-9a-fA-F]{1,4}'
ipv4seg = '(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])'
return (
# Pattern based on answer to
# http://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses
'^'
# 1:2:3:4:5:6:7:8
'(' + ipv6seg + ':){7,7}' + ipv6seg + '|'
# 1:: 1:2:3:4:5:6:7::
'(' + ipv6seg + ':){1,7}:|'
# 1::8 1:2:3:4:5:6::8 1:2:3:4:5:6::8
'(' + ipv6seg + ':){1,6}:' + ipv6seg + '|'
# 1::7:8 1:2:3:4:5::7:8 1:2:3:4:5::8
'(' + ipv6seg + ':){1,5}(:' + ipv6seg + '){1,2}|'
# 1::6:7:8 1:2:3:4::6:7:8 1:2:3:4::8
'(' + ipv6seg + ':){1,4}(:' + ipv6seg + '){1,3}|'
# 1::5:6:7:8 1:2:3::5:6:7:8 1:2:3::8
'(' + ipv6seg + ':){1,3}(:' + ipv6seg + '){1,4}|'
# 1::4:5:6:7:8 1:2::4:5:6:7:8 1:2::8
'(' + ipv6seg + ':){1,2}(:' + ipv6seg + '){1,5}|' +
# 1::3:4:5:6:7:8 1::3:4:5:6:7:8 1::8
ipv6seg + ':((:' + ipv6seg + '){1,6})|'
# ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::
':((:' + ipv6seg + '){1,7}|:)|'
# fe80::7:8%eth0 fe80::7:8%1
'fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|'
# ::255.255.255.255 ::ffff:255.255.255.255 ::ffff:0:255.255.255.255
'::(ffff(:0{1,4}){0,1}:){0,1}'
'(' + ipv4seg + '\.){3,3}' +
ipv4seg + '|'
# 2001:db8:3:4::192.0.2.33 64:ff9b::192.0.2.33
'(' + ipv6seg + ':){1,4}:'
'(' + ipv4seg + '\.){3,3}' +
ipv4seg +
# /128
'(\/(d|dd|1[0-1]d|12[0-8]))$'
)
def get_schema(self):
return {'type': ['string'], 'pattern': self._PATTERN}
class CompoundFieldType(FieldType):
def __init__(self, element_type, **field_args):
self._element_type = Field(element_type, **field_args)
class List(CompoundFieldType):
def coerce(self, obj, attr, value):
if not isinstance(value, list):
raise ValueError(_('A list is required in field %(attr)s, '
'not a %(type)s') %
{'attr': attr, 'type': type(value).__name__})
coerced_list = CoercedList()
coerced_list.enable_coercing(self._element_type, obj, attr)
coerced_list.extend(value)
return coerced_list
def to_primitive(self, obj, attr, value):
return [self._element_type.to_primitive(obj, attr, x) for x in value]
def from_primitive(self, obj, attr, value):
return [self._element_type.from_primitive(obj, attr, x) for x in value]
def stringify(self, value):
return '[%s]' % (
','.join([self._element_type.stringify(x) for x in value]))
def get_schema(self):
return {'type': ['array'], 'items': self._element_type.get_schema()}
class Dict(CompoundFieldType):
def coerce(self, obj, attr, value):
if not isinstance(value, dict):
raise ValueError(_('A dict is required in field %(attr)s, '
'not a %(type)s') %
{'attr': attr, 'type': type(value).__name__})
coerced_dict = CoercedDict()
coerced_dict.enable_coercing(self._element_type, obj, attr)
coerced_dict.update(value)
return coerced_dict
def to_primitive(self, obj, attr, value):
primitive = {}
for key, element in value.items():
primitive[key] = self._element_type.to_primitive(
obj, '%s["%s"]' % (attr, key), element)
return primitive
def from_primitive(self, obj, attr, value):
concrete = {}
for key, element in value.items():
concrete[key] = self._element_type.from_primitive(
obj, '%s["%s"]' % (attr, key), element)
return concrete
def stringify(self, value):
return '{%s}' % (
','.join(['%s=%s' % (key, self._element_type.stringify(val))
for key, val in sorted(value.items())]))
def get_schema(self):
return {'type': ['object']}
class DictProxyField(object):
"""Descriptor allowing us to assign pinning data as a dict of key_types
This allows us to have an object field that will be a dict of key_type
keys, allowing that will convert back to string-keyed dict.
This will take care of the conversion while the dict field will make sure
that we store the raw json-serializable data on the object.
key_type should return a type that unambiguously responds to six.text_type
so that calling key_type on it yields the same thing.
"""
def __init__(self, dict_field_name, key_type=int):
self._fld_name = dict_field_name
self._key_type = key_type
def __get__(self, obj, obj_type):
if obj is None:
return self
if getattr(obj, self._fld_name) is None:
return
return {self._key_type(k): v
for k, v in six.iteritems(getattr(obj, self._fld_name))}
def __set__(self, obj, val):
if val is None:
setattr(obj, self._fld_name, val)
else:
setattr(obj, self._fld_name,
{six.text_type(k): v for k, v in six.iteritems(val)})
class Set(CompoundFieldType):
def coerce(self, obj, attr, value):
if not isinstance(value, set):
raise ValueError(_('A set is required in field %(attr)s, '
'not a %(type)s') %
{'attr': attr, 'type': type(value).__name__})
coerced_set = CoercedSet()
coerced_set.enable_coercing(self._element_type, obj, attr)
coerced_set.update(value)
return coerced_set
def to_primitive(self, obj, attr, value):
return tuple(
self._element_type.to_primitive(obj, attr, x) for x in value)
def from_primitive(self, obj, attr, value):
return set([self._element_type.from_primitive(obj, attr, x)
for x in value])
def stringify(self, value):
return 'set([%s])' % (
','.join([self._element_type.stringify(x) for x in value]))
def get_schema(self):
return {'type': ['array'], 'uniqueItems': True,
'items': self._element_type.get_schema()}
class Object(FieldType):
def __init__(self, obj_name, subclasses=False, **kwargs):
self._obj_name = obj_name
self._subclasses = subclasses
super(Object, self).__init__(**kwargs)
@staticmethod
def _get_all_obj_names(obj):
obj_names = []
for parent in obj.__class__.mro():
# Skip mix-ins which are not versioned object subclasses
if not hasattr(parent, "obj_name"):
continue
obj_names.append(parent.obj_name())
return obj_names
def coerce(self, obj, attr, value):
try:
obj_name = value.obj_name()
except AttributeError:
obj_name = ""
if self._subclasses:
obj_names = self._get_all_obj_names(value)
else:
obj_names = [obj_name]
if self._obj_name not in obj_names:
if not obj_name:
# If we're not dealing with an object, it's probably a
# primitive so get it's type for the message below.
obj_name = type(value).__name__
obj_mod = ''
if hasattr(obj, '__module__'):
obj_mod = ''.join([obj.__module__, '.'])
val_mod = ''
if hasattr(value, '__module__'):
val_mod = ''.join([value.__module__, '.'])
raise ValueError(_('An object of type %(type)s is required '
'in field %(attr)s, not a %(valtype)s') %
{'type': ''.join([obj_mod, self._obj_name]),
'attr': attr, 'valtype': ''.join([val_mod,
obj_name])})
return value
@staticmethod
def to_primitive(obj, attr, value):
return value.obj_to_primitive()
@staticmethod
def from_primitive(obj, attr, value):
# FIXME(danms): Avoid circular import from base.py
from oslo_versionedobjects import base as obj_base
# NOTE (ndipanov): If they already got hydrated by the serializer, just
# pass them back unchanged
if isinstance(value, obj_base.VersionedObject):
return value
return obj.obj_from_primitive(value, obj._context)
def describe(self):
return "Object<%s>" % self._obj_name
def stringify(self, value):
if 'uuid' in value.fields:
ident = '(%s)' % (value.obj_attr_is_set('uuid') and value.uuid or
'UNKNOWN')
elif 'id' in value.fields:
ident = '(%s)' % (value.obj_attr_is_set('id') and value.id or
'UNKNOWN')
else:
ident = ''
return '%s%s' % (self._obj_name, ident)
class AutoTypedField(Field):
AUTO_TYPE = None
def __init__(self, **kwargs):
super(AutoTypedField, self).__init__(self.AUTO_TYPE, **kwargs)
class StringField(AutoTypedField):
AUTO_TYPE = String()
class SensitiveStringField(AutoTypedField):
"""Field type that masks passwords when the field is stringified."""
AUTO_TYPE = SensitiveString()
class VersionPredicateField(AutoTypedField):
AUTO_TYPE = VersionPredicate()
class BaseEnumField(AutoTypedField):
'''Base class for all enum field types
This class should not be directly instantiated. Instead
subclass it and set AUTO_TYPE to be a SomeEnum()
where SomeEnum is a subclass of Enum.
'''
def __init__(self, **kwargs):
if self.AUTO_TYPE is None:
raise exception.EnumFieldUnset(
fieldname=self.__class__.__name__)
if not isinstance(self.AUTO_TYPE, Enum):
raise exception.EnumFieldInvalid(
typename=self.AUTO_TYPE.__class__.__name__,
fieldname=self.__class__.__name__)
super(BaseEnumField, self).__init__(**kwargs)
def __repr__(self):
valid_values = self._type.valid_values
args = {
'nullable': self._nullable,
'default': self._default,
}
args.update({'valid_values': valid_values})
return '%s(%s)' % (self._type.__class__.__name__,
','.join(['%s=%s' % (k, v)
for k, v in sorted(args.items())]))
@property
def valid_values(self):
"""Return the list of valid values for the field."""
return self._type.valid_values
class EnumField(BaseEnumField):
'''Anonymous enum field type
This class allows for anonymous enum types to be
declared, simply by passing in a list of valid values
to its constructor. It is generally preferrable though,
to create an explicit named enum type by sub-classing
the BaseEnumField type directly.
'''
def __init__(self, valid_values, **kwargs):
self.AUTO_TYPE = Enum(valid_values=valid_values)
super(EnumField, self).__init__(**kwargs)
class StateMachine(EnumField):
"""A mixin that can be applied to an EnumField to enforce a state machine
e.g: Setting the code below on a field will ensure an object cannot
transition from ERROR to ACTIVE
:example:
.. code-block:: python
class FakeStateMachineField(fields.EnumField, fields.StateMachine):
ACTIVE = 'ACTIVE'
PENDING = 'PENDING'
ERROR = 'ERROR'
DELETED = 'DELETED'
ALLOWED_TRANSITIONS = {
ACTIVE: {
PENDING,
ERROR,
DELETED,
},
PENDING: {
ACTIVE,
ERROR
},
ERROR: {
PENDING,
},
DELETED: {} # This is a terminal state
}
_TYPES = (ACTIVE, PENDING, ERROR, DELETED)
def __init__(self, **kwargs):
super(FakeStateMachineField, self).__init__(
self._TYPES, **kwargs)
"""
# This is dict of states, that have dicts of states an object is
# allowed to transition to
ALLOWED_TRANSITIONS = {}
def _my_name(self, obj):
for name, field in obj.fields.items():
if field == self:
return name
return 'unknown'
def coerce(self, obj, attr, value):
super(StateMachine, self).coerce(obj, attr, value)
my_name = self._my_name(obj)
msg = _("%(object)s.%(name)s is not allowed to transition out of "
"%(value)s state")
if attr in obj:
current_value = getattr(obj, attr)
else:
return value
if current_value in self.ALLOWED_TRANSITIONS:
if value in self.ALLOWED_TRANSITIONS[current_value]:
return value
else:
msg = _(
"%(object)s.%(name)s is not allowed to transition out of "
"'%(current_value)s' state to '%(value)s' state, choose "
"from %(options)r")
msg = msg % {
'object': obj.obj_name(),
'name': my_name,
'current_value': current_value,
'value': value,
'options': [x for x in self.ALLOWED_TRANSITIONS[current_value]]
}
raise ValueError(msg)
class UUIDField(AutoTypedField):
"""UUID Field Type
.. warning::
This class does not actually validate UUIDs. This will happen in a
future major version of oslo.versionedobjects
To validate that you have valid UUIDs you need to do the following in
your own objects/fields.py
:Example:
.. code-block:: python
import oslo_versionedobjects.fields as ovo_fields
class UUID(ovo_fields.UUID):
def coerce(self, obj, attr, value):
uuid.UUID(value)
return str(value)
class UUIDField(ovo_fields.AutoTypedField):
AUTO_TYPE = UUID()
and then in your objects use
``<your_projects>.object.fields.UUIDField``.
This will become default behaviour in the future.
"""
AUTO_TYPE = UUID()
class MACAddressField(AutoTypedField):
AUTO_TYPE = MACAddress()
class PCIAddressField(AutoTypedField):
AUTO_TYPE = PCIAddress()
class IntegerField(AutoTypedField):
AUTO_TYPE = Integer()
class NonNegativeIntegerField(AutoTypedField):
AUTO_TYPE = NonNegativeInteger()
class FloatField(AutoTypedField):
AUTO_TYPE = Float()
class NonNegativeFloatField(AutoTypedField):
AUTO_TYPE = NonNegativeFloat()
# This is a strict interpretation of boolean
# values using Python's semantics for truth/falsehood
class BooleanField(AutoTypedField):
AUTO_TYPE = Boolean()
# This is a flexible interpretation of boolean
# values using common user friendly semantics for
# truth/falsehood. ie strings like 'yes', 'no',
# 'on', 'off', 't', 'f' get mapped to values you
# would expect.
class FlexibleBooleanField(AutoTypedField):
AUTO_TYPE = FlexibleBoolean()
class DateTimeField(AutoTypedField):
def __init__(self, tzinfo_aware=True, **kwargs):
self.AUTO_TYPE = DateTime(tzinfo_aware=tzinfo_aware)
super(DateTimeField, self).__init__(**kwargs)
class DictOfStringsField(AutoTypedField):
AUTO_TYPE = Dict(String())
class DictOfNullableStringsField(AutoTypedField):
AUTO_TYPE = Dict(String(), nullable=True)
class DictOfIntegersField(AutoTypedField):
AUTO_TYPE = Dict(Integer())
class ListOfStringsField(AutoTypedField):
AUTO_TYPE = List(String())
class DictOfListOfStringsField(AutoTypedField):
AUTO_TYPE = Dict(List(String()))
class ListOfEnumField(AutoTypedField):
def __init__(self, valid_values, **kwargs):
self.AUTO_TYPE = List(Enum(valid_values))
super(ListOfEnumField, self).__init__(**kwargs)
def __repr__(self):
valid_values = self._type._element_type._type.valid_values
args = {
'nullable': self._nullable,
'default': self._default,
}
args.update({'valid_values': valid_values})
return '%s(%s)' % (self._type.__class__.__name__,
','.join(['%s=%s' % (k, v)
for k, v in sorted(args.items())]))
class SetOfIntegersField(AutoTypedField):
AUTO_TYPE = Set(Integer())
class ListOfSetsOfIntegersField(AutoTypedField):
AUTO_TYPE = List(Set(Integer()))
class ListOfIntegersField(AutoTypedField):
AUTO_TYPE = List(Integer())
class ListOfDictOfNullableStringsField(AutoTypedField):
AUTO_TYPE = List(Dict(String(), nullable=True))
class ObjectField(AutoTypedField):
def __init__(self, objtype, subclasses=False, **kwargs):
self.AUTO_TYPE = Object(objtype, subclasses)
self.objname = objtype
super(ObjectField, self).__init__(**kwargs)
class ListOfObjectsField(AutoTypedField):
def __init__(self, objtype, subclasses=False, **kwargs):
self.AUTO_TYPE = List(Object(objtype, subclasses))
self.objname = objtype
super(ListOfObjectsField, self).__init__(**kwargs)
class IPAddressField(AutoTypedField):
AUTO_TYPE = IPAddress()
class IPV4AddressField(AutoTypedField):
AUTO_TYPE = IPV4Address()
class IPV6AddressField(AutoTypedField):
AUTO_TYPE = IPV6Address()
class IPV4AndV6AddressField(AutoTypedField):
AUTO_TYPE = IPV4AndV6Address()
class IPNetworkField(AutoTypedField):
AUTO_TYPE = IPNetwork()
class IPV4NetworkField(AutoTypedField):
AUTO_TYPE = IPV4Network()
class IPV6NetworkField(AutoTypedField):
AUTO_TYPE = IPV6Network()
class CoercedCollectionMixin(object):
def __init__(self, *args, **kwargs):
self._element_type = None
self._obj = None
self._field = None
super(CoercedCollectionMixin, self).__init__(*args, **kwargs)
def enable_coercing(self, element_type, obj, field):
self._element_type = element_type
self._obj = obj
self._field = field
class CoercedList(CoercedCollectionMixin, list):
"""List which coerces its elements
List implementation which overrides all element-adding methods and
coercing the element(s) being added to the required element type
"""
def _coerce_item(self, index, item):
if hasattr(self, "_element_type") and self._element_type is not None:
att_name = "%s[%i]" % (self._field, index)
return self._element_type.coerce(self._obj, att_name, item)
else:
return item
def __setitem__(self, i, y):
if type(i) is slice: # compatibility with py3 and [::] slices
start = i.start or 0
step = i.step or 1
coerced_items = [self._coerce_item(start + index * step, item)
for index, item in enumerate(y)]
super(CoercedList, self).__setitem__(i, coerced_items)
else:
super(CoercedList, self).__setitem__(i, self._coerce_item(i, y))
def append(self, x):
super(CoercedList, self).append(self._coerce_item(len(self) + 1, x))
def extend(self, t):
l = len(self)
coerced_items = [self._coerce_item(l + index, item)
for index, item in enumerate(t)]
super(CoercedList, self).extend(coerced_items)
def insert(self, i, x):
super(CoercedList, self).insert(i, self._coerce_item(i, x))
def __iadd__(self, y):
l = len(self)
coerced_items = [self._coerce_item(l + index, item)
for index, item in enumerate(y)]
return super(CoercedList, self).__iadd__(coerced_items)
def __setslice__(self, i, j, y):
coerced_items = [self._coerce_item(i + index, item)
for index, item in enumerate(y)]
return super(CoercedList, self).__setslice__(i, j, coerced_items)
class CoercedDict(CoercedCollectionMixin, dict):
"""Dict which coerces its values
Dict implementation which overrides all element-adding methods and
coercing the element(s) being added to the required element type
"""
def _coerce_dict(self, d):
res = {}
for key, element in six.iteritems(d):
res[key] = self._coerce_item(key, element)
return res
def _coerce_item(self, key, item):
if not isinstance(key, six.string_types):
# NOTE(guohliu) In order to keep compatibility with python3
# we need to use six.string_types rather than basestring here,
# since six.string_types is a tuple, so we need to pass the
# real type in.
raise KeyTypeError(six.string_types[0], key)
if hasattr(self, "_element_type") and self._element_type is not None:
att_name = "%s[%s]" % (self._field, key)
return self._element_type.coerce(self._obj, att_name, item)
else:
return item
def __setitem__(self, key, value):
super(CoercedDict, self).__setitem__(key,
self._coerce_item(key, value))
def update(self, other=None, **kwargs):
if other is not None:
super(CoercedDict, self).update(self._coerce_dict(other),
**self._coerce_dict(kwargs))
else:
super(CoercedDict, self).update(**self._coerce_dict(kwargs))
def setdefault(self, key, default=None):
return super(CoercedDict, self).setdefault(key,
self._coerce_item(key,
default))
class CoercedSet(CoercedCollectionMixin, set):
"""Set which coerces its values
Dict implementation which overrides all element-adding methods and
coercing the element(s) being added to the required element type
"""
def _coerce_element(self, element):
if hasattr(self, "_element_type") and self._element_type is not None:
return self._element_type.coerce(self._obj,
"%s[%s]" % (self._field, element),
element)
else:
return element
def _coerce_iterable(self, values):
coerced = set()
for element in values:
coerced.add(self._coerce_element(element))
return coerced
def add(self, value):
return super(CoercedSet, self).add(self._coerce_element(value))
def update(self, values):
return super(CoercedSet, self).update(self._coerce_iterable(values))
def symmetric_difference_update(self, values):
return super(CoercedSet, self).symmetric_difference_update(
self._coerce_iterable(values))
def __ior__(self, y):
return super(CoercedSet, self).__ior__(self._coerce_iterable(y))
def __ixor__(self, y):
return super(CoercedSet, self).__ixor__(self._coerce_iterable(y))
|