1#-----------------------------------------------------------------------------
2# Copyright (c) 2012 - 2021, Anaconda, Inc., and Bokeh Contributors.
3# All rights reserved.
4#
5# The full license is in the file LICENSE.txt, distributed with this software.
6#-----------------------------------------------------------------------------
7""" Provide the Instance property.
8
9The Instance property is used to construct object graphs of Bokeh models,
10where one Bokeh model refers to another.
11
12"""
13
14#-----------------------------------------------------------------------------
15# Boilerplate
16#-----------------------------------------------------------------------------
17import logging # isort:skip
18log = logging.getLogger(__name__)
19
20#-----------------------------------------------------------------------------
21# Imports
22#-----------------------------------------------------------------------------
23
24# Standard library imports
25from functools import wraps
26
27# Bokeh imports
28from .bases import Property
29
30#-----------------------------------------------------------------------------
31# Globals and constants
32#-----------------------------------------------------------------------------
33
34__all__ = (
35    'validate',
36    'without_property_validation',
37)
38
39#-----------------------------------------------------------------------------
40# General API
41#-----------------------------------------------------------------------------
42
43class validate:
44    """ Control validation of bokeh properties
45
46    This can be used as a context manager, or as a normal callable
47
48    Args:
49        value (bool) : Whether validation should occur or not
50
51    Example:
52        .. code-block:: python
53
54            with validate(False):  # do no validate while within this block
55                pass
56
57            validate(False)  # don't validate ever
58
59    See Also:
60        :func:`~bokeh.core.property.bases.validation_on`: check the state of validation
61
62        :func:`~bokeh.core.properties.without_property_validation`: function decorator
63
64    """
65    def __init__(self, value):
66        self.old = Property._should_validate
67        Property._should_validate = value
68
69    def __enter__(self):
70        pass
71
72    def __exit__(self, typ, value, traceback):
73        Property._should_validate = self.old
74
75
76def without_property_validation(input_function):
77    """ Turn off property validation during update callbacks
78
79    Example:
80        .. code-block:: python
81
82            @without_property_validation
83            def update(attr, old, new):
84                # do things without validation
85
86    See Also:
87        :class:`~bokeh.core.properties.validate`: context mangager for more fine-grained control
88
89    """
90    @wraps(input_function)
91    def func(*args, **kwargs):
92        with validate(False):
93            return input_function(*args, **kwargs)
94    return func
95
96#-----------------------------------------------------------------------------
97# Dev API
98#-----------------------------------------------------------------------------
99
100#-----------------------------------------------------------------------------
101# Private API
102#-----------------------------------------------------------------------------
103
104#-----------------------------------------------------------------------------
105# Code
106#-----------------------------------------------------------------------------
107