|
| 1 | +import json |
| 2 | +from typing import Dict, List, Optional |
| 3 | + |
| 4 | +from brainframe.api.bf_codecs import Capsule |
| 5 | +from .base_stub import BaseStub, DEFAULT_TIMEOUT |
| 6 | + |
| 7 | + |
| 8 | +class CapsuleStubMixin(BaseStub): |
| 9 | + """Provides stubs to call APIs to inspect and configure capsules.""" |
| 10 | + |
| 11 | + def get_capsule(self, name, |
| 12 | + timeout=DEFAULT_TIMEOUT) -> Capsule: |
| 13 | + """ |
| 14 | + :param name: The name of the capsule to get |
| 15 | + :param timeout: The timeout to use for this request |
| 16 | + :return: Capsule with the given name |
| 17 | + """ |
| 18 | + req = f"/api/plugins/{name}" |
| 19 | + capsule, _ = self._get_json(req, timeout) |
| 20 | + return Capsule.from_dict(capsule) |
| 21 | + |
| 22 | + def get_capsules(self, timeout=DEFAULT_TIMEOUT) -> List[Capsule]: |
| 23 | + """ |
| 24 | + :param timeout: The timeout to use for this request |
| 25 | + :return: All available capsules |
| 26 | + """ |
| 27 | + req = "/api/plugins" |
| 28 | + capsules, _ = self._get_json(req, timeout) |
| 29 | + return [Capsule.from_dict(d) for d in capsules] |
| 30 | + |
| 31 | + def get_capsule_option_vals(self, capsule_name, stream_id=None, |
| 32 | + timeout=DEFAULT_TIMEOUT) \ |
| 33 | + -> Dict[str, object]: |
| 34 | + """Gets the current values for every capsule option. See the |
| 35 | + documentation for the CapsuleOption codec for more info about global |
| 36 | + and stream level options and how they interact. |
| 37 | +
|
| 38 | + :param capsule_name: The capsule to find options for |
| 39 | + :param stream_id: The ID of the stream. If this value is None, then the |
| 40 | + global options are returned for that capsule |
| 41 | + :param timeout: The timeout to use for this request |
| 42 | + :return: A dict where the key is the option name and the value is the |
| 43 | + option's current value |
| 44 | + """ |
| 45 | + if stream_id is None: |
| 46 | + req = f"/api/plugins/{capsule_name}/options" |
| 47 | + else: |
| 48 | + req = f"/api/streams/{stream_id}/plugins/{capsule_name}/options" |
| 49 | + capsule_option_vals, _ = self._get_json(req, timeout) |
| 50 | + |
| 51 | + return capsule_option_vals |
| 52 | + |
| 53 | + def set_capsule_option_vals(self, *, capsule_name, stream_id=None, |
| 54 | + option_vals: Dict[str, object], |
| 55 | + timeout=DEFAULT_TIMEOUT): |
| 56 | + """Sets option values for a capsule. |
| 57 | +
|
| 58 | + :param capsule_name: The name of the capsule whose options to set |
| 59 | + :param stream_id: The ID of the stream, if these are stream-level |
| 60 | + options. If this value is None, then the global options are set |
| 61 | + :param option_vals: A dict where the key is the name of the option to |
| 62 | + set, and the value is the value to set that option to |
| 63 | + :param timeout: The timeout to use for this request |
| 64 | + """ |
| 65 | + if stream_id is None: |
| 66 | + req = f"/api/plugins/{capsule_name}/options" |
| 67 | + else: |
| 68 | + req = f"/api/streams/{stream_id}/plugins/{capsule_name}/options" |
| 69 | + |
| 70 | + option_values_json = json.dumps(option_vals) |
| 71 | + self._put_json(req, timeout, option_values_json) |
| 72 | + |
| 73 | + def patch_capsule_option_vals(self, *, capsule_name, stream_id=None, |
| 74 | + option_vals: Dict[str, object], |
| 75 | + timeout=DEFAULT_TIMEOUT): |
| 76 | + """Patches option values for a capsule. Only the provided options are |
| 77 | + changed. To unset an option, provide that option with a value of None. |
| 78 | +
|
| 79 | + :param capsule_name: The name of the capsule whose options to set |
| 80 | + :param stream_id: The ID of the stream, if these are stream-level |
| 81 | + options. If this value is None, then the global options are set |
| 82 | + :param option_vals: A dict where the key is the name of the option to |
| 83 | + set, and the value is the value to set that option to |
| 84 | + :param timeout: The timeout to use for this request |
| 85 | + """ |
| 86 | + if stream_id is None: |
| 87 | + req = f"/api/plugins/{capsule_name}/options" |
| 88 | + else: |
| 89 | + req = f"/api/streams/{stream_id}/plugins/{capsule_name}/options" |
| 90 | + |
| 91 | + option_values_json = json.dumps(option_vals) |
| 92 | + self._patch_json(req, timeout, option_values_json) |
| 93 | + |
| 94 | + def is_capsule_active(self, capsule_name, stream_id=None, |
| 95 | + timeout=DEFAULT_TIMEOUT) -> bool: |
| 96 | + """Returns True if the capsule is active. If a capsule is not marked as |
| 97 | + active, it will not run. Like capsule options, this can be configured |
| 98 | + globally and on a per-stream level. |
| 99 | +
|
| 100 | + :param capsule_name: The name of the capsule to get activity for |
| 101 | + :param stream_id: The ID of the stream, if you want the per-stream |
| 102 | + active setting |
| 103 | + :param timeout: The timeout to use for this request |
| 104 | + :return: True if the capsule is active |
| 105 | + """ |
| 106 | + if stream_id is None: |
| 107 | + req = f"/api/plugins/{capsule_name}/active" |
| 108 | + else: |
| 109 | + req = f"/api/streams/{stream_id}/plugins/{capsule_name}/active" |
| 110 | + capsules_active, _ = self._get_json(req, timeout) |
| 111 | + return capsules_active |
| 112 | + |
| 113 | + def set_capsule_active(self, *, capsule_name, stream_id=None, |
| 114 | + active: Optional[bool], |
| 115 | + timeout=DEFAULT_TIMEOUT): |
| 116 | + """Sets whether or not the capsule is active. If a capsule is active, it |
| 117 | + will be run on frames. |
| 118 | +
|
| 119 | + :param capsule_name: The name of the capsule to set activity for |
| 120 | + :param stream_id: The ID of the stream, if you want to set the |
| 121 | + per-stream active setting |
| 122 | + :param active: True if the capsule should be set to active |
| 123 | + :param timeout: The timeout to use for this request |
| 124 | + """ |
| 125 | + if stream_id is None: |
| 126 | + req = f"/api/plugins/{capsule_name}/active" |
| 127 | + else: |
| 128 | + req = f"/api/streams/{stream_id}/plugins/{capsule_name}/active" |
| 129 | + |
| 130 | + active_json = json.dumps(active) |
| 131 | + |
| 132 | + self._put_json(req, timeout, active_json) |
0 commit comments