From 281507f5359f7cd717841a6969f0942ed351eaae Mon Sep 17 00:00:00 2001 From: jhnnsrs Date: Mon, 15 Jan 2024 09:41:16 +0100 Subject: [PATCH] added utils --- LICENSE | 21 ++++++++++++++ pyproject.toml | 2 +- rath/turms/utils.py | 71 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 LICENSE create mode 100644 rath/turms/utils.py diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4e65205 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Johannes Roos + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index cbfccd8..13a5d72 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "rath" -version = "0.5.0a0" +version = "0.5.0" description = "async transport-agnostic graphql client" authors = ["jhnnsrs "] readme = "README.md" diff --git a/rath/turms/utils.py b/rath/turms/utils.py new file mode 100644 index 0000000..1439eda --- /dev/null +++ b/rath/turms/utils.py @@ -0,0 +1,71 @@ +from typing import Any, List + + +class NotQueriedError(Exception): + pass + + +def get_nested_error(obj: Any, nested: List[str], above: List[str]) -> Any: + """Get a nested attribute or raise an error + + + Raises an error if a nested attribut is not present + + This is used to raise an error if a nested attribute is not present. + And to give a hint where in a query tree a the nested attribute is missing. + + Parameters + ---------- + obj : Any + The object to query + nested : List[str] + The nested attributes to query + above : _type_ + The attributes above the nested attribute ( will be filled by the function) + + Returns + ------- + Any + The queried object + + Raises + ------ + NotQueriedError + The nested attribute was not queried + """ + if hasattr(obj, nested[0]): + obj = getattr(obj, nested[0]) + if len(nested) > 1: + return get_nested_error(obj, nested[1:], above + [nested[0]]) + else: + return obj + else: + raise NotQueriedError(f"{nested} not queried. {above} was queried") + + +def get_attributes_or_error(self, *args) -> Any: + """Get attributes or raise an error""" + returns = [] + errors = [] + for i in args: + if "." in i: + try: + returns.append(get_nested_error(self, i.split("."), [])) + continue + except NotQueriedError: + errors.append(i) + else: + if hasattr(self, i): + returns.append(getattr(self, i)) + else: + errors.append(i) + + if len(errors) > 0: + raise NotQueriedError( + f"Required fields {errors} not queried on {self.__class__.__name__}" + ) + + if len(args) == 1: + return returns[0] + else: + return tuple(returns)