/usr/lib/python3/dist-packages/plainbox/impl/pod.py is in python3-plainbox 0.25-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 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 | # encoding: utf-8
# This file is part of Checkbox.
#
# Copyright 2012-2015 Canonical Ltd.
# Written by:
# Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
"""
Plain Old Data.
:mod:`plainbox.impl.pod`
========================
This module contains the :class:`POD` and :class:`Field` classes that simplify
creation of declarative struct-like data holding classes. POD classes get a
useful repr() method, useful initializer and accessors for each of the fields
defined inside. POD classes can be inherited (properly detecting any field
clashes)
Defining POD classes:
>>> class Person(POD):
... name = Field("name of the person", str, MANDATORY)
... age = Field("age of the person", int)
Creating POD instances, positional arguments match field definition order:
>>> joe = Person("joe", age=42)
Full-blown comparison (not only equality):
>>> joe == Person("joe", 42)
True
Reading and writing attributes also works (obviously):
>>> joe.name
'joe'
>>> joe.age
42
>>> joe.age = 24
>>> joe.age
24
For a full description check out the documentation of the :class:`POD` and
:class:`Field`.
"""
from collections import OrderedDict
from collections import namedtuple
from functools import total_ordering
from logging import getLogger
from textwrap import dedent
from plainbox.i18n import gettext as _
from plainbox.vendor import morris
__all__ = ('POD', 'PODBase', 'podify', 'Field', 'MANDATORY', 'UNSET',
'read_only_assign_filter', 'type_convert_assign_filter',
'type_check_assign_filter', 'modify_field_docstring')
_logger = getLogger("plainbox.pod")
class _Singleton:
"""A simple object()-like singleton that has a more useful repr()."""
def __repr__(self):
return self.__class__.__name__
class MANDATORY(_Singleton):
"""
Class for the special MANDATORY object.
This object can be used as a value in :attr:`Field.initial`.
Using ``MANDATORY`` on a field like that makes the explicit initialization
of the field mandatory during POD initialization. Please use this value to
require that the caller supplies a given argument to the POD you are
working with.
"""
MANDATORY = MANDATORY()
class UNSET(_Singleton):
"""
Class of the special UNSET object.
Singleton that is implicitly assigned to the values of all fields during
POD initialization. This way all fields will have a value, even early at
the time a POD is initialized. This can be important if the POD is somehow
repr()-ed or inspected in other means.
This object is also used by the :func:`read_only_assign_filter` function.
"""
UNSET = UNSET()
class Field:
"""
A field in a plain-old-data class.
Each field declares one attribute that can be read and written to. Just
like a C structure. Attributes are readable _and_ writable but there is a
lot of flexibility in what happens.
:attr name:
Name of the field (this is how this field can be accessed on the class
or instance that contains it). This gets set by
:meth:`_FieldCollection.inspect_namespace()`
:attr instance_attr:
Name of the POD dictionary entry used as backing store. This is set the
same as ``name`` above. By default that's just name prepended with the
``'_'`` character.
:attr type:
An optional type hit. This is not used by default but assign filters
can inspect and use this for type checking. It can also be used for
documenting the intent of the field.
:attr __doc__:
The docstring of the field, as initialized by the caller.
:attr initial:
Initial value of this field, can be changed by passing arguments to
:meth:`POD.__init__()`. May be set to ``MANDATORY`` for a special
meaning (see below).
:attr initial_fn:
If not None this is a callable that produces the ``initial`` value for
each new POD object.
:attr notify:
If True, a on_{name}_changed
A flag controlling if notification events are sent for each
modification of POD data through field.
:attr notify_fn:
An (optional) function to use as the first responder to the change
notification signal. This field is only used if the ``notify``
attribute is set to ``True``.
:attr assign_filter_list:
An (optional) list of assignment filter functions.
A field is initialized based on the arguments passed to the POD
initializer. If no argument is passed that would correspond to a given
field the *initial* value is used. The *initial* value is either a constant
(reference) stored in the ``initial`` property of the field or the return
value of the callable in ``initial_fn``. Please make sure to use
``initial_fn`` if the value is not immutable as otherwise the produced
value may be unintentionally shared by multiple objects.
If the ``initial`` value is the special constant ``MANDATORY`` then the
corresponding field must be explicitly initialized by the POD initializer
argument list or a TypeError is raised.
The ``notify`` flag controls the existence of the ``on_{name}_changed(old,
new)`` signal on the class that includes the field. Applications can
connect to that signal to observe changes. The signal is fired whenever the
newly-assigned value compares *unequal* to the value currently stored in
the POD.
The ``notify_fn`` is an optional function that is used instead of the
default (internal) :meth:`on_changed()` method of the Field class itself.
If specified it must have the same three-argument signature. It will be
called whenever the value of the field changes. Note that it will also be
called on the initial assignment, when the ``old`` argument it receives
will be set to the special ``UNSET`` object.
Lastly a docstring and type hint can be provided for documentation. The
type check is not enforced.
Assignment filters are used to inspect and optionally modify a value during
assignment (including the assignment done on object initialization) and can
be used for various operations (including type conversions and validation).
Assignment filters are called whenever a field is used to write to a POD.
Since assignment filters are arranged in a list and executed in-order, they
can also be used to modify the value as it gets propagated through the list
of filters.
The signature of each filter is ``fn(pod, field, old_value, new_value)``.
The return value is the value shown to the subsequent filter or finally
assigned to the POD.
"""
_counter = 0
def __init__(self, doc=None, type=None, initial=None, initial_fn=None,
notify=False, notify_fn=None, assign_filter_list=None):
"""Initialize (define) a new POD field."""
self.__doc__ = dedent(doc) if doc is not None else None
self.type = type
self.initial = initial
self.initial_fn = initial_fn
self.notify = notify
self.notify_fn = notify_fn
self.assign_filter_list = assign_filter_list
self.name = None # Set via :meth:`gain_name()`
self.instance_attr = None # ditto
self.signal_name = None # ditto
doc_extra = []
for fn in self.assign_filter_list or ():
if hasattr(fn, 'field_docstring_ext'):
doc_extra.append(fn.field_docstring_ext.format(field=self))
if doc_extra:
self.__doc__ += (
'\n\nSide effects of assign filters:\n'
+ '\n'.join(' - {}'.format(extra) for extra in doc_extra))
self.counter = self.__class__._counter
self.__class__._counter += 1
@property
def change_notifier(self):
"""
Decorator for changing the change notification function.
This decorator can be used to define all the fields in one block and
all the notification function in another block. It helps to make the
code easier to read.
Example::
>>> class Person(POD):
... name = Field()
...
... @name.change_notifier
... def _name_changed(self, old, new):
... print("changed from {!r} to {!r}".format(old, new))
>>> person = Person()
changed from UNSET to None
>>> person.name = "bob"
changed from None to 'bob'
.. note::
Keep in mind that the decorated function is converted to a signal
automatically. The name of the function is also irrelevant, the POD
core automatically creates signals that have consistent names of
``on_{field}_changed()``.
"""
def decorator(fn):
self.notify = True
self.notify_fn = fn
return fn
return decorator
def __repr__(self):
"""Get a debugging representation of a field."""
return "<{} name:{!r}>".format(self.__class__.__name__, self.name)
@property
def is_mandatory(self) -> bool:
"""Flag indicating if the field needs a mandatory initializer."""
return self.initial is MANDATORY
def gain_name(self, name: str) -> None:
"""
Set field name.
:param name:
Name of the field as it appears in a class definition
Method called at most once on each Field instance embedded in a
:class:`POD` subclass. This method informs the field of the name it was
assigned to in the class.
"""
self.name = name
self.instance_attr = "_{}".format(name)
self.signal_name = "on_{}_changed".format(name)
def alter_cls(self, cls: type) -> None:
"""
Modify class definition this field belongs to.
This method is called during class construction. It allows the field to
alter the class and add the on_{field.name}_changed signal. The signal
is only added if notification is enabled *and* if there is no such
signal in the first place (this allows inheritance not to create
separate but identically-named signals and allows signal handlers
connected via the base class to work on child classes.
"""
if not self.notify:
return
assert self.signal_name is not None
if not hasattr(cls, self.signal_name):
signal_def = morris.signal(
self.notify_fn if self.notify_fn is not None
else self.on_changed,
signal_name='{}.{}'.format(cls.__name__, self.signal_name))
setattr(cls, self.signal_name, signal_def)
def __get__(self, instance: object, owner: type) -> "Any":
"""
Get field value from an object or from a class.
This method is part of the Python descriptor protocol.
"""
if instance is None:
return self
else:
return getattr(instance, self.instance_attr)
def __set__(self, instance: object, new_value: "Any") -> None:
"""
Set field value from on an object.
This method is part of the Python descriptor protocol.
Assignments respect the assign filter chain, that is, the new value is
being pushed through the chain of callbacks (each has a chance to alter
the value) until it is finally assigned. Any of the callbacks can raise
an exception and abort the setting process.
This can be used to implement simple type checking, value checking or
even type and value conversions.
"""
if self.assign_filter_list is not None or self.notify:
old_value = getattr(instance, self.instance_attr, UNSET)
# Run the value through assign filters
if self.assign_filter_list is not None:
for assign_filter in self.assign_filter_list:
new_value = assign_filter(instance, self, old_value, new_value)
# Do value modification check if change notification is enabled
if self.notify and hasattr(instance, self.instance_attr):
if new_value != old_value:
setattr(instance, self.instance_attr, new_value)
on_field_change = getattr(instance, self.signal_name)
on_field_change(old_value, new_value)
else:
# Or just fire away
setattr(instance, self.instance_attr, new_value)
def on_changed(self, pod: "POD", old: "Any", new: "Any") -> None:
"""
The first responder of the per-field modification signal.
:param pod:
The object that contains the modified values
:param old:
The old value of the field
:param new:
The new value of the field
"""
_logger.debug("<%s %s>.%s(%r, %r)", pod.__class__.__name__, id(pod),
self.signal_name, old, new)
@total_ordering
class PODBase:
"""Base class for POD-like classes."""
field_list = []
namedtuple_cls = namedtuple('PODBase', '')
def __init__(self, *args, **kwargs):
"""
Initialize a new POD object.
Positional arguments bind to fields in declaration order. Keyword
arguments bind to fields in any order but fields cannot be initialized
twice.
:raises TypeError:
If there are more positional arguments than fields to initialize
:raises TypeError:
If a keyword argument doesn't correspond to a field name.
:raises TypeError:
If a field is initialized twice (first with positional arguments,
then again with keyword arguments).
:raises TypeError:
If a ``MANDATORY`` field is not initialized.
"""
field_list = self.__class__.field_list
# Set all of the instance attributes to the special UNSET value, this
# is useful if something fails and the object is inspected somehow.
# Then all the attributes will be still UNSET.
for field in field_list:
setattr(self, field.instance_attr, UNSET)
# Check if the number of positional arguments is correct
if len(args) > len(field_list):
raise TypeError("too many arguments")
# Initialize mandatory fields using positional arguments
for field, field_value in zip(field_list, args):
setattr(self, field.name, field_value)
# Initialize fields using keyword arguments
for field_name, field_value in kwargs.items():
field = getattr(self.__class__, field_name, None)
if not isinstance(field, Field):
raise TypeError("no such field: {}".format(field_name))
if getattr(self, field.instance_attr) is not UNSET:
raise TypeError(
"field initialized twice: {}".format(field_name))
setattr(self, field_name, field_value)
# Initialize remaining fields using their default initializers
for field in field_list:
if getattr(self, field.instance_attr) is not UNSET:
continue
if field.is_mandatory:
raise TypeError(
"mandatory argument missing: {}".format(field.name))
if field.initial_fn is not None:
field_value = field.initial_fn()
else:
field_value = field.initial
setattr(self, field.name, field_value)
def __repr__(self):
"""Get a debugging representation of a POD object."""
return "{}({})".format(
self.__class__.__name__,
', '.join([
'{}={!r}'.format(field.name, getattr(self, field.name))
for field in self.__class__.field_list]))
def __eq__(self, other: "POD") -> bool:
"""
Check that this POD is equal to another POD.
POD comparison is implemented by converting them to tuples and
comparing the two tuples.
"""
if not isinstance(other, POD):
return NotImplemented
return self.as_tuple() == other.as_tuple()
def __lt__(self, other: "POD") -> bool:
"""
Check that this POD is "less" than an another POD.
POD comparison is implemented by converting them to tuples and
comparing the two tuples.
"""
if not isinstance(other, POD):
return NotImplemented
return self.as_tuple() < other.as_tuple()
def as_tuple(self) -> tuple:
"""
Return the data in this POD as a tuple.
Order of elements in the tuple corresponds to the order of field
declarations.
"""
return self.__class__.namedtuple_cls(*[
getattr(self, field.name)
for field in self.__class__.field_list
])
def as_dict(self) -> dict:
"""
Return the data in this POD as a dictionary.
.. note::
UNSET values are not added to the dictionary.
"""
return {
field.name: getattr(self, field.name)
for field in self.__class__.field_list
if getattr(self, field.name) is not UNSET
}
class _FieldCollection:
"""
Support class for constructing POD meta-data information.
Helper class that simplifies :class:`PODMeta` code that harvests
:class:`Field` instances during class construction. Looking at the
namespace and a list of base classes come up with a list of Field objects
that belong to the given POD.
:attr field_list:
A list of :class:`Field` instances
:attr field_origin_map:
A dictionary mapping from field name to the *name* of the class that
defines it.
"""
def __init__(self):
self.field_list = []
self.field_origin_map = {} # field name -> defining class name
def inspect_cls_for_decorator(self, cls: type) -> None:
"""Analyze a bare POD class."""
self.inspect_base_classes(cls.__bases__)
self.inspect_namespace(cls.__dict__, cls.__name__)
def inspect_base_classes(self, base_cls_list: "List[type]") -> None:
"""
Analyze base classes of a POD class.
Analyze a list of base classes and check if they have consistent
fields. All analyzed fields are added to the internal data structures.
:param base_cls_list:
A list of classes to inspect. Only subclasses of POD are inspected.
"""
for base_cls in base_cls_list:
if not issubclass(base_cls, PODBase):
continue
base_cls_name = base_cls.__name__
for field in base_cls.field_list:
self.add_field(field, base_cls_name)
def inspect_namespace(self, namespace: dict, cls_name: str) -> None:
"""
Analyze namespace of a POD class.
Analyze a namespace of a newly (being formed) class and check if it has
consistent fields. All analyzed fields are added to the internal data
structures.
.. note::
This method calls :meth:`Field.gain_name()` on all fields it finds.
"""
fields = []
for field_name, field in namespace.items():
if not isinstance(field, Field):
continue
field.gain_name(field_name)
fields.append(field)
fields.sort(key=lambda field: field.counter)
for field in fields:
self.add_field(field, cls_name)
def get_namedtuple_cls(self, name: str) -> type:
"""
Create a new namedtuple that corresponds to the fields seen so far.
:parm name:
Name of the namedtuple class
:returns:
A new namedtuple class
"""
return namedtuple(name, [field.name for field in self.field_list])
def add_field(self, field: Field, base_cls_name: str) -> None:
"""
Add a field to the collection.
:param field:
A :class:`Field` instance
:param base_cls_name:
The name of the class that defines the field
:raises TypeError:
If any of the base classes have overlapping fields.
"""
assert field.name is not None
field_name = field.name
if field_name not in self.field_origin_map:
self.field_origin_map[field_name] = base_cls_name
self.field_list.append(field)
else:
raise TypeError("field {1}.{0} clashes with {2}.{0}".format(
field_name, base_cls_name, self.field_origin_map[field_name]))
class PODMeta(type):
"""
Meta-class for all POD classes.
This meta-class is responsible for correctly handling field inheritance.
This class sets up ``field_list`` and ``namedtuple_cls`` attributes on the
newly-created class.
"""
def __new__(mcls, name, bases, namespace):
fc = _FieldCollection()
fc.inspect_base_classes(bases)
fc.inspect_namespace(namespace, name)
namespace['field_list'] = fc.field_list
namespace['namedtuple_cls'] = fc.get_namedtuple_cls(name)
cls = super().__new__(mcls, name, bases, namespace)
for field in fc.field_list:
field.alter_cls(cls)
return cls
@classmethod
def __prepare__(mcls, name, bases, **kwargs):
"""
Get a namespace for defining new POD classes.
Prepare the namespace for the definition of a class using PODMeta as a
meta-class. Since we want to observe the order of fields, using an
OrderedDict makes that task trivial.
"""
return OrderedDict()
def podify(cls):
"""
Decorator for POD classes.
The decorator offers an alternative from using the POD class (with the
PODMeta meta-class). Instead of using that, one can use the ``@podify``
decorator on a PODBase-derived class.
"""
if not isinstance(cls, type) or not issubclass(cls, PODBase):
raise TypeError("cls must be a subclass of PODBase")
fc = _FieldCollection()
fc.inspect_cls_for_decorator(cls)
cls.field_list = fc.field_list
cls.namedtuple_cls = fc.get_namedtuple_cls(cls.__name__)
for field in fc.field_list:
field.alter_cls(cls)
return cls
@total_ordering
class POD(PODBase, metaclass=PODMeta):
"""
Base class that removes boilerplate from plain-old-data classes.
Use POD as your base class and define :class:`Field` objects inside. Don't
define any __init__() (unless you really, really have to have one) and
instead set appropriate attributes on the initializer of a particular field
object.
What you get for *free* is, all the properties (for each field),
documentation, initializer, comparison methods (PODs have total ordering)
and the __repr__() method.
There are some additional methods, such as :meth:`as_tuple()` and
:meth:`as_dict()` that may be of use in some circumstances.
All fields in a single POD subclass are collected (including all of the
fields in the parent classes) and arranged in a list. That list is
available as ``POD.field_list``.
In addition each POD class has an unique named tuple that corresponds to
each field stored inside the POD, the named tuple is available as
``POD.namedtuple_cls``. The return value of :meth:`as_tuple()` actually
uses that type.
"""
def modify_field_docstring(field_docstring_ext: str):
"""
Decorator for altering field docstrings via assign filter functions.
A decorator for assign filter functions that allows them to declaratively
modify the docstring of the field they are used on.
:param field_docstring_ext:
A string compatible with python's str.format() method. The string
should be one line long (newlines will look odd) and may reference any
of the field attributes, as exposed by the {field} named format
attribute.
Example:
>>> @modify_field_docstring("not even")
... def not_even(instance, field, old, new):
... if new % 2 == 0:
... raise ValueError("value cannot be even")
... return new
"""
def decorator(fn):
fn.field_docstring_ext = field_docstring_ext
return fn
return decorator
@modify_field_docstring("constant (read-only after initialization)")
def read_only_assign_filter(
instance: POD, field: Field, old: "Any", new: "Any") -> "Any":
"""
An assign filter that makes a field read-only.
The field can be only assigned if the old value is ``UNSET``, that is,
during the initial construction of a POD object.
:param instance:
A subclass of :class:`POD` that contains ``field``
:param field:
The :class:`Field` being assigned to
:param old:
The current value of the field
:param new:
The proposed value of the field
:returns:
``new``, as-is
:raises AttributeError:
if ``old`` is anything but the special object ``UNSET``
"""
if old is UNSET:
return new
raise AttributeError(_(
"{}.{} is read-only"
).format(instance.__class__.__name__, field.name))
const = read_only_assign_filter
@modify_field_docstring(
"type-converted (value must be convertible to {field.type.__name__})")
def type_convert_assign_filter(
instance: POD, field: Field, old: "Any", new: "Any") -> "Any":
"""
An assign filter that converts the value to the field type.
The field must have a valid python type object stored in the .type field.
:param instance:
A subclass of :class:`POD` that contains ``field``
:param field:
The :class:`Field` being assigned to
:param old:
The current value of the field
:param new:
The proposed value of the field
:returns:
``new`` type-converted to ``field.type``.
:raises ValueError:
if ``new`` cannot be converted to ``field.type``
"""
return field.type(new)
@modify_field_docstring(
"type-checked (value must be of type {field.type.__name__})")
def type_check_assign_filter(
instance: POD, field: Field, old: "Any", new: "Any") -> "Any":
"""
An assign filter that type-checks the value according to the field type.
The field must have a valid python type object stored in the .type field.
:param instance:
A subclass of :class:`POD` that contains ``field``
:param field:
The :class:`Field` being assigned to
:param old:
The current value of the field
:param new:
The proposed value of the field
:returns:
``new``, as-is
:raises TypeError:
if ``new`` is not an instance of ``field.type``
"""
if isinstance(new, field.type):
return new
raise TypeError("{}.{} requires objects of type {}".format(
instance.__class__.__name__, field.name, field.type.__name__))
typed = type_check_assign_filter
@modify_field_docstring(
"unset or type-checked (value must be of type {field.type.__name__})")
def unset_or_type_check_assign_filter(
instance: POD, field: Field, old: "Any", new: "Any") -> "Any":
"""
An assign filter that type-checks the value according to the field type.
.. note::
This filter allows (passes through) the special ``UNSET`` value as-is.
The field must have a valid python type object stored in the .type field.
:param instance:
A subclass of :class:`POD` that contains ``field``
:param field:
The :class:`Field` being assigned to
:param old:
The current value of the field
:param new:
The proposed value of the field
:returns:
``new``, as-is
:raises TypeError:
if ``new`` is not an instance of ``field.type``
"""
if new is UNSET:
return new
return type_check_assign_filter(instance, field, old, new)
unset_or_typed = unset_or_type_check_assign_filter
class sequence_type_check_assign_filter:
"""
Assign filter for typed sequences.
An assign filter for typed sequences (lists or tuples) that must contain an
object of the given type.
"""
def __init__(self, item_type: type):
"""
Initialize the assign filter with the given sequence item type.
:param item_type:
Desired type of each sequence item.
"""
self.item_type = item_type
@property
def field_docstring_ext(self) -> str:
return "type-checked sequence (items must be of type {})".format(
self.item_type.__name__)
def __call__(
self, instance: POD, field: Field, old: "Any", new: "Any"
) -> "Any":
"""
An assign filter that type-checks the value of all sequence elements.
:param instance:
A subclass of :class:`POD` that contains ``field``
:param field:
The :class:`Field` being assigned to
:param old:
The current value of the field
:param new:
The proposed value of the field
:returns:
``new``, as-is
:raises TypeError:
if ``new`` is not an instance of ``field.type``
"""
for item in new:
if not isinstance(item, self.item_type):
raise TypeError(
"{}.{} requires all sequence elements of type {}".format(
instance.__class__.__name__, field.name,
self.item_type.__name__))
return new
typed.sequence = sequence_type_check_assign_filter
class unset_or_sequence_type_check_assign_filter(typed.sequence):
"""
Assign filter for typed sequences.
.. note::
This filter allows (passes through) the special ``UNSET`` value as-is.
An assign filter for typed sequences (lists or tuples) that must contain an
object of the given type.
"""
@property
def field_docstring_ext(self) -> str:
return (
"unset or type-checked sequence (items must be of type {})"
).format(self.item_type.__name__)
def __call__(
self, instance: POD, field: Field, old: "Any", new: "Any"
) -> "Any":
"""
An assign filter that type-checks the value of all sequence elements.
.. note::
This filter allows (passes through) the special ``UNSET`` value
as-is.
:param instance:
A subclass of :class:`POD` that contains ``field``
:param field:
The :class:`Field` being assigned to
:param old:
The current value of the field
:param new:
The proposed value of the field
:returns:
``new``, as-is
:raises TypeError:
if ``new`` is not an instance of ``field.type``
"""
if new is UNSET:
return new
return super().__call__(instance, field, old, new)
unset_or_typed.sequence = unset_or_sequence_type_check_assign_filter
@modify_field_docstring("unique elements (sequence elements cannot repeat)")
def unique_elements_assign_filter(
instance: POD, field: Field, old: "Any", new: "Any") -> "Any":
"""
An assign filter that ensures a sequence has non-repeating items.
:param instance:
A subclass of :class:`POD` that contains ``field``
:param field:
The :class:`Field` being assigned to
:param old:
The current value of the field
:param new:
The proposed value of the field
:returns:
``new``, as-is
:raises ValueError:
if ``new`` contains any duplicates
"""
seen = set()
for item in new:
if new in seen:
raise ValueError("Duplicate element: {!r}".format(item))
seen.add(item)
return new
unique = unique_elements_assign_filter
|