-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplaybook.py
112 lines (85 loc) · 3.79 KB
/
playbook.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
"""TcEx Framework Module"""
# standard library
import logging
from functools import cached_property
from ...app.key_value_store.key_value_store import KeyValueStore
from ...util.model.playbook_variable_model import PlaybookVariableModel
from ...util.util import Util
from .playbook_create import PlaybookCreate
from .playbook_delete import PlaybookDelete
from .playbook_output import PlaybookOutput
from .playbook_read import PlaybookRead
# get logger
_logger = logging.getLogger(__name__.split('.', maxsplit=1)[0])
class Playbook:
"""Playbook methods for accessing key value store.
Args:
key_value_store: A KV store instance.
context: The KV Store context/session_id. For PB Apps the context is provided on
startup, but for service Apps each request gets a different context.
output_variables: The requested output variables. For PB Apps outputs are provided on
startup, but for service Apps each request gets different outputs.
"""
def __init__(
self,
key_value_store: KeyValueStore,
context: str | None = None,
output_variables: list | None = None,
):
"""Initialize the class properties."""
self.context = context
self.key_value_store = key_value_store
self.output_variables = output_variables or []
# properties
self.log = _logger
self.util = Util()
def check_key_requested(self, key: str) -> bool:
"""Return True if output key was requested by downstream app.
Provide key should be in format "app.output".
"""
variables = []
for variable in self.output_variables:
var = self.util.get_playbook_variable_model(variable)
if isinstance(var, PlaybookVariableModel):
variables.append(var.key)
return key in variables
def check_variable_requested(self, variable: str) -> bool:
"""Return True if output variable was requested by downstream app.
Provide variable should be in format of "#App:1234:app.output!String".
"""
return variable in self.create.output_variables
def get_variable_type(self, variable: str) -> str:
"""Get the Type from the variable string or default to String type.
The default type is "String" for those cases when the input variable is
contains not "DB variable" and is just a String.
Example Variable:
#App:1234:output!StringArray returns **StringArray**
Example String:
"My Data" returns **String**
"""
return self.util.get_playbook_variable_type(variable)
@cached_property
def create(self) -> PlaybookCreate:
"""Return instance of PlaybookCreate"""
if self.context is None:
raise RuntimeError('Playbook context is required for PlaybookCreate.')
return PlaybookCreate(self.context, self.key_value_store, self.output_variables)
@cached_property
def delete(self) -> PlaybookDelete:
"""Return instance of PlaybookDelete"""
if self.context is None:
raise RuntimeError('Playbook context is required for PlaybookDelete.')
return PlaybookDelete(self.context, self.key_value_store)
def is_variable(self, key: str) -> bool:
"""Return True if provided key is a properly formatted playbook variable."""
return self.util.is_playbook_variable(key)
@cached_property
def output(self) -> PlaybookOutput:
"""Return instance of PlaybookOutput"""
return PlaybookOutput(self)
@cached_property
def read(self) -> PlaybookRead:
"""Return instance of PlaybookRead"""
if self.context is None:
raise RuntimeError('Playbook context is required for PlaybookRead.')
return PlaybookRead(self.context, self.key_value_store)