-
Notifications
You must be signed in to change notification settings - Fork 0
Add keyword classes and keys.list/describe services #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,29 @@ | ||
| from bamboo.protocol import Protocol | ||
| from bamboo.builder import MessageBuilder | ||
| from bamboo.keys import KeyRegistry | ||
| from .zmq_transport import ZmqTransport | ||
| from .libby import Libby | ||
| from .keyword import ( | ||
| Keyword, | ||
| BoolKeyword, | ||
| IntKeyword, | ||
| FloatKeyword, | ||
| StringKeyword, | ||
| TriggerKeyword, | ||
| match_pattern, | ||
| ) | ||
| from .keyword_registry import KeywordRegistry | ||
|
|
||
| __all__ = ["Libby", "ZmqTransport", "Protocol", "MessageBuilder", "KeyRegistry"] | ||
| __all__ = [ | ||
| "Libby", | ||
| "Protocol", | ||
| "MessageBuilder", | ||
| "KeyRegistry", | ||
| "Keyword", | ||
| "BoolKeyword", | ||
| "IntKeyword", | ||
| "FloatKeyword", | ||
| "StringKeyword", | ||
| "TriggerKeyword", | ||
| "KeywordRegistry", | ||
| "match_pattern", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| """Keyword classes for Libby.""" | ||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| from typing import Any, Callable, Iterable, List, Optional | ||
|
|
||
| Getter = Callable[[], Any] | ||
| Setter = Callable[[Any], None] | ||
| Validator = Callable[[Any], Optional[str]] | ||
| Action = Callable[[], None] | ||
|
|
||
|
|
||
| def match_pattern(pattern: str, names: Iterable[str]) -> List[str]: | ||
| """Return names matching ``pattern``, sorted. ``%`` is a wildcard; | ||
| it matches any run of characters excluding ``.``.""" | ||
| parts = pattern.split("%") | ||
| rx = re.compile("^" + "[^.]*".join(re.escape(p) for p in parts) + "$") | ||
| return sorted(n for n in names if rx.match(n)) | ||
|
|
||
|
|
||
| class Keyword: | ||
| """Named value exposed as a libby service.""" | ||
|
|
||
| type_name: str = "any" | ||
|
|
||
| def __init__( | ||
| self, | ||
| name: str, | ||
| *, | ||
| getter: Optional[Getter] = None, | ||
| setter: Optional[Setter] = None, | ||
| units: Optional[str] = None, | ||
| description: str = "", | ||
| nullable: bool = False, | ||
| validator: Optional[Validator] = None, | ||
| ) -> None: | ||
| if getter is None and setter is None: | ||
| raise ValueError( | ||
| f"keyword '{name}' must supply at least one of getter/setter" | ||
| ) | ||
| self.name = name | ||
| self._getter = getter | ||
| self._setter = setter | ||
| self.units = units | ||
| self.description = description | ||
| self.nullable = nullable | ||
| self._validator = validator | ||
|
|
||
| @property | ||
| def readonly(self) -> bool: | ||
| return self._setter is None | ||
|
|
||
| @property | ||
| def writeonly(self) -> bool: | ||
| return self._getter is None | ||
|
|
||
| def _type_check(self, v: Any) -> Any: | ||
| """Type-check or cast ``v``. Override per subclass.""" | ||
| return v | ||
|
|
||
| def _response(self, value: Any) -> dict: | ||
| out: dict[str, Any] = {"ok": True, "value": value} | ||
| if self.units is not None: | ||
| out["units"] = self.units | ||
| return out | ||
|
|
||
| def describe(self) -> dict: | ||
| out: dict[str, Any] = { | ||
| "name": self.name, | ||
| "type": self.type_name, | ||
| "readonly": self.readonly, | ||
| "writeonly": self.writeonly, | ||
| "nullable": self.nullable, | ||
| } | ||
| if self.units is not None: | ||
| out["units"] = self.units | ||
| if self.description: | ||
| out["description"] = self.description | ||
| return out | ||
|
|
||
| def handle(self, payload: dict) -> dict: | ||
| if "value" in payload: | ||
| return self._modify(payload["value"]) | ||
| return self._show() | ||
|
|
||
| def _show(self) -> dict: | ||
| if self.writeonly: | ||
| return {"ok": False, "error": "keyword is write-only"} | ||
| try: | ||
| value = self._getter() # type: ignore[misc] | ||
| except Exception as e: | ||
| return {"ok": False, "error": str(e)} | ||
| return self._response(value) | ||
|
|
||
| def _modify(self, raw: Any) -> dict: | ||
| if self.readonly: | ||
| return {"ok": False, "error": "keyword is read-only"} | ||
| if raw is None: | ||
| if not self.nullable: | ||
| return {"ok": False, "error": f"value must be {self.type_name}"} | ||
| value: Any = None | ||
| else: | ||
| try: | ||
| value = self._type_check(raw) | ||
| except (TypeError, ValueError) as e: | ||
| return {"ok": False, "error": str(e)} | ||
| if self._validator is not None: | ||
| verr = self._validator(value) | ||
| if verr: | ||
| return {"ok": False, "error": verr} | ||
| try: | ||
| self._setter(value) # type: ignore[misc] | ||
| except Exception as e: | ||
| return {"ok": False, "error": str(e)} | ||
| return self._response(value) | ||
|
|
||
|
|
||
| class BoolKeyword(Keyword): | ||
| """Keyword whose value is a Python ``bool``.""" | ||
|
|
||
| type_name = "bool" | ||
|
|
||
| def _type_check(self, v: Any) -> bool: | ||
| if not isinstance(v, bool): | ||
| raise TypeError("value must be a bool") | ||
| return v | ||
|
|
||
|
|
||
| class IntKeyword(Keyword): | ||
| """Keyword whose value is a Python ``int``; ``bool`` is rejected.""" | ||
|
|
||
| type_name = "int" | ||
|
|
||
| def _type_check(self, v: Any) -> int: | ||
| # bool is a subclass of int in Python; reject explicitly | ||
| if isinstance(v, bool) or not isinstance(v, int): | ||
| raise TypeError("value must be an int") | ||
| return v | ||
|
|
||
|
|
||
| class FloatKeyword(Keyword): | ||
| """Keyword whose value is a Python ``float``; accepts ``int``, rejects ``bool``.""" | ||
|
|
||
| type_name = "float" | ||
|
|
||
| def _type_check(self, v: Any) -> float: | ||
| if isinstance(v, bool) or not isinstance(v, (int, float)): | ||
| raise TypeError("value must be a number") | ||
| return float(v) | ||
|
|
||
|
|
||
| class StringKeyword(Keyword): | ||
| """Keyword whose value is a Python ``str``.""" | ||
|
|
||
| type_name = "string" | ||
|
|
||
| def _type_check(self, v: Any) -> str: | ||
| if not isinstance(v, str): | ||
| raise TypeError("value must be a string") | ||
| return v | ||
|
|
||
|
|
||
| class TriggerKeyword(Keyword): | ||
| """Write-only action keyword. Any modify fires ``action``; show returns ``false``.""" | ||
|
|
||
| type_name = "trigger" | ||
|
|
||
| def __init__( | ||
| self, | ||
| name: str, | ||
| *, | ||
| action: Action, | ||
| description: str = "", | ||
| ) -> None: | ||
| self.name = name | ||
| self._action = action | ||
| self._getter = None | ||
| self._setter = None | ||
| self.units = None | ||
| self.description = description | ||
| self.nullable = False | ||
| self._validator = None | ||
|
|
||
| @property | ||
| def readonly(self) -> bool: | ||
| return False | ||
|
|
||
| @property | ||
| def writeonly(self) -> bool: | ||
| return True | ||
|
|
||
| def handle(self, payload: dict) -> dict: | ||
| if "value" not in payload: | ||
| return {"ok": True, "value": False} | ||
| try: | ||
| self._action() | ||
| except Exception as e: | ||
| return {"ok": False, "error": str(e)} | ||
| return {"ok": True, "value": True} | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "Keyword", | ||
| "BoolKeyword", | ||
| "IntKeyword", | ||
| "FloatKeyword", | ||
| "StringKeyword", | ||
| "TriggerKeyword", | ||
| "match_pattern", | ||
| ] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes it
What about making a builder object?
Then in Hispec