-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcore.py
127 lines (99 loc) · 2.95 KB
/
core.py
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
# Copyright 2022 Google 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.
"""
Public code that is shared across modules.
"""
import dataclasses as _dataclass
import datetime as _datetime
import typing as _typing
from . import logger as _logger
T = _typing.TypeVar("T")
@_dataclass.dataclass(frozen=True)
class CloudEvent(_typing.Generic[T]):
"""
A CloudEvent is the base of a cross-platform format for encoding a serverless event.
More information can be found at https://github.com/cloudevents/spec.
"""
specversion: str
"""
Version of the CloudEvents spec for this event.
"""
id: str
"""
A globally unique ID for this event.
"""
source: str
"""
The resource which published this event.
"""
type: str
"""
The type of event that this represents.
"""
time: _datetime.datetime
"""
When this event occurred.
"""
data: T
"""
Information about this specific event.
"""
subject: str | None
"""
The resource, provided by source, that this event relates to.
"""
@_dataclass.dataclass(frozen=True)
class Change(_typing.Generic[T]):
"""
The Cloud Functions interface for events that change state, such as
Realtime Database `on_value_written`.
"""
before: T
"""
The state of data before the change.
"""
after: T
"""
The state of data after the change.
"""
_did_init = False
_init_callback: _typing.Callable[[], _typing.Any] | None = None
def init(callback: _typing.Callable[[], _typing.Any]) -> None:
"""
Registers a function that should be run when in a production environment
before executing any functions code.
Calling this decorator more than once leads to undefined behavior.
"""
global _did_init
global _init_callback
if _did_init:
_logger.warn(
"Setting init callback more than once. Only the most recent callback will be called"
)
_init_callback = callback
_did_init = False
def _with_init(
fn: _typing.Callable[...,
_typing.Any]) -> _typing.Callable[..., _typing.Any]:
"""
A decorator that runs the init callback before running the decorated function.
"""
def wrapper(*args, **kwargs):
global _did_init
if not _did_init:
if _init_callback is not None:
_init_callback()
_did_init = True
return fn(*args, **kwargs)
return wrapper